Merge commit 'origin/340_inrorrect_handling_of_wildcards'
[midnight-commander.git] / vfs / smbfs.c
bloba906dfee8ddd070277cb987694cf75e95a36643f
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 /**
24 * \file
25 * \brief Source: Virtual File System: smb file system
26 * \author Wayne Roberts <wroberts1@home.com>
27 * \author Andrew V. Samoilov <sav@bcs.zp.ua>
28 * \date 1997, 2002, 2003
30 * Namespace: exports init_smbfs, smbfs_set_debug(), smbfs_set_debugf()
33 #include <config.h>
35 #include <stdio.h>
36 #include <sys/types.h>
38 #undef USE_NCURSES /* Don't include *curses.h */
39 #undef USE_NCURSESW
40 #include "../src/global.h"
41 #include "../src/tty.h" /* enable/disable interrupt key */
42 #include "../src/wtools.h" /* message() */
43 #include "../src/main.h" /* print_vfs_message */
44 #include "utilvfs.h"
46 #undef PACKAGE_BUGREPORT
47 #undef PACKAGE_NAME
48 #undef PACKAGE_STRING
49 #undef PACKAGE_TARNAME
50 #undef PACKAGE_VERSION
52 #include "samba/include/config.h"
53 /* don't load crap in "samba/include/includes.h" we don't use and which
54 conflicts with definitions in other includes */
55 #undef HAVE_LIBREADLINE
56 #define NO_CONFIG_H
57 #undef VERSION
59 #include "samba/include/includes.h"
61 #include <string.h>
63 #include "vfs.h"
64 #include "vfs-impl.h"
65 #include "smbfs.h"
67 #define SMBFS_MAX_CONNECTIONS 16
68 static const char * const IPC = "IPC$";
69 static const char * const URL_HEADER = "/#smb:";
70 #define HEADER_LEN 6
72 static int my_errno;
73 static uint32 err;
75 /* stuff that is same with each connection */
76 extern int DEBUGLEVEL;
77 extern pstring myhostname;
78 extern struct in_addr ipzero;
79 static mode_t myumask = 0755;
80 extern pstring global_myname;
81 static int smbfs_open_connections = 0;
82 static gboolean got_user = FALSE;
83 static gboolean got_pass = FALSE;
84 static pstring password;
85 static pstring username;
86 static struct vfs_class vfs_smbfs_ops;
88 static struct _smbfs_connection {
89 struct cli_state *cli;
90 struct in_addr dest_ip;
91 BOOL have_ip;
92 char *host; /* server name */
93 char *service; /* share name */
94 char *domain;
95 char *user;
96 char *home;
97 char *password;
98 int port;
99 int name_type;
100 time_t last_use;
101 } smbfs_connections [SMBFS_MAX_CONNECTIONS];
102 /* unique to each connection */
104 /* modifies *share */
105 static struct cli_state * smbfs_do_connect (const char *server, char *share);
107 typedef struct _smbfs_connection smbfs_connection;
108 static smbfs_connection *current_bucket;
110 typedef struct {
111 struct cli_state *cli;
112 int fnum;
113 off_t nread;
114 uint16 attr;
115 } smbfs_handle;
117 static GSList *auth_list;
119 /* this function allows you to write:
120 * char *s = g_strdup("hello, world");
121 * s = free_after(g_strconcat(s, s, (char *)0), s);
123 static inline char *
124 free_after (char *result, char *string_to_free)
126 g_free(string_to_free);
127 return result;
131 static void
132 smbfs_auth_free (struct smb_authinfo const *a)
134 g_free (a->host);
135 g_free (a->share);
136 g_free (a->domain);
137 g_free (a->user);
138 wipe_password (a->password);
141 static void
142 smbfs_auth_free_all (void)
144 if (auth_list) {
145 g_slist_foreach (auth_list, (GFunc)smbfs_auth_free, 0);
146 g_slist_free (auth_list);
147 auth_list = 0;
151 static gint
152 smbfs_auth_cmp_host_and_share (gconstpointer _a, gconstpointer _b)
154 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
155 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
157 if (!a->host || !a->share || !b->host || !b->share)
158 return 1;
159 if (strcmp (a->host, b->host) != 0)
160 return 1;
161 if (strcmp (a->share, b->share) != 0)
162 return 1;
163 return 0;
166 static gint
167 smbfs_auth_cmp_host (gconstpointer _a, gconstpointer _b)
169 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
170 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
172 if (!a->host || !b->host)
173 return 1;
174 if (strcmp (a->host, b->host) != 0)
175 return 1;
176 if (strcmp (a->share, IPC) != 0)
177 return 1;
178 return 0;
181 static void
182 smbfs_auth_add (const char *host, const char *share, const char *domain,
183 const char *user, const char *password)
185 struct smb_authinfo *auth = g_new (struct smb_authinfo, 1);
187 if (!auth)
188 return;
190 /* Don't check for NULL, g_strdup already does. */
191 auth->host = g_strdup (host);
192 auth->share = g_strdup (share);
193 auth->domain = g_strdup (domain);
194 auth->user = g_strdup (user);
195 auth->password = g_strdup (password);
196 auth_list = g_slist_prepend (auth_list, auth);
199 static void
200 smbfs_auth_remove (const char *host, const char *share)
202 struct smb_authinfo data;
203 struct smb_authinfo *auth;
204 GSList *list;
206 data.host = g_strdup (host);
207 data.share = g_strdup (share);
208 list = g_slist_find_custom (auth_list,
209 &data,
210 smbfs_auth_cmp_host_and_share);
211 g_free (data.host);
212 g_free (data.share);
213 if (!list)
214 return;
215 auth = list->data;
216 auth_list = g_slist_remove (auth_list, auth);
217 smbfs_auth_free (auth);
220 /* Set authentication information in bucket. Return 1 if successful, else 0 */
221 /* Information in auth_list overrides user if pass is NULL. */
222 /* bucket->host and bucket->service must be valid. */
223 static int
224 smbfs_bucket_set_authinfo (smbfs_connection *bucket,
225 const char *domain, const char *user, const char *pass,
226 int fallback_to_host)
228 struct smb_authinfo data;
229 struct smb_authinfo *auth;
230 GSList *list;
232 if (domain && user && pass) {
233 g_free (bucket->domain);
234 g_free (bucket->user);
235 g_free (bucket->password);
236 bucket->domain = g_strdup (domain);
237 bucket->user = g_strdup (user);
238 bucket->password = g_strdup (pass);
239 smbfs_auth_remove (bucket->host, bucket->service);
240 smbfs_auth_add (bucket->host, bucket->service,
241 domain, user, pass);
242 return 1;
245 data.host = bucket->host;
246 data.share = bucket->service;
247 list = g_slist_find_custom (auth_list, &data, smbfs_auth_cmp_host_and_share);
248 if (!list && fallback_to_host)
249 list = g_slist_find_custom (auth_list, &data, smbfs_auth_cmp_host);
250 if (list) {
251 auth = list->data;
252 bucket->domain = g_strdup (auth->domain);
253 bucket->user = g_strdup (auth->user);
254 bucket->password = g_strdup (auth->password);
255 return 1;
258 if (got_pass) {
259 bucket->domain = g_strdup (lp_workgroup ());
260 bucket->user = g_strdup (got_user ? username : user);
261 bucket->password = g_strdup (password);
262 return 1;
265 auth = vfs_smb_get_authinfo (bucket->host,
266 bucket->service,
267 (domain ? domain : lp_workgroup ()),
268 user);
269 if (auth) {
270 g_free (bucket->domain);
271 g_free (bucket->user);
272 g_free (bucket->password);
273 bucket->domain = g_strdup (auth->domain);
274 bucket->user = g_strdup (auth->user);
275 bucket->password = g_strdup (auth->password);
276 smbfs_auth_remove (bucket->host, bucket->service);
277 auth_list = g_slist_prepend (auth_list, auth);
278 return 1;
280 return 0;
283 void
284 smbfs_set_debug (int arg)
286 DEBUGLEVEL = arg;
289 void
290 smbfs_set_debugf (const char *filename)
292 extern pstring debugf;
293 extern FILE *dbf;
294 if (DEBUGLEVEL > 0) {
295 FILE *outfile = fopen (filename, "w");
296 if (outfile) {
297 setup_logging ("", True); /* No needs for timestamp for each message */
298 dbf = outfile;
299 setbuf (dbf, NULL);
300 pstrcpy (debugf, filename);
305 /********************** The callbacks ******************************/
306 static int
307 smbfs_init (struct vfs_class * me)
309 const char *servicesf = CONFIGDIR PATH_SEP_STR "smb.conf";
311 /* DEBUGLEVEL = 4; */
313 TimeInit ();
314 charset_initialise ();
316 DEBUG (3, ("smbfs_init(%s)\n", me->name));
318 if (!get_myname (myhostname, NULL))
319 DEBUG (0, ("Failed to get my hostname.\n"));
321 if (!lp_load (servicesf, True, False, False))
322 DEBUG (0, ("Cannot load %s - run testparm to debug it\n", servicesf));
324 codepage_initialise (lp_client_code_page ());
326 load_interfaces ();
328 myumask = umask (0);
329 umask (myumask);
330 myumask = ~myumask;
332 if (getenv ("USER")) {
333 char *p;
335 pstrcpy (username, getenv ("USER"));
336 got_user = TRUE;
337 DEBUG (3, ("smbfs_init(): $USER:%s\n", username));
338 if ((p = strchr (username, '%'))) {
339 *p = 0;
340 pstrcpy (password, p + 1);
341 got_pass = TRUE;
342 memset (strchr (getenv ("USER"), '%') + 1, 'X', strlen (password));
343 DEBUG (3, ("smbfs_init(): $USER%%pass: %s%%%s\n",
344 username, password));
346 strupper (username);
348 if (getenv ("PASSWD")) {
349 pstrcpy (password, getenv ("PASSWD"));
350 got_pass = TRUE;
352 return 1;
355 static void
356 smbfs_fill_names (struct vfs_class *me, fill_names_f func)
358 int i;
359 char *path;
361 (void) me;
363 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
364 if (smbfs_connections [i].cli) {
365 path = g_strconcat (URL_HEADER,
366 smbfs_connections[i].user, "@",
367 smbfs_connections[i].host,
368 "/", smbfs_connections[i].service,
369 NULL);
370 (*func)(path);
371 g_free (path);
376 #define CNV_LANG(s) dos_to_unix(s,False)
377 #define GNAL_VNC(s) unix_to_dos(s,False)
378 /* does same as do_get() in client.c */
379 /* called from vfs.c:1080, count = buffer size */
380 static ssize_t
381 smbfs_read (void *data, char *buffer, int count)
383 smbfs_handle *info = (smbfs_handle *) data;
384 int n;
386 DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%d)\n",
387 info->fnum, (int)info->nread, count));
388 n = cli_read(info->cli, info->fnum, buffer, info->nread, count);
389 if (n > 0)
390 info->nread += n;
391 return n;
394 static ssize_t
395 smbfs_write (void *data, const char *buf, int nbyte)
397 smbfs_handle *info = (smbfs_handle *) data;
398 int n;
400 DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%d)\n",
401 info->fnum, (int)info->nread, nbyte));
402 n = cli_write(info->cli, info->fnum, 0, buf, info->nread, nbyte);
403 if (n > 0)
404 info->nread += n;
405 return n;
408 static int
409 smbfs_close (void *data)
411 smbfs_handle *info = (smbfs_handle *) data;
412 DEBUG (3, ("smbfs_close(fnum:%d)\n", info->fnum));
414 /* FIXME: Why too different cli have the same outbuf
415 * if file is copied to share
417 if (info->cli->outbuf == NULL) {
418 my_errno = EINVAL;
419 return -1;
421 #if 0
422 /* if imlementing archive_level: add rname to smbfs_handle */
423 if (archive_level >= 2 && (inf->attr & aARCH)) {
424 cli_setatr (info->cli, rname, info->attr & ~(uint16) aARCH, 0);
426 #endif
427 return (cli_close (info->cli, info->fnum) == True) ? 0 : -1;
430 static int
431 smbfs_errno (struct vfs_class *me)
433 (void) me;
435 DEBUG(3, ("smbfs_errno: %s\n", g_strerror(my_errno)));
436 return my_errno;
439 typedef struct dir_entry {
440 char *text;
441 struct dir_entry *next;
442 struct stat my_stat;
443 int merrno;
444 } dir_entry;
446 typedef struct {
447 gboolean server_list;
448 char *dirname;
449 char *path; /* the dir originally passed to smbfs_opendir */
450 smbfs_connection *conn;
451 dir_entry *entries;
452 dir_entry *current;
453 } opendir_info;
455 static opendir_info
456 *previous_info,
457 *current_info,
458 *current_share_info,
459 *current_server_info;
461 static gboolean first_direntry;
463 static dir_entry *
464 smbfs_new_dir_entry (const char *name)
466 static int inode_counter;
467 dir_entry *new_entry;
468 new_entry = g_new0 (dir_entry, 1);
469 new_entry->text = dos_to_unix (g_strdup (name), 1);
471 if (first_direntry) {
472 current_info->entries = new_entry;
473 first_direntry = FALSE;
474 } else {
475 current_info->current->next = new_entry;
477 current_info->current = new_entry;
478 new_entry->my_stat.st_ino = inode_counter++;
480 return new_entry;
483 /* browse for shares on server */
484 static void
485 smbfs_browsing_helper (const char *name, uint32 type, const char *comment, void *state)
487 const char *typestr = "";
488 dir_entry *new_entry = smbfs_new_dir_entry (name);
490 (void) state;
492 switch (type) {
493 case STYPE_DISKTREE:
494 typestr = "Disk";
495 /* show this as dir */
496 new_entry->my_stat.st_mode =
497 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
498 S_IXOTH) & myumask;
499 break;
500 case STYPE_PRINTQ:
501 typestr = "Printer";
502 break;
503 case STYPE_DEVICE:
504 typestr = "Device";
505 break;
506 case STYPE_IPC:
507 typestr = "IPC";
508 break;
510 DEBUG (3, ("\t%-15.15s%-10.10s%s\n", name, typestr, comment));
513 static void
514 smbfs_loaddir_helper (file_info * finfo, const char *mask, void *entry)
516 dir_entry *new_entry = (dir_entry *) entry;
517 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
519 (void) mask;
521 #if 0 /* I want to see dot files */
522 if (finfo->mode & aHIDDEN)
523 return; /* don't bother with hidden files, "~$" screws up mc */
524 #endif
525 if (!entry)
526 new_entry = smbfs_new_dir_entry (finfo->name);
528 new_entry->my_stat.st_size = finfo->size;
529 new_entry->my_stat.st_mtime = finfo->mtime;
530 new_entry->my_stat.st_atime = finfo->atime;
531 new_entry->my_stat.st_ctime = finfo->ctime;
532 new_entry->my_stat.st_uid = finfo->uid;
533 new_entry->my_stat.st_gid = finfo->gid;
535 new_entry->my_stat.st_mode = /* rw-rw-rw */
536 S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH;
538 /* if (finfo->mode & aVOLID); nothing similar in real world */
539 if (finfo->mode & aDIR)
540 new_entry->my_stat.st_mode |= /* drwxrwxrwx */
541 S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
542 else
543 new_entry->my_stat.st_mode |= S_IFREG; /* if not dir, regular file? */
544 /* if (finfo->mode & aARCH); DOS archive */
545 /* if (finfo->mode & aHIDDEN); like a dot file? */
546 /* if (finfo->mode & aSYSTEM); like a kernel? */
547 if (finfo->mode & aRONLY)
548 new_entry->my_stat.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
549 new_entry->my_stat.st_mode &= myumask;
551 DEBUG (entry ? 3 : 6, (" %-30s%7.7s%8.0f %s",
552 CNV_LANG (finfo->name),
553 attrib_string (finfo->mode),
554 (double) finfo->size,
555 asctime (LocalTime (&t))));
558 /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
559 static char *
560 smbfs_convert_path (const char *remote_file, gboolean trailing_asterik)
562 const char *p, *my_remote;
563 char *result;
565 my_remote = remote_file;
566 if (strncmp (my_remote, URL_HEADER, HEADER_LEN) == 0) { /* if passed directly */
567 my_remote += HEADER_LEN;
568 if (*my_remote == '/') /* from server browsing */
569 my_remote++;
570 p = strchr(my_remote, '/');
571 if (p)
572 my_remote = p+1; /* advance to end of server name */
575 if (*my_remote == '/')
576 my_remote++; /* strip off leading '/' */
577 p = strchr(my_remote, '/');
578 if (p)
579 my_remote = p; /* strip off share/service name */
580 /* create remote filename as understood by smb clientgen */
581 result = g_strconcat (my_remote, trailing_asterik ? "/*" : "", (char *) NULL);
582 unix_to_dos (result, /* inplace = */ 1); /* code page conversion */
583 str_replace(result, '/', '\\');
584 return result;
587 static void
588 smbfs_srv_browsing_helper (const char *name, uint32 m, const char *comment,
589 void *state)
591 dir_entry *new_entry = smbfs_new_dir_entry (name);
593 (void) m;
594 (void) state;
596 /* show this as dir */
597 new_entry->my_stat.st_mode =
598 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
599 S_IXOTH) & myumask;
601 DEBUG (3, ("\t%-16.16s %s\n", name, comment));
604 static BOOL
605 smbfs_reconnect(smbfs_connection *conn, int *retries)
607 char *host;
608 DEBUG(3, ("RECONNECT\n"));
610 if (*(conn->host) == 0)
611 host = g_strdup(conn->cli->desthost); /* server browsing */
612 else
613 host = g_strdup(conn->host);
615 cli_shutdown(conn->cli);
617 if (!(conn->cli = smbfs_do_connect(host, conn->service))) {
618 message (D_ERROR, MSG_ERROR,
619 _(" reconnect to %s failed\n "), conn->host);
620 g_free(host);
621 return False;
623 g_free(host);
624 if (++(*retries) == 2)
625 return False;
626 return True;
629 static BOOL
630 smbfs_send(struct cli_state *cli)
632 size_t len;
633 size_t nwritten=0;
634 ssize_t ret;
636 len = smb_len(cli->outbuf) + 4;
638 while (nwritten < len) {
639 ret = write_socket(cli->fd, cli->outbuf+nwritten, len - nwritten);
640 if (ret <= 0) {
641 if (errno == EPIPE)
642 return False;
643 } else
644 nwritten += ret;
647 return True;
650 /****************************************************************************
651 See if server has cut us off by checking for EPIPE when writing.
652 Taken from cli_chkpath()
653 ****************************************************************************/
654 static BOOL
655 smbfs_chkpath(struct cli_state *cli, const char *path, BOOL send_only)
657 fstring path2;
658 char *p;
660 fstrcpy(path2,path);
661 unix_to_dos (path2, 1);
662 trim_string(path2,NULL,"\\");
663 if (!*path2) *path2 = '\\';
665 memset(cli->outbuf,'\0',smb_size);
666 set_message(cli->outbuf,0,4 + strlen(path2),True);
667 SCVAL(cli->outbuf,smb_com,SMBchkpth);
668 SSVAL(cli->outbuf,smb_tid,cli->cnum);
670 cli->rap_error = 0;
671 cli->nt_error = 0;
672 SSVAL(cli->outbuf,smb_pid,cli->pid);
673 SSVAL(cli->outbuf,smb_uid,cli->vuid);
674 SSVAL(cli->outbuf,smb_mid,cli->mid);
675 if (cli->protocol > PROTOCOL_CORE) {
676 SCVAL(cli->outbuf,smb_flg,0x8);
677 SSVAL(cli->outbuf,smb_flg2,0x1);
680 p = smb_buf(cli->outbuf);
681 *p++ = 4;
682 fstrcpy(p,path2);
684 if (!smbfs_send(cli)) {
685 DEBUG(3, ("smbfs_chkpath: couldnt send\n"));
686 return False;
688 if (send_only) {
689 client_receive_smb(cli->fd, cli->inbuf, cli->timeout);
690 DEBUG(3, ("smbfs_chkpath: send only OK\n"));
691 return True; /* just testing for EPIPE */
693 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout)) {
694 DEBUG(3, ("smbfs_chkpath: receive error\n"));
695 return False;
697 if ((my_errno = cli_error(cli, NULL, NULL, NULL))) {
698 if (my_errno == 20 || my_errno == 13)
699 return True; /* ignore if 'not a directory' error */
700 DEBUG(3, ("smbfs_chkpath: cli_error: %s\n", g_strerror(my_errno)));
701 return False;
704 return True;
707 #if 1
708 static int
709 smbfs_fs (const char *text)
711 const char *p = text;
712 int count = 0;
714 while ((p = strchr(p, '/')) != NULL) {
715 count++;
716 p++;
718 if (count == 1)
719 return strlen(text);
720 return count;
722 #endif
724 static int
725 smbfs_loaddir (opendir_info *smbfs_info)
727 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
728 int servlen = strlen (smbfs_info->conn->service);
729 const char *info_dirname = smbfs_info->dirname;
730 char *my_dirname;
732 DEBUG (3, ("smbfs_loaddir: dirname:%s\n", info_dirname));
733 first_direntry = TRUE;
735 if (current_info) {
736 DEBUG (3,
737 ("smbfs_loaddir: new:'%s', cached:'%s'\n", info_dirname,
738 current_info->dirname));
739 /* if new desired dir is longer than cached in current_info */
740 if (smbfs_fs (info_dirname) > smbfs_fs (current_info->dirname)) {
741 DEBUG (3, ("saving to previous_info\n"));
742 previous_info = current_info;
746 current_info = smbfs_info;
748 if (strcmp (info_dirname, "/") == 0) {
749 if (!strcmp (smbfs_info->path, URL_HEADER)) {
750 DEBUG (6, ("smbfs_loaddir: browsing %s\n", IPC));
751 /* browse for servers */
752 if (!cli_NetServerEnum
753 (smbfs_info->conn->cli, smbfs_info->conn->domain,
754 SV_TYPE_ALL, smbfs_srv_browsing_helper, NULL))
755 return 0;
756 else
757 current_server_info = smbfs_info;
758 smbfs_info->server_list = TRUE;
759 } else {
760 /* browse for shares */
761 if (cli_RNetShareEnum
762 (smbfs_info->conn->cli, smbfs_browsing_helper, NULL) < 1)
763 return 0;
764 else
765 current_share_info = smbfs_info;
767 goto done;
770 /* do regular directory listing */
771 if (strncmp (smbfs_info->conn->service, info_dirname + 1, servlen) == 0) {
772 /* strip share name from dir */
773 my_dirname = g_strdup (info_dirname + servlen);
774 *my_dirname = '/';
775 my_dirname = free_after(smbfs_convert_path (my_dirname, TRUE), my_dirname);
776 } else
777 my_dirname = smbfs_convert_path (info_dirname, TRUE);
779 DEBUG (6, ("smbfs_loaddir: service: %s\n", smbfs_info->conn->service));
780 DEBUG (6,
781 ("smbfs_loaddir: cli->share: %s\n",
782 smbfs_info->conn->cli->share));
783 DEBUG (6,
784 ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname));
785 /* do file listing: cli_list returns number of files */
786 if (cli_list
787 (smbfs_info->conn->cli, my_dirname, attribute,
788 smbfs_loaddir_helper, NULL) < 0) {
789 /* cli_list returns -1 if directory empty or cannot read socket */
790 my_errno = cli_error (smbfs_info->conn->cli, NULL, &err, NULL);
791 g_free (my_dirname);
792 return 0;
794 if (*(my_dirname) == 0)
795 smbfs_info->dirname = smbfs_info->conn->service;
796 g_free (my_dirname);
797 /* do_dskattr(); */
799 done:
800 /* current_info->parent = smbfs_info->dirname; */
802 smbfs_info->current = smbfs_info->entries;
803 return 1; /* 1 = ok */
806 #ifdef SMBFS_FREE_DIR
807 static void
808 smbfs_free_dir (dir_entry *de)
810 if (!de) return;
812 smbfs_free_dir (de->next);
813 g_free (de->text);
814 g_free (de);
816 #endif
819 /* The readdir routine loads the complete directory */
820 /* It's too slow to ask the server each time */
821 /* It now also sends the complete lstat information for each file */
822 static void *
823 smbfs_readdir(void *info)
825 static union vfs_dirent smbfs_readdir_data;
826 static char *const dirent_dest = smbfs_readdir_data.dent.d_name;
827 opendir_info *smbfs_info = (opendir_info *) info;
829 DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info->dirname));
831 if (!smbfs_info->entries)
832 if (!smbfs_loaddir(smbfs_info))
833 return NULL;
835 if (smbfs_info->current == 0) { /* reached end of dir entries */
836 DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
837 #ifdef SMBFS_FREE_DIR
838 smbfs_free_dir(smbfs_info->entries);
839 smbfs_info->entries = 0;
840 #endif
841 return NULL;
843 g_strlcpy(dirent_dest, smbfs_info->current->text, MC_MAXPATHLEN);
844 smbfs_info->current = smbfs_info->current->next;
846 compute_namelen(&smbfs_readdir_data.dent);
848 return &smbfs_readdir_data;
851 static int
852 smbfs_closedir (void *info)
854 opendir_info *smbfs_info = (opendir_info *) info;
855 /* dir_entry *p, *q; */
857 DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info->dirname));
858 /* CLOSE HERE */
860 /* for (p = smbfs_info->entries; p;){
861 q = p;
862 p = p->next;
863 g_free (q->text);
864 g_free (q);
866 g_free (info); */
867 return 0;
870 static int
871 smbfs_chmod (struct vfs_class *me, const char *path, int mode)
873 (void) me;
875 DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path, mode));
876 /* my_errno = EOPNOTSUPP;
877 return -1; */ /* cannot chmod on smb filesystem */
878 return 0; /* make mc happy */
881 static int
882 smbfs_chown (struct vfs_class *me, const char *path, int owner, int group)
884 (void) me;
886 DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path, owner, group));
887 my_errno = EOPNOTSUPP; /* ready for your labotomy? */
888 return -1;
891 static int
892 smbfs_utime (struct vfs_class *me, const char *path, struct utimbuf *times)
894 (void) me;
895 (void) times;
897 DEBUG(3, ("smbfs_utime(path:%s)\n", path));
898 my_errno = EOPNOTSUPP;
899 return -1;
902 static int
903 smbfs_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
905 (void) me;
907 DEBUG (3,
908 ("smbfs_readlink(path:%s, buf:%s, size:%d)\n", path, buf,
909 (int) size));
910 my_errno = EOPNOTSUPP;
911 return -1; /* no symlinks on smb filesystem? */
914 static int
915 smbfs_symlink (struct vfs_class *me, const char *n1, const char *n2)
917 (void) me;
919 DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1, n2));
920 my_errno = EOPNOTSUPP;
921 return -1; /* no symlinks on smb filesystem? */
924 /* Extract the hostname and username from the path */
925 /* path is in the form: [user@]hostname/share/remote-dir */
926 #define smbfs_get_host_and_username(path, host, user, port, pass) \
927 vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
929 /*****************************************************
930 return a connection to a SMB server
931 current_bucket needs to be set before calling
932 *******************************************************/
933 static struct cli_state *
934 smbfs_do_connect (const char *server, char *share)
936 struct cli_state *c;
937 struct nmb_name called, calling;
938 struct in_addr ip;
940 DEBUG(3, ("smbfs_do_connect(%s, %s)\n", server, share));
941 if (*share == '\\') {
942 server = share+2;
943 share = strchr(server,'\\');
944 if (!share) return NULL;
945 *share = 0;
946 share++;
949 make_nmb_name(&calling, global_myname, 0x0);
950 make_nmb_name(&called , server, current_bucket->name_type);
952 for (;;) {
954 ip = (current_bucket->have_ip) ? current_bucket->dest_ip : ipzero;
956 /* have to open a new connection */
957 if (!(c = cli_initialise(NULL))) {
958 my_errno = ENOMEM;
959 return NULL;
962 pwd_init(&(c->pwd)); /* should be moved into cli_initialise()? */
963 pwd_set_cleartext(&(c->pwd), current_bucket->password);
965 if ((cli_set_port(c, current_bucket->port) == 0) ||
966 !cli_connect(c, server, &ip)) {
967 DEBUG(1, ("Connection to %s failed\n", server));
968 break;
971 if (!cli_session_request(c, &calling, &called)) {
972 my_errno = cli_error(c, NULL, &err, NULL);
973 DEBUG(1, ("session request to %s failed\n", called.name));
974 cli_shutdown(c);
975 if (strcmp(called.name, "*SMBSERVER")) {
976 make_nmb_name(&called , "*SMBSERVER", 0x20);
977 continue;
979 return NULL;
982 DEBUG(3, (" session request ok\n"));
984 if (!cli_negprot(c)) {
985 DEBUG(1, ("protocol negotiation failed\n"));
986 break;
989 if (!cli_session_setup(c, current_bucket->user,
990 current_bucket->password, strlen(current_bucket->password),
991 current_bucket->password, strlen(current_bucket->password),
992 current_bucket->domain)) {
993 DEBUG(1,("session setup failed: %s\n", cli_errstr(c)));
994 smbfs_auth_remove (server, share);
995 break;
998 if (*c->server_domain || *c->server_os || *c->server_type)
999 DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
1000 c->server_domain,c->server_os,c->server_type));
1002 DEBUG(3, (" session setup ok\n"));
1004 if (!cli_send_tconX(c, share, "?????",
1005 current_bucket->password, strlen(current_bucket->password)+1)) {
1006 DEBUG(1,("%s: tree connect failed: %s\n", share, cli_errstr(c)));
1007 break;
1010 DEBUG(3, (" tconx ok\n"));
1012 my_errno = 0;
1013 return c;
1016 my_errno = cli_error(c, NULL, &err, NULL);
1017 cli_shutdown(c);
1018 return NULL;
1022 static int
1023 smbfs_get_master_browser(char **host)
1025 static char so_broadcast[] = "SO_BROADCAST";
1026 int count;
1027 struct in_addr *ip_list, bcast_addr;
1029 /* does port = 137 for win95 master browser? */
1030 int fd= open_socket_in( SOCK_DGRAM, 0, 3,
1031 interpret_addr(lp_socket_address()), True );
1032 if (fd == -1)
1033 return 0;
1034 set_socket_options(fd, so_broadcast);
1035 ip_list = iface_bcast(ipzero);
1036 bcast_addr = *ip_list;
1037 if ((ip_list = name_query(fd, "\01\02__MSBROWSE__\02", 1, True,
1038 True, bcast_addr, &count, NULL))) {
1039 if (!count)
1040 return 0;
1041 /* just return first master browser */
1042 *host = g_strdup(inet_ntoa(ip_list[0]));
1043 return 1;
1045 return 0;
1048 static void
1049 smbfs_free_bucket (smbfs_connection *bucket)
1051 g_free (bucket->host);
1052 g_free (bucket->service);
1053 g_free (bucket->domain);
1054 g_free (bucket->user);
1055 wipe_password (bucket->password);
1056 g_free (bucket->home);
1057 memset (bucket, 0, sizeof (smbfs_connection));
1060 static smbfs_connection *
1061 smbfs_get_free_bucket (void)
1063 int i;
1065 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++)
1066 if (!smbfs_connections [i].cli) return &smbfs_connections [i];
1068 { /* search for most dormant connection */
1069 int oldest = 0; /* index */
1070 time_t oldest_time = smbfs_connections[0].last_use;
1071 for (i = 1; i < SMBFS_MAX_CONNECTIONS; i++) {
1072 if (smbfs_connections[i].last_use < oldest_time) {
1073 oldest_time = smbfs_connections[i].last_use;
1074 oldest = i;
1077 cli_shutdown(smbfs_connections[oldest].cli);
1078 smbfs_free_bucket (&smbfs_connections[oldest]);
1079 return &smbfs_connections[oldest];
1082 /* This can't happend, since we have checked for max connections before */
1083 vfs_die("Internal error: smbfs_get_free_bucket");
1084 return 0; /* shut up, stupid gcc */
1087 /* This routine keeps track of open connections */
1088 /* Returns a connected socket to host */
1089 static smbfs_connection *
1090 smbfs_open_link (char *host, char *path, const char *user, int *port,
1091 char *this_pass)
1093 int i;
1094 smbfs_connection *bucket;
1095 pstring service;
1096 struct in_addr *dest_ip = NULL;
1098 DEBUG (3, ("smbfs_open_link(host:%s, path:%s)\n", host, path));
1100 if (strcmp (host, path) == 0) /* if host & path are same: */
1101 pstrcpy (service, IPC); /* setup for browse */
1102 else { /* get share name from path, path starts with server name */
1103 char *p;
1104 if ((p = strchr (path, '/'))) /* get share aka */
1105 pstrcpy (service, ++p); /* service name from path */
1106 else
1107 pstrcpy (service, "");
1108 /* now check for trailing directory/filenames */
1109 p = strchr (service, '/');
1110 if (p)
1111 *p = 0; /* cut off dir/files: sharename only */
1112 if (!*service)
1113 pstrcpy (service, IPC); /* setup for browse */
1114 DEBUG (6, ("smbfs_open_link: service from path:%s\n", service));
1117 if (got_user)
1118 user = username; /* global from getenv */
1120 /* Is the link actually open? */
1121 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1122 if (!smbfs_connections[i].cli)
1123 continue;
1124 if ((strcmp (host, smbfs_connections[i].host) == 0) &&
1125 (strcmp (user, smbfs_connections[i].user) == 0) &&
1126 (strcmp (service, smbfs_connections[i].service) == 0)) {
1127 int retries = 0;
1128 BOOL inshare = (*host != 0 && *path != 0 && strchr (path, '/'));
1129 /* check if this connection has died */
1130 while (!smbfs_chkpath (smbfs_connections[i].cli, "\\", !inshare)) {
1131 if (!smbfs_reconnect (&smbfs_connections[i], &retries))
1132 return 0;
1134 DEBUG (6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i));
1135 current_bucket = &smbfs_connections[i];
1136 smbfs_connections[i].last_use = time (NULL);
1137 return &smbfs_connections[i];
1139 /* connection not found, find if we have ip for new connection */
1140 if (strcmp (host, smbfs_connections[i].host) == 0)
1141 dest_ip = &smbfs_connections[i].cli->dest_ip;
1144 /* make new connection */
1145 bucket = smbfs_get_free_bucket ();
1146 bucket->name_type = 0x20;
1147 bucket->home = 0;
1148 bucket->port = *port;
1149 bucket->have_ip = False;
1150 if (dest_ip) {
1151 bucket->have_ip = True;
1152 bucket->dest_ip = *dest_ip;
1154 current_bucket = bucket;
1156 bucket->user = g_strdup (user);
1157 bucket->service = g_strdup (service);
1159 if (!(*host)) { /* if blank host name, browse for servers */
1160 if (!smbfs_get_master_browser (&host)) /* set host to ip of master browser */
1161 return 0; /* could not find master browser? */
1162 g_free (host);
1163 bucket->host = g_strdup (""); /* blank host means master browser */
1164 } else
1165 bucket->host = g_strdup (host);
1167 if (!smbfs_bucket_set_authinfo (bucket, 0, /* domain currently not used */
1168 user, this_pass, 1))
1169 return 0;
1171 /* connect to share */
1172 while (!(bucket->cli = smbfs_do_connect (host, service))) {
1174 if (my_errno != EPERM)
1175 return 0;
1176 message (D_ERROR, MSG_ERROR, _(" Authentication failed "));
1178 /* authentication failed, try again */
1179 smbfs_auth_remove (bucket->host, bucket->service);
1180 if (!smbfs_bucket_set_authinfo (bucket, bucket->domain, bucket->user, 0, 0))
1181 return 0;
1185 smbfs_open_connections++;
1186 DEBUG (3, ("smbfs_open_link:smbfs_open_connections: %d\n",
1187 smbfs_open_connections));
1188 return bucket;
1191 static char *
1192 smbfs_get_path (smbfs_connection ** sc, const char *path)
1194 char *user, *host, *remote_path, *pass;
1195 int port = SMB_PORT;
1197 DEBUG (3, ("smbfs_get_path(%s)\n", path));
1198 if (strncmp (path, URL_HEADER, HEADER_LEN))
1199 return NULL;
1200 path += HEADER_LEN;
1202 if (*path == '/') /* '/' leading server name */
1203 path++; /* probably came from server browsing */
1205 if ((remote_path =
1206 smbfs_get_host_and_username (&path, &host, &user, &port, &pass)))
1207 if ((*sc =
1208 smbfs_open_link (host, remote_path, user, &port, pass)) == NULL) {
1209 g_free (remote_path);
1210 remote_path = NULL;
1212 g_free (host);
1213 g_free (user);
1214 if (pass)
1215 wipe_password (pass);
1217 if (!remote_path)
1218 return NULL;
1220 /* NOTE: tildes are deprecated. See ftpfs.c */
1222 int f = !strcmp (remote_path, "/~");
1223 if (f || !strncmp (remote_path, "/~/", 3)) {
1224 char *s;
1225 s = concat_dir_and_file ((*sc)->home, remote_path + 3 - f);
1226 g_free (remote_path);
1227 return s;
1230 return remote_path;
1233 #if 0
1234 static int
1235 is_error (int result, int errno_num)
1237 if (!(result == -1))
1238 return my_errno = 0;
1239 else
1240 my_errno = errno_num;
1241 return 1;
1243 #endif
1245 static void *
1246 smbfs_opendir (struct vfs_class *me, const char *dirname)
1248 opendir_info *smbfs_info;
1249 smbfs_connection *sc;
1250 char *remote_dir;
1252 (void) me;
1254 DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname));
1256 if (!(remote_dir = smbfs_get_path (&sc, dirname)))
1257 return NULL;
1259 /* FIXME: where freed? */
1260 smbfs_info = g_new (opendir_info, 1);
1261 smbfs_info->server_list = FALSE;
1262 smbfs_info->path = g_strdup(dirname); /* keep original */
1263 smbfs_info->dirname = remote_dir;
1264 smbfs_info->conn = sc;
1265 smbfs_info->entries = 0;
1266 smbfs_info->current = 0;
1268 return smbfs_info;
1271 static int
1272 smbfs_fake_server_stat (const char *server_url, const char *path, struct stat *buf)
1274 dir_entry *dentry;
1275 const char *p;
1277 (void) server_url;
1279 if ((p = strrchr (path, '/')))
1280 path = p + 1; /* advance until last '/' */
1282 if (!current_info->entries) {
1283 if (!smbfs_loaddir (current_info)) /* browse host */
1284 return -1;
1287 if (current_info->server_list == True) {
1288 dentry = current_info->entries;
1289 DEBUG (4, ("fake stat for SERVER \"%s\"\n", path));
1290 while (dentry) {
1291 if (strcmp (dentry->text, path) == 0) {
1292 DEBUG (4, ("smbfs_fake_server_stat: %s:%4o\n",
1293 dentry->text, (int)dentry->my_stat.st_mode));
1294 memcpy (buf, &dentry->my_stat, sizeof (struct stat));
1295 return 0;
1297 dentry = dentry->next;
1300 my_errno = ENOENT;
1301 return -1;
1304 static int
1305 smbfs_fake_share_stat (const char *server_url, const char *path, struct stat *buf)
1307 dir_entry *dentry;
1308 if (strlen (path) < strlen (server_url))
1309 return -1;
1311 if (!current_share_info) { /* Server was not stat()ed */
1312 /* Make sure there is such share at server */
1313 smbfs_connection *sc;
1314 char *p;
1315 p = smbfs_get_path (&sc, path);
1316 g_free (p);
1317 if (p) {
1318 memset (buf, 0, sizeof (*buf));
1319 /* show this as dir */
1320 buf->st_mode =
1321 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
1322 S_IXOTH) & myumask;
1323 return 0;
1325 return -1;
1328 path += strlen (server_url); /* we only want share name */
1329 path++;
1331 if (*path == '/') /* '/' leading server name */
1332 path++; /* probably came from server browsing */
1334 if (!current_share_info->entries) {
1335 if (!smbfs_loaddir (current_share_info)) /* browse host */
1336 return -1;
1338 dentry = current_share_info->entries;
1339 DEBUG (3, ("smbfs_fake_share_stat: %s on %s\n", path, server_url));
1340 while (dentry) {
1341 if (strcmp (dentry->text, path) == 0) {
1342 DEBUG (6, ("smbfs_fake_share_stat: %s:%4o\n",
1343 dentry->text, (int) dentry->my_stat.st_mode));
1344 memcpy (buf, &dentry->my_stat, sizeof (struct stat));
1345 return 0;
1347 dentry = dentry->next;
1349 my_errno = ENOENT;
1350 return -1;
1353 /* stat a single file, smbfs_get_remote_stat callback */
1354 static dir_entry *single_entry;
1356 /* stat a single file */
1357 static int
1358 smbfs_get_remote_stat (smbfs_connection * sc, const char *path, struct stat *buf)
1360 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
1361 char *mypath;
1363 DEBUG (3, ("smbfs_get_remote_stat(): mypath:%s\n", path));
1365 mypath = smbfs_convert_path (path, FALSE);
1367 #if 0 /* single_entry is never free()d now. And only my_stat is used */
1368 single_entry = g_new (dir_entry, 1);
1370 single_entry->text = dos_to_unix (g_strdup (finfo->name), 1);
1372 single_entry->next = 0;
1373 #endif
1374 if (!single_entry)
1375 single_entry = g_new0 (dir_entry, 1);
1377 if (cli_list
1378 (sc->cli, mypath, attribute, smbfs_loaddir_helper, single_entry) < 1) {
1379 my_errno = ENOENT;
1380 g_free (mypath);
1381 return -1; /* cli_list returns number of files */
1384 memcpy (buf, &single_entry->my_stat, sizeof (struct stat));
1386 /* don't free here, use for smbfs_fstat() */
1387 /* g_free(single_entry->text);
1388 g_free(single_entry); */
1389 g_free (mypath);
1390 return 0;
1393 static int
1394 smbfs_search_dir_entry (dir_entry *dentry, const char *text, struct stat *buf)
1396 while (dentry) {
1397 if (strcmp(text, dentry->text) == 0) {
1398 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1399 memcpy(&single_entry->my_stat, &dentry->my_stat,
1400 sizeof(struct stat));
1401 return 0;
1403 dentry = dentry->next;
1405 return -1;
1408 static int
1409 smbfs_get_stat_info (smbfs_connection * sc, const char *path, struct stat *buf)
1411 char *p;
1412 #if 0
1413 dir_entry *dentry = current_info->entries;
1414 #endif
1415 const char *mypath = path;
1417 mypath++; /* cut off leading '/' */
1418 if ((p = strrchr (mypath, '/')))
1419 mypath = p + 1; /* advance until last file/dir name */
1420 DEBUG (3, ("smbfs_get_stat_info: mypath:%s, current_info->dirname:%s\n",
1421 mypath, current_info->dirname));
1422 #if 0
1423 if (!dentry) {
1424 DEBUG (1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n",
1425 current_info->dirname, path));
1426 return -1;
1428 #endif
1429 if (!single_entry) /* when found, this will be written too */
1430 single_entry = g_new (dir_entry, 1);
1431 if (smbfs_search_dir_entry (current_info->entries, mypath, buf) == 0) {
1432 return 0;
1434 /* now try to identify mypath as PARENT dir */
1436 char *mdp;
1437 char *mydir;
1438 mdp = mydir = g_strdup (current_info->dirname);
1439 if ((p = strrchr (mydir, '/')))
1440 *p = 0; /* advance util last '/' */
1441 if ((p = strrchr (mydir, '/')))
1442 mydir = p + 1; /* advance util last '/' */
1443 if (strcmp (mydir, mypath) == 0) { /* fake a stat for ".." */
1444 memset (buf, 0, sizeof (struct stat));
1445 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1446 memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
1447 g_free (mdp);
1448 DEBUG (1, (" PARENT:found in %s\n", current_info->dirname));
1449 return 0;
1451 g_free (mdp);
1453 /* now try to identify as CURRENT dir? */
1455 char *dnp = current_info->dirname;
1456 DEBUG (6, ("smbfs_get_stat_info: is %s current dir? this dir is: %s\n",
1457 mypath, current_info->dirname));
1458 if (*dnp == '/')
1459 dnp++;
1460 else {
1461 return -1;
1463 if (strcmp (mypath, dnp) == 0) {
1464 memset (buf, 0, sizeof (struct stat));
1465 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1466 memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
1467 DEBUG (1, (" CURRENT:found in %s\n", current_info->dirname));
1468 return 0;
1471 DEBUG (3, ("'%s' not found in current_info '%s'\n", path,
1472 current_info->dirname));
1473 /* try to find this in the PREVIOUS listing */
1474 if (previous_info) {
1475 if (smbfs_search_dir_entry (previous_info->entries, mypath, buf) == 0)
1476 return 0;
1477 DEBUG (3, ("'%s' not found in previous_info '%s'\n", path,
1478 previous_info->dirname));
1480 /* try to find this in the SHARE listing */
1481 if (current_share_info) {
1482 if (smbfs_search_dir_entry (current_share_info->entries, mypath, buf) == 0)
1483 return 0;
1484 DEBUG (3, ("'%s' not found in share_info '%s'\n", path,
1485 current_share_info->dirname));
1487 /* try to find this in the SERVER listing */
1488 if (current_server_info) {
1489 if (smbfs_search_dir_entry (current_server_info->entries, mypath, buf) == 0)
1490 return 0;
1491 DEBUG (3, ("'%s' not found in server_info '%s'\n", path,
1492 current_server_info->dirname));
1494 /* nothing found. get stat file info from server */
1495 return smbfs_get_remote_stat (sc, path, buf);
1498 static int
1499 smbfs_chdir (struct vfs_class *me, const char *path)
1501 char *remote_dir;
1502 smbfs_connection *sc;
1504 (void) me;
1506 DEBUG (3, ("smbfs_chdir(path:%s)\n", path));
1507 if (!(remote_dir = smbfs_get_path (&sc, path)))
1508 return -1;
1509 g_free (remote_dir);
1511 return 0;
1514 static int
1515 smbfs_loaddir_by_name (struct vfs_class *me, const char *path)
1517 void *info;
1518 char *mypath, *p;
1520 mypath = g_strdup(path);
1521 p = strrchr(mypath, '/');
1523 if (p > mypath)
1524 *p = 0;
1525 DEBUG(6, ("smbfs_loaddir_by_name(%s)\n", mypath));
1526 smbfs_chdir(me, mypath);
1527 info = smbfs_opendir (me, mypath);
1528 g_free(mypath);
1529 if (!info)
1530 return -1;
1531 smbfs_readdir(info);
1532 smbfs_loaddir(info);
1533 return 0;
1536 static int
1537 smbfs_stat (struct vfs_class * me, const char *path, struct stat *buf)
1539 smbfs_connection *sc;
1540 pstring server_url;
1541 char *service, *pp, *at;
1542 const char *p;
1544 DEBUG (3, ("smbfs_stat(path:%s)\n", path));
1546 if (!current_info) {
1547 DEBUG (1, ("current_info = NULL: "));
1548 if (smbfs_loaddir_by_name (me, path) < 0)
1549 return -1;
1552 /* check if stating server */
1553 p = path;
1554 if (strncmp (p, URL_HEADER, HEADER_LEN)) {
1555 DEBUG (1, ("'%s' doesnt start with '%s' (length %d)\n",
1556 p, URL_HEADER, HEADER_LEN));
1557 return -1;
1560 p += HEADER_LEN;
1561 if (*p == '/')
1562 p++;
1564 pp = strchr (p, '/'); /* advance past next '/' */
1565 at = strchr (p, '@');
1566 pstrcpy (server_url, URL_HEADER);
1567 if (at && at < pp) { /* user@server */
1568 char *z = &(server_url[sizeof (server_url) - 1]);
1569 const char *s = p;
1571 at = &(server_url [HEADER_LEN]) + (at - p + 1);
1572 if (z > at)
1573 z = at;
1574 at = &(server_url [HEADER_LEN]);
1575 while (at < z)
1576 *at++ = *s++;
1577 *z = 0;
1579 pstrcat (server_url, current_bucket->host);
1581 if (!pp) {
1582 if (!current_info->server_list) {
1583 if (smbfs_loaddir_by_name (me, path) < 0)
1584 return -1;
1586 return smbfs_fake_server_stat (server_url, path, buf);
1589 if (!strchr (++pp, '/')) {
1590 return smbfs_fake_share_stat (server_url, path, buf);
1593 /* stating inside share at this point */
1594 if (!(service = smbfs_get_path (&sc, path))) /* connects if necessary */
1595 return -1;
1597 int hostlen = strlen (current_bucket->host);
1598 char *ppp = service + strlen (service) - hostlen;
1599 char *sp = server_url + strlen (server_url) - hostlen;
1601 if (strcmp (sp, ppp) == 0) {
1602 /* make server name appear as directory */
1603 DEBUG (1, ("smbfs_stat: showing server as directory\n"));
1604 memset (buf, 0, sizeof (struct stat));
1605 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1606 g_free (service);
1607 return 0;
1610 /* check if current_info is in share requested */
1611 p = service;
1612 pp = strchr (p, '/');
1613 if (pp) {
1614 p = ++pp; /* advance past server name */
1615 pp = strchr (p, '/');
1617 if (pp)
1618 *pp = 0; /* cut off everthing after service name */
1619 else
1620 p = IPC; /* browsing for services */
1621 pp = current_info->dirname;
1622 if (*pp == '/')
1623 pp++;
1624 if (strncmp (p, pp, strlen (p)) != 0) {
1625 DEBUG (6, ("desired '%s' is not loaded, we have '%s'\n", p, pp));
1626 if (smbfs_loaddir_by_name (me, path) < 0) {
1627 g_free (service);
1628 return -1;
1630 DEBUG (6, ("loaded dir: '%s'\n", current_info->dirname));
1632 g_free (service);
1633 /* stat dirs & files under shares now */
1634 return smbfs_get_stat_info (sc, path, buf);
1637 #define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */
1639 static off_t
1640 smbfs_lseek (void *data, off_t offset, int whence)
1642 smbfs_handle *info = (smbfs_handle *) data;
1643 size_t size;
1645 DEBUG (3,
1646 ("smbfs_lseek(info->nread => %d, offset => %d, whence => %d) \n",
1647 (int) info->nread, (int) offset, whence));
1649 switch (whence) {
1650 case SEEK_SET:
1651 info->nread = offset;
1652 break;
1653 case SEEK_CUR:
1654 info->nread += offset;
1655 break;
1656 case SEEK_END:
1657 if (!cli_qfileinfo (info->cli, info->fnum,
1658 NULL, &size, NULL, NULL, NULL,
1659 NULL, NULL) &&
1660 !cli_getattrE (info->cli, info->fnum,
1661 NULL, &size, NULL, NULL, NULL)) {
1662 errno = EINVAL;
1663 return -1;
1665 info->nread = size + offset;
1666 break;
1669 return info->nread;
1672 static int
1673 smbfs_mknod (struct vfs_class *me, const char *path, int mode, int dev)
1675 (void) me;
1677 DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%d)\n", path, mode, dev));
1678 my_errno = EOPNOTSUPP;
1679 return -1;
1682 static int
1683 smbfs_mkdir (struct vfs_class * me, const char *path, mode_t mode)
1685 smbfs_connection *sc;
1686 char *remote_file;
1687 char *cpath;
1689 (void) me;
1691 DEBUG (3, ("smbfs_mkdir(path:%s, mode:%d)\n", path, (int) mode));
1692 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1693 return -1;
1694 g_free (remote_file);
1695 cpath = smbfs_convert_path (path, FALSE);
1697 if (!cli_mkdir (sc->cli, cpath)) {
1698 my_errno = cli_error (sc->cli, NULL, &err, NULL);
1699 message (D_ERROR, MSG_ERROR, _(" Error %s creating directory %s "),
1700 cli_errstr (sc->cli), CNV_LANG (cpath));
1701 g_free (cpath);
1702 return -1;
1704 g_free (cpath);
1705 return 0;
1708 static int
1709 smbfs_rmdir (struct vfs_class *me, const char *path)
1711 smbfs_connection *sc;
1712 char *remote_file;
1713 char *cpath;
1715 (void) me;
1717 DEBUG(3, ("smbfs_rmdir(path:%s)\n", path));
1718 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1719 return -1;
1720 g_free (remote_file);
1721 cpath = smbfs_convert_path (path, FALSE);
1723 if (!cli_rmdir(sc->cli, cpath)) {
1724 my_errno = cli_error(sc->cli, NULL, &err, NULL);
1725 message (D_ERROR, MSG_ERROR, _(" Error %s removing directory %s "),
1726 cli_errstr(sc->cli), CNV_LANG(cpath));
1727 g_free (cpath);
1728 return -1;
1731 g_free (cpath);
1732 return 0;
1735 static int
1736 smbfs_link (struct vfs_class *me, const char *p1, const char *p2)
1738 (void) me;
1740 DEBUG (3, ("smbfs_link(p1:%s, p2:%s)\n", p1, p2));
1741 my_errno = EOPNOTSUPP;
1742 return -1;
1745 static void
1746 smbfs_free (vfsid id)
1748 DEBUG (3, ("smbfs_free(%p)\n", id));
1749 smbfs_auth_free_all ();
1752 /* Gives up on a socket and reopens the connection, the child own the socket
1753 * now
1755 static void
1756 smbfs_forget (const char *path)
1758 char *host, *user, *p;
1759 int port, i;
1761 if (strncmp (path, URL_HEADER, HEADER_LEN))
1762 return;
1764 DEBUG (3, ("smbfs_forget(path:%s)\n", path));
1766 path += 6;
1767 if (path[0] == '/' && path[1] == '/')
1768 path += 2;
1770 if ((p = smbfs_get_host_and_username (&path, &host, &user, &port, NULL))) {
1771 g_free (p);
1772 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1773 if (smbfs_connections[i].cli
1774 && (strcmp (host, smbfs_connections[i].host) == 0)
1775 && (strcmp (user, smbfs_connections[i].user) == 0)
1776 && (port == smbfs_connections[i].port)) {
1778 /* close socket: the child owns it now */
1779 cli_shutdown (smbfs_connections[i].cli);
1781 /* reopen the connection */
1782 smbfs_connections[i].cli =
1783 smbfs_do_connect (host, smbfs_connections[i].service);
1787 g_free (host);
1788 g_free (user);
1791 static int
1792 smbfs_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg)
1794 (void) me;
1795 (void) arg;
1797 DEBUG (3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path, ctlop));
1798 switch (ctlop) {
1799 case VFS_SETCTL_FORGET:
1800 smbfs_forget (path);
1801 return 0;
1803 return 0;
1807 static smbfs_handle *
1808 smbfs_open_readwrite (smbfs_handle *remote_handle, char *rname, int flags, int mode)
1810 size_t size;
1812 (void) mode;
1814 if (flags & O_TRUNC) /* if it exists truncate to zero */
1815 DEBUG (3, ("smbfs_open: O_TRUNC\n"));
1817 remote_handle->fnum =
1818 #if 1 /* Don't play with flags, it is cli_open() headache */
1819 cli_open (remote_handle->cli, rname, flags, DENY_NONE);
1820 #else /* What's a reasons to has this code ? */
1821 cli_open (remote_handle->cli, rname, ((flags & O_CREAT)
1822 || (flags ==
1823 (O_WRONLY | O_APPEND))) ?
1824 flags : O_RDONLY, DENY_NONE);
1825 #endif
1826 if (remote_handle->fnum == -1) {
1827 message (D_ERROR, MSG_ERROR, _(" %s opening remote file %s "),
1828 cli_errstr (remote_handle->cli), CNV_LANG (rname));
1829 DEBUG (1, ("smbfs_open(rname:%s) error:%s\n",
1830 rname, cli_errstr (remote_handle->cli)));
1831 my_errno = cli_error (remote_handle->cli, NULL, &err, NULL);
1832 return NULL;
1835 if (flags & O_CREAT)
1836 return remote_handle;
1838 if (!cli_qfileinfo (remote_handle->cli, remote_handle->fnum,
1839 &remote_handle->attr, &size, NULL, NULL, NULL, NULL,
1840 NULL)
1841 && !cli_getattrE (remote_handle->cli, remote_handle->fnum,
1842 &remote_handle->attr, &size, NULL, NULL, NULL)) {
1843 message (D_ERROR, MSG_ERROR, " getattrib: %s ",
1844 cli_errstr (remote_handle->cli));
1845 DEBUG (1,
1846 ("smbfs_open(rname:%s) getattrib:%s\n", rname,
1847 cli_errstr (remote_handle->cli)));
1848 my_errno = cli_error (remote_handle->cli, NULL, &err, NULL);
1849 cli_close (remote_handle->cli, remote_handle->fnum);
1850 return NULL;
1853 if ((flags == (O_WRONLY | O_APPEND)) /* file.c:copy_file_file() -> do_append */
1854 && smbfs_lseek (remote_handle, 0, SEEK_END) == -1) {
1855 cli_close (remote_handle->cli, remote_handle->fnum);
1856 return NULL;
1859 return remote_handle;
1862 static void *
1863 smbfs_open (struct vfs_class *me, const char *file, int flags, int mode)
1865 char *remote_file;
1866 void *ret;
1867 smbfs_connection *sc;
1868 smbfs_handle *remote_handle;
1870 (void) me;
1872 DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%o)\n", file, flags, mode));
1874 if (!(remote_file = smbfs_get_path (&sc, file)))
1875 return 0;
1877 remote_file = free_after(smbfs_convert_path (remote_file, FALSE), remote_file);
1879 remote_handle = g_new (smbfs_handle, 2);
1880 remote_handle->cli = sc->cli;
1881 remote_handle->nread = 0;
1883 ret = smbfs_open_readwrite (remote_handle, remote_file, flags, mode);
1885 g_free (remote_file);
1886 if (!ret)
1887 g_free (remote_handle);
1889 return ret;
1892 static int
1893 smbfs_unlink (struct vfs_class *me, const char *path)
1895 smbfs_connection *sc;
1896 char *remote_file;
1898 (void) me;
1900 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1901 return -1;
1903 remote_file = free_after(smbfs_convert_path (remote_file, FALSE), remote_file);
1905 if (!cli_unlink(sc->cli, remote_file)) {
1906 message (D_ERROR, MSG_ERROR, _(" %s removing remote file %s "),
1907 cli_errstr(sc->cli), CNV_LANG(remote_file));
1908 g_free (remote_file);
1909 return -1;
1911 g_free (remote_file);
1912 return 0;
1915 static int
1916 smbfs_rename (struct vfs_class *me, const char *a, const char *b)
1918 smbfs_connection *sc;
1919 char *ra, *rb;
1920 int retval;
1922 (void) me;
1924 if ((ra = smbfs_get_path (&sc, a)) == 0)
1925 return -1;
1927 if ((rb = smbfs_get_path (&sc, b)) == 0) {
1928 g_free (ra);
1929 return -1;
1932 ra = free_after (smbfs_convert_path (ra, FALSE), ra);
1933 rb = free_after (smbfs_convert_path (rb, FALSE), rb);
1935 retval = cli_rename(sc->cli, ra, rb);
1937 g_free (ra);
1938 g_free (rb);
1940 if (!retval) {
1941 message (D_ERROR, MSG_ERROR, _(" %s renaming files\n"),
1942 cli_errstr(sc->cli));
1943 return -1;
1945 return 0;
1948 static int
1949 smbfs_fstat (void *data, struct stat *buf)
1951 smbfs_handle *remote_handle = (smbfs_handle *)data;
1953 DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle->fnum));
1955 /* use left over from previous smbfs_get_remote_stat, if available */
1956 if (single_entry)
1957 memcpy(buf, &single_entry->my_stat, sizeof(struct stat));
1958 else { /* single_entry not set up: bug */
1959 my_errno = EFAULT;
1960 return -EFAULT;
1962 return 0;
1965 void
1966 init_smbfs (void)
1968 vfs_smbfs_ops.name = "smbfs";
1969 vfs_smbfs_ops.prefix = "smb:";
1970 vfs_smbfs_ops.flags = VFSF_NOLINKS;
1971 vfs_smbfs_ops.init = smbfs_init;
1972 vfs_smbfs_ops.fill_names = smbfs_fill_names;
1973 vfs_smbfs_ops.open = smbfs_open;
1974 vfs_smbfs_ops.close = smbfs_close;
1975 vfs_smbfs_ops.read = smbfs_read;
1976 vfs_smbfs_ops.write = smbfs_write;
1977 vfs_smbfs_ops.opendir = smbfs_opendir;
1978 vfs_smbfs_ops.readdir = smbfs_readdir;
1979 vfs_smbfs_ops.closedir = smbfs_closedir;
1980 vfs_smbfs_ops.stat = smbfs_stat;
1981 vfs_smbfs_ops.lstat = smbfs_lstat;
1982 vfs_smbfs_ops.fstat = smbfs_fstat;
1983 vfs_smbfs_ops.chmod = smbfs_chmod;
1984 vfs_smbfs_ops.chown = smbfs_chown;
1985 vfs_smbfs_ops.utime = smbfs_utime;
1986 vfs_smbfs_ops.readlink = smbfs_readlink;
1987 vfs_smbfs_ops.symlink = smbfs_symlink;
1988 vfs_smbfs_ops.link = smbfs_link;
1989 vfs_smbfs_ops.unlink = smbfs_unlink;
1990 vfs_smbfs_ops.rename = smbfs_rename;
1991 vfs_smbfs_ops.chdir = smbfs_chdir;
1992 vfs_smbfs_ops.ferrno = smbfs_errno;
1993 vfs_smbfs_ops.lseek = smbfs_lseek;
1994 vfs_smbfs_ops.mknod = smbfs_mknod;
1995 vfs_smbfs_ops.free = smbfs_free;
1996 vfs_smbfs_ops.mkdir = smbfs_mkdir;
1997 vfs_smbfs_ops.rmdir = smbfs_rmdir;
1998 vfs_smbfs_ops.setctl = smbfs_setctl;
1999 vfs_register_class (&vfs_smbfs_ops);