fix for #188
[free-mc.git] / vfs / smbfs.c
blob2f15ebf1f15655729f86bcc89b5e8f0bb66365eb
1 /* Virtual File System: Midnight Commander file system.
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
4 Free Software Foundation, Inc.
6 Written by Wayne Roberts <wroberts1@home.com>, 1997
7 Andrew V. Samoilov <sav@bcs.zp.ua> 2002, 2003
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public License
11 as published by the Free Software Foundation; either version 2 of
12 the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public
20 License along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 /* Namespace: exports init_smbfs, smbfs_set_debug(), smbfs_set_debugf() */
24 #include <config.h>
26 #include <stdio.h>
27 #include <sys/types.h>
29 #undef USE_NCURSES /* Don't include *curses.h */
30 #include "../src/global.h"
31 #include "../src/tty.h" /* enable/disable interrupt key */
32 #include "../src/wtools.h" /* message() */
33 #include "../src/main.h" /* print_vfs_message */
34 #include "utilvfs.h"
36 #undef PACKAGE_BUGREPORT
37 #undef PACKAGE_NAME
38 #undef PACKAGE_STRING
39 #undef PACKAGE_TARNAME
40 #undef PACKAGE_VERSION
42 #include "samba/include/config.h"
43 /* don't load crap in "samba/include/includes.h" we don't use and which
44 conflicts with definitions in other includes */
45 #undef HAVE_LIBREADLINE
46 #define NO_CONFIG_H
47 #undef VERSION
49 #include "samba/include/includes.h"
51 #include <string.h>
53 #include "vfs.h"
54 #include "vfs-impl.h"
55 #include "smbfs.h"
57 #define SMBFS_MAX_CONNECTIONS 16
58 static const char * const IPC = "IPC$";
59 static const char * const URL_HEADER = "/#smb:";
60 #define HEADER_LEN 6
62 static int my_errno;
63 static uint32 err;
65 /* stuff that is same with each connection */
66 extern int DEBUGLEVEL;
67 extern pstring myhostname;
68 extern struct in_addr ipzero;
69 static mode_t myumask = 0755;
70 extern pstring global_myname;
71 static int smbfs_open_connections = 0;
72 static gboolean got_user = FALSE;
73 static gboolean got_pass = FALSE;
74 static pstring password;
75 static pstring username;
76 static struct vfs_class vfs_smbfs_ops;
78 static struct _smbfs_connection {
79 struct cli_state *cli;
80 struct in_addr dest_ip;
81 BOOL have_ip;
82 char *host; /* server name */
83 char *service; /* share name */
84 char *domain;
85 char *user;
86 char *home;
87 char *password;
88 int port;
89 int name_type;
90 time_t last_use;
91 } smbfs_connections [SMBFS_MAX_CONNECTIONS];
92 /* unique to each connection */
94 /* modifies *share */
95 static struct cli_state * smbfs_do_connect (const char *server, char *share);
97 typedef struct _smbfs_connection smbfs_connection;
98 static smbfs_connection *current_bucket;
100 typedef struct {
101 struct cli_state *cli;
102 int fnum;
103 off_t nread;
104 uint16 attr;
105 } smbfs_handle;
107 static GSList *auth_list;
109 /* this function allows you to write:
110 * char *s = g_strdup("hello, world");
111 * s = free_after(g_strconcat(s, s, (char *)0), s);
113 static inline char *
114 free_after (char *result, char *string_to_free)
116 g_free(string_to_free);
117 return result;
121 static void
122 smbfs_auth_free (struct smb_authinfo const *a)
124 g_free (a->host);
125 g_free (a->share);
126 g_free (a->domain);
127 g_free (a->user);
128 wipe_password (a->password);
131 static void
132 smbfs_auth_free_all (void)
134 if (auth_list) {
135 g_slist_foreach (auth_list, (GFunc)smbfs_auth_free, 0);
136 g_slist_free (auth_list);
137 auth_list = 0;
141 static gint
142 smbfs_auth_cmp_host_and_share (gconstpointer _a, gconstpointer _b)
144 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
145 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
147 if (!a->host || !a->share || !b->host || !b->share)
148 return 1;
149 if (strcmp (a->host, b->host) != 0)
150 return 1;
151 if (strcmp (a->share, b->share) != 0)
152 return 1;
153 return 0;
156 static gint
157 smbfs_auth_cmp_host (gconstpointer _a, gconstpointer _b)
159 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
160 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
162 if (!a->host || !b->host)
163 return 1;
164 if (strcmp (a->host, b->host) != 0)
165 return 1;
166 if (strcmp (a->share, IPC) != 0)
167 return 1;
168 return 0;
171 static void
172 smbfs_auth_add (const char *host, const char *share, const char *domain,
173 const char *user, const char *password)
175 struct smb_authinfo *auth = g_new (struct smb_authinfo, 1);
177 if (!auth)
178 return;
180 /* Don't check for NULL, g_strdup already does. */
181 auth->host = g_strdup (host);
182 auth->share = g_strdup (share);
183 auth->domain = g_strdup (domain);
184 auth->user = g_strdup (user);
185 auth->password = g_strdup (password);
186 auth_list = g_slist_prepend (auth_list, auth);
189 static void
190 smbfs_auth_remove (const char *host, const char *share)
192 struct smb_authinfo data;
193 struct smb_authinfo *auth;
194 GSList *list;
196 data.host = g_strdup (host);
197 data.share = g_strdup (share);
198 list = g_slist_find_custom (auth_list,
199 &data,
200 smbfs_auth_cmp_host_and_share);
201 g_free (data.host);
202 g_free (data.share);
203 if (!list)
204 return;
205 auth = list->data;
206 auth_list = g_slist_remove (auth_list, auth);
207 smbfs_auth_free (auth);
210 /* Set authentication information in bucket. Return 1 if successful, else 0 */
211 /* Information in auth_list overrides user if pass is NULL. */
212 /* bucket->host and bucket->service must be valid. */
213 static int
214 smbfs_bucket_set_authinfo (smbfs_connection *bucket,
215 const char *domain, const char *user, const char *pass,
216 int fallback_to_host)
218 struct smb_authinfo data;
219 struct smb_authinfo *auth;
220 GSList *list;
222 if (domain && user && pass) {
223 g_free (bucket->domain);
224 g_free (bucket->user);
225 g_free (bucket->password);
226 bucket->domain = g_strdup (domain);
227 bucket->user = g_strdup (user);
228 bucket->password = g_strdup (pass);
229 smbfs_auth_remove (bucket->host, bucket->service);
230 smbfs_auth_add (bucket->host, bucket->service,
231 domain, user, pass);
232 return 1;
235 data.host = bucket->host;
236 data.share = bucket->service;
237 list = g_slist_find_custom (auth_list, &data, smbfs_auth_cmp_host_and_share);
238 if (!list && fallback_to_host)
239 list = g_slist_find_custom (auth_list, &data, smbfs_auth_cmp_host);
240 if (list) {
241 auth = list->data;
242 bucket->domain = g_strdup (auth->domain);
243 bucket->user = g_strdup (auth->user);
244 bucket->password = g_strdup (auth->password);
245 return 1;
248 if (got_pass) {
249 bucket->domain = g_strdup (lp_workgroup ());
250 bucket->user = g_strdup (got_user ? username : user);
251 bucket->password = g_strdup (password);
252 return 1;
255 auth = vfs_smb_get_authinfo (bucket->host,
256 bucket->service,
257 (domain ? domain : lp_workgroup ()),
258 user);
259 if (auth) {
260 g_free (bucket->domain);
261 g_free (bucket->user);
262 g_free (bucket->password);
263 bucket->domain = g_strdup (auth->domain);
264 bucket->user = g_strdup (auth->user);
265 bucket->password = g_strdup (auth->password);
266 smbfs_auth_remove (bucket->host, bucket->service);
267 auth_list = g_slist_prepend (auth_list, auth);
268 return 1;
270 return 0;
273 void
274 smbfs_set_debug (int arg)
276 DEBUGLEVEL = arg;
279 void
280 smbfs_set_debugf (const char *filename)
282 extern pstring debugf;
283 extern FILE *dbf;
284 if (DEBUGLEVEL > 0) {
285 FILE *outfile = fopen (filename, "w");
286 if (outfile) {
287 setup_logging ("", True); /* No needs for timestamp for each message */
288 dbf = outfile;
289 setbuf (dbf, NULL);
290 pstrcpy (debugf, filename);
295 /********************** The callbacks ******************************/
296 static int
297 smbfs_init (struct vfs_class * me)
299 const char *servicesf = CONFIGDIR PATH_SEP_STR "smb.conf";
301 /* DEBUGLEVEL = 4; */
303 TimeInit ();
304 charset_initialise ();
306 DEBUG (3, ("smbfs_init(%s)\n", me->name));
308 if (!get_myname (myhostname, NULL))
309 DEBUG (0, ("Failed to get my hostname.\n"));
311 if (!lp_load (servicesf, True, False, False))
312 DEBUG (0, ("Cannot load %s - run testparm to debug it\n", servicesf));
314 codepage_initialise (lp_client_code_page ());
316 load_interfaces ();
318 myumask = umask (0);
319 umask (myumask);
320 myumask = ~myumask;
322 if (getenv ("USER")) {
323 char *p;
325 pstrcpy (username, getenv ("USER"));
326 got_user = TRUE;
327 DEBUG (3, ("smbfs_init(): $USER:%s\n", username));
328 if ((p = strchr (username, '%'))) {
329 *p = 0;
330 pstrcpy (password, p + 1);
331 got_pass = TRUE;
332 memset (strchr (getenv ("USER"), '%') + 1, 'X', strlen (password));
333 DEBUG (3, ("smbfs_init(): $USER%%pass: %s%%%s\n",
334 username, password));
336 strupper (username);
338 if (getenv ("PASSWD")) {
339 pstrcpy (password, getenv ("PASSWD"));
340 got_pass = TRUE;
342 return 1;
345 static void
346 smbfs_fill_names (struct vfs_class *me, fill_names_f func)
348 int i;
349 char *path;
351 (void) me;
353 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
354 if (smbfs_connections [i].cli) {
355 path = g_strconcat (URL_HEADER,
356 smbfs_connections[i].user, "@",
357 smbfs_connections[i].host,
358 "/", smbfs_connections[i].service,
359 NULL);
360 (*func)(path);
361 g_free (path);
366 #define CNV_LANG(s) dos_to_unix(s,False)
367 #define GNAL_VNC(s) unix_to_dos(s,False)
368 /* does same as do_get() in client.c */
369 /* called from vfs.c:1080, count = buffer size */
370 static ssize_t
371 smbfs_read (void *data, char *buffer, int count)
373 smbfs_handle *info = (smbfs_handle *) data;
374 int n;
376 DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%d)\n",
377 info->fnum, (int)info->nread, count));
378 n = cli_read(info->cli, info->fnum, buffer, info->nread, count);
379 if (n > 0)
380 info->nread += n;
381 return n;
384 static ssize_t
385 smbfs_write (void *data, const char *buf, int nbyte)
387 smbfs_handle *info = (smbfs_handle *) data;
388 int n;
390 DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%d)\n",
391 info->fnum, (int)info->nread, nbyte));
392 n = cli_write(info->cli, info->fnum, 0, buf, info->nread, nbyte);
393 if (n > 0)
394 info->nread += n;
395 return n;
398 static int
399 smbfs_close (void *data)
401 smbfs_handle *info = (smbfs_handle *) data;
402 DEBUG (3, ("smbfs_close(fnum:%d)\n", info->fnum));
404 /* FIXME: Why too different cli have the same outbuf
405 * if file is copied to share
407 if (info->cli->outbuf == NULL) {
408 my_errno = EINVAL;
409 return -1;
411 #if 0
412 /* if imlementing archive_level: add rname to smbfs_handle */
413 if (archive_level >= 2 && (inf->attr & aARCH)) {
414 cli_setatr (info->cli, rname, info->attr & ~(uint16) aARCH, 0);
416 #endif
417 return (cli_close (info->cli, info->fnum) == True) ? 0 : -1;
420 static int
421 smbfs_errno (struct vfs_class *me)
423 (void) me;
425 DEBUG(3, ("smbfs_errno: %s\n", g_strerror(my_errno)));
426 return my_errno;
429 typedef struct dir_entry {
430 char *text;
431 struct dir_entry *next;
432 struct stat my_stat;
433 int merrno;
434 } dir_entry;
436 typedef struct {
437 gboolean server_list;
438 char *dirname;
439 char *path; /* the dir originally passed to smbfs_opendir */
440 smbfs_connection *conn;
441 dir_entry *entries;
442 dir_entry *current;
443 } opendir_info;
445 static opendir_info
446 *previous_info,
447 *current_info,
448 *current_share_info,
449 *current_server_info;
451 static gboolean first_direntry;
453 static dir_entry *
454 smbfs_new_dir_entry (const char *name)
456 static int inode_counter;
457 dir_entry *new_entry;
458 new_entry = g_new0 (dir_entry, 1);
459 new_entry->text = dos_to_unix (g_strdup (name), 1);
461 if (first_direntry) {
462 current_info->entries = new_entry;
463 first_direntry = FALSE;
464 } else {
465 current_info->current->next = new_entry;
467 current_info->current = new_entry;
468 new_entry->my_stat.st_ino = inode_counter++;
470 return new_entry;
473 /* browse for shares on server */
474 static void
475 smbfs_browsing_helper (const char *name, uint32 type, const char *comment, void *state)
477 const char *typestr = "";
478 dir_entry *new_entry = smbfs_new_dir_entry (name);
480 (void) state;
482 switch (type) {
483 case STYPE_DISKTREE:
484 typestr = "Disk";
485 /* show this as dir */
486 new_entry->my_stat.st_mode =
487 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
488 S_IXOTH) & myumask;
489 break;
490 case STYPE_PRINTQ:
491 typestr = "Printer";
492 break;
493 case STYPE_DEVICE:
494 typestr = "Device";
495 break;
496 case STYPE_IPC:
497 typestr = "IPC";
498 break;
500 DEBUG (3, ("\t%-15.15s%-10.10s%s\n", name, typestr, comment));
503 static void
504 smbfs_loaddir_helper (file_info * finfo, const char *mask, void *entry)
506 dir_entry *new_entry = (dir_entry *) entry;
507 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
509 (void) mask;
511 #if 0 /* I want to see dot files */
512 if (finfo->mode & aHIDDEN)
513 return; /* don't bother with hidden files, "~$" screws up mc */
514 #endif
515 if (!entry)
516 new_entry = smbfs_new_dir_entry (finfo->name);
518 new_entry->my_stat.st_size = finfo->size;
519 new_entry->my_stat.st_mtime = finfo->mtime;
520 new_entry->my_stat.st_atime = finfo->atime;
521 new_entry->my_stat.st_ctime = finfo->ctime;
522 new_entry->my_stat.st_uid = finfo->uid;
523 new_entry->my_stat.st_gid = finfo->gid;
525 new_entry->my_stat.st_mode = /* rw-rw-rw */
526 S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH;
528 /* if (finfo->mode & aVOLID); nothing similar in real world */
529 if (finfo->mode & aDIR)
530 new_entry->my_stat.st_mode |= /* drwxrwxrwx */
531 S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
532 else
533 new_entry->my_stat.st_mode |= S_IFREG; /* if not dir, regular file? */
534 /* if (finfo->mode & aARCH); DOS archive */
535 /* if (finfo->mode & aHIDDEN); like a dot file? */
536 /* if (finfo->mode & aSYSTEM); like a kernel? */
537 if (finfo->mode & aRONLY)
538 new_entry->my_stat.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
539 new_entry->my_stat.st_mode &= myumask;
541 DEBUG (entry ? 3 : 6, (" %-30s%7.7s%8.0f %s",
542 CNV_LANG (finfo->name),
543 attrib_string (finfo->mode),
544 (double) finfo->size,
545 asctime (LocalTime (&t))));
548 /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
549 static char *
550 smbfs_convert_path (const char *remote_file, gboolean trailing_asterik)
552 const char *p, *my_remote;
553 char *result;
555 my_remote = remote_file;
556 if (strncmp (my_remote, URL_HEADER, HEADER_LEN) == 0) { /* if passed directly */
557 my_remote += HEADER_LEN;
558 if (*my_remote == '/') /* from server browsing */
559 my_remote++;
560 p = strchr(my_remote, '/');
561 if (p)
562 my_remote = p+1; /* advance to end of server name */
565 if (*my_remote == '/')
566 my_remote++; /* strip off leading '/' */
567 p = strchr(my_remote, '/');
568 if (p)
569 my_remote = p; /* strip off share/service name */
570 /* create remote filename as understood by smb clientgen */
571 result = g_strconcat (my_remote, trailing_asterik ? "/*" : "", (char *) NULL);
572 unix_to_dos (result, /* inplace = */ 1); /* code page conversion */
573 str_replace(result, '/', '\\');
574 return result;
577 static void
578 smbfs_srv_browsing_helper (const char *name, uint32 m, const char *comment,
579 void *state)
581 dir_entry *new_entry = smbfs_new_dir_entry (name);
583 (void) m;
584 (void) state;
586 /* show this as dir */
587 new_entry->my_stat.st_mode =
588 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
589 S_IXOTH) & myumask;
591 DEBUG (3, ("\t%-16.16s %s\n", name, comment));
594 static BOOL
595 smbfs_reconnect(smbfs_connection *conn, int *retries)
597 char *host;
598 DEBUG(3, ("RECONNECT\n"));
600 if (*(conn->host) == 0)
601 host = g_strdup(conn->cli->desthost); /* server browsing */
602 else
603 host = g_strdup(conn->host);
605 cli_shutdown(conn->cli);
607 if (!(conn->cli = smbfs_do_connect(host, conn->service))) {
608 message (D_ERROR, MSG_ERROR,
609 _(" reconnect to %s failed\n "), conn->host);
610 g_free(host);
611 return False;
613 g_free(host);
614 if (++(*retries) == 2)
615 return False;
616 return True;
619 static BOOL
620 smbfs_send(struct cli_state *cli)
622 size_t len;
623 size_t nwritten=0;
624 ssize_t ret;
626 len = smb_len(cli->outbuf) + 4;
628 while (nwritten < len) {
629 ret = write_socket(cli->fd, cli->outbuf+nwritten, len - nwritten);
630 if (ret <= 0) {
631 if (errno == EPIPE)
632 return False;
633 } else
634 nwritten += ret;
637 return True;
640 /****************************************************************************
641 See if server has cut us off by checking for EPIPE when writing.
642 Taken from cli_chkpath()
643 ****************************************************************************/
644 static BOOL
645 smbfs_chkpath(struct cli_state *cli, const char *path, BOOL send_only)
647 fstring path2;
648 char *p;
650 fstrcpy(path2,path);
651 unix_to_dos (path2, 1);
652 trim_string(path2,NULL,"\\");
653 if (!*path2) *path2 = '\\';
655 memset(cli->outbuf,'\0',smb_size);
656 set_message(cli->outbuf,0,4 + strlen(path2),True);
657 SCVAL(cli->outbuf,smb_com,SMBchkpth);
658 SSVAL(cli->outbuf,smb_tid,cli->cnum);
660 cli->rap_error = 0;
661 cli->nt_error = 0;
662 SSVAL(cli->outbuf,smb_pid,cli->pid);
663 SSVAL(cli->outbuf,smb_uid,cli->vuid);
664 SSVAL(cli->outbuf,smb_mid,cli->mid);
665 if (cli->protocol > PROTOCOL_CORE) {
666 SCVAL(cli->outbuf,smb_flg,0x8);
667 SSVAL(cli->outbuf,smb_flg2,0x1);
670 p = smb_buf(cli->outbuf);
671 *p++ = 4;
672 fstrcpy(p,path2);
674 if (!smbfs_send(cli)) {
675 DEBUG(3, ("smbfs_chkpath: couldnt send\n"));
676 return False;
678 if (send_only) {
679 client_receive_smb(cli->fd, cli->inbuf, cli->timeout);
680 DEBUG(3, ("smbfs_chkpath: send only OK\n"));
681 return True; /* just testing for EPIPE */
683 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout)) {
684 DEBUG(3, ("smbfs_chkpath: receive error\n"));
685 return False;
687 if ((my_errno = cli_error(cli, NULL, NULL, NULL))) {
688 if (my_errno == 20 || my_errno == 13)
689 return True; /* ignore if 'not a directory' error */
690 DEBUG(3, ("smbfs_chkpath: cli_error: %s\n", g_strerror(my_errno)));
691 return False;
694 return True;
697 #if 1
698 static int
699 smbfs_fs (const char *text)
701 const char *p = text;
702 int count = 0;
704 while ((p = strchr(p, '/')) != NULL) {
705 count++;
706 p++;
708 if (count == 1)
709 return strlen(text);
710 return count;
712 #endif
714 static int
715 smbfs_loaddir (opendir_info *smbfs_info)
717 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
718 int servlen = strlen (smbfs_info->conn->service);
719 const char *info_dirname = smbfs_info->dirname;
720 char *my_dirname;
722 DEBUG (3, ("smbfs_loaddir: dirname:%s\n", info_dirname));
723 first_direntry = TRUE;
725 if (current_info) {
726 DEBUG (3,
727 ("smbfs_loaddir: new:'%s', cached:'%s'\n", info_dirname,
728 current_info->dirname));
729 /* if new desired dir is longer than cached in current_info */
730 if (smbfs_fs (info_dirname) > smbfs_fs (current_info->dirname)) {
731 DEBUG (3, ("saving to previous_info\n"));
732 previous_info = current_info;
736 current_info = smbfs_info;
738 if (strcmp (info_dirname, "/") == 0) {
739 if (!strcmp (smbfs_info->path, URL_HEADER)) {
740 DEBUG (6, ("smbfs_loaddir: browsing %s\n", IPC));
741 /* browse for servers */
742 if (!cli_NetServerEnum
743 (smbfs_info->conn->cli, smbfs_info->conn->domain,
744 SV_TYPE_ALL, smbfs_srv_browsing_helper, NULL))
745 return 0;
746 else
747 current_server_info = smbfs_info;
748 smbfs_info->server_list = TRUE;
749 } else {
750 /* browse for shares */
751 if (cli_RNetShareEnum
752 (smbfs_info->conn->cli, smbfs_browsing_helper, NULL) < 1)
753 return 0;
754 else
755 current_share_info = smbfs_info;
757 goto done;
760 /* do regular directory listing */
761 if (strncmp (smbfs_info->conn->service, info_dirname + 1, servlen) == 0) {
762 /* strip share name from dir */
763 my_dirname = g_strdup (info_dirname + servlen);
764 *my_dirname = '/';
765 my_dirname = free_after(smbfs_convert_path (my_dirname, TRUE), my_dirname);
766 } else
767 my_dirname = smbfs_convert_path (info_dirname, TRUE);
769 DEBUG (6, ("smbfs_loaddir: service: %s\n", smbfs_info->conn->service));
770 DEBUG (6,
771 ("smbfs_loaddir: cli->share: %s\n",
772 smbfs_info->conn->cli->share));
773 DEBUG (6,
774 ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname));
775 /* do file listing: cli_list returns number of files */
776 if (cli_list
777 (smbfs_info->conn->cli, my_dirname, attribute,
778 smbfs_loaddir_helper, NULL) < 0) {
779 /* cli_list returns -1 if directory empty or cannot read socket */
780 my_errno = cli_error (smbfs_info->conn->cli, NULL, &err, NULL);
781 g_free (my_dirname);
782 return 0;
784 if (*(my_dirname) == 0)
785 smbfs_info->dirname = smbfs_info->conn->service;
786 g_free (my_dirname);
787 /* do_dskattr(); */
789 done:
790 /* current_info->parent = smbfs_info->dirname; */
792 smbfs_info->current = smbfs_info->entries;
793 return 1; /* 1 = ok */
796 #ifdef SMBFS_FREE_DIR
797 static void
798 smbfs_free_dir (dir_entry *de)
800 if (!de) return;
802 smbfs_free_dir (de->next);
803 g_free (de->text);
804 g_free (de);
806 #endif
809 /* The readdir routine loads the complete directory */
810 /* It's too slow to ask the server each time */
811 /* It now also sends the complete lstat information for each file */
812 static void *
813 smbfs_readdir(void *info)
815 static union vfs_dirent smbfs_readdir_data;
816 static char *const dirent_dest = smbfs_readdir_data.dent.d_name;
817 opendir_info *smbfs_info = (opendir_info *) info;
819 DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info->dirname));
821 if (!smbfs_info->entries)
822 if (!smbfs_loaddir(smbfs_info))
823 return NULL;
825 if (smbfs_info->current == 0) { /* reached end of dir entries */
826 DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
827 #ifdef SMBFS_FREE_DIR
828 smbfs_free_dir(smbfs_info->entries);
829 smbfs_info->entries = 0;
830 #endif
831 return NULL;
833 g_strlcpy(dirent_dest, smbfs_info->current->text, MC_MAXPATHLEN);
834 smbfs_info->current = smbfs_info->current->next;
836 compute_namelen(&smbfs_readdir_data.dent);
838 return &smbfs_readdir_data;
841 static int
842 smbfs_closedir (void *info)
844 opendir_info *smbfs_info = (opendir_info *) info;
845 /* dir_entry *p, *q; */
847 DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info->dirname));
848 /* CLOSE HERE */
850 /* for (p = smbfs_info->entries; p;){
851 q = p;
852 p = p->next;
853 g_free (q->text);
854 g_free (q);
856 g_free (info); */
857 return 0;
860 static int
861 smbfs_chmod (struct vfs_class *me, const char *path, int mode)
863 (void) me;
865 DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path, mode));
866 /* my_errno = EOPNOTSUPP;
867 return -1; */ /* cannot chmod on smb filesystem */
868 return 0; /* make mc happy */
871 static int
872 smbfs_chown (struct vfs_class *me, const char *path, int owner, int group)
874 (void) me;
876 DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path, owner, group));
877 my_errno = EOPNOTSUPP; /* ready for your labotomy? */
878 return -1;
881 static int
882 smbfs_utime (struct vfs_class *me, const char *path, struct utimbuf *times)
884 (void) me;
885 (void) times;
887 DEBUG(3, ("smbfs_utime(path:%s)\n", path));
888 my_errno = EOPNOTSUPP;
889 return -1;
892 static int
893 smbfs_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
895 (void) me;
897 DEBUG (3,
898 ("smbfs_readlink(path:%s, buf:%s, size:%d)\n", path, buf,
899 (int) size));
900 my_errno = EOPNOTSUPP;
901 return -1; /* no symlinks on smb filesystem? */
904 static int
905 smbfs_symlink (struct vfs_class *me, const char *n1, const char *n2)
907 (void) me;
909 DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1, n2));
910 my_errno = EOPNOTSUPP;
911 return -1; /* no symlinks on smb filesystem? */
914 /* Extract the hostname and username from the path */
915 /* path is in the form: [user@]hostname/share/remote-dir */
916 #define smbfs_get_host_and_username(path, host, user, port, pass) \
917 vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
919 /*****************************************************
920 return a connection to a SMB server
921 current_bucket needs to be set before calling
922 *******************************************************/
923 static struct cli_state *
924 smbfs_do_connect (const char *server, char *share)
926 struct cli_state *c;
927 struct nmb_name called, calling;
928 struct in_addr ip;
930 DEBUG(3, ("smbfs_do_connect(%s, %s)\n", server, share));
931 if (*share == '\\') {
932 server = share+2;
933 share = strchr(server,'\\');
934 if (!share) return NULL;
935 *share = 0;
936 share++;
939 make_nmb_name(&calling, global_myname, 0x0);
940 make_nmb_name(&called , server, current_bucket->name_type);
942 for (;;) {
944 ip = (current_bucket->have_ip) ? current_bucket->dest_ip : ipzero;
946 /* have to open a new connection */
947 if (!(c = cli_initialise(NULL))) {
948 my_errno = ENOMEM;
949 return NULL;
952 pwd_init(&(c->pwd)); /* should be moved into cli_initialise()? */
953 pwd_set_cleartext(&(c->pwd), current_bucket->password);
955 if ((cli_set_port(c, current_bucket->port) == 0) ||
956 !cli_connect(c, server, &ip)) {
957 DEBUG(1, ("Connection to %s failed\n", server));
958 break;
961 if (!cli_session_request(c, &calling, &called)) {
962 my_errno = cli_error(c, NULL, &err, NULL);
963 DEBUG(1, ("session request to %s failed\n", called.name));
964 cli_shutdown(c);
965 if (strcmp(called.name, "*SMBSERVER")) {
966 make_nmb_name(&called , "*SMBSERVER", 0x20);
967 continue;
969 return NULL;
972 DEBUG(3, (" session request ok\n"));
974 if (!cli_negprot(c)) {
975 DEBUG(1, ("protocol negotiation failed\n"));
976 break;
979 if (!cli_session_setup(c, current_bucket->user,
980 current_bucket->password, strlen(current_bucket->password),
981 current_bucket->password, strlen(current_bucket->password),
982 current_bucket->domain)) {
983 DEBUG(1,("session setup failed: %s\n", cli_errstr(c)));
984 smbfs_auth_remove (server, share);
985 break;
988 if (*c->server_domain || *c->server_os || *c->server_type)
989 DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
990 c->server_domain,c->server_os,c->server_type));
992 DEBUG(3, (" session setup ok\n"));
994 if (!cli_send_tconX(c, share, "?????",
995 current_bucket->password, strlen(current_bucket->password)+1)) {
996 DEBUG(1,("%s: tree connect failed: %s\n", share, cli_errstr(c)));
997 break;
1000 DEBUG(3, (" tconx ok\n"));
1002 my_errno = 0;
1003 return c;
1006 my_errno = cli_error(c, NULL, &err, NULL);
1007 cli_shutdown(c);
1008 return NULL;
1012 static int
1013 smbfs_get_master_browser(char **host)
1015 static char so_broadcast[] = "SO_BROADCAST";
1016 int count;
1017 struct in_addr *ip_list, bcast_addr;
1019 /* does port = 137 for win95 master browser? */
1020 int fd= open_socket_in( SOCK_DGRAM, 0, 3,
1021 interpret_addr(lp_socket_address()), True );
1022 if (fd == -1)
1023 return 0;
1024 set_socket_options(fd, so_broadcast);
1025 ip_list = iface_bcast(ipzero);
1026 bcast_addr = *ip_list;
1027 if ((ip_list = name_query(fd, "\01\02__MSBROWSE__\02", 1, True,
1028 True, bcast_addr, &count, NULL))) {
1029 if (!count)
1030 return 0;
1031 /* just return first master browser */
1032 *host = g_strdup(inet_ntoa(ip_list[0]));
1033 return 1;
1035 return 0;
1038 static void
1039 smbfs_free_bucket (smbfs_connection *bucket)
1041 g_free (bucket->host);
1042 g_free (bucket->service);
1043 g_free (bucket->domain);
1044 g_free (bucket->user);
1045 wipe_password (bucket->password);
1046 g_free (bucket->home);
1047 memset (bucket, 0, sizeof (smbfs_connection));
1050 static smbfs_connection *
1051 smbfs_get_free_bucket (void)
1053 int i;
1055 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++)
1056 if (!smbfs_connections [i].cli) return &smbfs_connections [i];
1058 { /* search for most dormant connection */
1059 int oldest = 0; /* index */
1060 time_t oldest_time = smbfs_connections[0].last_use;
1061 for (i = 1; i < SMBFS_MAX_CONNECTIONS; i++) {
1062 if (smbfs_connections[i].last_use < oldest_time) {
1063 oldest_time = smbfs_connections[i].last_use;
1064 oldest = i;
1067 cli_shutdown(smbfs_connections[oldest].cli);
1068 smbfs_free_bucket (&smbfs_connections[oldest]);
1069 return &smbfs_connections[oldest];
1072 /* This can't happend, since we have checked for max connections before */
1073 vfs_die("Internal error: smbfs_get_free_bucket");
1074 return 0; /* shut up, stupid gcc */
1077 /* This routine keeps track of open connections */
1078 /* Returns a connected socket to host */
1079 static smbfs_connection *
1080 smbfs_open_link (char *host, char *path, const char *user, int *port,
1081 char *this_pass)
1083 int i;
1084 smbfs_connection *bucket;
1085 pstring service;
1086 struct in_addr *dest_ip = NULL;
1088 DEBUG (3, ("smbfs_open_link(host:%s, path:%s)\n", host, path));
1090 if (strcmp (host, path) == 0) /* if host & path are same: */
1091 pstrcpy (service, IPC); /* setup for browse */
1092 else { /* get share name from path, path starts with server name */
1093 char *p;
1094 if ((p = strchr (path, '/'))) /* get share aka */
1095 pstrcpy (service, ++p); /* service name from path */
1096 else
1097 pstrcpy (service, "");
1098 /* now check for trailing directory/filenames */
1099 p = strchr (service, '/');
1100 if (p)
1101 *p = 0; /* cut off dir/files: sharename only */
1102 if (!*service)
1103 pstrcpy (service, IPC); /* setup for browse */
1104 DEBUG (6, ("smbfs_open_link: service from path:%s\n", service));
1107 if (got_user)
1108 user = username; /* global from getenv */
1110 /* Is the link actually open? */
1111 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1112 if (!smbfs_connections[i].cli)
1113 continue;
1114 if ((strcmp (host, smbfs_connections[i].host) == 0) &&
1115 (strcmp (user, smbfs_connections[i].user) == 0) &&
1116 (strcmp (service, smbfs_connections[i].service) == 0)) {
1117 int retries = 0;
1118 BOOL inshare = (*host != 0 && *path != 0 && strchr (path, '/'));
1119 /* check if this connection has died */
1120 while (!smbfs_chkpath (smbfs_connections[i].cli, "\\", !inshare)) {
1121 if (!smbfs_reconnect (&smbfs_connections[i], &retries))
1122 return 0;
1124 DEBUG (6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i));
1125 current_bucket = &smbfs_connections[i];
1126 smbfs_connections[i].last_use = time (NULL);
1127 return &smbfs_connections[i];
1129 /* connection not found, find if we have ip for new connection */
1130 if (strcmp (host, smbfs_connections[i].host) == 0)
1131 dest_ip = &smbfs_connections[i].cli->dest_ip;
1134 /* make new connection */
1135 bucket = smbfs_get_free_bucket ();
1136 bucket->name_type = 0x20;
1137 bucket->home = 0;
1138 bucket->port = *port;
1139 bucket->have_ip = False;
1140 if (dest_ip) {
1141 bucket->have_ip = True;
1142 bucket->dest_ip = *dest_ip;
1144 current_bucket = bucket;
1146 bucket->user = g_strdup (user);
1147 bucket->service = g_strdup (service);
1149 if (!(*host)) { /* if blank host name, browse for servers */
1150 if (!smbfs_get_master_browser (&host)) /* set host to ip of master browser */
1151 return 0; /* could not find master browser? */
1152 g_free (host);
1153 bucket->host = g_strdup (""); /* blank host means master browser */
1154 } else
1155 bucket->host = g_strdup (host);
1157 if (!smbfs_bucket_set_authinfo (bucket, 0, /* domain currently not used */
1158 user, this_pass, 1))
1159 return 0;
1161 /* connect to share */
1162 while (!(bucket->cli = smbfs_do_connect (host, service))) {
1164 if (my_errno != EPERM)
1165 return 0;
1166 message (D_ERROR, MSG_ERROR, _(" Authentication failed "));
1168 /* authentication failed, try again */
1169 smbfs_auth_remove (bucket->host, bucket->service);
1170 if (!smbfs_bucket_set_authinfo (bucket, bucket->domain, bucket->user, 0, 0))
1171 return 0;
1175 smbfs_open_connections++;
1176 DEBUG (3, ("smbfs_open_link:smbfs_open_connections: %d\n",
1177 smbfs_open_connections));
1178 return bucket;
1181 static char *
1182 smbfs_get_path (smbfs_connection ** sc, const char *path)
1184 char *user, *host, *remote_path, *pass;
1185 int port = SMB_PORT;
1187 DEBUG (3, ("smbfs_get_path(%s)\n", path));
1188 if (strncmp (path, URL_HEADER, HEADER_LEN))
1189 return NULL;
1190 path += HEADER_LEN;
1192 if (*path == '/') /* '/' leading server name */
1193 path++; /* probably came from server browsing */
1195 if ((remote_path =
1196 smbfs_get_host_and_username (&path, &host, &user, &port, &pass)))
1197 if ((*sc =
1198 smbfs_open_link (host, remote_path, user, &port, pass)) == NULL) {
1199 g_free (remote_path);
1200 remote_path = NULL;
1202 g_free (host);
1203 g_free (user);
1204 if (pass)
1205 wipe_password (pass);
1207 if (!remote_path)
1208 return NULL;
1210 /* NOTE: tildes are deprecated. See ftpfs.c */
1212 int f = !strcmp (remote_path, "/~");
1213 if (f || !strncmp (remote_path, "/~/", 3)) {
1214 char *s;
1215 s = concat_dir_and_file ((*sc)->home, remote_path + 3 - f);
1216 g_free (remote_path);
1217 return s;
1220 return remote_path;
1223 #if 0
1224 static int
1225 is_error (int result, int errno_num)
1227 if (!(result == -1))
1228 return my_errno = 0;
1229 else
1230 my_errno = errno_num;
1231 return 1;
1233 #endif
1235 static void *
1236 smbfs_opendir (struct vfs_class *me, const char *dirname)
1238 opendir_info *smbfs_info;
1239 smbfs_connection *sc;
1240 char *remote_dir;
1242 (void) me;
1244 DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname));
1246 if (!(remote_dir = smbfs_get_path (&sc, dirname)))
1247 return NULL;
1249 /* FIXME: where freed? */
1250 smbfs_info = g_new (opendir_info, 1);
1251 smbfs_info->server_list = FALSE;
1252 smbfs_info->path = g_strdup(dirname); /* keep original */
1253 smbfs_info->dirname = remote_dir;
1254 smbfs_info->conn = sc;
1255 smbfs_info->entries = 0;
1256 smbfs_info->current = 0;
1258 return smbfs_info;
1261 static int
1262 smbfs_fake_server_stat (const char *server_url, const char *path, struct stat *buf)
1264 dir_entry *dentry;
1265 const char *p;
1267 (void) server_url;
1269 if ((p = strrchr (path, '/')))
1270 path = p + 1; /* advance until last '/' */
1272 if (!current_info->entries) {
1273 if (!smbfs_loaddir (current_info)) /* browse host */
1274 return -1;
1277 if (current_info->server_list == True) {
1278 dentry = current_info->entries;
1279 DEBUG (4, ("fake stat for SERVER \"%s\"\n", path));
1280 while (dentry) {
1281 if (strcmp (dentry->text, path) == 0) {
1282 DEBUG (4, ("smbfs_fake_server_stat: %s:%4o\n",
1283 dentry->text, (int)dentry->my_stat.st_mode));
1284 memcpy (buf, &dentry->my_stat, sizeof (struct stat));
1285 return 0;
1287 dentry = dentry->next;
1290 my_errno = ENOENT;
1291 return -1;
1294 static int
1295 smbfs_fake_share_stat (const char *server_url, const char *path, struct stat *buf)
1297 dir_entry *dentry;
1298 if (strlen (path) < strlen (server_url))
1299 return -1;
1301 if (!current_share_info) { /* Server was not stat()ed */
1302 /* Make sure there is such share at server */
1303 smbfs_connection *sc;
1304 char *p;
1305 p = smbfs_get_path (&sc, path);
1306 g_free (p);
1307 if (p) {
1308 memset (buf, 0, sizeof (*buf));
1309 /* show this as dir */
1310 buf->st_mode =
1311 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
1312 S_IXOTH) & myumask;
1313 return 0;
1315 return -1;
1318 path += strlen (server_url); /* we only want share name */
1319 path++;
1321 if (*path == '/') /* '/' leading server name */
1322 path++; /* probably came from server browsing */
1324 if (!current_share_info->entries) {
1325 if (!smbfs_loaddir (current_share_info)) /* browse host */
1326 return -1;
1328 dentry = current_share_info->entries;
1329 DEBUG (3, ("smbfs_fake_share_stat: %s on %s\n", path, server_url));
1330 while (dentry) {
1331 if (strcmp (dentry->text, path) == 0) {
1332 DEBUG (6, ("smbfs_fake_share_stat: %s:%4o\n",
1333 dentry->text, (int) dentry->my_stat.st_mode));
1334 memcpy (buf, &dentry->my_stat, sizeof (struct stat));
1335 return 0;
1337 dentry = dentry->next;
1339 my_errno = ENOENT;
1340 return -1;
1343 /* stat a single file, smbfs_get_remote_stat callback */
1344 static dir_entry *single_entry;
1346 /* stat a single file */
1347 static int
1348 smbfs_get_remote_stat (smbfs_connection * sc, const char *path, struct stat *buf)
1350 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
1351 char *mypath;
1353 DEBUG (3, ("smbfs_get_remote_stat(): mypath:%s\n", path));
1355 mypath = smbfs_convert_path (path, FALSE);
1357 #if 0 /* single_entry is never free()d now. And only my_stat is used */
1358 single_entry = g_new (dir_entry, 1);
1360 single_entry->text = dos_to_unix (g_strdup (finfo->name), 1);
1362 single_entry->next = 0;
1363 #endif
1364 if (!single_entry)
1365 single_entry = g_new0 (dir_entry, 1);
1367 if (cli_list
1368 (sc->cli, mypath, attribute, smbfs_loaddir_helper, single_entry) < 1) {
1369 my_errno = ENOENT;
1370 g_free (mypath);
1371 return -1; /* cli_list returns number of files */
1374 memcpy (buf, &single_entry->my_stat, sizeof (struct stat));
1376 /* don't free here, use for smbfs_fstat() */
1377 /* g_free(single_entry->text);
1378 g_free(single_entry); */
1379 g_free (mypath);
1380 return 0;
1383 static int
1384 smbfs_search_dir_entry (dir_entry *dentry, const char *text, struct stat *buf)
1386 while (dentry) {
1387 if (strcmp(text, dentry->text) == 0) {
1388 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1389 memcpy(&single_entry->my_stat, &dentry->my_stat,
1390 sizeof(struct stat));
1391 return 0;
1393 dentry = dentry->next;
1395 return -1;
1398 static int
1399 smbfs_get_stat_info (smbfs_connection * sc, const char *path, struct stat *buf)
1401 char *p;
1402 #if 0
1403 dir_entry *dentry = current_info->entries;
1404 #endif
1405 const char *mypath = path;
1407 mypath++; /* cut off leading '/' */
1408 if ((p = strrchr (mypath, '/')))
1409 mypath = p + 1; /* advance until last file/dir name */
1410 DEBUG (3, ("smbfs_get_stat_info: mypath:%s, current_info->dirname:%s\n",
1411 mypath, current_info->dirname));
1412 #if 0
1413 if (!dentry) {
1414 DEBUG (1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n",
1415 current_info->dirname, path));
1416 return -1;
1418 #endif
1419 if (!single_entry) /* when found, this will be written too */
1420 single_entry = g_new (dir_entry, 1);
1421 if (smbfs_search_dir_entry (current_info->entries, mypath, buf) == 0) {
1422 return 0;
1424 /* now try to identify mypath as PARENT dir */
1426 char *mdp;
1427 char *mydir;
1428 mdp = mydir = g_strdup (current_info->dirname);
1429 if ((p = strrchr (mydir, '/')))
1430 *p = 0; /* advance util last '/' */
1431 if ((p = strrchr (mydir, '/')))
1432 mydir = p + 1; /* advance util last '/' */
1433 if (strcmp (mydir, mypath) == 0) { /* fake a stat for ".." */
1434 memset (buf, 0, sizeof (struct stat));
1435 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1436 memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
1437 g_free (mdp);
1438 DEBUG (1, (" PARENT:found in %s\n", current_info->dirname));
1439 return 0;
1441 g_free (mdp);
1443 /* now try to identify as CURRENT dir? */
1445 char *dnp = current_info->dirname;
1446 DEBUG (6, ("smbfs_get_stat_info: is %s current dir? this dir is: %s\n",
1447 mypath, current_info->dirname));
1448 if (*dnp == '/')
1449 dnp++;
1450 else {
1451 return -1;
1453 if (strcmp (mypath, dnp) == 0) {
1454 memset (buf, 0, sizeof (struct stat));
1455 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1456 memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
1457 DEBUG (1, (" CURRENT:found in %s\n", current_info->dirname));
1458 return 0;
1461 DEBUG (3, ("'%s' not found in current_info '%s'\n", path,
1462 current_info->dirname));
1463 /* try to find this in the PREVIOUS listing */
1464 if (previous_info) {
1465 if (smbfs_search_dir_entry (previous_info->entries, mypath, buf) == 0)
1466 return 0;
1467 DEBUG (3, ("'%s' not found in previous_info '%s'\n", path,
1468 previous_info->dirname));
1470 /* try to find this in the SHARE listing */
1471 if (current_share_info) {
1472 if (smbfs_search_dir_entry (current_share_info->entries, mypath, buf) == 0)
1473 return 0;
1474 DEBUG (3, ("'%s' not found in share_info '%s'\n", path,
1475 current_share_info->dirname));
1477 /* try to find this in the SERVER listing */
1478 if (current_server_info) {
1479 if (smbfs_search_dir_entry (current_server_info->entries, mypath, buf) == 0)
1480 return 0;
1481 DEBUG (3, ("'%s' not found in server_info '%s'\n", path,
1482 current_server_info->dirname));
1484 /* nothing found. get stat file info from server */
1485 return smbfs_get_remote_stat (sc, path, buf);
1488 static int
1489 smbfs_chdir (struct vfs_class *me, const char *path)
1491 char *remote_dir;
1492 smbfs_connection *sc;
1494 (void) me;
1496 DEBUG (3, ("smbfs_chdir(path:%s)\n", path));
1497 if (!(remote_dir = smbfs_get_path (&sc, path)))
1498 return -1;
1499 g_free (remote_dir);
1501 return 0;
1504 static int
1505 smbfs_loaddir_by_name (struct vfs_class *me, const char *path)
1507 void *info;
1508 char *mypath, *p;
1510 mypath = g_strdup(path);
1511 p = strrchr(mypath, '/');
1513 if (p > mypath)
1514 *p = 0;
1515 DEBUG(6, ("smbfs_loaddir_by_name(%s)\n", mypath));
1516 smbfs_chdir(me, mypath);
1517 info = smbfs_opendir (me, mypath);
1518 g_free(mypath);
1519 if (!info)
1520 return -1;
1521 smbfs_readdir(info);
1522 smbfs_loaddir(info);
1523 return 0;
1526 static int
1527 smbfs_stat (struct vfs_class * me, const char *path, struct stat *buf)
1529 smbfs_connection *sc;
1530 pstring server_url;
1531 char *service, *pp, *at;
1532 const char *p;
1534 DEBUG (3, ("smbfs_stat(path:%s)\n", path));
1536 if (!current_info) {
1537 DEBUG (1, ("current_info = NULL: "));
1538 if (smbfs_loaddir_by_name (me, path) < 0)
1539 return -1;
1542 /* check if stating server */
1543 p = path;
1544 if (strncmp (p, URL_HEADER, HEADER_LEN)) {
1545 DEBUG (1, ("'%s' doesnt start with '%s' (length %d)\n",
1546 p, URL_HEADER, HEADER_LEN));
1547 return -1;
1550 p += HEADER_LEN;
1551 if (*p == '/')
1552 p++;
1554 pp = strchr (p, '/'); /* advance past next '/' */
1555 at = strchr (p, '@');
1556 pstrcpy (server_url, URL_HEADER);
1557 if (at && at < pp) { /* user@server */
1558 char *z = &(server_url[sizeof (server_url) - 1]);
1559 const char *s = p;
1561 at = &(server_url [HEADER_LEN]) + (at - p + 1);
1562 if (z > at)
1563 z = at;
1564 at = &(server_url [HEADER_LEN]);
1565 while (at < z)
1566 *at++ = *s++;
1567 *z = 0;
1569 pstrcat (server_url, current_bucket->host);
1571 if (!pp) {
1572 if (!current_info->server_list) {
1573 if (smbfs_loaddir_by_name (me, path) < 0)
1574 return -1;
1576 return smbfs_fake_server_stat (server_url, path, buf);
1579 if (!strchr (++pp, '/')) {
1580 return smbfs_fake_share_stat (server_url, path, buf);
1583 /* stating inside share at this point */
1584 if (!(service = smbfs_get_path (&sc, path))) /* connects if necessary */
1585 return -1;
1587 int hostlen = strlen (current_bucket->host);
1588 char *ppp = service + strlen (service) - hostlen;
1589 char *sp = server_url + strlen (server_url) - hostlen;
1591 if (strcmp (sp, ppp) == 0) {
1592 /* make server name appear as directory */
1593 DEBUG (1, ("smbfs_stat: showing server as directory\n"));
1594 memset (buf, 0, sizeof (struct stat));
1595 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1596 g_free (service);
1597 return 0;
1600 /* check if current_info is in share requested */
1601 p = service;
1602 pp = strchr (p, '/');
1603 if (pp) {
1604 p = ++pp; /* advance past server name */
1605 pp = strchr (p, '/');
1607 if (pp)
1608 *pp = 0; /* cut off everthing after service name */
1609 else
1610 p = IPC; /* browsing for services */
1611 pp = current_info->dirname;
1612 if (*pp == '/')
1613 pp++;
1614 if (strncmp (p, pp, strlen (p)) != 0) {
1615 DEBUG (6, ("desired '%s' is not loaded, we have '%s'\n", p, pp));
1616 if (smbfs_loaddir_by_name (me, path) < 0) {
1617 g_free (service);
1618 return -1;
1620 DEBUG (6, ("loaded dir: '%s'\n", current_info->dirname));
1622 g_free (service);
1623 /* stat dirs & files under shares now */
1624 return smbfs_get_stat_info (sc, path, buf);
1627 #define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */
1629 static off_t
1630 smbfs_lseek (void *data, off_t offset, int whence)
1632 smbfs_handle *info = (smbfs_handle *) data;
1633 size_t size;
1635 DEBUG (3,
1636 ("smbfs_lseek(info->nread => %d, offset => %d, whence => %d) \n",
1637 (int) info->nread, (int) offset, whence));
1639 switch (whence) {
1640 case SEEK_SET:
1641 info->nread = offset;
1642 break;
1643 case SEEK_CUR:
1644 info->nread += offset;
1645 break;
1646 case SEEK_END:
1647 if (!cli_qfileinfo (info->cli, info->fnum,
1648 NULL, &size, NULL, NULL, NULL,
1649 NULL, NULL) &&
1650 !cli_getattrE (info->cli, info->fnum,
1651 NULL, &size, NULL, NULL, NULL)) {
1652 errno = EINVAL;
1653 return -1;
1655 info->nread = size + offset;
1656 break;
1659 return info->nread;
1662 static int
1663 smbfs_mknod (struct vfs_class *me, const char *path, int mode, int dev)
1665 (void) me;
1667 DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%d)\n", path, mode, dev));
1668 my_errno = EOPNOTSUPP;
1669 return -1;
1672 static int
1673 smbfs_mkdir (struct vfs_class * me, const char *path, mode_t mode)
1675 smbfs_connection *sc;
1676 char *remote_file;
1677 char *cpath;
1679 (void) me;
1681 DEBUG (3, ("smbfs_mkdir(path:%s, mode:%d)\n", path, (int) mode));
1682 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1683 return -1;
1684 g_free (remote_file);
1685 cpath = smbfs_convert_path (path, FALSE);
1687 if (!cli_mkdir (sc->cli, cpath)) {
1688 my_errno = cli_error (sc->cli, NULL, &err, NULL);
1689 message (D_ERROR, MSG_ERROR, _(" Error %s creating directory %s "),
1690 cli_errstr (sc->cli), CNV_LANG (cpath));
1691 g_free (cpath);
1692 return -1;
1694 g_free (cpath);
1695 return 0;
1698 static int
1699 smbfs_rmdir (struct vfs_class *me, const char *path)
1701 smbfs_connection *sc;
1702 char *remote_file;
1703 char *cpath;
1705 (void) me;
1707 DEBUG(3, ("smbfs_rmdir(path:%s)\n", path));
1708 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1709 return -1;
1710 g_free (remote_file);
1711 cpath = smbfs_convert_path (path, FALSE);
1713 if (!cli_rmdir(sc->cli, cpath)) {
1714 my_errno = cli_error(sc->cli, NULL, &err, NULL);
1715 message (D_ERROR, MSG_ERROR, _(" Error %s removing directory %s "),
1716 cli_errstr(sc->cli), CNV_LANG(cpath));
1717 g_free (cpath);
1718 return -1;
1721 g_free (cpath);
1722 return 0;
1725 static int
1726 smbfs_link (struct vfs_class *me, const char *p1, const char *p2)
1728 (void) me;
1730 DEBUG (3, ("smbfs_link(p1:%s, p2:%s)\n", p1, p2));
1731 my_errno = EOPNOTSUPP;
1732 return -1;
1735 static void
1736 smbfs_free (vfsid id)
1738 DEBUG (3, ("smbfs_free(%p)\n", id));
1739 smbfs_auth_free_all ();
1742 /* Gives up on a socket and reopens the connection, the child own the socket
1743 * now
1745 static void
1746 smbfs_forget (const char *path)
1748 char *host, *user, *p;
1749 int port, i;
1751 if (strncmp (path, URL_HEADER, HEADER_LEN))
1752 return;
1754 DEBUG (3, ("smbfs_forget(path:%s)\n", path));
1756 path += 6;
1757 if (path[0] == '/' && path[1] == '/')
1758 path += 2;
1760 if ((p = smbfs_get_host_and_username (&path, &host, &user, &port, NULL))) {
1761 g_free (p);
1762 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1763 if (smbfs_connections[i].cli
1764 && (strcmp (host, smbfs_connections[i].host) == 0)
1765 && (strcmp (user, smbfs_connections[i].user) == 0)
1766 && (port == smbfs_connections[i].port)) {
1768 /* close socket: the child owns it now */
1769 cli_shutdown (smbfs_connections[i].cli);
1771 /* reopen the connection */
1772 smbfs_connections[i].cli =
1773 smbfs_do_connect (host, smbfs_connections[i].service);
1777 g_free (host);
1778 g_free (user);
1781 static int
1782 smbfs_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg)
1784 (void) me;
1785 (void) arg;
1787 DEBUG (3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path, ctlop));
1788 switch (ctlop) {
1789 case VFS_SETCTL_FORGET:
1790 smbfs_forget (path);
1791 return 0;
1793 return 0;
1797 static smbfs_handle *
1798 smbfs_open_readwrite (smbfs_handle *remote_handle, char *rname, int flags, int mode)
1800 size_t size;
1802 (void) mode;
1804 if (flags & O_TRUNC) /* if it exists truncate to zero */
1805 DEBUG (3, ("smbfs_open: O_TRUNC\n"));
1807 remote_handle->fnum =
1808 #if 1 /* Don't play with flags, it is cli_open() headache */
1809 cli_open (remote_handle->cli, rname, flags, DENY_NONE);
1810 #else /* What's a reasons to has this code ? */
1811 cli_open (remote_handle->cli, rname, ((flags & O_CREAT)
1812 || (flags ==
1813 (O_WRONLY | O_APPEND))) ?
1814 flags : O_RDONLY, DENY_NONE);
1815 #endif
1816 if (remote_handle->fnum == -1) {
1817 message (D_ERROR, MSG_ERROR, _(" %s opening remote file %s "),
1818 cli_errstr (remote_handle->cli), CNV_LANG (rname));
1819 DEBUG (1, ("smbfs_open(rname:%s) error:%s\n",
1820 rname, cli_errstr (remote_handle->cli)));
1821 my_errno = cli_error (remote_handle->cli, NULL, &err, NULL);
1822 return NULL;
1825 if (flags & O_CREAT)
1826 return remote_handle;
1828 if (!cli_qfileinfo (remote_handle->cli, remote_handle->fnum,
1829 &remote_handle->attr, &size, NULL, NULL, NULL, NULL,
1830 NULL)
1831 && !cli_getattrE (remote_handle->cli, remote_handle->fnum,
1832 &remote_handle->attr, &size, NULL, NULL, NULL)) {
1833 message (D_ERROR, MSG_ERROR, " getattrib: %s ",
1834 cli_errstr (remote_handle->cli));
1835 DEBUG (1,
1836 ("smbfs_open(rname:%s) getattrib:%s\n", rname,
1837 cli_errstr (remote_handle->cli)));
1838 my_errno = cli_error (remote_handle->cli, NULL, &err, NULL);
1839 cli_close (remote_handle->cli, remote_handle->fnum);
1840 return NULL;
1843 if ((flags == (O_WRONLY | O_APPEND)) /* file.c:copy_file_file() -> do_append */
1844 && smbfs_lseek (remote_handle, 0, SEEK_END) == -1) {
1845 cli_close (remote_handle->cli, remote_handle->fnum);
1846 return NULL;
1849 return remote_handle;
1852 static void *
1853 smbfs_open (struct vfs_class *me, const char *file, int flags, int mode)
1855 char *remote_file;
1856 void *ret;
1857 smbfs_connection *sc;
1858 smbfs_handle *remote_handle;
1860 (void) me;
1862 DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%o)\n", file, flags, mode));
1864 if (!(remote_file = smbfs_get_path (&sc, file)))
1865 return 0;
1867 remote_file = free_after(smbfs_convert_path (remote_file, FALSE), remote_file);
1869 remote_handle = g_new (smbfs_handle, 2);
1870 remote_handle->cli = sc->cli;
1871 remote_handle->nread = 0;
1873 ret = smbfs_open_readwrite (remote_handle, remote_file, flags, mode);
1875 g_free (remote_file);
1876 if (!ret)
1877 g_free (remote_handle);
1879 return ret;
1882 static int
1883 smbfs_unlink (struct vfs_class *me, const char *path)
1885 smbfs_connection *sc;
1886 char *remote_file;
1888 (void) me;
1890 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1891 return -1;
1893 remote_file = free_after(smbfs_convert_path (remote_file, FALSE), remote_file);
1895 if (!cli_unlink(sc->cli, remote_file)) {
1896 message (D_ERROR, MSG_ERROR, _(" %s removing remote file %s "),
1897 cli_errstr(sc->cli), CNV_LANG(remote_file));
1898 g_free (remote_file);
1899 return -1;
1901 g_free (remote_file);
1902 return 0;
1905 static int
1906 smbfs_rename (struct vfs_class *me, const char *a, const char *b)
1908 smbfs_connection *sc;
1909 char *ra, *rb;
1910 int retval;
1912 (void) me;
1914 if ((ra = smbfs_get_path (&sc, a)) == 0)
1915 return -1;
1917 if ((rb = smbfs_get_path (&sc, b)) == 0) {
1918 g_free (ra);
1919 return -1;
1922 ra = free_after (smbfs_convert_path (ra, FALSE), ra);
1923 rb = free_after (smbfs_convert_path (rb, FALSE), rb);
1925 retval = cli_rename(sc->cli, ra, rb);
1927 g_free (ra);
1928 g_free (rb);
1930 if (!retval) {
1931 message (D_ERROR, MSG_ERROR, _(" %s renaming files\n"),
1932 cli_errstr(sc->cli));
1933 return -1;
1935 return 0;
1938 static int
1939 smbfs_fstat (void *data, struct stat *buf)
1941 smbfs_handle *remote_handle = (smbfs_handle *)data;
1943 DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle->fnum));
1945 /* use left over from previous smbfs_get_remote_stat, if available */
1946 if (single_entry)
1947 memcpy(buf, &single_entry->my_stat, sizeof(struct stat));
1948 else { /* single_entry not set up: bug */
1949 my_errno = EFAULT;
1950 return -EFAULT;
1952 return 0;
1955 void
1956 init_smbfs (void)
1958 vfs_smbfs_ops.name = "smbfs";
1959 vfs_smbfs_ops.prefix = "smb:";
1960 vfs_smbfs_ops.flags = VFSF_NOLINKS;
1961 vfs_smbfs_ops.init = smbfs_init;
1962 vfs_smbfs_ops.fill_names = smbfs_fill_names;
1963 vfs_smbfs_ops.open = smbfs_open;
1964 vfs_smbfs_ops.close = smbfs_close;
1965 vfs_smbfs_ops.read = smbfs_read;
1966 vfs_smbfs_ops.write = smbfs_write;
1967 vfs_smbfs_ops.opendir = smbfs_opendir;
1968 vfs_smbfs_ops.readdir = smbfs_readdir;
1969 vfs_smbfs_ops.closedir = smbfs_closedir;
1970 vfs_smbfs_ops.stat = smbfs_stat;
1971 vfs_smbfs_ops.lstat = smbfs_lstat;
1972 vfs_smbfs_ops.fstat = smbfs_fstat;
1973 vfs_smbfs_ops.chmod = smbfs_chmod;
1974 vfs_smbfs_ops.chown = smbfs_chown;
1975 vfs_smbfs_ops.utime = smbfs_utime;
1976 vfs_smbfs_ops.readlink = smbfs_readlink;
1977 vfs_smbfs_ops.symlink = smbfs_symlink;
1978 vfs_smbfs_ops.link = smbfs_link;
1979 vfs_smbfs_ops.unlink = smbfs_unlink;
1980 vfs_smbfs_ops.rename = smbfs_rename;
1981 vfs_smbfs_ops.chdir = smbfs_chdir;
1982 vfs_smbfs_ops.ferrno = smbfs_errno;
1983 vfs_smbfs_ops.lseek = smbfs_lseek;
1984 vfs_smbfs_ops.mknod = smbfs_mknod;
1985 vfs_smbfs_ops.free = smbfs_free;
1986 vfs_smbfs_ops.mkdir = smbfs_mkdir;
1987 vfs_smbfs_ops.rmdir = smbfs_rmdir;
1988 vfs_smbfs_ops.setctl = smbfs_setctl;
1989 vfs_register_class (&vfs_smbfs_ops);