2008-04-09 Marco Ciampa <ciampix@libero.it>
[midnight-commander.git] / vfs / smbfs.c
bloba133fa341c531cc9576b234e848766dd78358e16
1 /* Virtual File System: Midnight Commander file system.
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
4 Free Software Foundation, Inc.
6 Written by Wayne Roberts <wroberts1@home.com>, 1997
7 Andrew V. Samoilov <sav@bcs.zp.ua> 2002, 2003
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public License
11 as published by the Free Software Foundation; either version 2 of
12 the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public
20 License along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 /* Namespace: exports init_smbfs, smbfs_set_debug(), smbfs_set_debugf() */
24 #include <config.h>
25 #include <stdio.h>
26 #include <sys/types.h>
28 #undef USE_NCURSES /* Don't include *curses.h */
29 #include "../src/global.h"
30 #include "../src/tty.h" /* enable/disable interrupt key */
31 #include "../src/wtools.h" /* message() */
32 #include "../src/main.h" /* print_vfs_message */
33 #include "utilvfs.h"
35 #undef PACKAGE_BUGREPORT
36 #undef PACKAGE_NAME
37 #undef PACKAGE_STRING
38 #undef PACKAGE_TARNAME
39 #undef PACKAGE_VERSION
41 #include "samba/include/config.h"
42 /* don't load crap in "samba/include/includes.h" we don't use and which
43 conflicts with definitions in other includes */
44 #undef HAVE_LIBREADLINE
45 #define NO_CONFIG_H
46 #undef VERSION
48 #include "samba/include/includes.h"
50 #include <string.h>
52 #include "vfs.h"
53 #include "vfs-impl.h"
54 #include "smbfs.h"
56 #define SMBFS_MAX_CONNECTIONS 16
57 static const char * const IPC = "IPC$";
58 static const char * const URL_HEADER = "/#smb:";
59 #define HEADER_LEN 6
61 static int my_errno;
62 static uint32 err;
64 /* stuff that is same with each connection */
65 extern int DEBUGLEVEL;
66 extern pstring myhostname;
67 extern struct in_addr ipzero;
68 static mode_t myumask = 0755;
69 extern pstring global_myname;
70 static int smbfs_open_connections = 0;
71 static gboolean got_user = FALSE;
72 static gboolean got_pass = FALSE;
73 static pstring password;
74 static pstring username;
75 static struct vfs_class vfs_smbfs_ops;
77 static struct _smbfs_connection {
78 struct cli_state *cli;
79 struct in_addr dest_ip;
80 BOOL have_ip;
81 char *host; /* server name */
82 char *service; /* share name */
83 char *domain;
84 char *user;
85 char *home;
86 char *password;
87 int port;
88 int name_type;
89 time_t last_use;
90 } smbfs_connections [SMBFS_MAX_CONNECTIONS];
91 /* unique to each connection */
93 /* modifies *share */
94 static struct cli_state * smbfs_do_connect (const char *server, char *share);
96 typedef struct _smbfs_connection smbfs_connection;
97 static smbfs_connection *current_bucket;
99 typedef struct {
100 struct cli_state *cli;
101 int fnum;
102 off_t nread;
103 uint16 attr;
104 } smbfs_handle;
106 static GSList *auth_list;
108 /* this function allows you to write:
109 * char *s = g_strdup("hello, world");
110 * s = free_after(g_strconcat(s, s, (char *)0), s);
112 static inline char *
113 free_after (char *result, char *string_to_free)
115 g_free(string_to_free);
116 return result;
120 static void
121 smbfs_auth_free (struct smb_authinfo const *a)
123 g_free (a->host);
124 g_free (a->share);
125 g_free (a->domain);
126 g_free (a->user);
127 wipe_password (a->password);
130 static void
131 smbfs_auth_free_all (void)
133 if (auth_list) {
134 g_slist_foreach (auth_list, (GFunc)smbfs_auth_free, 0);
135 g_slist_free (auth_list);
136 auth_list = 0;
140 static gint
141 smbfs_auth_cmp_host_and_share (gconstpointer _a, gconstpointer _b)
143 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
144 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
146 if (!a->host || !a->share || !b->host || !b->share)
147 return 1;
148 if (strcmp (a->host, b->host) != 0)
149 return 1;
150 if (strcmp (a->share, b->share) != 0)
151 return 1;
152 return 0;
155 static gint
156 smbfs_auth_cmp_host (gconstpointer _a, gconstpointer _b)
158 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
159 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
161 if (!a->host || !b->host)
162 return 1;
163 if (strcmp (a->host, b->host) != 0)
164 return 1;
165 if (strcmp (a->share, IPC) != 0)
166 return 1;
167 return 0;
170 static void
171 smbfs_auth_add (const char *host, const char *share, const char *domain,
172 const char *user, const char *password)
174 struct smb_authinfo *auth = g_new (struct smb_authinfo, 1);
176 if (!auth)
177 return;
179 /* Don't check for NULL, g_strdup already does. */
180 auth->host = g_strdup (host);
181 auth->share = g_strdup (share);
182 auth->domain = g_strdup (domain);
183 auth->user = g_strdup (user);
184 auth->password = g_strdup (password);
185 auth_list = g_slist_prepend (auth_list, auth);
188 static void
189 smbfs_auth_remove (const char *host, const char *share)
191 struct smb_authinfo data;
192 struct smb_authinfo *auth;
193 GSList *list;
195 data.host = g_strdup (host);
196 data.share = g_strdup (share);
197 list = g_slist_find_custom (auth_list,
198 &data,
199 smbfs_auth_cmp_host_and_share);
200 g_free (data.host);
201 g_free (data.share);
202 if (!list)
203 return;
204 auth = list->data;
205 auth_list = g_slist_remove (auth_list, auth);
206 smbfs_auth_free (auth);
209 /* Set authentication information in bucket. Return 1 if successful, else 0 */
210 /* Information in auth_list overrides user if pass is NULL. */
211 /* bucket->host and bucket->service must be valid. */
212 static int
213 smbfs_bucket_set_authinfo (smbfs_connection *bucket,
214 const char *domain, const char *user, const char *pass,
215 int fallback_to_host)
217 struct smb_authinfo data;
218 struct smb_authinfo *auth;
219 GSList *list;
221 if (domain && user && pass) {
222 g_free (bucket->domain);
223 g_free (bucket->user);
224 g_free (bucket->password);
225 bucket->domain = g_strdup (domain);
226 bucket->user = g_strdup (user);
227 bucket->password = g_strdup (pass);
228 smbfs_auth_remove (bucket->host, bucket->service);
229 smbfs_auth_add (bucket->host, bucket->service,
230 domain, user, pass);
231 return 1;
234 data.host = bucket->host;
235 data.share = bucket->service;
236 list = g_slist_find_custom (auth_list, &data, smbfs_auth_cmp_host_and_share);
237 if (!list && fallback_to_host)
238 list = g_slist_find_custom (auth_list, &data, smbfs_auth_cmp_host);
239 if (list) {
240 auth = list->data;
241 bucket->domain = g_strdup (auth->domain);
242 bucket->user = g_strdup (auth->user);
243 bucket->password = g_strdup (auth->password);
244 return 1;
247 if (got_pass) {
248 bucket->domain = g_strdup (lp_workgroup ());
249 bucket->user = g_strdup (got_user ? username : user);
250 bucket->password = g_strdup (password);
251 return 1;
254 auth = vfs_smb_get_authinfo (bucket->host,
255 bucket->service,
256 (domain ? domain : lp_workgroup ()),
257 user);
258 if (auth) {
259 g_free (bucket->domain);
260 g_free (bucket->user);
261 g_free (bucket->password);
262 bucket->domain = g_strdup (auth->domain);
263 bucket->user = g_strdup (auth->user);
264 bucket->password = g_strdup (auth->password);
265 smbfs_auth_remove (bucket->host, bucket->service);
266 auth_list = g_slist_prepend (auth_list, auth);
267 return 1;
269 return 0;
272 void
273 smbfs_set_debug (int arg)
275 DEBUGLEVEL = arg;
278 void
279 smbfs_set_debugf (const char *filename)
281 extern pstring debugf;
282 extern FILE *dbf;
283 if (DEBUGLEVEL > 0) {
284 FILE *outfile = fopen (filename, "w");
285 if (outfile) {
286 setup_logging ("", True); /* No needs for timestamp for each message */
287 dbf = outfile;
288 setbuf (dbf, NULL);
289 pstrcpy (debugf, filename);
294 /********************** The callbacks ******************************/
295 static int
296 smbfs_init (struct vfs_class * me)
298 const char *servicesf = CONFIGDIR PATH_SEP_STR "smb.conf";
300 /* DEBUGLEVEL = 4; */
302 TimeInit ();
303 charset_initialise ();
305 DEBUG (3, ("smbfs_init(%s)\n", me->name));
307 if (!get_myname (myhostname, NULL))
308 DEBUG (0, ("Failed to get my hostname.\n"));
310 if (!lp_load (servicesf, True, False, False))
311 DEBUG (0, ("Cannot load %s - run testparm to debug it\n", servicesf));
313 codepage_initialise (lp_client_code_page ());
315 load_interfaces ();
317 myumask = umask (0);
318 umask (myumask);
319 myumask = ~myumask;
321 if (getenv ("USER")) {
322 char *p;
324 pstrcpy (username, getenv ("USER"));
325 got_user = TRUE;
326 DEBUG (3, ("smbfs_init(): $USER:%s\n", username));
327 if ((p = strchr (username, '%'))) {
328 *p = 0;
329 pstrcpy (password, p + 1);
330 got_pass = TRUE;
331 memset (strchr (getenv ("USER"), '%') + 1, 'X', strlen (password));
332 DEBUG (3, ("smbfs_init(): $USER%%pass: %s%%%s\n",
333 username, password));
335 strupper (username);
337 if (getenv ("PASSWD")) {
338 pstrcpy (password, getenv ("PASSWD"));
339 got_pass = TRUE;
341 return 1;
344 static void
345 smbfs_fill_names (struct vfs_class *me, fill_names_f func)
347 int i;
348 char *path;
350 (void) me;
352 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
353 if (smbfs_connections [i].cli) {
354 path = g_strconcat (URL_HEADER,
355 smbfs_connections[i].user, "@",
356 smbfs_connections[i].host,
357 "/", smbfs_connections[i].service,
358 NULL);
359 (*func)(path);
360 g_free (path);
365 #define CNV_LANG(s) dos_to_unix(s,False)
366 #define GNAL_VNC(s) unix_to_dos(s,False)
367 /* does same as do_get() in client.c */
368 /* called from vfs.c:1080, count = buffer size */
369 static int
370 smbfs_read (void *data, char *buffer, int count)
372 smbfs_handle *info = (smbfs_handle *) data;
373 int n;
375 DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%d)\n",
376 info->fnum, (int)info->nread, count));
377 n = cli_read(info->cli, info->fnum, buffer, info->nread, count);
378 if (n > 0)
379 info->nread += n;
380 return n;
383 static int
384 smbfs_write (void *data, const char *buf, int nbyte)
386 smbfs_handle *info = (smbfs_handle *) data;
387 int n;
389 DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%d)\n",
390 info->fnum, (int)info->nread, nbyte));
391 n = cli_write(info->cli, info->fnum, 0, buf, info->nread, nbyte);
392 if (n > 0)
393 info->nread += n;
394 return n;
397 static int
398 smbfs_close (void *data)
400 smbfs_handle *info = (smbfs_handle *) data;
401 DEBUG (3, ("smbfs_close(fnum:%d)\n", info->fnum));
403 /* FIXME: Why too different cli have the same outbuf
404 * if file is copied to share
406 if (info->cli->outbuf == NULL) {
407 my_errno = EINVAL;
408 return -1;
410 #if 0
411 /* if imlementing archive_level: add rname to smbfs_handle */
412 if (archive_level >= 2 && (inf->attr & aARCH)) {
413 cli_setatr (info->cli, rname, info->attr & ~(uint16) aARCH, 0);
415 #endif
416 return (cli_close (info->cli, info->fnum) == True) ? 0 : -1;
419 static int
420 smbfs_errno (struct vfs_class *me)
422 (void) me;
424 DEBUG(3, ("smbfs_errno: %s\n", g_strerror(my_errno)));
425 return my_errno;
428 typedef struct dir_entry {
429 char *text;
430 struct dir_entry *next;
431 struct stat my_stat;
432 int merrno;
433 } dir_entry;
435 typedef struct {
436 gboolean server_list;
437 char *dirname;
438 char *path; /* the dir originally passed to smbfs_opendir */
439 smbfs_connection *conn;
440 dir_entry *entries;
441 dir_entry *current;
442 } opendir_info;
444 static opendir_info
445 *previous_info,
446 *current_info,
447 *current_share_info,
448 *current_server_info;
450 static gboolean first_direntry;
452 static dir_entry *
453 smbfs_new_dir_entry (const char *name)
455 static int inode_counter;
456 dir_entry *new_entry;
457 new_entry = g_new0 (dir_entry, 1);
458 new_entry->text = dos_to_unix (g_strdup (name), 1);
460 if (first_direntry) {
461 current_info->entries = new_entry;
462 first_direntry = FALSE;
463 } else {
464 current_info->current->next = new_entry;
466 current_info->current = new_entry;
467 new_entry->my_stat.st_ino = inode_counter++;
469 return new_entry;
472 /* browse for shares on server */
473 static void
474 smbfs_browsing_helper (const char *name, uint32 type, const char *comment, void *state)
476 const char *typestr = "";
477 dir_entry *new_entry = smbfs_new_dir_entry (name);
479 (void) state;
481 switch (type) {
482 case STYPE_DISKTREE:
483 typestr = "Disk";
484 /* show this as dir */
485 new_entry->my_stat.st_mode =
486 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
487 S_IXOTH) & myumask;
488 break;
489 case STYPE_PRINTQ:
490 typestr = "Printer";
491 break;
492 case STYPE_DEVICE:
493 typestr = "Device";
494 break;
495 case STYPE_IPC:
496 typestr = "IPC";
497 break;
499 DEBUG (3, ("\t%-15.15s%-10.10s%s\n", name, typestr, comment));
502 static void
503 smbfs_loaddir_helper (file_info * finfo, const char *mask, void *entry)
505 dir_entry *new_entry = (dir_entry *) entry;
506 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
508 (void) mask;
510 #if 0 /* I want to see dot files */
511 if (finfo->mode & aHIDDEN)
512 return; /* don't bother with hidden files, "~$" screws up mc */
513 #endif
514 if (!entry)
515 new_entry = smbfs_new_dir_entry (finfo->name);
517 new_entry->my_stat.st_size = finfo->size;
518 new_entry->my_stat.st_mtime = finfo->mtime;
519 new_entry->my_stat.st_atime = finfo->atime;
520 new_entry->my_stat.st_ctime = finfo->ctime;
521 new_entry->my_stat.st_uid = finfo->uid;
522 new_entry->my_stat.st_gid = finfo->gid;
524 new_entry->my_stat.st_mode = /* rw-rw-rw */
525 S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH;
527 /* if (finfo->mode & aVOLID); nothing similar in real world */
528 if (finfo->mode & aDIR)
529 new_entry->my_stat.st_mode |= /* drwxrwxrwx */
530 S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
531 else
532 new_entry->my_stat.st_mode |= S_IFREG; /* if not dir, regular file? */
533 /* if (finfo->mode & aARCH); DOS archive */
534 /* if (finfo->mode & aHIDDEN); like a dot file? */
535 /* if (finfo->mode & aSYSTEM); like a kernel? */
536 if (finfo->mode & aRONLY)
537 new_entry->my_stat.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
538 new_entry->my_stat.st_mode &= myumask;
540 DEBUG (entry ? 3 : 6, (" %-30s%7.7s%8.0f %s",
541 CNV_LANG (finfo->name),
542 attrib_string (finfo->mode),
543 (double) finfo->size,
544 asctime (LocalTime (&t))));
547 /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
548 static char *
549 smbfs_convert_path (const char *remote_file, gboolean trailing_asterik)
551 const char *p, *my_remote;
552 char *result;
554 my_remote = remote_file;
555 if (strncmp (my_remote, URL_HEADER, HEADER_LEN) == 0) { /* if passed directly */
556 my_remote += HEADER_LEN;
557 if (*my_remote == '/') /* from server browsing */
558 my_remote++;
559 p = strchr(my_remote, '/');
560 if (p)
561 my_remote = p+1; /* advance to end of server name */
564 if (*my_remote == '/')
565 my_remote++; /* strip off leading '/' */
566 p = strchr(my_remote, '/');
567 if (p)
568 my_remote = p; /* strip off share/service name */
569 /* create remote filename as understood by smb clientgen */
570 result = g_strconcat (my_remote, trailing_asterik ? "/*" : "", (char *) NULL);
571 unix_to_dos (result, /* inplace = */ 1); /* code page conversion */
572 str_replace(result, '/', '\\');
573 return result;
576 static void
577 smbfs_srv_browsing_helper (const char *name, uint32 m, const char *comment,
578 void *state)
580 dir_entry *new_entry = smbfs_new_dir_entry (name);
582 (void) m;
583 (void) state;
585 /* show this as dir */
586 new_entry->my_stat.st_mode =
587 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
588 S_IXOTH) & myumask;
590 DEBUG (3, ("\t%-16.16s %s\n", name, comment));
593 static BOOL
594 smbfs_reconnect(smbfs_connection *conn, int *retries)
596 char *host;
597 DEBUG(3, ("RECONNECT\n"));
599 if (*(conn->host) == 0)
600 host = g_strdup(conn->cli->desthost); /* server browsing */
601 else
602 host = g_strdup(conn->host);
604 cli_shutdown(conn->cli);
606 if (!(conn->cli = smbfs_do_connect(host, conn->service))) {
607 message (1, MSG_ERROR,
608 _(" reconnect to %s failed\n "), conn->host);
609 g_free(host);
610 return False;
612 g_free(host);
613 if (++(*retries) == 2)
614 return False;
615 return True;
618 static BOOL
619 smbfs_send(struct cli_state *cli)
621 size_t len;
622 size_t nwritten=0;
623 ssize_t ret;
625 len = smb_len(cli->outbuf) + 4;
627 while (nwritten < len) {
628 ret = write_socket(cli->fd, cli->outbuf+nwritten, len - nwritten);
629 if (ret <= 0) {
630 if (errno == EPIPE)
631 return False;
632 } else
633 nwritten += ret;
636 return True;
639 /****************************************************************************
640 See if server has cut us off by checking for EPIPE when writing.
641 Taken from cli_chkpath()
642 ****************************************************************************/
643 static BOOL
644 smbfs_chkpath(struct cli_state *cli, const char *path, BOOL send_only)
646 fstring path2;
647 char *p;
649 fstrcpy(path2,path);
650 unix_to_dos (path2, 1);
651 trim_string(path2,NULL,"\\");
652 if (!*path2) *path2 = '\\';
654 memset(cli->outbuf,'\0',smb_size);
655 set_message(cli->outbuf,0,4 + strlen(path2),True);
656 SCVAL(cli->outbuf,smb_com,SMBchkpth);
657 SSVAL(cli->outbuf,smb_tid,cli->cnum);
659 cli->rap_error = 0;
660 cli->nt_error = 0;
661 SSVAL(cli->outbuf,smb_pid,cli->pid);
662 SSVAL(cli->outbuf,smb_uid,cli->vuid);
663 SSVAL(cli->outbuf,smb_mid,cli->mid);
664 if (cli->protocol > PROTOCOL_CORE) {
665 SCVAL(cli->outbuf,smb_flg,0x8);
666 SSVAL(cli->outbuf,smb_flg2,0x1);
669 p = smb_buf(cli->outbuf);
670 *p++ = 4;
671 fstrcpy(p,path2);
673 if (!smbfs_send(cli)) {
674 DEBUG(3, ("smbfs_chkpath: couldnt send\n"));
675 return False;
677 if (send_only) {
678 client_receive_smb(cli->fd, cli->inbuf, cli->timeout);
679 DEBUG(3, ("smbfs_chkpath: send only OK\n"));
680 return True; /* just testing for EPIPE */
682 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout)) {
683 DEBUG(3, ("smbfs_chkpath: receive error\n"));
684 return False;
686 if ((my_errno = cli_error(cli, NULL, NULL, NULL))) {
687 if (my_errno == 20 || my_errno == 13)
688 return True; /* ignore if 'not a directory' error */
689 DEBUG(3, ("smbfs_chkpath: cli_error: %s\n", g_strerror(my_errno)));
690 return False;
693 return True;
696 #if 1
697 static int
698 smbfs_fs (const char *text)
700 const char *p = text;
701 int count = 0;
703 while ((p = strchr(p, '/')) != NULL) {
704 count++;
705 p++;
707 if (count == 1)
708 return strlen(text);
709 return count;
711 #endif
713 static int
714 smbfs_loaddir (opendir_info *smbfs_info)
716 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
717 int servlen = strlen (smbfs_info->conn->service);
718 const char *info_dirname = smbfs_info->dirname;
719 char *my_dirname;
721 DEBUG (3, ("smbfs_loaddir: dirname:%s\n", info_dirname));
722 first_direntry = TRUE;
724 if (current_info) {
725 DEBUG (3,
726 ("smbfs_loaddir: new:'%s', cached:'%s'\n", info_dirname,
727 current_info->dirname));
728 /* if new desired dir is longer than cached in current_info */
729 if (smbfs_fs (info_dirname) > smbfs_fs (current_info->dirname)) {
730 DEBUG (3, ("saving to previous_info\n"));
731 previous_info = current_info;
735 current_info = smbfs_info;
737 if (strcmp (info_dirname, "/") == 0) {
738 if (!strcmp (smbfs_info->path, URL_HEADER)) {
739 DEBUG (6, ("smbfs_loaddir: browsing %s\n", IPC));
740 /* browse for servers */
741 if (!cli_NetServerEnum
742 (smbfs_info->conn->cli, smbfs_info->conn->domain,
743 SV_TYPE_ALL, smbfs_srv_browsing_helper, NULL))
744 return 0;
745 else
746 current_server_info = smbfs_info;
747 smbfs_info->server_list = TRUE;
748 } else {
749 /* browse for shares */
750 if (cli_RNetShareEnum
751 (smbfs_info->conn->cli, smbfs_browsing_helper, NULL) < 1)
752 return 0;
753 else
754 current_share_info = smbfs_info;
756 goto done;
759 /* do regular directory listing */
760 if (strncmp (smbfs_info->conn->service, info_dirname + 1, servlen) == 0) {
761 /* strip share name from dir */
762 my_dirname = g_strdup (info_dirname + servlen);
763 *my_dirname = '/';
764 my_dirname = free_after(smbfs_convert_path (my_dirname, TRUE), my_dirname);
765 } else
766 my_dirname = smbfs_convert_path (info_dirname, TRUE);
768 DEBUG (6, ("smbfs_loaddir: service: %s\n", smbfs_info->conn->service));
769 DEBUG (6,
770 ("smbfs_loaddir: cli->share: %s\n",
771 smbfs_info->conn->cli->share));
772 DEBUG (6,
773 ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname));
774 /* do file listing: cli_list returns number of files */
775 if (cli_list
776 (smbfs_info->conn->cli, my_dirname, attribute,
777 smbfs_loaddir_helper, NULL) < 0) {
778 /* cli_list returns -1 if directory empty or cannot read socket */
779 my_errno = cli_error (smbfs_info->conn->cli, NULL, &err, NULL);
780 g_free (my_dirname);
781 return 0;
783 if (*(my_dirname) == 0)
784 smbfs_info->dirname = smbfs_info->conn->service;
785 g_free (my_dirname);
786 /* do_dskattr(); */
788 done:
789 /* current_info->parent = smbfs_info->dirname; */
791 smbfs_info->current = smbfs_info->entries;
792 return 1; /* 1 = ok */
795 #ifdef SMBFS_FREE_DIR
796 static void
797 smbfs_free_dir (dir_entry *de)
799 if (!de) return;
801 smbfs_free_dir (de->next);
802 g_free (de->text);
803 g_free (de);
805 #endif
808 /* The readdir routine loads the complete directory */
809 /* It's too slow to ask the server each time */
810 /* It now also sends the complete lstat information for each file */
811 static void *
812 smbfs_readdir(void *info)
814 static union vfs_dirent smbfs_readdir_data;
815 static char *const dirent_dest = smbfs_readdir_data.dent.d_name;
816 opendir_info *smbfs_info = (opendir_info *) info;
818 DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info->dirname));
820 if (!smbfs_info->entries)
821 if (!smbfs_loaddir(smbfs_info))
822 return NULL;
824 if (smbfs_info->current == 0) { /* reached end of dir entries */
825 DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
826 #ifdef SMBFS_FREE_DIR
827 smbfs_free_dir(smbfs_info->entries);
828 smbfs_info->entries = 0;
829 #endif
830 return NULL;
832 g_strlcpy(dirent_dest, smbfs_info->current->text, MC_MAXPATHLEN);
833 smbfs_info->current = smbfs_info->current->next;
835 compute_namelen(&smbfs_readdir_data.dent);
837 return &smbfs_readdir_data;
840 static int
841 smbfs_closedir (void *info)
843 opendir_info *smbfs_info = (opendir_info *) info;
844 /* dir_entry *p, *q; */
846 DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info->dirname));
847 /* CLOSE HERE */
849 /* for (p = smbfs_info->entries; p;){
850 q = p;
851 p = p->next;
852 g_free (q->text);
853 g_free (q);
855 g_free (info); */
856 return 0;
859 static int
860 smbfs_chmod (struct vfs_class *me, const char *path, int mode)
862 (void) me;
864 DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path, mode));
865 /* my_errno = EOPNOTSUPP;
866 return -1; */ /* cannot chmod on smb filesystem */
867 return 0; /* make mc happy */
870 static int
871 smbfs_chown (struct vfs_class *me, const char *path, int owner, int group)
873 (void) me;
875 DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path, owner, group));
876 my_errno = EOPNOTSUPP; /* ready for your labotomy? */
877 return -1;
880 static int
881 smbfs_utime (struct vfs_class *me, const char *path, struct utimbuf *times)
883 (void) me;
884 (void) times;
886 DEBUG(3, ("smbfs_utime(path:%s)\n", path));
887 my_errno = EOPNOTSUPP;
888 return -1;
891 static int
892 smbfs_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
894 (void) me;
896 DEBUG (3,
897 ("smbfs_readlink(path:%s, buf:%s, size:%d)\n", path, buf,
898 (int) size));
899 my_errno = EOPNOTSUPP;
900 return -1; /* no symlinks on smb filesystem? */
903 static int
904 smbfs_symlink (struct vfs_class *me, const char *n1, const char *n2)
906 (void) me;
908 DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1, n2));
909 my_errno = EOPNOTSUPP;
910 return -1; /* no symlinks on smb filesystem? */
913 /* Extract the hostname and username from the path */
914 /* path is in the form: [user@]hostname/share/remote-dir */
915 #define smbfs_get_host_and_username(path, host, user, port, pass) \
916 vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
918 /*****************************************************
919 return a connection to a SMB server
920 current_bucket needs to be set before calling
921 *******************************************************/
922 static struct cli_state *
923 smbfs_do_connect (const char *server, char *share)
925 struct cli_state *c;
926 struct nmb_name called, calling;
927 struct in_addr ip;
929 DEBUG(3, ("smbfs_do_connect(%s, %s)\n", server, share));
930 if (*share == '\\') {
931 server = share+2;
932 share = strchr(server,'\\');
933 if (!share) return NULL;
934 *share = 0;
935 share++;
938 make_nmb_name(&calling, global_myname, 0x0);
939 make_nmb_name(&called , server, current_bucket->name_type);
941 for (;;) {
943 ip = (current_bucket->have_ip) ? current_bucket->dest_ip : ipzero;
945 /* have to open a new connection */
946 if (!(c = cli_initialise(NULL))) {
947 my_errno = ENOMEM;
948 return NULL;
951 pwd_init(&(c->pwd)); /* should be moved into cli_initialise()? */
952 pwd_set_cleartext(&(c->pwd), current_bucket->password);
954 if ((cli_set_port(c, current_bucket->port) == 0) ||
955 !cli_connect(c, server, &ip)) {
956 DEBUG(1, ("Connection to %s failed\n", server));
957 break;
960 if (!cli_session_request(c, &calling, &called)) {
961 my_errno = cli_error(c, NULL, &err, NULL);
962 DEBUG(1, ("session request to %s failed\n", called.name));
963 cli_shutdown(c);
964 if (strcmp(called.name, "*SMBSERVER")) {
965 make_nmb_name(&called , "*SMBSERVER", 0x20);
966 continue;
968 return NULL;
971 DEBUG(3, (" session request ok\n"));
973 if (!cli_negprot(c)) {
974 DEBUG(1, ("protocol negotiation failed\n"));
975 break;
978 if (!cli_session_setup(c, current_bucket->user,
979 current_bucket->password, strlen(current_bucket->password),
980 current_bucket->password, strlen(current_bucket->password),
981 current_bucket->domain)) {
982 DEBUG(1,("session setup failed: %s\n", cli_errstr(c)));
983 smbfs_auth_remove (server, share);
984 break;
987 if (*c->server_domain || *c->server_os || *c->server_type)
988 DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
989 c->server_domain,c->server_os,c->server_type));
991 DEBUG(3, (" session setup ok\n"));
993 if (!cli_send_tconX(c, share, "?????",
994 current_bucket->password, strlen(current_bucket->password)+1)) {
995 DEBUG(1,("%s: tree connect failed: %s\n", share, cli_errstr(c)));
996 break;
999 DEBUG(3, (" tconx ok\n"));
1001 my_errno = 0;
1002 return c;
1005 my_errno = cli_error(c, NULL, &err, NULL);
1006 cli_shutdown(c);
1007 return NULL;
1011 static int
1012 smbfs_get_master_browser(char **host)
1014 static char so_broadcast[] = "SO_BROADCAST";
1015 int count;
1016 struct in_addr *ip_list, bcast_addr;
1018 /* does port = 137 for win95 master browser? */
1019 int fd= open_socket_in( SOCK_DGRAM, 0, 3,
1020 interpret_addr(lp_socket_address()), True );
1021 if (fd == -1)
1022 return 0;
1023 set_socket_options(fd, so_broadcast);
1024 ip_list = iface_bcast(ipzero);
1025 bcast_addr = *ip_list;
1026 if ((ip_list = name_query(fd, "\01\02__MSBROWSE__\02", 1, True,
1027 True, bcast_addr, &count, NULL))) {
1028 if (!count)
1029 return 0;
1030 /* just return first master browser */
1031 *host = g_strdup(inet_ntoa(ip_list[0]));
1032 return 1;
1034 return 0;
1037 static void
1038 smbfs_free_bucket (smbfs_connection *bucket)
1040 g_free (bucket->host);
1041 g_free (bucket->service);
1042 g_free (bucket->domain);
1043 g_free (bucket->user);
1044 wipe_password (bucket->password);
1045 g_free (bucket->home);
1046 memset (bucket, 0, sizeof (smbfs_connection));
1049 static smbfs_connection *
1050 smbfs_get_free_bucket (void)
1052 int i;
1054 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++)
1055 if (!smbfs_connections [i].cli) return &smbfs_connections [i];
1057 { /* search for most dormant connection */
1058 int oldest = 0; /* index */
1059 time_t oldest_time = smbfs_connections[0].last_use;
1060 for (i = 1; i < SMBFS_MAX_CONNECTIONS; i++) {
1061 if (smbfs_connections[i].last_use < oldest_time) {
1062 oldest_time = smbfs_connections[i].last_use;
1063 oldest = i;
1066 cli_shutdown(smbfs_connections[oldest].cli);
1067 smbfs_free_bucket (&smbfs_connections[oldest]);
1068 return &smbfs_connections[oldest];
1071 /* This can't happend, since we have checked for max connections before */
1072 vfs_die("Internal error: smbfs_get_free_bucket");
1073 return 0; /* shut up, stupid gcc */
1076 /* This routine keeps track of open connections */
1077 /* Returns a connected socket to host */
1078 static smbfs_connection *
1079 smbfs_open_link (char *host, char *path, const char *user, int *port,
1080 char *this_pass)
1082 int i;
1083 smbfs_connection *bucket;
1084 pstring service;
1085 struct in_addr *dest_ip = NULL;
1087 DEBUG (3, ("smbfs_open_link(host:%s, path:%s)\n", host, path));
1089 if (strcmp (host, path) == 0) /* if host & path are same: */
1090 pstrcpy (service, IPC); /* setup for browse */
1091 else { /* get share name from path, path starts with server name */
1092 char *p;
1093 if ((p = strchr (path, '/'))) /* get share aka */
1094 pstrcpy (service, ++p); /* service name from path */
1095 else
1096 pstrcpy (service, "");
1097 /* now check for trailing directory/filenames */
1098 p = strchr (service, '/');
1099 if (p)
1100 *p = 0; /* cut off dir/files: sharename only */
1101 if (!*service)
1102 pstrcpy (service, IPC); /* setup for browse */
1103 DEBUG (6, ("smbfs_open_link: service from path:%s\n", service));
1106 if (got_user)
1107 user = username; /* global from getenv */
1109 /* Is the link actually open? */
1110 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1111 if (!smbfs_connections[i].cli)
1112 continue;
1113 if ((strcmp (host, smbfs_connections[i].host) == 0) &&
1114 (strcmp (user, smbfs_connections[i].user) == 0) &&
1115 (strcmp (service, smbfs_connections[i].service) == 0)) {
1116 int retries = 0;
1117 BOOL inshare = (*host != 0 && *path != 0 && strchr (path, '/'));
1118 /* check if this connection has died */
1119 while (!smbfs_chkpath (smbfs_connections[i].cli, "\\", !inshare)) {
1120 if (!smbfs_reconnect (&smbfs_connections[i], &retries))
1121 return 0;
1123 DEBUG (6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i));
1124 current_bucket = &smbfs_connections[i];
1125 smbfs_connections[i].last_use = time (NULL);
1126 return &smbfs_connections[i];
1128 /* connection not found, find if we have ip for new connection */
1129 if (strcmp (host, smbfs_connections[i].host) == 0)
1130 dest_ip = &smbfs_connections[i].cli->dest_ip;
1133 /* make new connection */
1134 bucket = smbfs_get_free_bucket ();
1135 bucket->name_type = 0x20;
1136 bucket->home = 0;
1137 bucket->port = *port;
1138 bucket->have_ip = False;
1139 if (dest_ip) {
1140 bucket->have_ip = True;
1141 bucket->dest_ip = *dest_ip;
1143 current_bucket = bucket;
1145 bucket->user = g_strdup (user);
1146 bucket->service = g_strdup (service);
1148 if (!(*host)) { /* if blank host name, browse for servers */
1149 if (!smbfs_get_master_browser (&host)) /* set host to ip of master browser */
1150 return 0; /* could not find master browser? */
1151 g_free (host);
1152 bucket->host = g_strdup (""); /* blank host means master browser */
1153 } else
1154 bucket->host = g_strdup (host);
1156 if (!smbfs_bucket_set_authinfo (bucket, 0, /* domain currently not used */
1157 user, this_pass, 1))
1158 return 0;
1160 /* connect to share */
1161 while (!(bucket->cli = smbfs_do_connect (host, service))) {
1163 if (my_errno != EPERM)
1164 return 0;
1165 message (1, MSG_ERROR, _(" Authentication failed "));
1167 /* authentication failed, try again */
1168 smbfs_auth_remove (bucket->host, bucket->service);
1169 if (!smbfs_bucket_set_authinfo (bucket, bucket->domain, bucket->user, 0, 0))
1170 return 0;
1174 smbfs_open_connections++;
1175 DEBUG (3, ("smbfs_open_link:smbfs_open_connections: %d\n",
1176 smbfs_open_connections));
1177 return bucket;
1180 static char *
1181 smbfs_get_path (smbfs_connection ** sc, const char *path)
1183 char *user, *host, *remote_path, *pass;
1184 int port = SMB_PORT;
1186 DEBUG (3, ("smbfs_get_path(%s)\n", path));
1187 if (strncmp (path, URL_HEADER, HEADER_LEN))
1188 return NULL;
1189 path += HEADER_LEN;
1191 if (*path == '/') /* '/' leading server name */
1192 path++; /* probably came from server browsing */
1194 if ((remote_path =
1195 smbfs_get_host_and_username (&path, &host, &user, &port, &pass)))
1196 if ((*sc =
1197 smbfs_open_link (host, remote_path, user, &port, pass)) == NULL) {
1198 g_free (remote_path);
1199 remote_path = NULL;
1201 g_free (host);
1202 g_free (user);
1203 if (pass)
1204 wipe_password (pass);
1206 if (!remote_path)
1207 return NULL;
1209 /* NOTE: tildes are deprecated. See ftpfs.c */
1211 int f = !strcmp (remote_path, "/~");
1212 if (f || !strncmp (remote_path, "/~/", 3)) {
1213 char *s;
1214 s = concat_dir_and_file ((*sc)->home, remote_path + 3 - f);
1215 g_free (remote_path);
1216 return s;
1219 return remote_path;
1222 #if 0
1223 static int
1224 is_error (int result, int errno_num)
1226 if (!(result == -1))
1227 return my_errno = 0;
1228 else
1229 my_errno = errno_num;
1230 return 1;
1232 #endif
1234 static void *
1235 smbfs_opendir (struct vfs_class *me, const char *dirname)
1237 opendir_info *smbfs_info;
1238 smbfs_connection *sc;
1239 char *remote_dir;
1241 (void) me;
1243 DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname));
1245 if (!(remote_dir = smbfs_get_path (&sc, dirname)))
1246 return NULL;
1248 /* FIXME: where freed? */
1249 smbfs_info = g_new (opendir_info, 1);
1250 smbfs_info->server_list = FALSE;
1251 smbfs_info->path = g_strdup(dirname); /* keep original */
1252 smbfs_info->dirname = remote_dir;
1253 smbfs_info->conn = sc;
1254 smbfs_info->entries = 0;
1255 smbfs_info->current = 0;
1257 return smbfs_info;
1260 static int
1261 smbfs_fake_server_stat (const char *server_url, const char *path, struct stat *buf)
1263 dir_entry *dentry;
1264 const char *p;
1266 (void) server_url;
1268 if ((p = strrchr (path, '/')))
1269 path = p + 1; /* advance until last '/' */
1271 if (!current_info->entries) {
1272 if (!smbfs_loaddir (current_info)) /* browse host */
1273 return -1;
1276 if (current_info->server_list == True) {
1277 dentry = current_info->entries;
1278 DEBUG (4, ("fake stat for SERVER \"%s\"\n", path));
1279 while (dentry) {
1280 if (strcmp (dentry->text, path) == 0) {
1281 DEBUG (4, ("smbfs_fake_server_stat: %s:%4o\n",
1282 dentry->text, (int)dentry->my_stat.st_mode));
1283 memcpy (buf, &dentry->my_stat, sizeof (struct stat));
1284 return 0;
1286 dentry = dentry->next;
1289 my_errno = ENOENT;
1290 return -1;
1293 static int
1294 smbfs_fake_share_stat (const char *server_url, const char *path, struct stat *buf)
1296 dir_entry *dentry;
1297 if (strlen (path) < strlen (server_url))
1298 return -1;
1300 if (!current_share_info) { /* Server was not stat()ed */
1301 /* Make sure there is such share at server */
1302 smbfs_connection *sc;
1303 char *p;
1304 p = smbfs_get_path (&sc, path);
1305 g_free (p);
1306 if (p) {
1307 memset (buf, 0, sizeof (*buf));
1308 /* show this as dir */
1309 buf->st_mode =
1310 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
1311 S_IXOTH) & myumask;
1312 return 0;
1314 return -1;
1317 path += strlen (server_url); /* we only want share name */
1318 path++;
1320 if (*path == '/') /* '/' leading server name */
1321 path++; /* probably came from server browsing */
1323 if (!current_share_info->entries) {
1324 if (!smbfs_loaddir (current_share_info)) /* browse host */
1325 return -1;
1327 dentry = current_share_info->entries;
1328 DEBUG (3, ("smbfs_fake_share_stat: %s on %s\n", path, server_url));
1329 while (dentry) {
1330 if (strcmp (dentry->text, path) == 0) {
1331 DEBUG (6, ("smbfs_fake_share_stat: %s:%4o\n",
1332 dentry->text, (int) dentry->my_stat.st_mode));
1333 memcpy (buf, &dentry->my_stat, sizeof (struct stat));
1334 return 0;
1336 dentry = dentry->next;
1338 my_errno = ENOENT;
1339 return -1;
1342 /* stat a single file, smbfs_get_remote_stat callback */
1343 static dir_entry *single_entry;
1345 /* stat a single file */
1346 static int
1347 smbfs_get_remote_stat (smbfs_connection * sc, const char *path, struct stat *buf)
1349 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
1350 char *mypath;
1352 DEBUG (3, ("smbfs_get_remote_stat(): mypath:%s\n", path));
1354 mypath = smbfs_convert_path (path, FALSE);
1356 #if 0 /* single_entry is never free()d now. And only my_stat is used */
1357 single_entry = g_new (dir_entry, 1);
1359 single_entry->text = dos_to_unix (g_strdup (finfo->name), 1);
1361 single_entry->next = 0;
1362 #endif
1363 if (!single_entry)
1364 single_entry = g_new0 (dir_entry, 1);
1366 if (cli_list
1367 (sc->cli, mypath, attribute, smbfs_loaddir_helper, single_entry) < 1) {
1368 my_errno = ENOENT;
1369 g_free (mypath);
1370 return -1; /* cli_list returns number of files */
1373 memcpy (buf, &single_entry->my_stat, sizeof (struct stat));
1375 /* don't free here, use for smbfs_fstat() */
1376 /* g_free(single_entry->text);
1377 g_free(single_entry); */
1378 g_free (mypath);
1379 return 0;
1382 static int
1383 smbfs_search_dir_entry (dir_entry *dentry, const char *text, struct stat *buf)
1385 while (dentry) {
1386 if (strcmp(text, dentry->text) == 0) {
1387 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1388 memcpy(&single_entry->my_stat, &dentry->my_stat,
1389 sizeof(struct stat));
1390 return 0;
1392 dentry = dentry->next;
1394 return -1;
1397 static int
1398 smbfs_get_stat_info (smbfs_connection * sc, const char *path, struct stat *buf)
1400 char *p;
1401 #if 0
1402 dir_entry *dentry = current_info->entries;
1403 #endif
1404 const char *mypath = path;
1406 mypath++; /* cut off leading '/' */
1407 if ((p = strrchr (mypath, '/')))
1408 mypath = p + 1; /* advance until last file/dir name */
1409 DEBUG (3, ("smbfs_get_stat_info: mypath:%s, current_info->dirname:%s\n",
1410 mypath, current_info->dirname));
1411 #if 0
1412 if (!dentry) {
1413 DEBUG (1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n",
1414 current_info->dirname, path));
1415 return -1;
1417 #endif
1418 if (!single_entry) /* when found, this will be written too */
1419 single_entry = g_new (dir_entry, 1);
1420 if (smbfs_search_dir_entry (current_info->entries, mypath, buf) == 0) {
1421 return 0;
1423 /* now try to identify mypath as PARENT dir */
1425 char *mdp;
1426 char *mydir;
1427 mdp = mydir = g_strdup (current_info->dirname);
1428 if ((p = strrchr (mydir, '/')))
1429 *p = 0; /* advance util last '/' */
1430 if ((p = strrchr (mydir, '/')))
1431 mydir = p + 1; /* advance util last '/' */
1432 if (strcmp (mydir, mypath) == 0) { /* fake a stat for ".." */
1433 memset (buf, 0, sizeof (struct stat));
1434 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1435 memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
1436 g_free (mdp);
1437 DEBUG (1, (" PARENT:found in %s\n", current_info->dirname));
1438 return 0;
1440 g_free (mdp);
1442 /* now try to identify as CURRENT dir? */
1444 char *dnp = current_info->dirname;
1445 DEBUG (6, ("smbfs_get_stat_info: is %s current dir? this dir is: %s\n",
1446 mypath, current_info->dirname));
1447 if (*dnp == '/')
1448 dnp++;
1449 else {
1450 return -1;
1452 if (strcmp (mypath, dnp) == 0) {
1453 memset (buf, 0, sizeof (struct stat));
1454 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1455 memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
1456 DEBUG (1, (" CURRENT:found in %s\n", current_info->dirname));
1457 return 0;
1460 DEBUG (3, ("'%s' not found in current_info '%s'\n", path,
1461 current_info->dirname));
1462 /* try to find this in the PREVIOUS listing */
1463 if (previous_info) {
1464 if (smbfs_search_dir_entry (previous_info->entries, mypath, buf) == 0)
1465 return 0;
1466 DEBUG (3, ("'%s' not found in previous_info '%s'\n", path,
1467 previous_info->dirname));
1469 /* try to find this in the SHARE listing */
1470 if (current_share_info) {
1471 if (smbfs_search_dir_entry (current_share_info->entries, mypath, buf) == 0)
1472 return 0;
1473 DEBUG (3, ("'%s' not found in share_info '%s'\n", path,
1474 current_share_info->dirname));
1476 /* try to find this in the SERVER listing */
1477 if (current_server_info) {
1478 if (smbfs_search_dir_entry (current_server_info->entries, mypath, buf) == 0)
1479 return 0;
1480 DEBUG (3, ("'%s' not found in server_info '%s'\n", path,
1481 current_server_info->dirname));
1483 /* nothing found. get stat file info from server */
1484 return smbfs_get_remote_stat (sc, path, buf);
1487 static int
1488 smbfs_chdir (struct vfs_class *me, const char *path)
1490 char *remote_dir;
1491 smbfs_connection *sc;
1493 (void) me;
1495 DEBUG (3, ("smbfs_chdir(path:%s)\n", path));
1496 if (!(remote_dir = smbfs_get_path (&sc, path)))
1497 return -1;
1498 g_free (remote_dir);
1500 return 0;
1503 static int
1504 smbfs_loaddir_by_name (struct vfs_class *me, const char *path)
1506 void *info;
1507 char *mypath, *p;
1509 mypath = g_strdup(path);
1510 p = strrchr(mypath, '/');
1512 if (p > mypath)
1513 *p = 0;
1514 DEBUG(6, ("smbfs_loaddir_by_name(%s)\n", mypath));
1515 smbfs_chdir(me, mypath);
1516 info = smbfs_opendir (me, mypath);
1517 g_free(mypath);
1518 if (!info)
1519 return -1;
1520 smbfs_readdir(info);
1521 smbfs_loaddir(info);
1522 return 0;
1525 static int
1526 smbfs_stat (struct vfs_class * me, const char *path, struct stat *buf)
1528 smbfs_connection *sc;
1529 pstring server_url;
1530 char *service, *pp, *at;
1531 const char *p;
1533 DEBUG (3, ("smbfs_stat(path:%s)\n", path));
1535 if (!current_info) {
1536 DEBUG (1, ("current_info = NULL: "));
1537 if (smbfs_loaddir_by_name (me, path) < 0)
1538 return -1;
1541 /* check if stating server */
1542 p = path;
1543 if (strncmp (p, URL_HEADER, HEADER_LEN)) {
1544 DEBUG (1, ("'%s' doesnt start with '%s' (length %d)\n",
1545 p, URL_HEADER, HEADER_LEN));
1546 return -1;
1549 p += HEADER_LEN;
1550 if (*p == '/')
1551 p++;
1553 pp = strchr (p, '/'); /* advance past next '/' */
1554 at = strchr (p, '@');
1555 pstrcpy (server_url, URL_HEADER);
1556 if (at && at < pp) { /* user@server */
1557 char *z = &(server_url[sizeof (server_url) - 1]);
1558 const char *s = p;
1560 at = &(server_url [HEADER_LEN]) + (at - p + 1);
1561 if (z > at)
1562 z = at;
1563 at = &(server_url [HEADER_LEN]);
1564 while (at < z)
1565 *at++ = *s++;
1566 *z = 0;
1568 pstrcat (server_url, current_bucket->host);
1570 if (!pp) {
1571 if (!current_info->server_list) {
1572 if (smbfs_loaddir_by_name (me, path) < 0)
1573 return -1;
1575 return smbfs_fake_server_stat (server_url, path, buf);
1578 if (!strchr (++pp, '/')) {
1579 return smbfs_fake_share_stat (server_url, path, buf);
1582 /* stating inside share at this point */
1583 if (!(service = smbfs_get_path (&sc, path))) /* connects if necessary */
1584 return -1;
1586 int hostlen = strlen (current_bucket->host);
1587 char *ppp = service + strlen (service) - hostlen;
1588 char *sp = server_url + strlen (server_url) - hostlen;
1590 if (strcmp (sp, ppp) == 0) {
1591 /* make server name appear as directory */
1592 DEBUG (1, ("smbfs_stat: showing server as directory\n"));
1593 memset (buf, 0, sizeof (struct stat));
1594 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1595 g_free (service);
1596 return 0;
1599 /* check if current_info is in share requested */
1600 p = service;
1601 pp = strchr (p, '/');
1602 if (pp) {
1603 p = ++pp; /* advance past server name */
1604 pp = strchr (p, '/');
1606 if (pp)
1607 *pp = 0; /* cut off everthing after service name */
1608 else
1609 p = IPC; /* browsing for services */
1610 pp = current_info->dirname;
1611 if (*pp == '/')
1612 pp++;
1613 if (strncmp (p, pp, strlen (p)) != 0) {
1614 DEBUG (6, ("desired '%s' is not loaded, we have '%s'\n", p, pp));
1615 if (smbfs_loaddir_by_name (me, path) < 0) {
1616 g_free (service);
1617 return -1;
1619 DEBUG (6, ("loaded dir: '%s'\n", current_info->dirname));
1621 g_free (service);
1622 /* stat dirs & files under shares now */
1623 return smbfs_get_stat_info (sc, path, buf);
1626 #define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */
1628 static int
1629 smbfs_lseek (void *data, off_t offset, int whence)
1631 smbfs_handle *info = (smbfs_handle *) data;
1632 size_t size;
1634 DEBUG (3,
1635 ("smbfs_lseek(info->nread => %d, offset => %d, whence => %d) \n",
1636 (int) info->nread, (int) offset, whence));
1638 switch (whence) {
1639 case SEEK_SET:
1640 info->nread = offset;
1641 break;
1642 case SEEK_CUR:
1643 info->nread += offset;
1644 break;
1645 case SEEK_END:
1646 if (!cli_qfileinfo (info->cli, info->fnum,
1647 NULL, &size, NULL, NULL, NULL,
1648 NULL, NULL) &&
1649 !cli_getattrE (info->cli, info->fnum,
1650 NULL, &size, NULL, NULL, NULL)) {
1651 errno = EINVAL;
1652 return -1;
1654 info->nread = size + offset;
1655 break;
1658 return info->nread;
1661 static int
1662 smbfs_mknod (struct vfs_class *me, const char *path, int mode, int dev)
1664 (void) me;
1666 DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%d)\n", path, mode, dev));
1667 my_errno = EOPNOTSUPP;
1668 return -1;
1671 static int
1672 smbfs_mkdir (struct vfs_class * me, const char *path, mode_t mode)
1674 smbfs_connection *sc;
1675 char *remote_file;
1676 char *cpath;
1678 (void) me;
1680 DEBUG (3, ("smbfs_mkdir(path:%s, mode:%d)\n", path, (int) mode));
1681 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1682 return -1;
1683 g_free (remote_file);
1684 cpath = smbfs_convert_path (path, FALSE);
1686 if (!cli_mkdir (sc->cli, cpath)) {
1687 my_errno = cli_error (sc->cli, NULL, &err, NULL);
1688 message (1, MSG_ERROR, _(" Error %s creating directory %s "),
1689 cli_errstr (sc->cli), CNV_LANG (cpath));
1690 g_free (cpath);
1691 return -1;
1693 g_free (cpath);
1694 return 0;
1697 static int
1698 smbfs_rmdir (struct vfs_class *me, const char *path)
1700 smbfs_connection *sc;
1701 char *remote_file;
1702 char *cpath;
1704 (void) me;
1706 DEBUG(3, ("smbfs_rmdir(path:%s)\n", path));
1707 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1708 return -1;
1709 g_free (remote_file);
1710 cpath = smbfs_convert_path (path, FALSE);
1712 if (!cli_rmdir(sc->cli, cpath)) {
1713 my_errno = cli_error(sc->cli, NULL, &err, NULL);
1714 message (1, MSG_ERROR, _(" Error %s removing directory %s "),
1715 cli_errstr(sc->cli), CNV_LANG(cpath));
1716 g_free (cpath);
1717 return -1;
1720 g_free (cpath);
1721 return 0;
1724 static int
1725 smbfs_link (struct vfs_class *me, const char *p1, const char *p2)
1727 (void) me;
1729 DEBUG (3, ("smbfs_link(p1:%s, p2:%s)\n", p1, p2));
1730 my_errno = EOPNOTSUPP;
1731 return -1;
1734 static void
1735 smbfs_free (vfsid id)
1737 DEBUG (3, ("smbfs_free(%p)\n", id));
1738 smbfs_auth_free_all ();
1741 /* Gives up on a socket and reopens the connection, the child own the socket
1742 * now
1744 static void
1745 smbfs_forget (const char *path)
1747 char *host, *user, *p;
1748 int port, i;
1750 if (strncmp (path, URL_HEADER, HEADER_LEN))
1751 return;
1753 DEBUG (3, ("smbfs_forget(path:%s)\n", path));
1755 path += 6;
1756 if (path[0] == '/' && path[1] == '/')
1757 path += 2;
1759 if ((p = smbfs_get_host_and_username (&path, &host, &user, &port, NULL))) {
1760 g_free (p);
1761 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1762 if (smbfs_connections[i].cli
1763 && (strcmp (host, smbfs_connections[i].host) == 0)
1764 && (strcmp (user, smbfs_connections[i].user) == 0)
1765 && (port == smbfs_connections[i].port)) {
1767 /* close socket: the child owns it now */
1768 cli_shutdown (smbfs_connections[i].cli);
1770 /* reopen the connection */
1771 smbfs_connections[i].cli =
1772 smbfs_do_connect (host, smbfs_connections[i].service);
1776 g_free (host);
1777 g_free (user);
1780 static int
1781 smbfs_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg)
1783 (void) me;
1784 (void) arg;
1786 DEBUG (3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path, ctlop));
1787 switch (ctlop) {
1788 case VFS_SETCTL_FORGET:
1789 smbfs_forget (path);
1790 return 0;
1792 return 0;
1796 static smbfs_handle *
1797 smbfs_open_readwrite (smbfs_handle *remote_handle, char *rname, int flags, int mode)
1799 size_t size;
1801 (void) mode;
1803 if (flags & O_TRUNC) /* if it exists truncate to zero */
1804 DEBUG (3, ("smbfs_open: O_TRUNC\n"));
1806 remote_handle->fnum =
1807 #if 1 /* Don't play with flags, it is cli_open() headache */
1808 cli_open (remote_handle->cli, rname, flags, DENY_NONE);
1809 #else /* What's a reasons to has this code ? */
1810 cli_open (remote_handle->cli, rname, ((flags & O_CREAT)
1811 || (flags ==
1812 (O_WRONLY | O_APPEND))) ?
1813 flags : O_RDONLY, DENY_NONE);
1814 #endif
1815 if (remote_handle->fnum == -1) {
1816 message (1, MSG_ERROR, _(" %s opening remote file %s "),
1817 cli_errstr (remote_handle->cli), CNV_LANG (rname));
1818 DEBUG (1, ("smbfs_open(rname:%s) error:%s\n",
1819 rname, cli_errstr (remote_handle->cli)));
1820 my_errno = cli_error (remote_handle->cli, NULL, &err, NULL);
1821 return NULL;
1824 if (flags & O_CREAT)
1825 return remote_handle;
1827 if (!cli_qfileinfo (remote_handle->cli, remote_handle->fnum,
1828 &remote_handle->attr, &size, NULL, NULL, NULL, NULL,
1829 NULL)
1830 && !cli_getattrE (remote_handle->cli, remote_handle->fnum,
1831 &remote_handle->attr, &size, NULL, NULL, NULL)) {
1832 message (1, MSG_ERROR, " getattrib: %s ",
1833 cli_errstr (remote_handle->cli));
1834 DEBUG (1,
1835 ("smbfs_open(rname:%s) getattrib:%s\n", rname,
1836 cli_errstr (remote_handle->cli)));
1837 my_errno = cli_error (remote_handle->cli, NULL, &err, NULL);
1838 cli_close (remote_handle->cli, remote_handle->fnum);
1839 return NULL;
1842 if ((flags == (O_WRONLY | O_APPEND)) /* file.c:copy_file_file() -> do_append */
1843 && smbfs_lseek (remote_handle, 0, SEEK_END) == -1) {
1844 cli_close (remote_handle->cli, remote_handle->fnum);
1845 return NULL;
1848 return remote_handle;
1851 static void *
1852 smbfs_open (struct vfs_class *me, const char *file, int flags, int mode)
1854 char *remote_file;
1855 void *ret;
1856 smbfs_connection *sc;
1857 smbfs_handle *remote_handle;
1859 (void) me;
1861 DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%o)\n", file, flags, mode));
1863 if (!(remote_file = smbfs_get_path (&sc, file)))
1864 return 0;
1866 remote_file = free_after(smbfs_convert_path (remote_file, FALSE), remote_file);
1868 remote_handle = g_new (smbfs_handle, 2);
1869 remote_handle->cli = sc->cli;
1870 remote_handle->nread = 0;
1872 ret = smbfs_open_readwrite (remote_handle, remote_file, flags, mode);
1874 g_free (remote_file);
1875 if (!ret)
1876 g_free (remote_handle);
1878 return ret;
1881 static int
1882 smbfs_unlink (struct vfs_class *me, const char *path)
1884 smbfs_connection *sc;
1885 char *remote_file;
1887 (void) me;
1889 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1890 return -1;
1892 remote_file = free_after(smbfs_convert_path (remote_file, FALSE), remote_file);
1894 if (!cli_unlink(sc->cli, remote_file)) {
1895 message (1, MSG_ERROR, _(" %s removing remote file %s "),
1896 cli_errstr(sc->cli), CNV_LANG(remote_file));
1897 g_free (remote_file);
1898 return -1;
1900 g_free (remote_file);
1901 return 0;
1904 static int
1905 smbfs_rename (struct vfs_class *me, const char *a, const char *b)
1907 smbfs_connection *sc;
1908 char *ra, *rb;
1909 int retval;
1911 (void) me;
1913 if ((ra = smbfs_get_path (&sc, a)) == 0)
1914 return -1;
1916 if ((rb = smbfs_get_path (&sc, b)) == 0) {
1917 g_free (ra);
1918 return -1;
1921 ra = free_after (smbfs_convert_path (ra, FALSE), ra);
1922 rb = free_after (smbfs_convert_path (rb, FALSE), rb);
1924 retval = cli_rename(sc->cli, ra, rb);
1926 g_free (ra);
1927 g_free (rb);
1929 if (!retval) {
1930 message (1, MSG_ERROR, _(" %s renaming files\n"),
1931 cli_errstr(sc->cli));
1932 return -1;
1934 return 0;
1937 static int
1938 smbfs_fstat (void *data, struct stat *buf)
1940 smbfs_handle *remote_handle = (smbfs_handle *)data;
1942 DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle->fnum));
1944 /* use left over from previous smbfs_get_remote_stat, if available */
1945 if (single_entry)
1946 memcpy(buf, &single_entry->my_stat, sizeof(struct stat));
1947 else { /* single_entry not set up: bug */
1948 my_errno = EFAULT;
1949 return -EFAULT;
1951 return 0;
1954 void
1955 init_smbfs (void)
1957 vfs_smbfs_ops.name = "smbfs";
1958 vfs_smbfs_ops.prefix = "smb:";
1959 vfs_smbfs_ops.flags = VFSF_NOLINKS;
1960 vfs_smbfs_ops.init = smbfs_init;
1961 vfs_smbfs_ops.fill_names = smbfs_fill_names;
1962 vfs_smbfs_ops.open = smbfs_open;
1963 vfs_smbfs_ops.close = smbfs_close;
1964 vfs_smbfs_ops.read = smbfs_read;
1965 vfs_smbfs_ops.write = smbfs_write;
1966 vfs_smbfs_ops.opendir = smbfs_opendir;
1967 vfs_smbfs_ops.readdir = smbfs_readdir;
1968 vfs_smbfs_ops.closedir = smbfs_closedir;
1969 vfs_smbfs_ops.stat = smbfs_stat;
1970 vfs_smbfs_ops.lstat = smbfs_lstat;
1971 vfs_smbfs_ops.fstat = smbfs_fstat;
1972 vfs_smbfs_ops.chmod = smbfs_chmod;
1973 vfs_smbfs_ops.chown = smbfs_chown;
1974 vfs_smbfs_ops.utime = smbfs_utime;
1975 vfs_smbfs_ops.readlink = smbfs_readlink;
1976 vfs_smbfs_ops.symlink = smbfs_symlink;
1977 vfs_smbfs_ops.link = smbfs_link;
1978 vfs_smbfs_ops.unlink = smbfs_unlink;
1979 vfs_smbfs_ops.rename = smbfs_rename;
1980 vfs_smbfs_ops.chdir = smbfs_chdir;
1981 vfs_smbfs_ops.ferrno = smbfs_errno;
1982 vfs_smbfs_ops.lseek = smbfs_lseek;
1983 vfs_smbfs_ops.mknod = smbfs_mknod;
1984 vfs_smbfs_ops.free = smbfs_free;
1985 vfs_smbfs_ops.mkdir = smbfs_mkdir;
1986 vfs_smbfs_ops.rmdir = smbfs_rmdir;
1987 vfs_smbfs_ops.setctl = smbfs_setctl;
1988 vfs_register_class (&vfs_smbfs_ops);