Makefile.am: Fixed incorrect variable name if maintainer mode is active.
[midnight-commander.git] / vfs / smbfs.c
bloba64405de7c515f7d4b615d600719114eb898bc3d
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
41 #include "../src/global.h"
43 #include "../src/wtools.h" /* message() */
44 #include "../src/main.h" /* print_vfs_message */
46 #include "utilvfs.h"
48 #undef PACKAGE_BUGREPORT
49 #undef PACKAGE_NAME
50 #undef PACKAGE_STRING
51 #undef PACKAGE_TARNAME
52 #undef PACKAGE_VERSION
54 #include "samba/include/config.h"
55 /* don't load crap in "samba/include/includes.h" we don't use and which
56 conflicts with definitions in other includes */
57 #undef HAVE_LIBREADLINE
58 #define NO_CONFIG_H
59 #undef VERSION
61 #include "samba/include/includes.h"
63 #include <string.h>
65 #include "vfs.h"
66 #include "vfs-impl.h"
67 #include "smbfs.h"
69 #define SMBFS_MAX_CONNECTIONS 16
70 static const char * const IPC = "IPC$";
71 static const char * const URL_HEADER = "/#smb:";
72 #define HEADER_LEN 6
74 static int my_errno;
75 static uint32 err;
77 /* stuff that is same with each connection */
78 extern int DEBUGLEVEL;
79 extern pstring myhostname;
80 extern struct in_addr ipzero;
81 static mode_t myumask = 0755;
82 extern pstring global_myname;
83 static int smbfs_open_connections = 0;
84 static gboolean got_user = FALSE;
85 static gboolean got_pass = FALSE;
86 static pstring password;
87 static pstring username;
88 static struct vfs_class vfs_smbfs_ops;
90 static struct _smbfs_connection {
91 struct cli_state *cli;
92 struct in_addr dest_ip;
93 BOOL have_ip;
94 char *host; /* server name */
95 char *service; /* share name */
96 char *domain;
97 char *user;
98 char *home;
99 char *password;
100 int port;
101 int name_type;
102 time_t last_use;
103 } smbfs_connections [SMBFS_MAX_CONNECTIONS];
104 /* unique to each connection */
106 /* modifies *share */
107 static struct cli_state * smbfs_do_connect (const char *server, char *share);
109 typedef struct _smbfs_connection smbfs_connection;
110 static smbfs_connection *current_bucket;
112 typedef struct {
113 struct cli_state *cli;
114 int fnum;
115 off_t nread;
116 uint16 attr;
117 } smbfs_handle;
119 static GSList *auth_list;
121 /* this function allows you to write:
122 * char *s = g_strdup("hello, world");
123 * s = free_after(g_strconcat(s, s, (char *)0), s);
125 static inline char *
126 free_after (char *result, char *string_to_free)
128 g_free(string_to_free);
129 return result;
133 static void
134 smbfs_auth_free (struct smb_authinfo const *a)
136 g_free (a->host);
137 g_free (a->share);
138 g_free (a->domain);
139 g_free (a->user);
140 wipe_password (a->password);
143 static void
144 smbfs_auth_free_all (void)
146 if (auth_list) {
147 g_slist_foreach (auth_list, (GFunc)smbfs_auth_free, 0);
148 g_slist_free (auth_list);
149 auth_list = 0;
153 static gint
154 smbfs_auth_cmp_host_and_share (gconstpointer _a, gconstpointer _b)
156 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
157 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
159 if (!a->host || !a->share || !b->host || !b->share)
160 return 1;
161 if (strcmp (a->host, b->host) != 0)
162 return 1;
163 if (strcmp (a->share, b->share) != 0)
164 return 1;
165 return 0;
168 static gint
169 smbfs_auth_cmp_host (gconstpointer _a, gconstpointer _b)
171 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
172 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
174 if (!a->host || !b->host)
175 return 1;
176 if (strcmp (a->host, b->host) != 0)
177 return 1;
178 if (strcmp (a->share, IPC) != 0)
179 return 1;
180 return 0;
183 static void
184 smbfs_auth_add (const char *host, const char *share, const char *domain,
185 const char *user, const char *param_password)
187 struct smb_authinfo *auth = g_new (struct smb_authinfo, 1);
189 if (!auth)
190 return;
192 /* Don't check for NULL, g_strdup already does. */
193 auth->host = g_strdup (host);
194 auth->share = g_strdup (share);
195 auth->domain = g_strdup (domain);
196 auth->user = g_strdup (user);
197 auth->password = g_strdup (param_password);
198 auth_list = g_slist_prepend (auth_list, auth);
201 static void
202 smbfs_auth_remove (const char *host, const char *share)
204 struct smb_authinfo data;
205 struct smb_authinfo *auth;
206 GSList *list;
208 data.host = g_strdup (host);
209 data.share = g_strdup (share);
210 list = g_slist_find_custom (auth_list,
211 &data,
212 smbfs_auth_cmp_host_and_share);
213 g_free (data.host);
214 g_free (data.share);
215 if (!list)
216 return;
217 auth = list->data;
218 auth_list = g_slist_remove (auth_list, auth);
219 smbfs_auth_free (auth);
222 /* Set authentication information in bucket. Return 1 if successful, else 0 */
223 /* Information in auth_list overrides user if pass is NULL. */
224 /* bucket->host and bucket->service must be valid. */
225 static int
226 smbfs_bucket_set_authinfo (smbfs_connection *bucket,
227 const char *domain, const char *user, const char *pass,
228 int fallback_to_host)
230 struct smb_authinfo data;
231 struct smb_authinfo *auth;
232 GSList *list;
234 if (domain && user && pass) {
235 g_free (bucket->domain);
236 g_free (bucket->user);
237 g_free (bucket->password);
238 bucket->domain = g_strdup (domain);
239 bucket->user = g_strdup (user);
240 bucket->password = g_strdup (pass);
241 smbfs_auth_remove (bucket->host, bucket->service);
242 smbfs_auth_add (bucket->host, bucket->service,
243 domain, user, pass);
244 return 1;
247 data.host = bucket->host;
248 data.share = bucket->service;
249 list = g_slist_find_custom (auth_list, &data, smbfs_auth_cmp_host_and_share);
250 if (!list && fallback_to_host)
251 list = g_slist_find_custom (auth_list, &data, smbfs_auth_cmp_host);
252 if (list) {
253 auth = list->data;
254 bucket->domain = g_strdup (auth->domain);
255 bucket->user = g_strdup (auth->user);
256 bucket->password = g_strdup (auth->password);
257 return 1;
260 if (got_pass) {
261 bucket->domain = g_strdup (lp_workgroup ());
262 bucket->user = g_strdup (got_user ? username : user);
263 bucket->password = g_strdup (password);
264 return 1;
267 auth = vfs_smb_get_authinfo (bucket->host,
268 bucket->service,
269 (domain ? domain : lp_workgroup ()),
270 user);
271 if (auth) {
272 g_free (bucket->domain);
273 g_free (bucket->user);
274 g_free (bucket->password);
275 bucket->domain = g_strdup (auth->domain);
276 bucket->user = g_strdup (auth->user);
277 bucket->password = g_strdup (auth->password);
278 smbfs_auth_remove (bucket->host, bucket->service);
279 auth_list = g_slist_prepend (auth_list, auth);
280 return 1;
282 return 0;
285 void
286 smbfs_set_debug (int arg)
288 DEBUGLEVEL = arg;
291 extern pstring debugf;
292 extern FILE *dbf;
294 void
295 smbfs_set_debugf (const char *filename)
297 if (DEBUGLEVEL > 0) {
298 FILE *outfile = fopen (filename, "w");
299 if (outfile) {
300 setup_logging ("", True); /* No needs for timestamp for each message */
301 dbf = outfile;
302 setbuf (dbf, NULL);
303 pstrcpy (debugf, filename);
308 /********************** The callbacks ******************************/
309 static int
310 smbfs_init (struct vfs_class * me)
312 const char *servicesf = CONFIGDIR PATH_SEP_STR "smb.conf";
314 /* DEBUGLEVEL = 4; */
316 TimeInit ();
317 charset_initialise ();
319 DEBUG (3, ("smbfs_init(%s)\n", me->name));
321 if (!get_myname (myhostname, NULL))
322 DEBUG (0, ("Failed to get my hostname.\n"));
324 if (!lp_load (servicesf, True, False, False))
325 DEBUG (0, ("Cannot load %s - run testparm to debug it\n", servicesf));
327 codepage_initialise (lp_client_code_page ());
329 load_interfaces ();
331 myumask = umask (0);
332 umask (myumask);
333 myumask = ~myumask;
335 if (getenv ("USER")) {
336 char *p;
338 pstrcpy (username, getenv ("USER"));
339 got_user = TRUE;
340 DEBUG (3, ("smbfs_init(): $USER:%s\n", username));
341 if ((p = strchr (username, '%'))) {
342 *p = 0;
343 pstrcpy (password, p + 1);
344 got_pass = TRUE;
345 memset (strchr (getenv ("USER"), '%') + 1, 'X', strlen (password));
346 DEBUG (3, ("smbfs_init(): $USER%%pass: %s%%%s\n",
347 username, password));
349 strupper (username);
351 if (getenv ("PASSWD")) {
352 pstrcpy (password, getenv ("PASSWD"));
353 got_pass = TRUE;
355 return 1;
358 static void
359 smbfs_fill_names (struct vfs_class *me, fill_names_f func)
361 int i;
362 char *path;
364 (void) me;
366 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
367 if (smbfs_connections [i].cli) {
368 path = g_strconcat (URL_HEADER,
369 smbfs_connections[i].user, "@",
370 smbfs_connections[i].host,
371 "/", smbfs_connections[i].service,
372 NULL);
373 (*func)(path);
374 g_free (path);
379 #define CNV_LANG(s) dos_to_unix(s,False)
380 #define GNAL_VNC(s) unix_to_dos(s,False)
381 /* does same as do_get() in client.c */
382 /* called from vfs.c:1080, count = buffer size */
383 static ssize_t
384 smbfs_read (void *data, char *buffer, int count)
386 smbfs_handle *info = (smbfs_handle *) data;
387 int n;
389 DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%d)\n",
390 info->fnum, (int)info->nread, count));
391 n = cli_read(info->cli, info->fnum, buffer, info->nread, count);
392 if (n > 0)
393 info->nread += n;
394 return n;
397 static ssize_t
398 smbfs_write (void *data, const char *buf, int nbyte)
400 smbfs_handle *info = (smbfs_handle *) data;
401 int n;
403 DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%d)\n",
404 info->fnum, (int)info->nread, nbyte));
405 n = cli_write(info->cli, info->fnum, 0, buf, info->nread, nbyte);
406 if (n > 0)
407 info->nread += n;
408 return n;
411 static int
412 smbfs_close (void *data)
414 smbfs_handle *info = (smbfs_handle *) data;
415 DEBUG (3, ("smbfs_close(fnum:%d)\n", info->fnum));
417 /* FIXME: Why too different cli have the same outbuf
418 * if file is copied to share
420 if (info->cli->outbuf == NULL) {
421 my_errno = EINVAL;
422 return -1;
424 #if 0
425 /* if imlementing archive_level: add rname to smbfs_handle */
426 if (archive_level >= 2 && (inf->attr & aARCH)) {
427 cli_setatr (info->cli, rname, info->attr & ~(uint16) aARCH, 0);
429 #endif
430 return (cli_close (info->cli, info->fnum) == True) ? 0 : -1;
433 static int
434 smbfs_errno (struct vfs_class *me)
436 (void) me;
438 DEBUG(3, ("smbfs_errno: %s\n", g_strerror(my_errno)));
439 return my_errno;
442 typedef struct dir_entry {
443 char *text;
444 struct dir_entry *next;
445 struct stat my_stat;
446 int merrno;
447 } dir_entry;
449 typedef struct {
450 gboolean server_list;
451 char *dirname;
452 char *path; /* the dir originally passed to smbfs_opendir */
453 smbfs_connection *conn;
454 dir_entry *entries;
455 dir_entry *current;
456 } opendir_info;
458 static opendir_info
459 *previous_info,
460 *current_info,
461 *current_share_info,
462 *current_server_info;
464 static gboolean first_direntry;
466 static dir_entry *
467 smbfs_new_dir_entry (const char *name)
469 static int inode_counter;
470 dir_entry *new_entry;
471 new_entry = g_new0 (dir_entry, 1);
472 new_entry->text = dos_to_unix (g_strdup (name), 1);
474 if (first_direntry) {
475 current_info->entries = new_entry;
476 first_direntry = FALSE;
477 } else {
478 current_info->current->next = new_entry;
480 current_info->current = new_entry;
481 new_entry->my_stat.st_ino = inode_counter++;
483 return new_entry;
486 /* browse for shares on server */
487 static void
488 smbfs_browsing_helper (const char *name, uint32 type, const char *comment, void *state)
490 const char *typestr = "";
491 dir_entry *new_entry = smbfs_new_dir_entry (name);
493 (void) state;
495 switch (type) {
496 case STYPE_DISKTREE:
497 typestr = "Disk";
498 /* show this as dir */
499 new_entry->my_stat.st_mode =
500 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
501 S_IXOTH) & myumask;
502 break;
503 case STYPE_PRINTQ:
504 typestr = "Printer";
505 break;
506 case STYPE_DEVICE:
507 typestr = "Device";
508 break;
509 case STYPE_IPC:
510 typestr = "IPC";
511 break;
513 DEBUG (3, ("\t%-15.15s%-10.10s%s\n", name, typestr, comment));
516 static void
517 smbfs_loaddir_helper (file_info * finfo, const char *mask, void *entry)
519 dir_entry *new_entry = (dir_entry *) entry;
520 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
522 (void) mask;
524 #if 0 /* I want to see dot files */
525 if (finfo->mode & aHIDDEN)
526 return; /* don't bother with hidden files, "~$" screws up mc */
527 #endif
528 if (!entry)
529 new_entry = smbfs_new_dir_entry (finfo->name);
531 new_entry->my_stat.st_size = finfo->size;
532 new_entry->my_stat.st_mtime = finfo->mtime;
533 new_entry->my_stat.st_atime = finfo->atime;
534 new_entry->my_stat.st_ctime = finfo->ctime;
535 new_entry->my_stat.st_uid = finfo->uid;
536 new_entry->my_stat.st_gid = finfo->gid;
538 new_entry->my_stat.st_mode = /* rw-rw-rw */
539 S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH;
541 /* if (finfo->mode & aVOLID); nothing similar in real world */
542 if (finfo->mode & aDIR)
543 new_entry->my_stat.st_mode |= /* drwxrwxrwx */
544 S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
545 else
546 new_entry->my_stat.st_mode |= S_IFREG; /* if not dir, regular file? */
547 /* if (finfo->mode & aARCH); DOS archive */
548 /* if (finfo->mode & aHIDDEN); like a dot file? */
549 /* if (finfo->mode & aSYSTEM); like a kernel? */
550 if (finfo->mode & aRONLY)
551 new_entry->my_stat.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
552 new_entry->my_stat.st_mode &= myumask;
554 DEBUG (entry ? 3 : 6, (" %-30s%7.7s%8.0f %s",
555 CNV_LANG (finfo->name),
556 attrib_string (finfo->mode),
557 (double) finfo->size,
558 asctime (LocalTime (&t))));
561 /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
562 static char *
563 smbfs_convert_path (const char *remote_file, gboolean trailing_asterik)
565 const char *p, *my_remote;
566 char *result;
568 my_remote = remote_file;
569 if (strncmp (my_remote, URL_HEADER, HEADER_LEN) == 0) { /* if passed directly */
570 my_remote += HEADER_LEN;
571 if (*my_remote == '/') /* from server browsing */
572 my_remote++;
573 p = strchr(my_remote, '/');
574 if (p)
575 my_remote = p+1; /* advance to end of server name */
578 if (*my_remote == '/')
579 my_remote++; /* strip off leading '/' */
580 p = strchr(my_remote, '/');
581 if (p)
582 my_remote = p; /* strip off share/service name */
583 /* create remote filename as understood by smb clientgen */
584 result = g_strconcat (my_remote, trailing_asterik ? "/*" : "", (char *) NULL);
585 unix_to_dos (result, /* inplace = */ 1); /* code page conversion */
586 str_replace(result, '/', '\\');
587 return result;
590 static void
591 smbfs_srv_browsing_helper (const char *name, uint32 m, const char *comment,
592 void *state)
594 dir_entry *new_entry = smbfs_new_dir_entry (name);
596 (void) m;
597 (void) state;
599 /* show this as dir */
600 new_entry->my_stat.st_mode =
601 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
602 S_IXOTH) & myumask;
604 DEBUG (3, ("\t%-16.16s %s\n", name, comment));
607 static BOOL
608 smbfs_reconnect(smbfs_connection *conn, int *retries)
610 char *host;
611 DEBUG(3, ("RECONNECT\n"));
613 if (*(conn->host) == 0)
614 host = g_strdup(conn->cli->desthost); /* server browsing */
615 else
616 host = g_strdup(conn->host);
618 cli_shutdown(conn->cli);
620 if (!(conn->cli = smbfs_do_connect(host, conn->service))) {
621 message (D_ERROR, MSG_ERROR,
622 _(" reconnect to %s failed\n "), conn->host);
623 g_free(host);
624 return False;
626 g_free(host);
627 if (++(*retries) == 2)
628 return False;
629 return True;
632 static BOOL
633 smbfs_send(struct cli_state *cli)
635 size_t len;
636 size_t nwritten=0;
637 ssize_t ret;
639 len = smb_len(cli->outbuf) + 4;
641 while (nwritten < len) {
642 ret = write_socket(cli->fd, cli->outbuf+nwritten, len - nwritten);
643 if (ret <= 0) {
644 if (errno == EPIPE)
645 return False;
646 } else
647 nwritten += ret;
650 return True;
653 /****************************************************************************
654 See if server has cut us off by checking for EPIPE when writing.
655 Taken from cli_chkpath()
656 ****************************************************************************/
657 static BOOL
658 smbfs_chkpath(struct cli_state *cli, const char *path, BOOL send_only)
660 fstring path2;
661 char *p;
663 fstrcpy(path2,path);
664 unix_to_dos (path2, 1);
665 trim_string(path2,NULL,"\\");
666 if (!*path2) *path2 = '\\';
668 memset(cli->outbuf,'\0',smb_size);
669 set_message(cli->outbuf,0,4 + strlen(path2),True);
670 SCVAL(cli->outbuf,smb_com,SMBchkpth);
671 SSVAL(cli->outbuf,smb_tid,cli->cnum);
673 cli->rap_error = 0;
674 cli->nt_error = 0;
675 SSVAL(cli->outbuf,smb_pid,cli->pid);
676 SSVAL(cli->outbuf,smb_uid,cli->vuid);
677 SSVAL(cli->outbuf,smb_mid,cli->mid);
678 if (cli->protocol > PROTOCOL_CORE) {
679 SCVAL(cli->outbuf,smb_flg,0x8);
680 SSVAL(cli->outbuf,smb_flg2,0x1);
683 p = smb_buf(cli->outbuf);
684 *p++ = 4;
685 fstrcpy(p,path2);
687 if (!smbfs_send(cli)) {
688 DEBUG(3, ("smbfs_chkpath: couldnt send\n"));
689 return False;
691 if (send_only) {
692 client_receive_smb(cli->fd, cli->inbuf, cli->timeout);
693 DEBUG(3, ("smbfs_chkpath: send only OK\n"));
694 return True; /* just testing for EPIPE */
696 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout)) {
697 DEBUG(3, ("smbfs_chkpath: receive error\n"));
698 return False;
700 if ((my_errno = cli_error(cli, NULL, NULL, NULL))) {
701 if (my_errno == 20 || my_errno == 13)
702 return True; /* ignore if 'not a directory' error */
703 DEBUG(3, ("smbfs_chkpath: cli_error: %s\n", g_strerror(my_errno)));
704 return False;
707 return True;
710 #if 1
711 static int
712 smbfs_fs (const char *text)
714 const char *p = text;
715 int count = 0;
717 while ((p = strchr(p, '/')) != NULL) {
718 count++;
719 p++;
721 if (count == 1)
722 return strlen(text);
723 return count;
725 #endif
727 static int
728 smbfs_loaddir (opendir_info *smbfs_info)
730 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
731 int servlen = strlen (smbfs_info->conn->service);
732 const char *info_dirname = smbfs_info->dirname;
733 char *my_dirname;
735 DEBUG (3, ("smbfs_loaddir: dirname:%s\n", info_dirname));
736 first_direntry = TRUE;
738 if (current_info) {
739 DEBUG (3,
740 ("smbfs_loaddir: new:'%s', cached:'%s'\n", info_dirname,
741 current_info->dirname));
742 /* if new desired dir is longer than cached in current_info */
743 if (smbfs_fs (info_dirname) > smbfs_fs (current_info->dirname)) {
744 DEBUG (3, ("saving to previous_info\n"));
745 previous_info = current_info;
749 current_info = smbfs_info;
751 if (strcmp (info_dirname, "/") == 0) {
752 if (!strcmp (smbfs_info->path, URL_HEADER)) {
753 DEBUG (6, ("smbfs_loaddir: browsing %s\n", IPC));
754 /* browse for servers */
755 if (!cli_NetServerEnum
756 (smbfs_info->conn->cli, smbfs_info->conn->domain,
757 SV_TYPE_ALL, smbfs_srv_browsing_helper, NULL))
758 return 0;
759 else
760 current_server_info = smbfs_info;
761 smbfs_info->server_list = TRUE;
762 } else {
763 /* browse for shares */
764 if (cli_RNetShareEnum
765 (smbfs_info->conn->cli, smbfs_browsing_helper, NULL) < 1)
766 return 0;
767 else
768 current_share_info = smbfs_info;
770 goto done;
773 /* do regular directory listing */
774 if (strncmp (smbfs_info->conn->service, info_dirname + 1, servlen) == 0) {
775 /* strip share name from dir */
776 my_dirname = g_strdup (info_dirname + servlen);
777 *my_dirname = '/';
778 my_dirname = free_after(smbfs_convert_path (my_dirname, TRUE), my_dirname);
779 } else
780 my_dirname = smbfs_convert_path (info_dirname, TRUE);
782 DEBUG (6, ("smbfs_loaddir: service: %s\n", smbfs_info->conn->service));
783 DEBUG (6,
784 ("smbfs_loaddir: cli->share: %s\n",
785 smbfs_info->conn->cli->share));
786 DEBUG (6,
787 ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname));
788 /* do file listing: cli_list returns number of files */
789 if (cli_list
790 (smbfs_info->conn->cli, my_dirname, attribute,
791 smbfs_loaddir_helper, NULL) < 0) {
792 /* cli_list returns -1 if directory empty or cannot read socket */
793 my_errno = cli_error (smbfs_info->conn->cli, NULL, &err, NULL);
794 g_free (my_dirname);
795 return 0;
797 if (*(my_dirname) == 0)
798 smbfs_info->dirname = smbfs_info->conn->service;
799 g_free (my_dirname);
800 /* do_dskattr(); */
802 done:
803 /* current_info->parent = smbfs_info->dirname; */
805 smbfs_info->current = smbfs_info->entries;
806 return 1; /* 1 = ok */
809 #ifdef SMBFS_FREE_DIR
810 static void
811 smbfs_free_dir (dir_entry *de)
813 if (!de) return;
815 smbfs_free_dir (de->next);
816 g_free (de->text);
817 g_free (de);
819 #endif
822 /* The readdir routine loads the complete directory */
823 /* It's too slow to ask the server each time */
824 /* It now also sends the complete lstat information for each file */
825 static void *
826 smbfs_readdir(void *info)
828 static union vfs_dirent smbfs_readdir_data;
829 static char *const dirent_dest = smbfs_readdir_data.dent.d_name;
830 opendir_info *smbfs_info = (opendir_info *) info;
832 DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info->dirname));
834 if (!smbfs_info->entries)
835 if (!smbfs_loaddir(smbfs_info))
836 return NULL;
838 if (smbfs_info->current == 0) { /* reached end of dir entries */
839 DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
840 #ifdef SMBFS_FREE_DIR
841 smbfs_free_dir(smbfs_info->entries);
842 smbfs_info->entries = 0;
843 #endif
844 return NULL;
846 g_strlcpy(dirent_dest, smbfs_info->current->text, MC_MAXPATHLEN);
847 smbfs_info->current = smbfs_info->current->next;
849 compute_namelen(&smbfs_readdir_data.dent);
851 return &smbfs_readdir_data;
854 static int
855 smbfs_closedir (void *info)
857 opendir_info *smbfs_info = (opendir_info *) info;
858 /* dir_entry *p, *q; */
860 DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info->dirname));
861 /* CLOSE HERE */
863 /* for (p = smbfs_info->entries; p;){
864 q = p;
865 p = p->next;
866 g_free (q->text);
867 g_free (q);
869 g_free (info); */
870 return 0;
873 static int
874 smbfs_chmod (struct vfs_class *me, const char *path, int mode)
876 (void) me;
878 DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path, mode));
879 /* my_errno = EOPNOTSUPP;
880 return -1; */ /* cannot chmod on smb filesystem */
881 return 0; /* make mc happy */
884 static int
885 smbfs_chown (struct vfs_class *me, const char *path, int owner, int group)
887 (void) me;
889 DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path, owner, group));
890 my_errno = EOPNOTSUPP; /* ready for your labotomy? */
891 return -1;
894 static int
895 smbfs_utime (struct vfs_class *me, const char *path, struct utimbuf *times)
897 (void) me;
898 (void) times;
900 DEBUG(3, ("smbfs_utime(path:%s)\n", path));
901 my_errno = EOPNOTSUPP;
902 return -1;
905 static int
906 smbfs_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
908 (void) me;
910 DEBUG (3,
911 ("smbfs_readlink(path:%s, buf:%s, size:%d)\n", path, buf,
912 (int) size));
913 my_errno = EOPNOTSUPP;
914 return -1; /* no symlinks on smb filesystem? */
917 static int
918 smbfs_symlink (struct vfs_class *me, const char *n1, const char *n2)
920 (void) me;
922 DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1, n2));
923 my_errno = EOPNOTSUPP;
924 return -1; /* no symlinks on smb filesystem? */
927 /* Extract the hostname and username from the path */
928 /* path is in the form: [user@]hostname/share/remote-dir */
929 #define smbfs_get_host_and_username(path, host, user, port, pass) \
930 vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
932 /*****************************************************
933 return a connection to a SMB server
934 current_bucket needs to be set before calling
935 *******************************************************/
936 static struct cli_state *
937 smbfs_do_connect (const char *server, char *share)
939 struct cli_state *c;
940 struct nmb_name called, calling;
941 struct in_addr ip;
943 DEBUG(3, ("smbfs_do_connect(%s, %s)\n", server, share));
944 if (*share == '\\') {
945 server = share+2;
946 share = strchr(server,'\\');
947 if (!share) return NULL;
948 *share = 0;
949 share++;
952 make_nmb_name(&calling, global_myname, 0x0);
953 make_nmb_name(&called , server, current_bucket->name_type);
955 for (;;) {
957 ip = (current_bucket->have_ip) ? current_bucket->dest_ip : ipzero;
959 /* have to open a new connection */
960 if (!(c = cli_initialise(NULL))) {
961 my_errno = ENOMEM;
962 return NULL;
965 pwd_init(&(c->pwd)); /* should be moved into cli_initialise()? */
966 pwd_set_cleartext(&(c->pwd), current_bucket->password);
968 if ((cli_set_port(c, current_bucket->port) == 0) ||
969 !cli_connect(c, server, &ip)) {
970 DEBUG(1, ("Connection to %s failed\n", server));
971 break;
974 if (!cli_session_request(c, &calling, &called)) {
975 my_errno = cli_error(c, NULL, &err, NULL);
976 DEBUG(1, ("session request to %s failed\n", called.name));
977 cli_shutdown(c);
978 if (strcmp(called.name, "*SMBSERVER")) {
979 make_nmb_name(&called , "*SMBSERVER", 0x20);
980 continue;
982 return NULL;
985 DEBUG(3, (" session request ok\n"));
987 if (!cli_negprot(c)) {
988 DEBUG(1, ("protocol negotiation failed\n"));
989 break;
992 if (!cli_session_setup(c, current_bucket->user,
993 current_bucket->password, strlen(current_bucket->password),
994 current_bucket->password, strlen(current_bucket->password),
995 current_bucket->domain)) {
996 DEBUG(1,("session setup failed: %s\n", cli_errstr(c)));
997 smbfs_auth_remove (server, share);
998 break;
1001 if (*c->server_domain || *c->server_os || *c->server_type)
1002 DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
1003 c->server_domain,c->server_os,c->server_type));
1005 DEBUG(3, (" session setup ok\n"));
1007 if (!cli_send_tconX(c, share, "?????",
1008 current_bucket->password, strlen(current_bucket->password)+1)) {
1009 DEBUG(1,("%s: tree connect failed: %s\n", share, cli_errstr(c)));
1010 break;
1013 DEBUG(3, (" tconx ok\n"));
1015 my_errno = 0;
1016 return c;
1019 my_errno = cli_error(c, NULL, &err, NULL);
1020 cli_shutdown(c);
1021 return NULL;
1025 static int
1026 smbfs_get_master_browser(char **host)
1028 static char so_broadcast[] = "SO_BROADCAST";
1029 int count;
1030 struct in_addr *ip_list, bcast_addr;
1032 /* does port = 137 for win95 master browser? */
1033 int fd= open_socket_in( SOCK_DGRAM, 0, 3,
1034 interpret_addr(lp_socket_address()), True );
1035 if (fd == -1)
1036 return 0;
1037 set_socket_options(fd, so_broadcast);
1038 ip_list = iface_bcast(ipzero);
1039 bcast_addr = *ip_list;
1040 if ((ip_list = name_query(fd, "\01\02__MSBROWSE__\02", 1, True,
1041 True, bcast_addr, &count, NULL))) {
1042 if (!count)
1043 return 0;
1044 /* just return first master browser */
1045 *host = g_strdup(inet_ntoa(ip_list[0]));
1046 return 1;
1048 return 0;
1051 static void
1052 smbfs_free_bucket (smbfs_connection *bucket)
1054 g_free (bucket->host);
1055 g_free (bucket->service);
1056 g_free (bucket->domain);
1057 g_free (bucket->user);
1058 wipe_password (bucket->password);
1059 g_free (bucket->home);
1060 memset (bucket, 0, sizeof (smbfs_connection));
1063 static smbfs_connection *
1064 smbfs_get_free_bucket (void)
1066 int i;
1068 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++)
1069 if (!smbfs_connections [i].cli) return &smbfs_connections [i];
1071 { /* search for most dormant connection */
1072 int oldest = 0; /* index */
1073 time_t oldest_time = smbfs_connections[0].last_use;
1074 for (i = 1; i < SMBFS_MAX_CONNECTIONS; i++) {
1075 if (smbfs_connections[i].last_use < oldest_time) {
1076 oldest_time = smbfs_connections[i].last_use;
1077 oldest = i;
1080 cli_shutdown(smbfs_connections[oldest].cli);
1081 smbfs_free_bucket (&smbfs_connections[oldest]);
1082 return &smbfs_connections[oldest];
1085 /* This can't happend, since we have checked for max connections before */
1086 vfs_die("Internal error: smbfs_get_free_bucket");
1087 return 0; /* shut up, stupid gcc */
1090 /* This routine keeps track of open connections */
1091 /* Returns a connected socket to host */
1092 static smbfs_connection *
1093 smbfs_open_link (char *host, char *path, const char *user, int *port,
1094 char *this_pass)
1096 int i;
1097 smbfs_connection *bucket;
1098 pstring service;
1099 struct in_addr *dest_ip = NULL;
1101 DEBUG (3, ("smbfs_open_link(host:%s, path:%s)\n", host, path));
1103 if (strcmp (host, path) == 0) /* if host & path are same: */
1104 pstrcpy (service, IPC); /* setup for browse */
1105 else { /* get share name from path, path starts with server name */
1106 char *p;
1107 if ((p = strchr (path, '/'))) /* get share aka */
1108 pstrcpy (service, ++p); /* service name from path */
1109 else
1110 pstrcpy (service, "");
1111 /* now check for trailing directory/filenames */
1112 p = strchr (service, '/');
1113 if (p)
1114 *p = 0; /* cut off dir/files: sharename only */
1115 if (!*service)
1116 pstrcpy (service, IPC); /* setup for browse */
1117 DEBUG (6, ("smbfs_open_link: service from path:%s\n", service));
1120 if (got_user)
1121 user = username; /* global from getenv */
1123 /* Is the link actually open? */
1124 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1125 if (!smbfs_connections[i].cli)
1126 continue;
1127 if ((strcmp (host, smbfs_connections[i].host) == 0) &&
1128 (strcmp (user, smbfs_connections[i].user) == 0) &&
1129 (strcmp (service, smbfs_connections[i].service) == 0)) {
1130 int retries = 0;
1131 BOOL inshare = (*host != 0 && *path != 0 && strchr (path, '/'));
1132 /* check if this connection has died */
1133 while (!smbfs_chkpath (smbfs_connections[i].cli, "\\", !inshare)) {
1134 if (!smbfs_reconnect (&smbfs_connections[i], &retries))
1135 return 0;
1137 DEBUG (6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i));
1138 current_bucket = &smbfs_connections[i];
1139 smbfs_connections[i].last_use = time (NULL);
1140 return &smbfs_connections[i];
1142 /* connection not found, find if we have ip for new connection */
1143 if (strcmp (host, smbfs_connections[i].host) == 0)
1144 dest_ip = &smbfs_connections[i].cli->dest_ip;
1147 /* make new connection */
1148 bucket = smbfs_get_free_bucket ();
1149 bucket->name_type = 0x20;
1150 bucket->home = 0;
1151 bucket->port = *port;
1152 bucket->have_ip = False;
1153 if (dest_ip) {
1154 bucket->have_ip = True;
1155 bucket->dest_ip = *dest_ip;
1157 current_bucket = bucket;
1159 bucket->user = g_strdup (user);
1160 bucket->service = g_strdup (service);
1162 if (!(*host)) { /* if blank host name, browse for servers */
1163 if (!smbfs_get_master_browser (&host)) /* set host to ip of master browser */
1164 return 0; /* could not find master browser? */
1165 g_free (host);
1166 bucket->host = g_strdup (""); /* blank host means master browser */
1167 } else
1168 bucket->host = g_strdup (host);
1170 if (!smbfs_bucket_set_authinfo (bucket, 0, /* domain currently not used */
1171 user, this_pass, 1))
1172 return 0;
1174 /* connect to share */
1175 while (!(bucket->cli = smbfs_do_connect (host, service))) {
1177 if (my_errno != EPERM)
1178 return 0;
1179 message (D_ERROR, MSG_ERROR, _(" Authentication failed "));
1181 /* authentication failed, try again */
1182 smbfs_auth_remove (bucket->host, bucket->service);
1183 if (!smbfs_bucket_set_authinfo (bucket, bucket->domain, bucket->user, 0, 0))
1184 return 0;
1188 smbfs_open_connections++;
1189 DEBUG (3, ("smbfs_open_link:smbfs_open_connections: %d\n",
1190 smbfs_open_connections));
1191 return bucket;
1194 static char *
1195 smbfs_get_path (smbfs_connection ** sc, const char *path)
1197 char *user, *host, *remote_path, *pass;
1198 int port = SMB_PORT;
1200 DEBUG (3, ("smbfs_get_path(%s)\n", path));
1201 if (strncmp (path, URL_HEADER, HEADER_LEN))
1202 return NULL;
1203 path += HEADER_LEN;
1205 if (*path == '/') /* '/' leading server name */
1206 path++; /* probably came from server browsing */
1208 if ((remote_path =
1209 smbfs_get_host_and_username (&path, &host, &user, &port, &pass)))
1210 if ((*sc =
1211 smbfs_open_link (host, remote_path, user, &port, pass)) == NULL) {
1212 g_free (remote_path);
1213 remote_path = NULL;
1215 g_free (host);
1216 g_free (user);
1217 if (pass)
1218 wipe_password (pass);
1220 if (!remote_path)
1221 return NULL;
1223 /* NOTE: tildes are deprecated. See ftpfs.c */
1225 int f = !strcmp (remote_path, "/~");
1226 if (f || !strncmp (remote_path, "/~/", 3)) {
1227 char *s;
1228 s = concat_dir_and_file ((*sc)->home, remote_path + 3 - f);
1229 g_free (remote_path);
1230 return s;
1233 return remote_path;
1236 #if 0
1237 static int
1238 is_error (int result, int errno_num)
1240 if (!(result == -1))
1241 return my_errno = 0;
1242 else
1243 my_errno = errno_num;
1244 return 1;
1246 #endif
1248 static void *
1249 smbfs_opendir (struct vfs_class *me, const char *dirname)
1251 opendir_info *smbfs_info;
1252 smbfs_connection *sc;
1253 char *remote_dir;
1255 (void) me;
1257 DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname));
1259 if (!(remote_dir = smbfs_get_path (&sc, dirname)))
1260 return NULL;
1262 /* FIXME: where freed? */
1263 smbfs_info = g_new (opendir_info, 1);
1264 smbfs_info->server_list = FALSE;
1265 smbfs_info->path = g_strdup(dirname); /* keep original */
1266 smbfs_info->dirname = remote_dir;
1267 smbfs_info->conn = sc;
1268 smbfs_info->entries = 0;
1269 smbfs_info->current = 0;
1271 return smbfs_info;
1274 static int
1275 smbfs_fake_server_stat (const char *server_url, const char *path, struct stat *buf)
1277 dir_entry *dentry;
1278 const char *p;
1280 (void) server_url;
1282 if ((p = strrchr (path, '/')))
1283 path = p + 1; /* advance until last '/' */
1285 if (!current_info->entries) {
1286 if (!smbfs_loaddir (current_info)) /* browse host */
1287 return -1;
1290 if (current_info->server_list == True) {
1291 dentry = current_info->entries;
1292 DEBUG (4, ("fake stat for SERVER \"%s\"\n", path));
1293 while (dentry) {
1294 if (strcmp (dentry->text, path) == 0) {
1295 DEBUG (4, ("smbfs_fake_server_stat: %s:%4o\n",
1296 dentry->text, (int)dentry->my_stat.st_mode));
1297 memcpy (buf, &dentry->my_stat, sizeof (struct stat));
1298 return 0;
1300 dentry = dentry->next;
1303 my_errno = ENOENT;
1304 return -1;
1307 static int
1308 smbfs_fake_share_stat (const char *server_url, const char *path, struct stat *buf)
1310 dir_entry *dentry;
1311 if (strlen (path) < strlen (server_url))
1312 return -1;
1314 if (!current_share_info) { /* Server was not stat()ed */
1315 /* Make sure there is such share at server */
1316 smbfs_connection *sc;
1317 char *p;
1318 p = smbfs_get_path (&sc, path);
1319 g_free (p);
1320 if (p) {
1321 memset (buf, 0, sizeof (*buf));
1322 /* show this as dir */
1323 buf->st_mode =
1324 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
1325 S_IXOTH) & myumask;
1326 return 0;
1328 return -1;
1331 path += strlen (server_url); /* we only want share name */
1332 path++;
1334 if (*path == '/') /* '/' leading server name */
1335 path++; /* probably came from server browsing */
1337 if (!current_share_info->entries) {
1338 if (!smbfs_loaddir (current_share_info)) /* browse host */
1339 return -1;
1341 dentry = current_share_info->entries;
1342 DEBUG (3, ("smbfs_fake_share_stat: %s on %s\n", path, server_url));
1343 while (dentry) {
1344 if (strcmp (dentry->text, path) == 0) {
1345 DEBUG (6, ("smbfs_fake_share_stat: %s:%4o\n",
1346 dentry->text, (int) dentry->my_stat.st_mode));
1347 memcpy (buf, &dentry->my_stat, sizeof (struct stat));
1348 return 0;
1350 dentry = dentry->next;
1352 my_errno = ENOENT;
1353 return -1;
1356 /* stat a single file, smbfs_get_remote_stat callback */
1357 static dir_entry *single_entry;
1359 /* stat a single file */
1360 static int
1361 smbfs_get_remote_stat (smbfs_connection * sc, const char *path, struct stat *buf)
1363 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
1364 char *mypath;
1366 DEBUG (3, ("smbfs_get_remote_stat(): mypath:%s\n", path));
1368 mypath = smbfs_convert_path (path, FALSE);
1370 #if 0 /* single_entry is never free()d now. And only my_stat is used */
1371 single_entry = g_new (dir_entry, 1);
1373 single_entry->text = dos_to_unix (g_strdup (finfo->name), 1);
1375 single_entry->next = 0;
1376 #endif
1377 if (!single_entry)
1378 single_entry = g_new0 (dir_entry, 1);
1380 if (cli_list
1381 (sc->cli, mypath, attribute, smbfs_loaddir_helper, single_entry) < 1) {
1382 my_errno = ENOENT;
1383 g_free (mypath);
1384 return -1; /* cli_list returns number of files */
1387 memcpy (buf, &single_entry->my_stat, sizeof (struct stat));
1389 /* don't free here, use for smbfs_fstat() */
1390 /* g_free(single_entry->text);
1391 g_free(single_entry); */
1392 g_free (mypath);
1393 return 0;
1396 static int
1397 smbfs_search_dir_entry (dir_entry *dentry, const char *text, struct stat *buf)
1399 while (dentry) {
1400 if (strcmp(text, dentry->text) == 0) {
1401 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1402 memcpy(&single_entry->my_stat, &dentry->my_stat,
1403 sizeof(struct stat));
1404 return 0;
1406 dentry = dentry->next;
1408 return -1;
1411 static int
1412 smbfs_get_stat_info (smbfs_connection * sc, const char *path, struct stat *buf)
1414 char *p;
1415 #if 0
1416 dir_entry *dentry = current_info->entries;
1417 #endif
1418 const char *mypath = path;
1420 mypath++; /* cut off leading '/' */
1421 if ((p = strrchr (mypath, '/')))
1422 mypath = p + 1; /* advance until last file/dir name */
1423 DEBUG (3, ("smbfs_get_stat_info: mypath:%s, current_info->dirname:%s\n",
1424 mypath, current_info->dirname));
1425 #if 0
1426 if (!dentry) {
1427 DEBUG (1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n",
1428 current_info->dirname, path));
1429 return -1;
1431 #endif
1432 if (!single_entry) /* when found, this will be written too */
1433 single_entry = g_new (dir_entry, 1);
1434 if (smbfs_search_dir_entry (current_info->entries, mypath, buf) == 0) {
1435 return 0;
1437 /* now try to identify mypath as PARENT dir */
1439 char *mdp;
1440 char *mydir;
1441 mdp = mydir = g_strdup (current_info->dirname);
1442 if ((p = strrchr (mydir, '/')))
1443 *p = 0; /* advance util last '/' */
1444 if ((p = strrchr (mydir, '/')))
1445 mydir = p + 1; /* advance util last '/' */
1446 if (strcmp (mydir, mypath) == 0) { /* fake a stat for ".." */
1447 memset (buf, 0, sizeof (struct stat));
1448 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1449 memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
1450 g_free (mdp);
1451 DEBUG (1, (" PARENT:found in %s\n", current_info->dirname));
1452 return 0;
1454 g_free (mdp);
1456 /* now try to identify as CURRENT dir? */
1458 char *dnp = current_info->dirname;
1459 DEBUG (6, ("smbfs_get_stat_info: is %s current dir? this dir is: %s\n",
1460 mypath, current_info->dirname));
1461 if (*dnp == '/')
1462 dnp++;
1463 else {
1464 return -1;
1466 if (strcmp (mypath, dnp) == 0) {
1467 memset (buf, 0, sizeof (struct stat));
1468 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1469 memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
1470 DEBUG (1, (" CURRENT:found in %s\n", current_info->dirname));
1471 return 0;
1474 DEBUG (3, ("'%s' not found in current_info '%s'\n", path,
1475 current_info->dirname));
1476 /* try to find this in the PREVIOUS listing */
1477 if (previous_info) {
1478 if (smbfs_search_dir_entry (previous_info->entries, mypath, buf) == 0)
1479 return 0;
1480 DEBUG (3, ("'%s' not found in previous_info '%s'\n", path,
1481 previous_info->dirname));
1483 /* try to find this in the SHARE listing */
1484 if (current_share_info) {
1485 if (smbfs_search_dir_entry (current_share_info->entries, mypath, buf) == 0)
1486 return 0;
1487 DEBUG (3, ("'%s' not found in share_info '%s'\n", path,
1488 current_share_info->dirname));
1490 /* try to find this in the SERVER listing */
1491 if (current_server_info) {
1492 if (smbfs_search_dir_entry (current_server_info->entries, mypath, buf) == 0)
1493 return 0;
1494 DEBUG (3, ("'%s' not found in server_info '%s'\n", path,
1495 current_server_info->dirname));
1497 /* nothing found. get stat file info from server */
1498 return smbfs_get_remote_stat (sc, path, buf);
1501 static int
1502 smbfs_chdir (struct vfs_class *me, const char *path)
1504 char *remote_dir;
1505 smbfs_connection *sc;
1507 (void) me;
1509 DEBUG (3, ("smbfs_chdir(path:%s)\n", path));
1510 if (!(remote_dir = smbfs_get_path (&sc, path)))
1511 return -1;
1512 g_free (remote_dir);
1514 return 0;
1517 static int
1518 smbfs_loaddir_by_name (struct vfs_class *me, const char *path)
1520 void *info;
1521 char *mypath, *p;
1523 mypath = g_strdup(path);
1524 p = strrchr(mypath, '/');
1526 if (p > mypath)
1527 *p = 0;
1528 DEBUG(6, ("smbfs_loaddir_by_name(%s)\n", mypath));
1529 smbfs_chdir(me, mypath);
1530 info = smbfs_opendir (me, mypath);
1531 g_free(mypath);
1532 if (!info)
1533 return -1;
1534 smbfs_readdir(info);
1535 smbfs_loaddir(info);
1536 return 0;
1539 static int
1540 smbfs_stat (struct vfs_class * me, const char *path, struct stat *buf)
1542 smbfs_connection *sc;
1543 pstring server_url;
1544 char *service, *pp, *at;
1545 const char *p;
1547 DEBUG (3, ("smbfs_stat(path:%s)\n", path));
1549 if (!current_info) {
1550 DEBUG (1, ("current_info = NULL: "));
1551 if (smbfs_loaddir_by_name (me, path) < 0)
1552 return -1;
1555 /* check if stating server */
1556 p = path;
1557 if (strncmp (p, URL_HEADER, HEADER_LEN)) {
1558 DEBUG (1, ("'%s' doesnt start with '%s' (length %d)\n",
1559 p, URL_HEADER, HEADER_LEN));
1560 return -1;
1563 p += HEADER_LEN;
1564 if (*p == '/')
1565 p++;
1567 pp = strchr (p, '/'); /* advance past next '/' */
1568 at = strchr (p, '@');
1569 pstrcpy (server_url, URL_HEADER);
1570 if (at && at < pp) { /* user@server */
1571 char *z = &(server_url[sizeof (server_url) - 1]);
1572 const char *s = p;
1574 at = &(server_url [HEADER_LEN]) + (at - p + 1);
1575 if (z > at)
1576 z = at;
1577 at = &(server_url [HEADER_LEN]);
1578 while (at < z)
1579 *at++ = *s++;
1580 *z = 0;
1582 pstrcat (server_url, current_bucket->host);
1584 if (!pp) {
1585 if (!current_info->server_list) {
1586 if (smbfs_loaddir_by_name (me, path) < 0)
1587 return -1;
1589 return smbfs_fake_server_stat (server_url, path, buf);
1592 if (!strchr (++pp, '/')) {
1593 return smbfs_fake_share_stat (server_url, path, buf);
1596 /* stating inside share at this point */
1597 if (!(service = smbfs_get_path (&sc, path))) /* connects if necessary */
1598 return -1;
1600 int hostlen = strlen (current_bucket->host);
1601 char *ppp = service + strlen (service) - hostlen;
1602 char *sp = server_url + strlen (server_url) - hostlen;
1604 if (strcmp (sp, ppp) == 0) {
1605 /* make server name appear as directory */
1606 DEBUG (1, ("smbfs_stat: showing server as directory\n"));
1607 memset (buf, 0, sizeof (struct stat));
1608 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1609 g_free (service);
1610 return 0;
1613 /* check if current_info is in share requested */
1614 p = service;
1615 pp = strchr (p, '/');
1616 if (pp) {
1617 p = ++pp; /* advance past server name */
1618 pp = strchr (p, '/');
1620 if (pp)
1621 *pp = 0; /* cut off everthing after service name */
1622 else
1623 p = IPC; /* browsing for services */
1624 pp = current_info->dirname;
1625 if (*pp == '/')
1626 pp++;
1627 if (strncmp (p, pp, strlen (p)) != 0) {
1628 DEBUG (6, ("desired '%s' is not loaded, we have '%s'\n", p, pp));
1629 if (smbfs_loaddir_by_name (me, path) < 0) {
1630 g_free (service);
1631 return -1;
1633 DEBUG (6, ("loaded dir: '%s'\n", current_info->dirname));
1635 g_free (service);
1636 /* stat dirs & files under shares now */
1637 return smbfs_get_stat_info (sc, path, buf);
1640 #define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */
1642 static off_t
1643 smbfs_lseek (void *data, off_t offset, int whence)
1645 smbfs_handle *info = (smbfs_handle *) data;
1646 size_t size;
1648 DEBUG (3,
1649 ("smbfs_lseek(info->nread => %d, offset => %d, whence => %d) \n",
1650 (int) info->nread, (int) offset, whence));
1652 switch (whence) {
1653 case SEEK_SET:
1654 info->nread = offset;
1655 break;
1656 case SEEK_CUR:
1657 info->nread += offset;
1658 break;
1659 case SEEK_END:
1660 if (!cli_qfileinfo (info->cli, info->fnum,
1661 NULL, &size, NULL, NULL, NULL,
1662 NULL, NULL) &&
1663 !cli_getattrE (info->cli, info->fnum,
1664 NULL, &size, NULL, NULL, NULL)) {
1665 errno = EINVAL;
1666 return -1;
1668 info->nread = size + offset;
1669 break;
1672 return info->nread;
1675 static int
1676 smbfs_mknod (struct vfs_class *me, const char *path, int mode, int dev)
1678 (void) me;
1680 DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%d)\n", path, mode, dev));
1681 my_errno = EOPNOTSUPP;
1682 return -1;
1685 static int
1686 smbfs_mkdir (struct vfs_class * me, const char *path, mode_t mode)
1688 smbfs_connection *sc;
1689 char *remote_file;
1690 char *cpath;
1692 (void) me;
1694 DEBUG (3, ("smbfs_mkdir(path:%s, mode:%d)\n", path, (int) mode));
1695 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1696 return -1;
1697 g_free (remote_file);
1698 cpath = smbfs_convert_path (path, FALSE);
1700 if (!cli_mkdir (sc->cli, cpath)) {
1701 my_errno = cli_error (sc->cli, NULL, &err, NULL);
1702 message (D_ERROR, MSG_ERROR, _(" Error %s creating directory %s "),
1703 cli_errstr (sc->cli), CNV_LANG (cpath));
1704 g_free (cpath);
1705 return -1;
1707 g_free (cpath);
1708 return 0;
1711 static int
1712 smbfs_rmdir (struct vfs_class *me, const char *path)
1714 smbfs_connection *sc;
1715 char *remote_file;
1716 char *cpath;
1718 (void) me;
1720 DEBUG(3, ("smbfs_rmdir(path:%s)\n", path));
1721 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1722 return -1;
1723 g_free (remote_file);
1724 cpath = smbfs_convert_path (path, FALSE);
1726 if (!cli_rmdir(sc->cli, cpath)) {
1727 my_errno = cli_error(sc->cli, NULL, &err, NULL);
1728 message (D_ERROR, MSG_ERROR, _(" Error %s removing directory %s "),
1729 cli_errstr(sc->cli), CNV_LANG(cpath));
1730 g_free (cpath);
1731 return -1;
1734 g_free (cpath);
1735 return 0;
1738 static int
1739 smbfs_link (struct vfs_class *me, const char *p1, const char *p2)
1741 (void) me;
1743 DEBUG (3, ("smbfs_link(p1:%s, p2:%s)\n", p1, p2));
1744 my_errno = EOPNOTSUPP;
1745 return -1;
1748 static void
1749 smbfs_free (vfsid id)
1751 DEBUG (3, ("smbfs_free(%p)\n", id));
1752 smbfs_auth_free_all ();
1755 /* Gives up on a socket and reopens the connection, the child own the socket
1756 * now
1758 static void
1759 smbfs_forget (const char *path)
1761 char *host, *user, *p;
1762 int port, i;
1764 if (strncmp (path, URL_HEADER, HEADER_LEN))
1765 return;
1767 DEBUG (3, ("smbfs_forget(path:%s)\n", path));
1769 path += 6;
1770 if (path[0] == '/' && path[1] == '/')
1771 path += 2;
1773 if ((p = smbfs_get_host_and_username (&path, &host, &user, &port, NULL))) {
1774 g_free (p);
1775 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1776 if (smbfs_connections[i].cli
1777 && (strcmp (host, smbfs_connections[i].host) == 0)
1778 && (strcmp (user, smbfs_connections[i].user) == 0)
1779 && (port == smbfs_connections[i].port)) {
1781 /* close socket: the child owns it now */
1782 cli_shutdown (smbfs_connections[i].cli);
1784 /* reopen the connection */
1785 smbfs_connections[i].cli =
1786 smbfs_do_connect (host, smbfs_connections[i].service);
1790 g_free (host);
1791 g_free (user);
1794 static int
1795 smbfs_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg)
1797 (void) me;
1798 (void) arg;
1800 DEBUG (3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path, ctlop));
1801 switch (ctlop) {
1802 case VFS_SETCTL_FORGET:
1803 smbfs_forget (path);
1804 return 0;
1806 return 0;
1810 static smbfs_handle *
1811 smbfs_open_readwrite (smbfs_handle *remote_handle, char *rname, int flags, int mode)
1813 size_t size;
1815 (void) mode;
1817 if (flags & O_TRUNC) /* if it exists truncate to zero */
1818 DEBUG (3, ("smbfs_open: O_TRUNC\n"));
1820 remote_handle->fnum =
1821 #if 1 /* Don't play with flags, it is cli_open() headache */
1822 cli_open (remote_handle->cli, rname, flags, DENY_NONE);
1823 #else /* What's a reasons to has this code ? */
1824 cli_open (remote_handle->cli, rname, ((flags & O_CREAT)
1825 || (flags ==
1826 (O_WRONLY | O_APPEND))) ?
1827 flags : O_RDONLY, DENY_NONE);
1828 #endif
1829 if (remote_handle->fnum == -1) {
1830 message (D_ERROR, MSG_ERROR, _(" %s opening remote file %s "),
1831 cli_errstr (remote_handle->cli), CNV_LANG (rname));
1832 DEBUG (1, ("smbfs_open(rname:%s) error:%s\n",
1833 rname, cli_errstr (remote_handle->cli)));
1834 my_errno = cli_error (remote_handle->cli, NULL, &err, NULL);
1835 return NULL;
1838 if (flags & O_CREAT)
1839 return remote_handle;
1841 if (!cli_qfileinfo (remote_handle->cli, remote_handle->fnum,
1842 &remote_handle->attr, &size, NULL, NULL, NULL, NULL,
1843 NULL)
1844 && !cli_getattrE (remote_handle->cli, remote_handle->fnum,
1845 &remote_handle->attr, &size, NULL, NULL, NULL)) {
1846 message (D_ERROR, MSG_ERROR, " getattrib: %s ",
1847 cli_errstr (remote_handle->cli));
1848 DEBUG (1,
1849 ("smbfs_open(rname:%s) getattrib:%s\n", rname,
1850 cli_errstr (remote_handle->cli)));
1851 my_errno = cli_error (remote_handle->cli, NULL, &err, NULL);
1852 cli_close (remote_handle->cli, remote_handle->fnum);
1853 return NULL;
1856 if ((flags == (O_WRONLY | O_APPEND)) /* file.c:copy_file_file() -> do_append */
1857 && smbfs_lseek (remote_handle, 0, SEEK_END) == -1) {
1858 cli_close (remote_handle->cli, remote_handle->fnum);
1859 return NULL;
1862 return remote_handle;
1865 static void *
1866 smbfs_open (struct vfs_class *me, const char *file, int flags, int mode)
1868 char *remote_file;
1869 void *ret;
1870 smbfs_connection *sc;
1871 smbfs_handle *remote_handle;
1873 (void) me;
1875 DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%o)\n", file, flags, mode));
1877 if (!(remote_file = smbfs_get_path (&sc, file)))
1878 return 0;
1880 remote_file = free_after(smbfs_convert_path (remote_file, FALSE), remote_file);
1882 remote_handle = g_new (smbfs_handle, 2);
1883 remote_handle->cli = sc->cli;
1884 remote_handle->nread = 0;
1886 ret = smbfs_open_readwrite (remote_handle, remote_file, flags, mode);
1888 g_free (remote_file);
1889 if (!ret)
1890 g_free (remote_handle);
1892 return ret;
1895 static int
1896 smbfs_unlink (struct vfs_class *me, const char *path)
1898 smbfs_connection *sc;
1899 char *remote_file;
1901 (void) me;
1903 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1904 return -1;
1906 remote_file = free_after(smbfs_convert_path (remote_file, FALSE), remote_file);
1908 if (!cli_unlink(sc->cli, remote_file)) {
1909 message (D_ERROR, MSG_ERROR, _(" %s removing remote file %s "),
1910 cli_errstr(sc->cli), CNV_LANG(remote_file));
1911 g_free (remote_file);
1912 return -1;
1914 g_free (remote_file);
1915 return 0;
1918 static int
1919 smbfs_rename (struct vfs_class *me, const char *a, const char *b)
1921 smbfs_connection *sc;
1922 char *ra, *rb;
1923 int retval;
1925 (void) me;
1927 if ((ra = smbfs_get_path (&sc, a)) == 0)
1928 return -1;
1930 if ((rb = smbfs_get_path (&sc, b)) == 0) {
1931 g_free (ra);
1932 return -1;
1935 ra = free_after (smbfs_convert_path (ra, FALSE), ra);
1936 rb = free_after (smbfs_convert_path (rb, FALSE), rb);
1938 retval = cli_rename(sc->cli, ra, rb);
1940 g_free (ra);
1941 g_free (rb);
1943 if (!retval) {
1944 message (D_ERROR, MSG_ERROR, _(" %s renaming files\n"),
1945 cli_errstr(sc->cli));
1946 return -1;
1948 return 0;
1951 static int
1952 smbfs_fstat (void *data, struct stat *buf)
1954 smbfs_handle *remote_handle = (smbfs_handle *)data;
1956 DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle->fnum));
1958 /* use left over from previous smbfs_get_remote_stat, if available */
1959 if (single_entry)
1960 memcpy(buf, &single_entry->my_stat, sizeof(struct stat));
1961 else { /* single_entry not set up: bug */
1962 my_errno = EFAULT;
1963 return -EFAULT;
1965 return 0;
1968 void
1969 init_smbfs (void)
1971 vfs_smbfs_ops.name = "smbfs";
1972 vfs_smbfs_ops.prefix = "smb:";
1973 vfs_smbfs_ops.flags = VFSF_NOLINKS;
1974 vfs_smbfs_ops.init = smbfs_init;
1975 vfs_smbfs_ops.fill_names = smbfs_fill_names;
1976 vfs_smbfs_ops.open = smbfs_open;
1977 vfs_smbfs_ops.close = smbfs_close;
1978 vfs_smbfs_ops.read = smbfs_read;
1979 vfs_smbfs_ops.write = smbfs_write;
1980 vfs_smbfs_ops.opendir = smbfs_opendir;
1981 vfs_smbfs_ops.readdir = smbfs_readdir;
1982 vfs_smbfs_ops.closedir = smbfs_closedir;
1983 vfs_smbfs_ops.stat = smbfs_stat;
1984 vfs_smbfs_ops.lstat = smbfs_lstat;
1985 vfs_smbfs_ops.fstat = smbfs_fstat;
1986 vfs_smbfs_ops.chmod = smbfs_chmod;
1987 vfs_smbfs_ops.chown = smbfs_chown;
1988 vfs_smbfs_ops.utime = smbfs_utime;
1989 vfs_smbfs_ops.readlink = smbfs_readlink;
1990 vfs_smbfs_ops.symlink = smbfs_symlink;
1991 vfs_smbfs_ops.link = smbfs_link;
1992 vfs_smbfs_ops.unlink = smbfs_unlink;
1993 vfs_smbfs_ops.rename = smbfs_rename;
1994 vfs_smbfs_ops.chdir = smbfs_chdir;
1995 vfs_smbfs_ops.ferrno = smbfs_errno;
1996 vfs_smbfs_ops.lseek = smbfs_lseek;
1997 vfs_smbfs_ops.mknod = smbfs_mknod;
1998 vfs_smbfs_ops.free = smbfs_free;
1999 vfs_smbfs_ops.mkdir = smbfs_mkdir;
2000 vfs_smbfs_ops.rmdir = smbfs_rmdir;
2001 vfs_smbfs_ops.setctl = smbfs_setctl;
2002 vfs_register_class (&vfs_smbfs_ops);