VFS: fixing symbol clash on vfs_init() and vfs_shut() at non-vfs build
[free-mc.git] / vfs / smbfs.c
blob7a5002da363de2c6975d919024b83f210aa5d3ce
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_try_new (struct smb_authinfo, 1);
189 if (auth == NULL)
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, (char *) NULL);
372 (*func)(path);
373 g_free (path);
378 #define CNV_LANG(s) dos_to_unix(s,False)
379 #define GNAL_VNC(s) unix_to_dos(s,False)
380 /* does same as do_get() in client.c */
381 /* called from vfs.c:1080, count = buffer size */
382 static ssize_t
383 smbfs_read (void *data, char *buffer, int count)
385 smbfs_handle *info = (smbfs_handle *) data;
386 int n;
388 DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%d)\n",
389 info->fnum, (int)info->nread, count));
390 n = cli_read(info->cli, info->fnum, buffer, info->nread, count);
391 if (n > 0)
392 info->nread += n;
393 return n;
396 static ssize_t
397 smbfs_write (void *data, const char *buf, int nbyte)
399 smbfs_handle *info = (smbfs_handle *) data;
400 int n;
402 DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%d)\n",
403 info->fnum, (int)info->nread, nbyte));
404 n = cli_write(info->cli, info->fnum, 0, buf, info->nread, nbyte);
405 if (n > 0)
406 info->nread += n;
407 return n;
410 static int
411 smbfs_close (void *data)
413 smbfs_handle *info = (smbfs_handle *) data;
414 DEBUG (3, ("smbfs_close(fnum:%d)\n", info->fnum));
416 /* FIXME: Why too different cli have the same outbuf
417 * if file is copied to share
419 if (info->cli->outbuf == NULL) {
420 my_errno = EINVAL;
421 return -1;
423 #if 0
424 /* if imlementing archive_level: add rname to smbfs_handle */
425 if (archive_level >= 2 && (inf->attr & aARCH)) {
426 cli_setatr (info->cli, rname, info->attr & ~(uint16) aARCH, 0);
428 #endif
429 return (cli_close (info->cli, info->fnum) == True) ? 0 : -1;
432 static int
433 smbfs_errno (struct vfs_class *me)
435 (void) me;
437 DEBUG(3, ("smbfs_errno: %s\n", g_strerror(my_errno)));
438 return my_errno;
441 typedef struct dir_entry {
442 char *text;
443 struct dir_entry *next;
444 struct stat my_stat;
445 int merrno;
446 } dir_entry;
448 typedef struct {
449 gboolean server_list;
450 char *dirname;
451 char *path; /* the dir originally passed to smbfs_opendir */
452 smbfs_connection *conn;
453 dir_entry *entries;
454 dir_entry *current;
455 } opendir_info;
457 static opendir_info
458 *previous_info,
459 *current_info,
460 *current_share_info,
461 *current_server_info;
463 static gboolean first_direntry;
465 static dir_entry *
466 smbfs_new_dir_entry (const char *name)
468 static int inode_counter;
469 dir_entry *new_entry;
470 new_entry = g_new0 (dir_entry, 1);
471 new_entry->text = dos_to_unix (g_strdup (name), 1);
473 if (first_direntry) {
474 current_info->entries = new_entry;
475 first_direntry = FALSE;
476 } else {
477 current_info->current->next = new_entry;
479 current_info->current = new_entry;
480 new_entry->my_stat.st_ino = inode_counter++;
482 return new_entry;
485 /* browse for shares on server */
486 static void
487 smbfs_browsing_helper (const char *name, uint32 type, const char *comment, void *state)
489 const char *typestr = "";
490 dir_entry *new_entry = smbfs_new_dir_entry (name);
492 (void) state;
494 switch (type) {
495 case STYPE_DISKTREE:
496 typestr = "Disk";
497 /* show this as dir */
498 new_entry->my_stat.st_mode =
499 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
500 S_IXOTH) & myumask;
501 break;
502 case STYPE_PRINTQ:
503 typestr = "Printer";
504 break;
505 case STYPE_DEVICE:
506 typestr = "Device";
507 break;
508 case STYPE_IPC:
509 typestr = "IPC";
510 break;
512 DEBUG (3, ("\t%-15.15s%-10.10s%s\n", name, typestr, comment));
515 static void
516 smbfs_loaddir_helper (file_info * finfo, const char *mask, void *entry)
518 dir_entry *new_entry = (dir_entry *) entry;
519 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
521 (void) mask;
523 #if 0 /* I want to see dot files */
524 if (finfo->mode & aHIDDEN)
525 return; /* don't bother with hidden files, "~$" screws up mc */
526 #endif
527 if (!entry)
528 new_entry = smbfs_new_dir_entry (finfo->name);
530 new_entry->my_stat.st_size = finfo->size;
531 new_entry->my_stat.st_mtime = finfo->mtime;
532 new_entry->my_stat.st_atime = finfo->atime;
533 new_entry->my_stat.st_ctime = finfo->ctime;
534 new_entry->my_stat.st_uid = finfo->uid;
535 new_entry->my_stat.st_gid = finfo->gid;
537 new_entry->my_stat.st_mode = /* rw-rw-rw */
538 S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH;
540 /* if (finfo->mode & aVOLID); nothing similar in real world */
541 if (finfo->mode & aDIR)
542 new_entry->my_stat.st_mode |= /* drwxrwxrwx */
543 S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
544 else
545 new_entry->my_stat.st_mode |= S_IFREG; /* if not dir, regular file? */
546 /* if (finfo->mode & aARCH); DOS archive */
547 /* if (finfo->mode & aHIDDEN); like a dot file? */
548 /* if (finfo->mode & aSYSTEM); like a kernel? */
549 if (finfo->mode & aRONLY)
550 new_entry->my_stat.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
551 new_entry->my_stat.st_mode &= myumask;
553 DEBUG (entry ? 3 : 6, (" %-30s%7.7s%8.0f %s",
554 CNV_LANG (finfo->name),
555 attrib_string (finfo->mode),
556 (double) finfo->size,
557 asctime (LocalTime (&t))));
560 /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
561 static char *
562 smbfs_convert_path (const char *remote_file, gboolean trailing_asterik)
564 const char *p, *my_remote;
565 char *result;
567 my_remote = remote_file;
568 if (strncmp (my_remote, URL_HEADER, HEADER_LEN) == 0) { /* if passed directly */
569 my_remote += HEADER_LEN;
570 if (*my_remote == '/') /* from server browsing */
571 my_remote++;
572 p = strchr(my_remote, '/');
573 if (p)
574 my_remote = p+1; /* advance to end of server name */
577 if (*my_remote == '/')
578 my_remote++; /* strip off leading '/' */
579 p = strchr(my_remote, '/');
580 if (p)
581 my_remote = p; /* strip off share/service name */
582 /* create remote filename as understood by smb clientgen */
583 result = g_strconcat (my_remote, trailing_asterik ? "/*" : "", (char *) NULL);
584 unix_to_dos (result, /* inplace = */ 1); /* code page conversion */
585 str_replace(result, '/', '\\');
586 return result;
589 static void
590 smbfs_srv_browsing_helper (const char *name, uint32 m, const char *comment,
591 void *state)
593 dir_entry *new_entry = smbfs_new_dir_entry (name);
595 (void) m;
596 (void) state;
598 /* show this as dir */
599 new_entry->my_stat.st_mode =
600 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
601 S_IXOTH) & myumask;
603 DEBUG (3, ("\t%-16.16s %s\n", name, comment));
606 static BOOL
607 smbfs_reconnect(smbfs_connection *conn, int *retries)
609 char *host;
610 DEBUG(3, ("RECONNECT\n"));
612 if (*(conn->host) == 0)
613 host = g_strdup(conn->cli->desthost); /* server browsing */
614 else
615 host = g_strdup(conn->host);
617 cli_shutdown(conn->cli);
619 if (!(conn->cli = smbfs_do_connect(host, conn->service))) {
620 message (D_ERROR, MSG_ERROR,
621 _(" reconnect to %s failed\n "), conn->host);
622 g_free(host);
623 return False;
625 g_free(host);
626 if (++(*retries) == 2)
627 return False;
628 return True;
631 static BOOL
632 smbfs_send(struct cli_state *cli)
634 size_t len;
635 size_t nwritten=0;
636 ssize_t ret;
638 len = smb_len(cli->outbuf) + 4;
640 while (nwritten < len) {
641 ret = write_socket(cli->fd, cli->outbuf+nwritten, len - nwritten);
642 if (ret <= 0) {
643 if (errno == EPIPE)
644 return False;
645 } else
646 nwritten += ret;
649 return True;
652 /****************************************************************************
653 See if server has cut us off by checking for EPIPE when writing.
654 Taken from cli_chkpath()
655 ****************************************************************************/
656 static BOOL
657 smbfs_chkpath(struct cli_state *cli, const char *path, BOOL send_only)
659 fstring path2;
660 char *p;
662 fstrcpy(path2,path);
663 unix_to_dos (path2, 1);
664 trim_string(path2,NULL,"\\");
665 if (!*path2) *path2 = '\\';
667 memset(cli->outbuf,'\0',smb_size);
668 set_message(cli->outbuf,0,4 + strlen(path2),True);
669 SCVAL(cli->outbuf,smb_com,SMBchkpth);
670 SSVAL(cli->outbuf,smb_tid,cli->cnum);
672 cli->rap_error = 0;
673 cli->nt_error = 0;
674 SSVAL(cli->outbuf,smb_pid,cli->pid);
675 SSVAL(cli->outbuf,smb_uid,cli->vuid);
676 SSVAL(cli->outbuf,smb_mid,cli->mid);
677 if (cli->protocol > PROTOCOL_CORE) {
678 SCVAL(cli->outbuf,smb_flg,0x8);
679 SSVAL(cli->outbuf,smb_flg2,0x1);
682 p = smb_buf(cli->outbuf);
683 *p++ = 4;
684 fstrcpy(p,path2);
686 if (!smbfs_send(cli)) {
687 DEBUG(3, ("smbfs_chkpath: couldnt send\n"));
688 return False;
690 if (send_only) {
691 client_receive_smb(cli->fd, cli->inbuf, cli->timeout);
692 DEBUG(3, ("smbfs_chkpath: send only OK\n"));
693 return True; /* just testing for EPIPE */
695 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout)) {
696 DEBUG(3, ("smbfs_chkpath: receive error\n"));
697 return False;
699 if ((my_errno = cli_error(cli, NULL, NULL, NULL))) {
700 if (my_errno == 20 || my_errno == 13)
701 return True; /* ignore if 'not a directory' error */
702 DEBUG(3, ("smbfs_chkpath: cli_error: %s\n", g_strerror(my_errno)));
703 return False;
706 return True;
709 #if 1
710 static int
711 smbfs_fs (const char *text)
713 const char *p = text;
714 int count = 0;
716 while ((p = strchr(p, '/')) != NULL) {
717 count++;
718 p++;
720 if (count == 1)
721 return strlen(text);
722 return count;
724 #endif
726 static int
727 smbfs_loaddir (opendir_info *smbfs_info)
729 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
730 int servlen = strlen (smbfs_info->conn->service);
731 const char *info_dirname = smbfs_info->dirname;
732 char *my_dirname;
734 DEBUG (3, ("smbfs_loaddir: dirname:%s\n", info_dirname));
735 first_direntry = TRUE;
737 if (current_info) {
738 DEBUG (3,
739 ("smbfs_loaddir: new:'%s', cached:'%s'\n", info_dirname,
740 current_info->dirname));
741 /* if new desired dir is longer than cached in current_info */
742 if (smbfs_fs (info_dirname) > smbfs_fs (current_info->dirname)) {
743 DEBUG (3, ("saving to previous_info\n"));
744 previous_info = current_info;
748 current_info = smbfs_info;
750 if (strcmp (info_dirname, "/") == 0) {
751 if (!strcmp (smbfs_info->path, URL_HEADER)) {
752 DEBUG (6, ("smbfs_loaddir: browsing %s\n", IPC));
753 /* browse for servers */
754 if (!cli_NetServerEnum
755 (smbfs_info->conn->cli, smbfs_info->conn->domain,
756 SV_TYPE_ALL, smbfs_srv_browsing_helper, NULL))
757 return 0;
758 else
759 current_server_info = smbfs_info;
760 smbfs_info->server_list = TRUE;
761 } else {
762 /* browse for shares */
763 if (cli_RNetShareEnum
764 (smbfs_info->conn->cli, smbfs_browsing_helper, NULL) < 1)
765 return 0;
766 else
767 current_share_info = smbfs_info;
769 goto done;
772 /* do regular directory listing */
773 if (strncmp (smbfs_info->conn->service, info_dirname + 1, servlen) == 0) {
774 /* strip share name from dir */
775 my_dirname = g_strdup (info_dirname + servlen);
776 *my_dirname = '/';
777 my_dirname = free_after(smbfs_convert_path (my_dirname, TRUE), my_dirname);
778 } else
779 my_dirname = smbfs_convert_path (info_dirname, TRUE);
781 DEBUG (6, ("smbfs_loaddir: service: %s\n", smbfs_info->conn->service));
782 DEBUG (6,
783 ("smbfs_loaddir: cli->share: %s\n",
784 smbfs_info->conn->cli->share));
785 DEBUG (6,
786 ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname));
787 /* do file listing: cli_list returns number of files */
788 if (cli_list
789 (smbfs_info->conn->cli, my_dirname, attribute,
790 smbfs_loaddir_helper, NULL) < 0) {
791 /* cli_list returns -1 if directory empty or cannot read socket */
792 my_errno = cli_error (smbfs_info->conn->cli, NULL, &err, NULL);
793 g_free (my_dirname);
794 return 0;
796 if (*(my_dirname) == 0)
797 smbfs_info->dirname = smbfs_info->conn->service;
798 g_free (my_dirname);
799 /* do_dskattr(); */
801 done:
802 /* current_info->parent = smbfs_info->dirname; */
804 smbfs_info->current = smbfs_info->entries;
805 return 1; /* 1 = ok */
808 #ifdef SMBFS_FREE_DIR
809 static void
810 smbfs_free_dir (dir_entry *de)
812 if (!de) return;
814 smbfs_free_dir (de->next);
815 g_free (de->text);
816 g_free (de);
818 #endif
821 /* The readdir routine loads the complete directory */
822 /* It's too slow to ask the server each time */
823 /* It now also sends the complete lstat information for each file */
824 static void *
825 smbfs_readdir(void *info)
827 static union vfs_dirent smbfs_readdir_data;
828 static char *const dirent_dest = smbfs_readdir_data.dent.d_name;
829 opendir_info *smbfs_info = (opendir_info *) info;
831 DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info->dirname));
833 if (!smbfs_info->entries)
834 if (!smbfs_loaddir(smbfs_info))
835 return NULL;
837 if (smbfs_info->current == 0) { /* reached end of dir entries */
838 DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
839 #ifdef SMBFS_FREE_DIR
840 smbfs_free_dir(smbfs_info->entries);
841 smbfs_info->entries = 0;
842 #endif
843 return NULL;
845 g_strlcpy(dirent_dest, smbfs_info->current->text, MC_MAXPATHLEN);
846 smbfs_info->current = smbfs_info->current->next;
848 compute_namelen(&smbfs_readdir_data.dent);
850 return &smbfs_readdir_data;
853 static int
854 smbfs_closedir (void *info)
856 opendir_info *smbfs_info = (opendir_info *) info;
857 /* dir_entry *p, *q; */
859 DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info->dirname));
860 /* CLOSE HERE */
862 /* for (p = smbfs_info->entries; p;){
863 q = p;
864 p = p->next;
865 g_free (q->text);
866 g_free (q);
868 g_free (info); */
869 return 0;
872 static int
873 smbfs_chmod (struct vfs_class *me, const char *path, int mode)
875 (void) me;
877 DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path, mode));
878 /* my_errno = EOPNOTSUPP;
879 return -1; */ /* cannot chmod on smb filesystem */
880 return 0; /* make mc happy */
883 static int
884 smbfs_chown (struct vfs_class *me, const char *path, int owner, int group)
886 (void) me;
888 DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path, owner, group));
889 my_errno = EOPNOTSUPP; /* ready for your labotomy? */
890 return -1;
893 static int
894 smbfs_utime (struct vfs_class *me, const char *path, struct utimbuf *times)
896 (void) me;
897 (void) times;
899 DEBUG(3, ("smbfs_utime(path:%s)\n", path));
900 my_errno = EOPNOTSUPP;
901 return -1;
904 static int
905 smbfs_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
907 (void) me;
909 DEBUG (3,
910 ("smbfs_readlink(path:%s, buf:%s, size:%d)\n", path, buf,
911 (int) size));
912 my_errno = EOPNOTSUPP;
913 return -1; /* no symlinks on smb filesystem? */
916 static int
917 smbfs_symlink (struct vfs_class *me, const char *n1, const char *n2)
919 (void) me;
921 DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1, n2));
922 my_errno = EOPNOTSUPP;
923 return -1; /* no symlinks on smb filesystem? */
926 /* Extract the hostname and username from the path */
927 /* path is in the form: [user@]hostname/share/remote-dir */
928 #define smbfs_get_host_and_username(path, host, user, port, pass) \
929 vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
931 /*****************************************************
932 return a connection to a SMB server
933 current_bucket needs to be set before calling
934 *******************************************************/
935 static struct cli_state *
936 smbfs_do_connect (const char *server, char *share)
938 struct cli_state *c;
939 struct nmb_name called, calling;
940 struct in_addr ip;
942 DEBUG(3, ("smbfs_do_connect(%s, %s)\n", server, share));
943 if (*share == '\\') {
944 server = share+2;
945 share = strchr(server,'\\');
946 if (!share) return NULL;
947 *share = 0;
948 share++;
951 make_nmb_name(&calling, global_myname, 0x0);
952 make_nmb_name(&called , server, current_bucket->name_type);
954 for (;;) {
956 ip = (current_bucket->have_ip) ? current_bucket->dest_ip : ipzero;
958 /* have to open a new connection */
959 if (!(c = cli_initialise(NULL))) {
960 my_errno = ENOMEM;
961 return NULL;
964 pwd_init(&(c->pwd)); /* should be moved into cli_initialise()? */
965 pwd_set_cleartext(&(c->pwd), current_bucket->password);
967 if ((cli_set_port(c, current_bucket->port) == 0) ||
968 !cli_connect(c, server, &ip)) {
969 DEBUG(1, ("Connection to %s failed\n", server));
970 break;
973 if (!cli_session_request(c, &calling, &called)) {
974 my_errno = cli_error(c, NULL, &err, NULL);
975 DEBUG(1, ("session request to %s failed\n", called.name));
976 cli_shutdown(c);
977 if (strcmp(called.name, "*SMBSERVER")) {
978 make_nmb_name(&called , "*SMBSERVER", 0x20);
979 continue;
981 return NULL;
984 DEBUG(3, (" session request ok\n"));
986 if (!cli_negprot(c)) {
987 DEBUG(1, ("protocol negotiation failed\n"));
988 break;
991 if (!cli_session_setup(c, current_bucket->user,
992 current_bucket->password, strlen(current_bucket->password),
993 current_bucket->password, strlen(current_bucket->password),
994 current_bucket->domain)) {
995 DEBUG(1,("session setup failed: %s\n", cli_errstr(c)));
996 smbfs_auth_remove (server, share);
997 break;
1000 if (*c->server_domain || *c->server_os || *c->server_type)
1001 DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
1002 c->server_domain,c->server_os,c->server_type));
1004 DEBUG(3, (" session setup ok\n"));
1006 if (!cli_send_tconX(c, share, "?????",
1007 current_bucket->password, strlen(current_bucket->password)+1)) {
1008 DEBUG(1,("%s: tree connect failed: %s\n", share, cli_errstr(c)));
1009 break;
1012 DEBUG(3, (" tconx ok\n"));
1014 my_errno = 0;
1015 return c;
1018 my_errno = cli_error(c, NULL, &err, NULL);
1019 cli_shutdown(c);
1020 return NULL;
1024 static int
1025 smbfs_get_master_browser(char **host)
1027 static char so_broadcast[] = "SO_BROADCAST";
1028 int count;
1029 struct in_addr *ip_list, bcast_addr;
1031 /* does port = 137 for win95 master browser? */
1032 int fd= open_socket_in( SOCK_DGRAM, 0, 3,
1033 interpret_addr(lp_socket_address()), True );
1034 if (fd == -1)
1035 return 0;
1036 set_socket_options(fd, so_broadcast);
1037 ip_list = iface_bcast(ipzero);
1038 bcast_addr = *ip_list;
1039 if ((ip_list = name_query(fd, "\01\02__MSBROWSE__\02", 1, True,
1040 True, bcast_addr, &count, NULL))) {
1041 if (!count)
1042 return 0;
1043 /* just return first master browser */
1044 *host = g_strdup(inet_ntoa(ip_list[0]));
1045 return 1;
1047 return 0;
1050 static void
1051 smbfs_free_bucket (smbfs_connection *bucket)
1053 g_free (bucket->host);
1054 g_free (bucket->service);
1055 g_free (bucket->domain);
1056 g_free (bucket->user);
1057 wipe_password (bucket->password);
1058 g_free (bucket->home);
1059 memset (bucket, 0, sizeof (smbfs_connection));
1062 static smbfs_connection *
1063 smbfs_get_free_bucket (void)
1065 int i;
1067 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++)
1068 if (!smbfs_connections [i].cli) return &smbfs_connections [i];
1070 { /* search for most dormant connection */
1071 int oldest = 0; /* index */
1072 time_t oldest_time = smbfs_connections[0].last_use;
1073 for (i = 1; i < SMBFS_MAX_CONNECTIONS; i++) {
1074 if (smbfs_connections[i].last_use < oldest_time) {
1075 oldest_time = smbfs_connections[i].last_use;
1076 oldest = i;
1079 cli_shutdown(smbfs_connections[oldest].cli);
1080 smbfs_free_bucket (&smbfs_connections[oldest]);
1081 return &smbfs_connections[oldest];
1084 /* This can't happend, since we have checked for max connections before */
1085 vfs_die("Internal error: smbfs_get_free_bucket");
1086 return 0; /* shut up, stupid gcc */
1089 /* This routine keeps track of open connections */
1090 /* Returns a connected socket to host */
1091 static smbfs_connection *
1092 smbfs_open_link (char *host, char *path, const char *user, int *port,
1093 char *this_pass)
1095 int i;
1096 smbfs_connection *bucket;
1097 pstring service;
1098 struct in_addr *dest_ip = NULL;
1100 DEBUG (3, ("smbfs_open_link(host:%s, path:%s)\n", host, path));
1102 if (strcmp (host, path) == 0) /* if host & path are same: */
1103 pstrcpy (service, IPC); /* setup for browse */
1104 else { /* get share name from path, path starts with server name */
1105 char *p;
1106 if ((p = strchr (path, '/'))) /* get share aka */
1107 pstrcpy (service, ++p); /* service name from path */
1108 else
1109 pstrcpy (service, "");
1110 /* now check for trailing directory/filenames */
1111 p = strchr (service, '/');
1112 if (p)
1113 *p = 0; /* cut off dir/files: sharename only */
1114 if (!*service)
1115 pstrcpy (service, IPC); /* setup for browse */
1116 DEBUG (6, ("smbfs_open_link: service from path:%s\n", service));
1119 if (got_user)
1120 user = username; /* global from getenv */
1122 /* Is the link actually open? */
1123 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1124 if (!smbfs_connections[i].cli)
1125 continue;
1126 if ((strcmp (host, smbfs_connections[i].host) == 0) &&
1127 (strcmp (user, smbfs_connections[i].user) == 0) &&
1128 (strcmp (service, smbfs_connections[i].service) == 0)) {
1129 int retries = 0;
1130 BOOL inshare = (*host != 0 && *path != 0 && strchr (path, '/'));
1131 /* check if this connection has died */
1132 while (!smbfs_chkpath (smbfs_connections[i].cli, "\\", !inshare)) {
1133 if (!smbfs_reconnect (&smbfs_connections[i], &retries))
1134 return 0;
1136 DEBUG (6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i));
1137 current_bucket = &smbfs_connections[i];
1138 smbfs_connections[i].last_use = time (NULL);
1139 return &smbfs_connections[i];
1141 /* connection not found, find if we have ip for new connection */
1142 if (strcmp (host, smbfs_connections[i].host) == 0)
1143 dest_ip = &smbfs_connections[i].cli->dest_ip;
1146 /* make new connection */
1147 bucket = smbfs_get_free_bucket ();
1148 bucket->name_type = 0x20;
1149 bucket->home = 0;
1150 bucket->port = *port;
1151 bucket->have_ip = False;
1152 if (dest_ip) {
1153 bucket->have_ip = True;
1154 bucket->dest_ip = *dest_ip;
1156 current_bucket = bucket;
1158 bucket->user = g_strdup (user);
1159 bucket->service = g_strdup (service);
1161 if (!(*host)) { /* if blank host name, browse for servers */
1162 if (!smbfs_get_master_browser (&host)) /* set host to ip of master browser */
1163 return 0; /* could not find master browser? */
1164 g_free (host);
1165 bucket->host = g_strdup (""); /* blank host means master browser */
1166 } else
1167 bucket->host = g_strdup (host);
1169 if (!smbfs_bucket_set_authinfo (bucket, 0, /* domain currently not used */
1170 user, this_pass, 1))
1171 return 0;
1173 /* connect to share */
1174 while (!(bucket->cli = smbfs_do_connect (host, service))) {
1176 if (my_errno != EPERM)
1177 return 0;
1178 message (D_ERROR, MSG_ERROR, _(" Authentication failed "));
1180 /* authentication failed, try again */
1181 smbfs_auth_remove (bucket->host, bucket->service);
1182 if (!smbfs_bucket_set_authinfo (bucket, bucket->domain, bucket->user, 0, 0))
1183 return 0;
1187 smbfs_open_connections++;
1188 DEBUG (3, ("smbfs_open_link:smbfs_open_connections: %d\n",
1189 smbfs_open_connections));
1190 return bucket;
1193 static char *
1194 smbfs_get_path (smbfs_connection ** sc, const char *path)
1196 char *user, *host, *remote_path, *pass;
1197 int port = SMB_PORT;
1199 DEBUG (3, ("smbfs_get_path(%s)\n", path));
1200 if (strncmp (path, URL_HEADER, HEADER_LEN))
1201 return NULL;
1202 path += HEADER_LEN;
1204 if (*path == '/') /* '/' leading server name */
1205 path++; /* probably came from server browsing */
1207 if ((remote_path =
1208 smbfs_get_host_and_username (&path, &host, &user, &port, &pass)))
1209 if ((*sc =
1210 smbfs_open_link (host, remote_path, user, &port, pass)) == NULL) {
1211 g_free (remote_path);
1212 remote_path = NULL;
1214 g_free (host);
1215 g_free (user);
1216 if (pass)
1217 wipe_password (pass);
1219 if (!remote_path)
1220 return NULL;
1222 /* NOTE: tildes are deprecated. See ftpfs.c */
1224 int f = !strcmp (remote_path, "/~");
1225 if (f || !strncmp (remote_path, "/~/", 3)) {
1226 char *s;
1227 s = concat_dir_and_file ((*sc)->home, remote_path + 3 - f);
1228 g_free (remote_path);
1229 return s;
1232 return remote_path;
1235 #if 0
1236 static int
1237 is_error (int result, int errno_num)
1239 if (!(result == -1))
1240 return my_errno = 0;
1241 else
1242 my_errno = errno_num;
1243 return 1;
1245 #endif
1247 static void *
1248 smbfs_opendir (struct vfs_class *me, const char *dirname)
1250 opendir_info *smbfs_info;
1251 smbfs_connection *sc;
1252 char *remote_dir;
1254 (void) me;
1256 DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname));
1258 if (!(remote_dir = smbfs_get_path (&sc, dirname)))
1259 return NULL;
1261 /* FIXME: where freed? */
1262 smbfs_info = g_new (opendir_info, 1);
1263 smbfs_info->server_list = FALSE;
1264 smbfs_info->path = g_strdup(dirname); /* keep original */
1265 smbfs_info->dirname = remote_dir;
1266 smbfs_info->conn = sc;
1267 smbfs_info->entries = 0;
1268 smbfs_info->current = 0;
1270 return smbfs_info;
1273 static int
1274 smbfs_fake_server_stat (const char *server_url, const char *path, struct stat *buf)
1276 dir_entry *dentry;
1277 const char *p;
1279 (void) server_url;
1281 if ((p = strrchr (path, '/')))
1282 path = p + 1; /* advance until last '/' */
1284 if (!current_info->entries) {
1285 if (!smbfs_loaddir (current_info)) /* browse host */
1286 return -1;
1289 if (current_info->server_list == True) {
1290 dentry = current_info->entries;
1291 DEBUG (4, ("fake stat for SERVER \"%s\"\n", path));
1292 while (dentry) {
1293 if (strcmp (dentry->text, path) == 0) {
1294 DEBUG (4, ("smbfs_fake_server_stat: %s:%4o\n",
1295 dentry->text, (int)dentry->my_stat.st_mode));
1296 memcpy (buf, &dentry->my_stat, sizeof (struct stat));
1297 return 0;
1299 dentry = dentry->next;
1302 my_errno = ENOENT;
1303 return -1;
1306 static int
1307 smbfs_fake_share_stat (const char *server_url, const char *path, struct stat *buf)
1309 dir_entry *dentry;
1310 if (strlen (path) < strlen (server_url))
1311 return -1;
1313 if (!current_share_info) { /* Server was not stat()ed */
1314 /* Make sure there is such share at server */
1315 smbfs_connection *sc;
1316 char *p;
1317 p = smbfs_get_path (&sc, path);
1318 g_free (p);
1319 if (p) {
1320 memset (buf, 0, sizeof (*buf));
1321 /* show this as dir */
1322 buf->st_mode =
1323 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
1324 S_IXOTH) & myumask;
1325 return 0;
1327 return -1;
1330 path += strlen (server_url); /* we only want share name */
1331 path++;
1333 if (*path == '/') /* '/' leading server name */
1334 path++; /* probably came from server browsing */
1336 if (!current_share_info->entries) {
1337 if (!smbfs_loaddir (current_share_info)) /* browse host */
1338 return -1;
1340 dentry = current_share_info->entries;
1341 DEBUG (3, ("smbfs_fake_share_stat: %s on %s\n", path, server_url));
1342 while (dentry) {
1343 if (strcmp (dentry->text, path) == 0) {
1344 DEBUG (6, ("smbfs_fake_share_stat: %s:%4o\n",
1345 dentry->text, (int) dentry->my_stat.st_mode));
1346 memcpy (buf, &dentry->my_stat, sizeof (struct stat));
1347 return 0;
1349 dentry = dentry->next;
1351 my_errno = ENOENT;
1352 return -1;
1355 /* stat a single file, smbfs_get_remote_stat callback */
1356 static dir_entry *single_entry;
1358 /* stat a single file */
1359 static int
1360 smbfs_get_remote_stat (smbfs_connection * sc, const char *path, struct stat *buf)
1362 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
1363 char *mypath;
1365 DEBUG (3, ("smbfs_get_remote_stat(): mypath:%s\n", path));
1367 mypath = smbfs_convert_path (path, FALSE);
1369 #if 0 /* single_entry is never free()d now. And only my_stat is used */
1370 single_entry = g_new (dir_entry, 1);
1372 single_entry->text = dos_to_unix (g_strdup (finfo->name), 1);
1374 single_entry->next = 0;
1375 #endif
1376 if (!single_entry)
1377 single_entry = g_new0 (dir_entry, 1);
1379 if (cli_list
1380 (sc->cli, mypath, attribute, smbfs_loaddir_helper, single_entry) < 1) {
1381 my_errno = ENOENT;
1382 g_free (mypath);
1383 return -1; /* cli_list returns number of files */
1386 memcpy (buf, &single_entry->my_stat, sizeof (struct stat));
1388 /* don't free here, use for smbfs_fstat() */
1389 /* g_free(single_entry->text);
1390 g_free(single_entry); */
1391 g_free (mypath);
1392 return 0;
1395 static int
1396 smbfs_search_dir_entry (dir_entry *dentry, const char *text, struct stat *buf)
1398 while (dentry) {
1399 if (strcmp(text, dentry->text) == 0) {
1400 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1401 memcpy(&single_entry->my_stat, &dentry->my_stat,
1402 sizeof(struct stat));
1403 return 0;
1405 dentry = dentry->next;
1407 return -1;
1410 static int
1411 smbfs_get_stat_info (smbfs_connection * sc, const char *path, struct stat *buf)
1413 char *p;
1414 #if 0
1415 dir_entry *dentry = current_info->entries;
1416 #endif
1417 const char *mypath = path;
1419 mypath++; /* cut off leading '/' */
1420 if ((p = strrchr (mypath, '/')))
1421 mypath = p + 1; /* advance until last file/dir name */
1422 DEBUG (3, ("smbfs_get_stat_info: mypath:%s, current_info->dirname:%s\n",
1423 mypath, current_info->dirname));
1424 #if 0
1425 if (!dentry) {
1426 DEBUG (1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n",
1427 current_info->dirname, path));
1428 return -1;
1430 #endif
1431 if (!single_entry) /* when found, this will be written too */
1432 single_entry = g_new (dir_entry, 1);
1433 if (smbfs_search_dir_entry (current_info->entries, mypath, buf) == 0) {
1434 return 0;
1436 /* now try to identify mypath as PARENT dir */
1438 char *mdp;
1439 char *mydir;
1440 mdp = mydir = g_strdup (current_info->dirname);
1441 if ((p = strrchr (mydir, '/')))
1442 *p = 0; /* advance util last '/' */
1443 if ((p = strrchr (mydir, '/')))
1444 mydir = p + 1; /* advance util last '/' */
1445 if (strcmp (mydir, mypath) == 0) { /* fake a stat for ".." */
1446 memset (buf, 0, sizeof (struct stat));
1447 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1448 memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
1449 g_free (mdp);
1450 DEBUG (1, (" PARENT:found in %s\n", current_info->dirname));
1451 return 0;
1453 g_free (mdp);
1455 /* now try to identify as CURRENT dir? */
1457 char *dnp = current_info->dirname;
1458 DEBUG (6, ("smbfs_get_stat_info: is %s current dir? this dir is: %s\n",
1459 mypath, current_info->dirname));
1460 if (*dnp == '/')
1461 dnp++;
1462 else {
1463 return -1;
1465 if (strcmp (mypath, dnp) == 0) {
1466 memset (buf, 0, sizeof (struct stat));
1467 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1468 memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
1469 DEBUG (1, (" CURRENT:found in %s\n", current_info->dirname));
1470 return 0;
1473 DEBUG (3, ("'%s' not found in current_info '%s'\n", path,
1474 current_info->dirname));
1475 /* try to find this in the PREVIOUS listing */
1476 if (previous_info) {
1477 if (smbfs_search_dir_entry (previous_info->entries, mypath, buf) == 0)
1478 return 0;
1479 DEBUG (3, ("'%s' not found in previous_info '%s'\n", path,
1480 previous_info->dirname));
1482 /* try to find this in the SHARE listing */
1483 if (current_share_info) {
1484 if (smbfs_search_dir_entry (current_share_info->entries, mypath, buf) == 0)
1485 return 0;
1486 DEBUG (3, ("'%s' not found in share_info '%s'\n", path,
1487 current_share_info->dirname));
1489 /* try to find this in the SERVER listing */
1490 if (current_server_info) {
1491 if (smbfs_search_dir_entry (current_server_info->entries, mypath, buf) == 0)
1492 return 0;
1493 DEBUG (3, ("'%s' not found in server_info '%s'\n", path,
1494 current_server_info->dirname));
1496 /* nothing found. get stat file info from server */
1497 return smbfs_get_remote_stat (sc, path, buf);
1500 static int
1501 smbfs_chdir (struct vfs_class *me, const char *path)
1503 char *remote_dir;
1504 smbfs_connection *sc;
1506 (void) me;
1508 DEBUG (3, ("smbfs_chdir(path:%s)\n", path));
1509 if (!(remote_dir = smbfs_get_path (&sc, path)))
1510 return -1;
1511 g_free (remote_dir);
1513 return 0;
1516 static int
1517 smbfs_loaddir_by_name (struct vfs_class *me, const char *path)
1519 void *info;
1520 char *mypath, *p;
1522 mypath = g_strdup(path);
1523 p = strrchr(mypath, '/');
1525 if (p > mypath)
1526 *p = 0;
1527 DEBUG(6, ("smbfs_loaddir_by_name(%s)\n", mypath));
1528 smbfs_chdir(me, mypath);
1529 info = smbfs_opendir (me, mypath);
1530 g_free(mypath);
1531 if (!info)
1532 return -1;
1533 smbfs_readdir(info);
1534 smbfs_loaddir(info);
1535 return 0;
1538 static int
1539 smbfs_stat (struct vfs_class * me, const char *path, struct stat *buf)
1541 smbfs_connection *sc;
1542 pstring server_url;
1543 char *service, *pp, *at;
1544 const char *p;
1546 DEBUG (3, ("smbfs_stat(path:%s)\n", path));
1548 if (!current_info) {
1549 DEBUG (1, ("current_info = NULL: "));
1550 if (smbfs_loaddir_by_name (me, path) < 0)
1551 return -1;
1554 /* check if stating server */
1555 p = path;
1556 if (strncmp (p, URL_HEADER, HEADER_LEN)) {
1557 DEBUG (1, ("'%s' doesnt start with '%s' (length %d)\n",
1558 p, URL_HEADER, HEADER_LEN));
1559 return -1;
1562 p += HEADER_LEN;
1563 if (*p == '/')
1564 p++;
1566 pp = strchr (p, '/'); /* advance past next '/' */
1567 at = strchr (p, '@');
1568 pstrcpy (server_url, URL_HEADER);
1569 if (at && at < pp) { /* user@server */
1570 char *z = &(server_url[sizeof (server_url) - 1]);
1571 const char *s = p;
1573 at = &(server_url [HEADER_LEN]) + (at - p + 1);
1574 if (z > at)
1575 z = at;
1576 at = &(server_url [HEADER_LEN]);
1577 while (at < z)
1578 *at++ = *s++;
1579 *z = 0;
1581 pstrcat (server_url, current_bucket->host);
1583 if (!pp) {
1584 if (!current_info->server_list) {
1585 if (smbfs_loaddir_by_name (me, path) < 0)
1586 return -1;
1588 return smbfs_fake_server_stat (server_url, path, buf);
1591 if (!strchr (++pp, '/')) {
1592 return smbfs_fake_share_stat (server_url, path, buf);
1595 /* stating inside share at this point */
1596 if (!(service = smbfs_get_path (&sc, path))) /* connects if necessary */
1597 return -1;
1599 int hostlen = strlen (current_bucket->host);
1600 char *ppp = service + strlen (service) - hostlen;
1601 char *sp = server_url + strlen (server_url) - hostlen;
1603 if (strcmp (sp, ppp) == 0) {
1604 /* make server name appear as directory */
1605 DEBUG (1, ("smbfs_stat: showing server as directory\n"));
1606 memset (buf, 0, sizeof (struct stat));
1607 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1608 g_free (service);
1609 return 0;
1612 /* check if current_info is in share requested */
1613 p = service;
1614 pp = strchr (p, '/');
1615 if (pp) {
1616 p = ++pp; /* advance past server name */
1617 pp = strchr (p, '/');
1619 if (pp)
1620 *pp = 0; /* cut off everthing after service name */
1621 else
1622 p = IPC; /* browsing for services */
1623 pp = current_info->dirname;
1624 if (*pp == '/')
1625 pp++;
1626 if (strncmp (p, pp, strlen (p)) != 0) {
1627 DEBUG (6, ("desired '%s' is not loaded, we have '%s'\n", p, pp));
1628 if (smbfs_loaddir_by_name (me, path) < 0) {
1629 g_free (service);
1630 return -1;
1632 DEBUG (6, ("loaded dir: '%s'\n", current_info->dirname));
1634 g_free (service);
1635 /* stat dirs & files under shares now */
1636 return smbfs_get_stat_info (sc, path, buf);
1639 #define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */
1641 static off_t
1642 smbfs_lseek (void *data, off_t offset, int whence)
1644 smbfs_handle *info = (smbfs_handle *) data;
1645 size_t size;
1647 DEBUG (3,
1648 ("smbfs_lseek(info->nread => %d, offset => %d, whence => %d) \n",
1649 (int) info->nread, (int) offset, whence));
1651 switch (whence) {
1652 case SEEK_SET:
1653 info->nread = offset;
1654 break;
1655 case SEEK_CUR:
1656 info->nread += offset;
1657 break;
1658 case SEEK_END:
1659 if (!cli_qfileinfo (info->cli, info->fnum,
1660 NULL, &size, NULL, NULL, NULL,
1661 NULL, NULL) &&
1662 !cli_getattrE (info->cli, info->fnum,
1663 NULL, &size, NULL, NULL, NULL)) {
1664 errno = EINVAL;
1665 return -1;
1667 info->nread = size + offset;
1668 break;
1671 return info->nread;
1674 static int
1675 smbfs_mknod (struct vfs_class *me, const char *path, int mode, int dev)
1677 (void) me;
1679 DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%d)\n", path, mode, dev));
1680 my_errno = EOPNOTSUPP;
1681 return -1;
1684 static int
1685 smbfs_mkdir (struct vfs_class * me, const char *path, mode_t mode)
1687 smbfs_connection *sc;
1688 char *remote_file;
1689 char *cpath;
1691 (void) me;
1693 DEBUG (3, ("smbfs_mkdir(path:%s, mode:%d)\n", path, (int) mode));
1694 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1695 return -1;
1696 g_free (remote_file);
1697 cpath = smbfs_convert_path (path, FALSE);
1699 if (!cli_mkdir (sc->cli, cpath)) {
1700 my_errno = cli_error (sc->cli, NULL, &err, NULL);
1701 message (D_ERROR, MSG_ERROR, _(" Error %s creating directory %s "),
1702 cli_errstr (sc->cli), CNV_LANG (cpath));
1703 g_free (cpath);
1704 return -1;
1706 g_free (cpath);
1707 return 0;
1710 static int
1711 smbfs_rmdir (struct vfs_class *me, const char *path)
1713 smbfs_connection *sc;
1714 char *remote_file;
1715 char *cpath;
1717 (void) me;
1719 DEBUG(3, ("smbfs_rmdir(path:%s)\n", path));
1720 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1721 return -1;
1722 g_free (remote_file);
1723 cpath = smbfs_convert_path (path, FALSE);
1725 if (!cli_rmdir(sc->cli, cpath)) {
1726 my_errno = cli_error(sc->cli, NULL, &err, NULL);
1727 message (D_ERROR, MSG_ERROR, _(" Error %s removing directory %s "),
1728 cli_errstr(sc->cli), CNV_LANG(cpath));
1729 g_free (cpath);
1730 return -1;
1733 g_free (cpath);
1734 return 0;
1737 static int
1738 smbfs_link (struct vfs_class *me, const char *p1, const char *p2)
1740 (void) me;
1742 DEBUG (3, ("smbfs_link(p1:%s, p2:%s)\n", p1, p2));
1743 my_errno = EOPNOTSUPP;
1744 return -1;
1747 static void
1748 smbfs_free (vfsid id)
1750 DEBUG (3, ("smbfs_free(%p)\n", id));
1751 smbfs_auth_free_all ();
1754 /* Gives up on a socket and reopens the connection, the child own the socket
1755 * now
1757 static void
1758 smbfs_forget (const char *path)
1760 char *host, *user, *p;
1761 int port, i;
1763 if (strncmp (path, URL_HEADER, HEADER_LEN))
1764 return;
1766 DEBUG (3, ("smbfs_forget(path:%s)\n", path));
1768 path += 6;
1769 if (path[0] == '/' && path[1] == '/')
1770 path += 2;
1772 if ((p = smbfs_get_host_and_username (&path, &host, &user, &port, NULL))) {
1773 g_free (p);
1774 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1775 if (smbfs_connections[i].cli
1776 && (strcmp (host, smbfs_connections[i].host) == 0)
1777 && (strcmp (user, smbfs_connections[i].user) == 0)
1778 && (port == smbfs_connections[i].port)) {
1780 /* close socket: the child owns it now */
1781 cli_shutdown (smbfs_connections[i].cli);
1783 /* reopen the connection */
1784 smbfs_connections[i].cli =
1785 smbfs_do_connect (host, smbfs_connections[i].service);
1789 g_free (host);
1790 g_free (user);
1793 static int
1794 smbfs_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg)
1796 (void) me;
1797 (void) arg;
1799 DEBUG (3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path, ctlop));
1800 switch (ctlop) {
1801 case VFS_SETCTL_FORGET:
1802 smbfs_forget (path);
1803 return 0;
1805 return 0;
1809 static smbfs_handle *
1810 smbfs_open_readwrite (smbfs_handle *remote_handle, char *rname, int flags, int mode)
1812 size_t size;
1814 (void) mode;
1816 if (flags & O_TRUNC) /* if it exists truncate to zero */
1817 DEBUG (3, ("smbfs_open: O_TRUNC\n"));
1819 remote_handle->fnum =
1820 #if 1 /* Don't play with flags, it is cli_open() headache */
1821 cli_open (remote_handle->cli, rname, flags, DENY_NONE);
1822 #else /* What's a reasons to has this code ? */
1823 cli_open (remote_handle->cli, rname, ((flags & O_CREAT)
1824 || (flags ==
1825 (O_WRONLY | O_APPEND))) ?
1826 flags : O_RDONLY, DENY_NONE);
1827 #endif
1828 if (remote_handle->fnum == -1) {
1829 message (D_ERROR, MSG_ERROR, _(" %s opening remote file %s "),
1830 cli_errstr (remote_handle->cli), CNV_LANG (rname));
1831 DEBUG (1, ("smbfs_open(rname:%s) error:%s\n",
1832 rname, cli_errstr (remote_handle->cli)));
1833 my_errno = cli_error (remote_handle->cli, NULL, &err, NULL);
1834 return NULL;
1837 if (flags & O_CREAT)
1838 return remote_handle;
1840 if (!cli_qfileinfo (remote_handle->cli, remote_handle->fnum,
1841 &remote_handle->attr, &size, NULL, NULL, NULL, NULL,
1842 NULL)
1843 && !cli_getattrE (remote_handle->cli, remote_handle->fnum,
1844 &remote_handle->attr, &size, NULL, NULL, NULL)) {
1845 message (D_ERROR, MSG_ERROR, " getattrib: %s ",
1846 cli_errstr (remote_handle->cli));
1847 DEBUG (1,
1848 ("smbfs_open(rname:%s) getattrib:%s\n", rname,
1849 cli_errstr (remote_handle->cli)));
1850 my_errno = cli_error (remote_handle->cli, NULL, &err, NULL);
1851 cli_close (remote_handle->cli, remote_handle->fnum);
1852 return NULL;
1855 if ((flags == (O_WRONLY | O_APPEND)) /* file.c:copy_file_file() -> do_append */
1856 && smbfs_lseek (remote_handle, 0, SEEK_END) == -1) {
1857 cli_close (remote_handle->cli, remote_handle->fnum);
1858 return NULL;
1861 return remote_handle;
1864 static void *
1865 smbfs_open (struct vfs_class *me, const char *file, int flags, int mode)
1867 char *remote_file;
1868 void *ret;
1869 smbfs_connection *sc;
1870 smbfs_handle *remote_handle;
1872 (void) me;
1874 DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%o)\n", file, flags, mode));
1876 if (!(remote_file = smbfs_get_path (&sc, file)))
1877 return 0;
1879 remote_file = free_after(smbfs_convert_path (remote_file, FALSE), remote_file);
1881 remote_handle = g_new (smbfs_handle, 2);
1882 remote_handle->cli = sc->cli;
1883 remote_handle->nread = 0;
1885 ret = smbfs_open_readwrite (remote_handle, remote_file, flags, mode);
1887 g_free (remote_file);
1888 if (!ret)
1889 g_free (remote_handle);
1891 return ret;
1894 static int
1895 smbfs_unlink (struct vfs_class *me, const char *path)
1897 smbfs_connection *sc;
1898 char *remote_file;
1900 (void) me;
1902 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1903 return -1;
1905 remote_file = free_after(smbfs_convert_path (remote_file, FALSE), remote_file);
1907 if (!cli_unlink(sc->cli, remote_file)) {
1908 message (D_ERROR, MSG_ERROR, _(" %s removing remote file %s "),
1909 cli_errstr(sc->cli), CNV_LANG(remote_file));
1910 g_free (remote_file);
1911 return -1;
1913 g_free (remote_file);
1914 return 0;
1917 static int
1918 smbfs_rename (struct vfs_class *me, const char *a, const char *b)
1920 smbfs_connection *sc;
1921 char *ra, *rb;
1922 int retval;
1924 (void) me;
1926 if ((ra = smbfs_get_path (&sc, a)) == 0)
1927 return -1;
1929 if ((rb = smbfs_get_path (&sc, b)) == 0) {
1930 g_free (ra);
1931 return -1;
1934 ra = free_after (smbfs_convert_path (ra, FALSE), ra);
1935 rb = free_after (smbfs_convert_path (rb, FALSE), rb);
1937 retval = cli_rename(sc->cli, ra, rb);
1939 g_free (ra);
1940 g_free (rb);
1942 if (!retval) {
1943 message (D_ERROR, MSG_ERROR, _(" %s renaming files\n"),
1944 cli_errstr(sc->cli));
1945 return -1;
1947 return 0;
1950 static int
1951 smbfs_fstat (void *data, struct stat *buf)
1953 smbfs_handle *remote_handle = (smbfs_handle *)data;
1955 DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle->fnum));
1957 /* use left over from previous smbfs_get_remote_stat, if available */
1958 if (single_entry)
1959 memcpy(buf, &single_entry->my_stat, sizeof(struct stat));
1960 else { /* single_entry not set up: bug */
1961 my_errno = EFAULT;
1962 return -EFAULT;
1964 return 0;
1967 void
1968 init_smbfs (void)
1970 vfs_smbfs_ops.name = "smbfs";
1971 vfs_smbfs_ops.prefix = "smb:";
1972 vfs_smbfs_ops.flags = VFSF_NOLINKS;
1973 vfs_smbfs_ops.init = smbfs_init;
1974 vfs_smbfs_ops.fill_names = smbfs_fill_names;
1975 vfs_smbfs_ops.open = smbfs_open;
1976 vfs_smbfs_ops.close = smbfs_close;
1977 vfs_smbfs_ops.read = smbfs_read;
1978 vfs_smbfs_ops.write = smbfs_write;
1979 vfs_smbfs_ops.opendir = smbfs_opendir;
1980 vfs_smbfs_ops.readdir = smbfs_readdir;
1981 vfs_smbfs_ops.closedir = smbfs_closedir;
1982 vfs_smbfs_ops.stat = smbfs_stat;
1983 vfs_smbfs_ops.lstat = smbfs_lstat;
1984 vfs_smbfs_ops.fstat = smbfs_fstat;
1985 vfs_smbfs_ops.chmod = smbfs_chmod;
1986 vfs_smbfs_ops.chown = smbfs_chown;
1987 vfs_smbfs_ops.utime = smbfs_utime;
1988 vfs_smbfs_ops.readlink = smbfs_readlink;
1989 vfs_smbfs_ops.symlink = smbfs_symlink;
1990 vfs_smbfs_ops.link = smbfs_link;
1991 vfs_smbfs_ops.unlink = smbfs_unlink;
1992 vfs_smbfs_ops.rename = smbfs_rename;
1993 vfs_smbfs_ops.chdir = smbfs_chdir;
1994 vfs_smbfs_ops.ferrno = smbfs_errno;
1995 vfs_smbfs_ops.lseek = smbfs_lseek;
1996 vfs_smbfs_ops.mknod = smbfs_mknod;
1997 vfs_smbfs_ops.free = smbfs_free;
1998 vfs_smbfs_ops.mkdir = smbfs_mkdir;
1999 vfs_smbfs_ops.rmdir = smbfs_rmdir;
2000 vfs_smbfs_ops.setctl = smbfs_setctl;
2001 vfs_register_class (&vfs_smbfs_ops);