VFS: cleaned up smbfs.
[midnight-commander.git] / lib / vfs / mc-vfs / smbfs.c
blobe85a70c9f8b66b079b2dae5707318c3f514bcb7f
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 <string.h>
43 #include "lib/global.h"
45 #include "src/wtools.h" /* message() */
46 #include "src/main.h" /* print_vfs_message */
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 "vfs-impl.h"
64 #include "netutil.h"
65 #include "utilvfs.h"
66 #include "smbfs.h"
68 #define SMBFS_MAX_CONNECTIONS 16
69 static const char * const IPC = "IPC$";
70 static const char * const URL_HEADER = "/#smb:";
71 #define HEADER_LEN 6
73 static int my_errno;
74 static uint32 err;
76 /* stuff that is same with each connection */
77 extern int DEBUGLEVEL;
78 extern pstring myhostname;
79 extern struct in_addr ipzero;
80 static mode_t myumask = 0755;
81 extern pstring global_myname;
82 static int smbfs_open_connections = 0;
83 static gboolean got_user = FALSE;
84 static gboolean got_pass = FALSE;
85 static pstring password;
86 static pstring username;
87 static struct vfs_class vfs_smbfs_ops;
89 static struct _smbfs_connection {
90 struct cli_state *cli;
91 struct in_addr dest_ip;
92 BOOL have_ip;
93 char *host; /* server name */
94 char *service; /* share name */
95 char *domain;
96 char *user;
97 char *home;
98 char *password;
99 int port;
100 int name_type;
101 time_t last_use;
102 } smbfs_connections [SMBFS_MAX_CONNECTIONS];
103 /* unique to each connection */
105 /* modifies *share */
106 static struct cli_state * smbfs_do_connect (const char *server, char *share);
108 typedef struct _smbfs_connection smbfs_connection;
109 static smbfs_connection *current_bucket;
111 typedef struct {
112 struct cli_state *cli;
113 int fnum;
114 off_t nread;
115 uint16 attr;
116 } smbfs_handle;
118 static GSList *auth_list;
120 /* this function allows you to write:
121 * char *s = g_strdup("hello, world");
122 * s = free_after(g_strconcat(s, s, (char *)0), s);
124 static inline char *
125 free_after (char *result, char *string_to_free)
127 g_free(string_to_free);
128 return result;
132 static void
133 smbfs_auth_free (struct smb_authinfo const *a)
135 g_free (a->host);
136 g_free (a->share);
137 g_free (a->domain);
138 g_free (a->user);
139 wipe_password (a->password);
142 static void
143 smbfs_auth_free_all (void)
145 if (auth_list) {
146 g_slist_foreach (auth_list, (GFunc)smbfs_auth_free, 0);
147 g_slist_free (auth_list);
148 auth_list = 0;
152 static gint
153 smbfs_auth_cmp_host_and_share (gconstpointer _a, gconstpointer _b)
155 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
156 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
158 if (!a->host || !a->share || !b->host || !b->share)
159 return 1;
160 if (strcmp (a->host, b->host) != 0)
161 return 1;
162 if (strcmp (a->share, b->share) != 0)
163 return 1;
164 return 0;
167 static gint
168 smbfs_auth_cmp_host (gconstpointer _a, gconstpointer _b)
170 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
171 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
173 if (!a->host || !b->host)
174 return 1;
175 if (strcmp (a->host, b->host) != 0)
176 return 1;
177 if (strcmp (a->share, IPC) != 0)
178 return 1;
179 return 0;
182 static void
183 smbfs_auth_add (const char *host, const char *share, const char *domain,
184 const char *user, const char *pass)
186 struct smb_authinfo *auth;
188 auth = vfs_smb_authinfo_new (host, share, domain, user, pass);
190 if (auth != NULL)
191 auth_list = g_slist_prepend (auth_list, auth);
194 static void
195 smbfs_auth_remove (const char *host, const char *share)
197 struct smb_authinfo data;
198 struct smb_authinfo *auth;
199 GSList *list;
201 data.host = g_strdup (host);
202 data.share = g_strdup (share);
203 list = g_slist_find_custom (auth_list,
204 &data,
205 smbfs_auth_cmp_host_and_share);
206 g_free (data.host);
207 g_free (data.share);
208 if (!list)
209 return;
210 auth = list->data;
211 auth_list = g_slist_remove (auth_list, auth);
212 smbfs_auth_free (auth);
215 /* Set authentication information in bucket. Return 1 if successful, else 0 */
216 /* Information in auth_list overrides user if pass is NULL. */
217 /* bucket->host and bucket->service must be valid. */
218 static int
219 smbfs_bucket_set_authinfo (smbfs_connection *bucket,
220 const char *domain, const char *user, const char *pass,
221 int fallback_to_host)
223 struct smb_authinfo data;
224 struct smb_authinfo *auth;
225 GSList *list;
227 if (domain && user && pass) {
228 g_free (bucket->domain);
229 g_free (bucket->user);
230 g_free (bucket->password);
231 bucket->domain = g_strdup (domain);
232 bucket->user = g_strdup (user);
233 bucket->password = g_strdup (pass);
234 smbfs_auth_remove (bucket->host, bucket->service);
235 smbfs_auth_add (bucket->host, bucket->service,
236 domain, user, pass);
237 return 1;
240 data.host = bucket->host;
241 data.share = bucket->service;
242 list = g_slist_find_custom (auth_list, &data, smbfs_auth_cmp_host_and_share);
243 if (!list && fallback_to_host)
244 list = g_slist_find_custom (auth_list, &data, smbfs_auth_cmp_host);
245 if (list) {
246 auth = list->data;
247 bucket->domain = g_strdup (auth->domain);
248 bucket->user = g_strdup (auth->user);
249 bucket->password = g_strdup (auth->password);
250 return 1;
253 if (got_pass) {
254 bucket->domain = g_strdup (lp_workgroup ());
255 bucket->user = g_strdup (got_user ? username : user);
256 bucket->password = g_strdup (password);
257 return 1;
260 auth = vfs_smb_get_authinfo (bucket->host,
261 bucket->service,
262 (domain ? domain : lp_workgroup ()),
263 user);
264 if (auth) {
265 g_free (bucket->domain);
266 g_free (bucket->user);
267 g_free (bucket->password);
268 bucket->domain = g_strdup (auth->domain);
269 bucket->user = g_strdup (auth->user);
270 bucket->password = g_strdup (auth->password);
271 smbfs_auth_remove (bucket->host, bucket->service);
272 auth_list = g_slist_prepend (auth_list, auth);
273 return 1;
275 return 0;
278 void
279 smbfs_set_debug (int arg)
281 DEBUGLEVEL = arg;
284 extern pstring debugf;
285 extern FILE *dbf;
287 void
288 smbfs_set_debugf (const char *filename)
290 if (DEBUGLEVEL > 0) {
291 FILE *outfile = fopen (filename, "w");
292 if (outfile) {
293 setup_logging ("", True); /* No needs for timestamp for each message */
294 dbf = outfile;
295 setbuf (dbf, NULL);
296 pstrcpy (debugf, filename);
301 /********************** The callbacks ******************************/
302 static int
303 smbfs_init (struct vfs_class * me)
305 const char *servicesf = CONFIGDIR PATH_SEP_STR "smb.conf";
307 /* DEBUGLEVEL = 4; */
309 TimeInit ();
310 charset_initialise ();
312 DEBUG (3, ("smbfs_init(%s)\n", me->name));
314 if (!get_myname (myhostname, NULL))
315 DEBUG (0, ("Failed to get my hostname.\n"));
317 if (!lp_load (servicesf, True, False, False))
318 DEBUG (0, ("Cannot load %s - run testparm to debug it\n", servicesf));
320 codepage_initialise (lp_client_code_page ());
322 load_interfaces ();
324 myumask = umask (0);
325 umask (myumask);
326 myumask = ~myumask;
328 if (getenv ("USER")) {
329 char *p;
331 pstrcpy (username, getenv ("USER"));
332 got_user = TRUE;
333 DEBUG (3, ("smbfs_init(): $USER:%s\n", username));
334 if ((p = strchr (username, '%'))) {
335 *p = 0;
336 pstrcpy (password, p + 1);
337 got_pass = TRUE;
338 memset (strchr (getenv ("USER"), '%') + 1, 'X', strlen (password));
339 DEBUG (3, ("smbfs_init(): $USER%%pass: %s%%%s\n",
340 username, password));
342 strupper (username);
344 if (getenv ("PASSWD")) {
345 pstrcpy (password, getenv ("PASSWD"));
346 got_pass = TRUE;
348 return 1;
351 static void
352 smbfs_fill_names (struct vfs_class *me, fill_names_f func)
354 size_t i;
355 char *path;
357 (void) me;
359 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
360 if (smbfs_connections [i].cli) {
361 path = g_strconcat (URL_HEADER,
362 smbfs_connections[i].user, "@",
363 smbfs_connections[i].host,
364 "/", smbfs_connections[i].service, (char *) NULL);
365 (*func)(path);
366 g_free (path);
371 #define CNV_LANG(s) dos_to_unix(s,False)
372 #define GNAL_VNC(s) unix_to_dos(s,False)
373 /* does same as do_get() in client.c */
374 /* called from vfs.c:1080, count = buffer size */
375 static ssize_t
376 smbfs_read (void *data, char *buffer, size_t count)
378 smbfs_handle *info = (smbfs_handle *) data;
379 ssize_t n;
381 DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%zu)\n",
382 info->fnum, (int)info->nread, count));
383 n = cli_read(info->cli, info->fnum, buffer, info->nread, count);
384 if (n > 0)
385 info->nread += n;
386 return n;
389 static ssize_t
390 smbfs_write (void *data, const char *buf, size_t nbyte)
392 smbfs_handle *info = (smbfs_handle *) data;
393 ssize_t n;
395 DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%zu)\n",
396 info->fnum, (int)info->nread, nbyte));
397 n = cli_write(info->cli, info->fnum, 0, buf, info->nread, nbyte);
398 if (n > 0)
399 info->nread += n;
400 return n;
403 static int
404 smbfs_close (void *data)
406 smbfs_handle *info = (smbfs_handle *) data;
407 DEBUG (3, ("smbfs_close(fnum:%d)\n", info->fnum));
409 /* FIXME: Why too different cli have the same outbuf
410 * if file is copied to share
412 if (info->cli->outbuf == NULL) {
413 my_errno = EINVAL;
414 return -1;
416 #if 0
417 /* if imlementing archive_level: add rname to smbfs_handle */
418 if (archive_level >= 2 && (inf->attr & aARCH)) {
419 cli_setatr (info->cli, rname, info->attr & ~(uint16) aARCH, 0);
421 #endif
422 return (cli_close (info->cli, info->fnum) == True) ? 0 : -1;
425 static int
426 smbfs_errno (struct vfs_class *me)
428 (void) me;
430 DEBUG(3, ("smbfs_errno: %s\n", unix_error_string (my_errno)));
431 return my_errno;
434 typedef struct dir_entry {
435 char *text;
436 struct dir_entry *next;
437 struct stat my_stat;
438 int merrno;
439 } dir_entry;
441 typedef struct {
442 gboolean server_list;
443 char *dirname;
444 char *path; /* the dir originally passed to smbfs_opendir */
445 smbfs_connection *conn;
446 dir_entry *entries;
447 dir_entry *current;
448 } opendir_info;
450 static opendir_info
451 *previous_info,
452 *current_info,
453 *current_share_info,
454 *current_server_info;
456 static gboolean first_direntry;
458 static dir_entry *
459 smbfs_new_dir_entry (const char *name)
461 static int inode_counter;
462 dir_entry *new_entry;
463 new_entry = g_new0 (dir_entry, 1);
464 new_entry->text = dos_to_unix (g_strdup (name), 1);
466 if (first_direntry) {
467 current_info->entries = new_entry;
468 first_direntry = FALSE;
469 } else {
470 current_info->current->next = new_entry;
472 current_info->current = new_entry;
473 new_entry->my_stat.st_ino = inode_counter++;
475 return new_entry;
478 /* browse for shares on server */
479 static void
480 smbfs_browsing_helper (const char *name, uint32 type, const char *comment, void *state)
482 const char *typestr = "";
483 dir_entry *new_entry = smbfs_new_dir_entry (name);
485 (void) state;
487 switch (type) {
488 case STYPE_DISKTREE:
489 typestr = "Disk";
490 /* show this as dir */
491 new_entry->my_stat.st_mode =
492 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
493 S_IXOTH) & myumask;
494 break;
495 case STYPE_PRINTQ:
496 typestr = "Printer";
497 break;
498 case STYPE_DEVICE:
499 typestr = "Device";
500 break;
501 case STYPE_IPC:
502 typestr = "IPC";
503 break;
505 DEBUG (3, ("\t%-15.15s%-10.10s%s\n", name, typestr, comment));
508 static void
509 smbfs_loaddir_helper (file_info * finfo, const char *mask, void *entry)
511 dir_entry *new_entry = (dir_entry *) entry;
512 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
514 (void) mask;
516 #if 0 /* I want to see dot files */
517 if (finfo->mode & aHIDDEN)
518 return; /* don't bother with hidden files, "~$" screws up mc */
519 #endif
520 if (!entry)
521 new_entry = smbfs_new_dir_entry (finfo->name);
523 new_entry->my_stat.st_size = finfo->size;
524 new_entry->my_stat.st_mtime = finfo->mtime;
525 new_entry->my_stat.st_atime = finfo->atime;
526 new_entry->my_stat.st_ctime = finfo->ctime;
527 new_entry->my_stat.st_uid = finfo->uid;
528 new_entry->my_stat.st_gid = finfo->gid;
530 new_entry->my_stat.st_mode = /* rw-rw-rw */
531 S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH;
533 /* if (finfo->mode & aVOLID); nothing similar in real world */
534 if (finfo->mode & aDIR)
535 new_entry->my_stat.st_mode |= /* drwxrwxrwx */
536 S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
537 else
538 new_entry->my_stat.st_mode |= S_IFREG; /* if not dir, regular file? */
539 /* if (finfo->mode & aARCH); DOS archive */
540 /* if (finfo->mode & aHIDDEN); like a dot file? */
541 /* if (finfo->mode & aSYSTEM); like a kernel? */
542 if (finfo->mode & aRONLY)
543 new_entry->my_stat.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
544 new_entry->my_stat.st_mode &= myumask;
546 DEBUG (entry ? 3 : 6, (" %-30s%7.7s%8.0f %s",
547 CNV_LANG (finfo->name),
548 attrib_string (finfo->mode),
549 (double) finfo->size,
550 asctime (LocalTime (&t))));
553 /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
554 static char *
555 smbfs_convert_path (const char *remote_file, gboolean trailing_asterik)
557 const char *p, *my_remote;
558 char *result;
560 my_remote = remote_file;
561 if (strncmp (my_remote, URL_HEADER, HEADER_LEN) == 0) { /* if passed directly */
562 my_remote += HEADER_LEN;
563 if (*my_remote == '/') /* from server browsing */
564 my_remote++;
565 p = strchr(my_remote, '/');
566 if (p)
567 my_remote = p+1; /* advance to end of server name */
570 if (*my_remote == '/')
571 my_remote++; /* strip off leading '/' */
572 p = strchr(my_remote, '/');
573 if (p)
574 my_remote = p; /* strip off share/service name */
575 /* create remote filename as understood by smb clientgen */
576 result = g_strconcat (my_remote, trailing_asterik ? "/*" : "", (char *) NULL);
577 unix_to_dos (result, /* inplace = */ 1); /* code page conversion */
578 str_replace(result, '/', '\\');
579 return result;
582 static void
583 smbfs_srv_browsing_helper (const char *name, uint32 m, const char *comment,
584 void *state)
586 dir_entry *new_entry = smbfs_new_dir_entry (name);
588 (void) m;
589 (void) state;
591 /* show this as dir */
592 new_entry->my_stat.st_mode =
593 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
594 S_IXOTH) & myumask;
596 DEBUG (3, ("\t%-16.16s %s\n", name, comment));
599 static BOOL
600 smbfs_reconnect(smbfs_connection *conn, int *retries)
602 char *host;
603 DEBUG(3, ("RECONNECT\n"));
605 if (*(conn->host) == 0)
606 host = g_strdup(conn->cli->desthost); /* server browsing */
607 else
608 host = g_strdup(conn->host);
610 cli_shutdown(conn->cli);
612 if (!(conn->cli = smbfs_do_connect(host, conn->service))) {
613 message (D_ERROR, MSG_ERROR,
614 _("reconnect to %s failed"), conn->host);
615 g_free(host);
616 return False;
618 g_free(host);
619 if (++(*retries) == 2)
620 return False;
621 return True;
624 static BOOL
625 smbfs_send(struct cli_state *cli)
627 size_t len;
628 size_t nwritten=0;
629 ssize_t ret;
631 len = smb_len(cli->outbuf) + 4;
633 while (nwritten < len) {
634 ret = write_socket(cli->fd, cli->outbuf+nwritten, len - nwritten);
635 if (ret <= 0) {
636 if (errno == EPIPE)
637 return False;
638 } else
639 nwritten += ret;
642 return True;
645 /****************************************************************************
646 See if server has cut us off by checking for EPIPE when writing.
647 Taken from cli_chkpath()
648 ****************************************************************************/
649 static BOOL
650 smbfs_chkpath(struct cli_state *cli, const char *path, BOOL send_only)
652 fstring path2;
653 char *p;
655 fstrcpy(path2,path);
656 unix_to_dos (path2, 1);
657 trim_string(path2,NULL,"\\");
658 if (!*path2) *path2 = '\\';
660 memset(cli->outbuf,'\0',smb_size);
661 set_message(cli->outbuf,0,4 + strlen(path2),True);
662 SCVAL(cli->outbuf,smb_com,SMBchkpth);
663 SSVAL(cli->outbuf,smb_tid,cli->cnum);
665 cli->rap_error = 0;
666 cli->nt_error = 0;
667 SSVAL(cli->outbuf,smb_pid,cli->pid);
668 SSVAL(cli->outbuf,smb_uid,cli->vuid);
669 SSVAL(cli->outbuf,smb_mid,cli->mid);
670 if (cli->protocol > PROTOCOL_CORE) {
671 SCVAL(cli->outbuf,smb_flg,0x8);
672 SSVAL(cli->outbuf,smb_flg2,0x1);
675 p = smb_buf(cli->outbuf);
676 *p++ = 4;
677 fstrcpy(p,path2);
679 if (!smbfs_send(cli)) {
680 DEBUG(3, ("smbfs_chkpath: couldnt send\n"));
681 return False;
683 if (send_only) {
684 client_receive_smb(cli->fd, cli->inbuf, cli->timeout);
685 DEBUG(3, ("smbfs_chkpath: send only OK\n"));
686 return True; /* just testing for EPIPE */
688 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout)) {
689 DEBUG(3, ("smbfs_chkpath: receive error\n"));
690 return False;
692 if ((my_errno = cli_error(cli, NULL, NULL, NULL))) {
693 if (my_errno == 20 || my_errno == 13)
694 return True; /* ignore if 'not a directory' error */
695 DEBUG(3, ("smbfs_chkpath: cli_error: %s\n", unix_error_string (my_errno)));
696 return False;
699 return True;
702 #if 1
703 static int
704 smbfs_fs (const char *text)
706 const char *p = text;
707 int count = 0;
709 while ((p = strchr(p, '/')) != NULL) {
710 count++;
711 p++;
713 if (count == 1)
714 return strlen(text);
715 return count;
717 #endif
719 static int
720 smbfs_loaddir (opendir_info *smbfs_info)
722 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
723 int servlen = strlen (smbfs_info->conn->service);
724 const char *info_dirname = smbfs_info->dirname;
725 char *my_dirname;
727 DEBUG (3, ("smbfs_loaddir: dirname:%s\n", info_dirname));
728 first_direntry = TRUE;
730 if (current_info) {
731 DEBUG (3,
732 ("smbfs_loaddir: new:'%s', cached:'%s'\n", info_dirname,
733 current_info->dirname));
734 /* if new desired dir is longer than cached in current_info */
735 if (smbfs_fs (info_dirname) > smbfs_fs (current_info->dirname)) {
736 DEBUG (3, ("saving to previous_info\n"));
737 previous_info = current_info;
741 current_info = smbfs_info;
743 if (strcmp (info_dirname, "/") == 0) {
744 if (!strcmp (smbfs_info->path, URL_HEADER)) {
745 DEBUG (6, ("smbfs_loaddir: browsing %s\n", IPC));
746 /* browse for servers */
747 if (!cli_NetServerEnum
748 (smbfs_info->conn->cli, smbfs_info->conn->domain,
749 SV_TYPE_ALL, smbfs_srv_browsing_helper, NULL))
750 return 0;
751 else
752 current_server_info = smbfs_info;
753 smbfs_info->server_list = TRUE;
754 } else {
755 /* browse for shares */
756 if (cli_RNetShareEnum
757 (smbfs_info->conn->cli, smbfs_browsing_helper, NULL) < 1)
758 return 0;
759 else
760 current_share_info = smbfs_info;
762 goto done;
765 /* do regular directory listing */
766 if (strncmp (smbfs_info->conn->service, info_dirname + 1, servlen) == 0) {
767 /* strip share name from dir */
768 my_dirname = g_strdup (info_dirname + servlen);
769 *my_dirname = '/';
770 my_dirname = free_after(smbfs_convert_path (my_dirname, TRUE), my_dirname);
771 } else
772 my_dirname = smbfs_convert_path (info_dirname, TRUE);
774 DEBUG (6, ("smbfs_loaddir: service: %s\n", smbfs_info->conn->service));
775 DEBUG (6,
776 ("smbfs_loaddir: cli->share: %s\n",
777 smbfs_info->conn->cli->share));
778 DEBUG (6,
779 ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname));
780 /* do file listing: cli_list returns number of files */
781 if (cli_list
782 (smbfs_info->conn->cli, my_dirname, attribute,
783 smbfs_loaddir_helper, NULL) < 0) {
784 /* cli_list returns -1 if directory empty or cannot read socket */
785 my_errno = cli_error (smbfs_info->conn->cli, NULL, &err, NULL);
786 g_free (my_dirname);
787 return 0;
789 if (*(my_dirname) == 0)
790 smbfs_info->dirname = smbfs_info->conn->service;
791 g_free (my_dirname);
792 /* do_dskattr(); */
794 done:
795 /* current_info->parent = smbfs_info->dirname; */
797 smbfs_info->current = smbfs_info->entries;
798 return 1; /* 1 = ok */
801 #ifdef SMBFS_FREE_DIR
802 static void
803 smbfs_free_dir (dir_entry *de)
805 if (!de) return;
807 smbfs_free_dir (de->next);
808 g_free (de->text);
809 g_free (de);
811 #endif
814 /* The readdir routine loads the complete directory */
815 /* It's too slow to ask the server each time */
816 /* It now also sends the complete lstat information for each file */
817 static void *
818 smbfs_readdir(void *info)
820 static union vfs_dirent smbfs_readdir_data;
821 static char *const dirent_dest = smbfs_readdir_data.dent.d_name;
822 opendir_info *smbfs_info = (opendir_info *) info;
824 DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info->dirname));
826 if (!smbfs_info->entries)
827 if (!smbfs_loaddir(smbfs_info))
828 return NULL;
830 if (smbfs_info->current == 0) { /* reached end of dir entries */
831 DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
832 #ifdef SMBFS_FREE_DIR
833 smbfs_free_dir(smbfs_info->entries);
834 smbfs_info->entries = 0;
835 #endif
836 return NULL;
838 g_strlcpy(dirent_dest, smbfs_info->current->text, MC_MAXPATHLEN);
839 smbfs_info->current = smbfs_info->current->next;
841 compute_namelen(&smbfs_readdir_data.dent);
843 return &smbfs_readdir_data;
846 static int
847 smbfs_closedir (void *info)
849 opendir_info *smbfs_info = (opendir_info *) info;
850 /* dir_entry *p, *q; */
852 DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info->dirname));
853 /* CLOSE HERE */
855 /* for (p = smbfs_info->entries; p;){
856 q = p;
857 p = p->next;
858 g_free (q->text);
859 g_free (q);
861 g_free (info); */
862 return 0;
865 static int
866 smbfs_chmod (struct vfs_class *me, const char *path, int mode)
868 (void) me;
870 DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path, mode));
871 /* my_errno = EOPNOTSUPP;
872 return -1; */ /* cannot chmod on smb filesystem */
873 return 0; /* make mc happy */
876 static int
877 smbfs_chown (struct vfs_class *me, const char *path, uid_t owner, gid_t group)
879 (void) me;
881 DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path, owner, group));
882 my_errno = EOPNOTSUPP; /* ready for your labotomy? */
883 return -1;
886 static int
887 smbfs_utime (struct vfs_class *me, const char *path, struct utimbuf *times)
889 (void) me;
890 (void) times;
892 DEBUG(3, ("smbfs_utime(path:%s)\n", path));
893 my_errno = EOPNOTSUPP;
894 return -1;
897 static int
898 smbfs_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
900 (void) me;
902 DEBUG (3,
903 ("smbfs_readlink(path:%s, buf:%s, size:%zu)\n", path, buf, size));
904 my_errno = EOPNOTSUPP;
905 return -1; /* no symlinks on smb filesystem? */
908 static int
909 smbfs_symlink (struct vfs_class *me, const char *n1, const char *n2)
911 (void) me;
913 DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1, n2));
914 my_errno = EOPNOTSUPP;
915 return -1; /* no symlinks on smb filesystem? */
918 /* Extract the hostname and username from the path */
919 /* path is in the form: [user@]hostname/share/remote-dir */
920 #define smbfs_get_host_and_username(path, host, user, port, pass) \
921 vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
923 /*****************************************************
924 return a connection to a SMB server
925 current_bucket needs to be set before calling
926 *******************************************************/
927 static struct cli_state *
928 smbfs_do_connect (const char *server, char *share)
930 struct cli_state *c;
931 struct nmb_name called, calling;
932 struct in_addr ip;
934 DEBUG(3, ("smbfs_do_connect(%s, %s)\n", server, share));
935 if (*share == '\\') {
936 server = share+2;
937 share = strchr(server,'\\');
938 if (!share) return NULL;
939 *share = 0;
940 share++;
943 make_nmb_name(&calling, global_myname, 0x0);
944 make_nmb_name(&called , server, current_bucket->name_type);
946 for (;;) {
948 ip = (current_bucket->have_ip) ? current_bucket->dest_ip : ipzero;
950 /* have to open a new connection */
951 if (!(c = cli_initialise(NULL))) {
952 my_errno = ENOMEM;
953 return NULL;
956 pwd_init(&(c->pwd)); /* should be moved into cli_initialise()? */
957 pwd_set_cleartext(&(c->pwd), current_bucket->password);
959 if ((cli_set_port(c, current_bucket->port) == 0) ||
960 !cli_connect(c, server, &ip)) {
961 DEBUG(1, ("Connection to %s failed\n", server));
962 break;
965 if (!cli_session_request(c, &calling, &called)) {
966 my_errno = cli_error(c, NULL, &err, NULL);
967 DEBUG(1, ("session request to %s failed\n", called.name));
968 cli_shutdown(c);
969 if (strcmp(called.name, "*SMBSERVER")) {
970 make_nmb_name(&called , "*SMBSERVER", 0x20);
971 continue;
973 return NULL;
976 DEBUG(3, (" session request ok\n"));
978 if (!cli_negprot(c)) {
979 DEBUG(1, ("protocol negotiation failed\n"));
980 break;
983 if (!cli_session_setup(c, current_bucket->user,
984 current_bucket->password, strlen(current_bucket->password),
985 current_bucket->password, strlen(current_bucket->password),
986 current_bucket->domain)) {
987 DEBUG(1,("session setup failed: %s\n", cli_errstr(c)));
988 smbfs_auth_remove (server, share);
989 break;
992 if (*c->server_domain || *c->server_os || *c->server_type)
993 DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
994 c->server_domain,c->server_os,c->server_type));
996 DEBUG(3, (" session setup ok\n"));
998 if (!cli_send_tconX(c, share, "?????",
999 current_bucket->password, strlen(current_bucket->password)+1)) {
1000 DEBUG(1,("%s: tree connect failed: %s\n", share, cli_errstr(c)));
1001 break;
1004 DEBUG(3, (" tconx ok\n"));
1006 my_errno = 0;
1007 return c;
1010 my_errno = cli_error(c, NULL, &err, NULL);
1011 cli_shutdown(c);
1012 return NULL;
1016 static int
1017 smbfs_get_master_browser(char **host)
1019 static char so_broadcast[] = "SO_BROADCAST";
1020 int count;
1021 struct in_addr *ip_list, bcast_addr;
1023 /* does port = 137 for win95 master browser? */
1024 int fd= open_socket_in( SOCK_DGRAM, 0, 3,
1025 interpret_addr(lp_socket_address()), True );
1026 if (fd == -1)
1027 return 0;
1028 set_socket_options(fd, so_broadcast);
1029 ip_list = iface_bcast(ipzero);
1030 bcast_addr = *ip_list;
1031 if ((ip_list = name_query(fd, "\01\02__MSBROWSE__\02", 1, True,
1032 True, bcast_addr, &count, NULL))) {
1033 if (!count)
1034 return 0;
1035 /* just return first master browser */
1036 *host = g_strdup(inet_ntoa(ip_list[0]));
1037 return 1;
1039 return 0;
1042 static void
1043 smbfs_free_bucket (smbfs_connection *bucket)
1045 g_free (bucket->host);
1046 g_free (bucket->service);
1047 g_free (bucket->domain);
1048 g_free (bucket->user);
1049 wipe_password (bucket->password);
1050 g_free (bucket->home);
1051 memset (bucket, 0, sizeof (smbfs_connection));
1054 static smbfs_connection *
1055 smbfs_get_free_bucket (void)
1057 int i;
1059 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++)
1060 if (!smbfs_connections [i].cli) return &smbfs_connections [i];
1062 { /* search for most dormant connection */
1063 int oldest = 0; /* index */
1064 time_t oldest_time = smbfs_connections[0].last_use;
1065 for (i = 1; i < SMBFS_MAX_CONNECTIONS; i++) {
1066 if (smbfs_connections[i].last_use < oldest_time) {
1067 oldest_time = smbfs_connections[i].last_use;
1068 oldest = i;
1071 cli_shutdown(smbfs_connections[oldest].cli);
1072 smbfs_free_bucket (&smbfs_connections[oldest]);
1073 return &smbfs_connections[oldest];
1076 /* This can't happend, since we have checked for max connections before */
1077 vfs_die("Internal error: smbfs_get_free_bucket");
1078 return 0; /* shut up, stupid gcc */
1081 /* This routine keeps track of open connections */
1082 /* Returns a connected socket to host */
1083 static smbfs_connection *
1084 smbfs_open_link (char *host, char *path, const char *user, int *port,
1085 char *this_pass)
1087 int i;
1088 smbfs_connection *bucket;
1089 pstring service;
1090 struct in_addr *dest_ip = NULL;
1092 DEBUG (3, ("smbfs_open_link(host:%s, path:%s)\n", host, path));
1094 if (strcmp (host, path) == 0) /* if host & path are same: */
1095 pstrcpy (service, IPC); /* setup for browse */
1096 else { /* get share name from path, path starts with server name */
1097 char *p;
1098 if ((p = strchr (path, '/'))) /* get share aka */
1099 pstrcpy (service, ++p); /* service name from path */
1100 else
1101 pstrcpy (service, "");
1102 /* now check for trailing directory/filenames */
1103 p = strchr (service, '/');
1104 if (p)
1105 *p = 0; /* cut off dir/files: sharename only */
1106 if (!*service)
1107 pstrcpy (service, IPC); /* setup for browse */
1108 DEBUG (6, ("smbfs_open_link: service from path:%s\n", service));
1111 if (got_user)
1112 user = username; /* global from getenv */
1114 /* Is the link actually open? */
1115 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1116 if (!smbfs_connections[i].cli)
1117 continue;
1118 if ((strcmp (host, smbfs_connections[i].host) == 0) &&
1119 (strcmp (user, smbfs_connections[i].user) == 0) &&
1120 (strcmp (service, smbfs_connections[i].service) == 0)) {
1121 int retries = 0;
1122 BOOL inshare = (*host != 0 && *path != 0 && strchr (path, '/'));
1123 /* check if this connection has died */
1124 while (!smbfs_chkpath (smbfs_connections[i].cli, "\\", !inshare)) {
1125 if (!smbfs_reconnect (&smbfs_connections[i], &retries))
1126 return 0;
1128 DEBUG (6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i));
1129 current_bucket = &smbfs_connections[i];
1130 smbfs_connections[i].last_use = time (NULL);
1131 return &smbfs_connections[i];
1133 /* connection not found, find if we have ip for new connection */
1134 if (strcmp (host, smbfs_connections[i].host) == 0)
1135 dest_ip = &smbfs_connections[i].cli->dest_ip;
1138 /* make new connection */
1139 bucket = smbfs_get_free_bucket ();
1140 bucket->name_type = 0x20;
1141 bucket->home = 0;
1142 bucket->port = *port;
1143 bucket->have_ip = False;
1144 if (dest_ip) {
1145 bucket->have_ip = True;
1146 bucket->dest_ip = *dest_ip;
1148 current_bucket = bucket;
1150 bucket->user = g_strdup (user);
1151 bucket->service = g_strdup (service);
1153 if (!(*host)) { /* if blank host name, browse for servers */
1154 if (!smbfs_get_master_browser (&host)) /* set host to ip of master browser */
1155 return 0; /* could not find master browser? */
1156 g_free (host);
1157 bucket->host = g_strdup (""); /* blank host means master browser */
1158 } else
1159 bucket->host = g_strdup (host);
1161 if (!smbfs_bucket_set_authinfo (bucket, 0, /* domain currently not used */
1162 user, this_pass, 1))
1163 return 0;
1165 /* connect to share */
1166 while (!(bucket->cli = smbfs_do_connect (host, service))) {
1168 if (my_errno != EPERM)
1169 return 0;
1170 message (D_ERROR, MSG_ERROR, _("Authentication failed"));
1172 /* authentication failed, try again */
1173 smbfs_auth_remove (bucket->host, bucket->service);
1174 if (!smbfs_bucket_set_authinfo (bucket, bucket->domain, bucket->user, 0, 0))
1175 return 0;
1179 smbfs_open_connections++;
1180 DEBUG (3, ("smbfs_open_link:smbfs_open_connections: %d\n",
1181 smbfs_open_connections));
1182 return bucket;
1185 static char *
1186 smbfs_get_path (smbfs_connection ** sc, const char *path)
1188 char *user, *host, *remote_path, *pass;
1189 int port = SMB_PORT;
1191 DEBUG (3, ("smbfs_get_path(%s)\n", path));
1192 if (strncmp (path, URL_HEADER, HEADER_LEN))
1193 return NULL;
1194 path += HEADER_LEN;
1196 if (*path == '/') /* '/' leading server name */
1197 path++; /* probably came from server browsing */
1199 if ((remote_path =
1200 smbfs_get_host_and_username (&path, &host, &user, &port, &pass)))
1201 if ((*sc =
1202 smbfs_open_link (host, remote_path, user, &port, pass)) == NULL) {
1203 g_free (remote_path);
1204 remote_path = NULL;
1206 g_free (host);
1207 g_free (user);
1208 if (pass)
1209 wipe_password (pass);
1211 if (!remote_path)
1212 return NULL;
1214 /* NOTE: tildes are deprecated. See ftpfs.c */
1216 int f = !strcmp (remote_path, "/~");
1217 if (f || !strncmp (remote_path, "/~/", 3)) {
1218 char *s;
1219 s = concat_dir_and_file ((*sc)->home, remote_path + 3 - f);
1220 g_free (remote_path);
1221 return s;
1224 return remote_path;
1227 #if 0
1228 static int
1229 is_error (int result, int errno_num)
1231 if (!(result == -1))
1232 return my_errno = 0;
1233 else
1234 my_errno = errno_num;
1235 return 1;
1237 #endif
1239 static void *
1240 smbfs_opendir (struct vfs_class *me, const char *dirname)
1242 opendir_info *smbfs_info;
1243 smbfs_connection *sc;
1244 char *remote_dir;
1246 (void) me;
1248 DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname));
1250 if (!(remote_dir = smbfs_get_path (&sc, dirname)))
1251 return NULL;
1253 /* FIXME: where freed? */
1254 smbfs_info = g_new (opendir_info, 1);
1255 smbfs_info->server_list = FALSE;
1256 smbfs_info->path = g_strdup(dirname); /* keep original */
1257 smbfs_info->dirname = remote_dir;
1258 smbfs_info->conn = sc;
1259 smbfs_info->entries = 0;
1260 smbfs_info->current = 0;
1262 return smbfs_info;
1265 static int
1266 smbfs_fake_server_stat (const char *server_url, const char *path, struct stat *buf)
1268 dir_entry *dentry;
1269 const char *p;
1271 (void) server_url;
1273 if ((p = strrchr (path, '/')))
1274 path = p + 1; /* advance until last '/' */
1276 if (!current_info->entries) {
1277 if (!smbfs_loaddir (current_info)) /* browse host */
1278 return -1;
1281 if (current_info->server_list == True) {
1282 dentry = current_info->entries;
1283 DEBUG (4, ("fake stat for SERVER \"%s\"\n", path));
1284 while (dentry) {
1285 if (strcmp (dentry->text, path) == 0) {
1286 DEBUG (4, ("smbfs_fake_server_stat: %s:%4o\n",
1287 dentry->text, (int)dentry->my_stat.st_mode));
1288 memcpy (buf, &dentry->my_stat, sizeof (struct stat));
1289 return 0;
1291 dentry = dentry->next;
1294 my_errno = ENOENT;
1295 return -1;
1298 static int
1299 smbfs_fake_share_stat (const char *server_url, const char *path, struct stat *buf)
1301 dir_entry *dentry;
1302 if (strlen (path) < strlen (server_url))
1303 return -1;
1305 if (!current_share_info) { /* Server was not stat()ed */
1306 /* Make sure there is such share at server */
1307 smbfs_connection *sc;
1308 char *p;
1309 p = smbfs_get_path (&sc, path);
1310 g_free (p);
1311 if (p) {
1312 memset (buf, 0, sizeof (*buf));
1313 /* show this as dir */
1314 buf->st_mode =
1315 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
1316 S_IXOTH) & myumask;
1317 return 0;
1319 return -1;
1322 path += strlen (server_url); /* we only want share name */
1323 path++;
1325 if (*path == '/') /* '/' leading server name */
1326 path++; /* probably came from server browsing */
1328 if (!current_share_info->entries) {
1329 if (!smbfs_loaddir (current_share_info)) /* browse host */
1330 return -1;
1332 dentry = current_share_info->entries;
1333 DEBUG (3, ("smbfs_fake_share_stat: %s on %s\n", path, server_url));
1334 while (dentry) {
1335 if (strcmp (dentry->text, path) == 0) {
1336 DEBUG (6, ("smbfs_fake_share_stat: %s:%4o\n",
1337 dentry->text, (int) dentry->my_stat.st_mode));
1338 memcpy (buf, &dentry->my_stat, sizeof (struct stat));
1339 return 0;
1341 dentry = dentry->next;
1343 my_errno = ENOENT;
1344 return -1;
1347 /* stat a single file, smbfs_get_remote_stat callback */
1348 static dir_entry *single_entry;
1350 /* stat a single file */
1351 static int
1352 smbfs_get_remote_stat (smbfs_connection * sc, const char *path, struct stat *buf)
1354 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
1355 char *mypath;
1357 DEBUG (3, ("smbfs_get_remote_stat(): mypath:%s\n", path));
1359 mypath = smbfs_convert_path (path, FALSE);
1361 #if 0 /* single_entry is never free()d now. And only my_stat is used */
1362 single_entry = g_new (dir_entry, 1);
1364 single_entry->text = dos_to_unix (g_strdup (finfo->name), 1);
1366 single_entry->next = 0;
1367 #endif
1368 if (!single_entry)
1369 single_entry = g_new0 (dir_entry, 1);
1371 if (cli_list
1372 (sc->cli, mypath, attribute, smbfs_loaddir_helper, single_entry) < 1) {
1373 my_errno = ENOENT;
1374 g_free (mypath);
1375 return -1; /* cli_list returns number of files */
1378 memcpy (buf, &single_entry->my_stat, sizeof (struct stat));
1380 /* don't free here, use for smbfs_fstat() */
1381 /* g_free(single_entry->text);
1382 g_free(single_entry); */
1383 g_free (mypath);
1384 return 0;
1387 static int
1388 smbfs_search_dir_entry (dir_entry *dentry, const char *text, struct stat *buf)
1390 while (dentry) {
1391 if (strcmp(text, dentry->text) == 0) {
1392 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1393 memcpy(&single_entry->my_stat, &dentry->my_stat,
1394 sizeof(struct stat));
1395 return 0;
1397 dentry = dentry->next;
1399 return -1;
1402 static int
1403 smbfs_get_stat_info (smbfs_connection * sc, const char *path, struct stat *buf)
1405 char *p;
1406 #if 0
1407 dir_entry *dentry = current_info->entries;
1408 #endif
1409 const char *mypath = path;
1411 mypath++; /* cut off leading '/' */
1412 if ((p = strrchr (mypath, '/')))
1413 mypath = p + 1; /* advance until last file/dir name */
1414 DEBUG (3, ("smbfs_get_stat_info: mypath:%s, current_info->dirname:%s\n",
1415 mypath, current_info->dirname));
1416 #if 0
1417 if (!dentry) {
1418 DEBUG (1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n",
1419 current_info->dirname, path));
1420 return -1;
1422 #endif
1423 if (!single_entry) /* when found, this will be written too */
1424 single_entry = g_new (dir_entry, 1);
1425 if (smbfs_search_dir_entry (current_info->entries, mypath, buf) == 0) {
1426 return 0;
1428 /* now try to identify mypath as PARENT dir */
1430 char *mdp;
1431 char *mydir;
1432 mdp = mydir = g_strdup (current_info->dirname);
1433 if ((p = strrchr (mydir, '/')))
1434 *p = 0; /* advance util last '/' */
1435 if ((p = strrchr (mydir, '/')))
1436 mydir = p + 1; /* advance util last '/' */
1437 if (strcmp (mydir, mypath) == 0) { /* fake a stat for ".." */
1438 memset (buf, 0, sizeof (struct stat));
1439 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1440 memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
1441 g_free (mdp);
1442 DEBUG (1, (" PARENT:found in %s\n", current_info->dirname));
1443 return 0;
1445 g_free (mdp);
1447 /* now try to identify as CURRENT dir? */
1449 char *dnp = current_info->dirname;
1450 DEBUG (6, ("smbfs_get_stat_info: is %s current dir? this dir is: %s\n",
1451 mypath, current_info->dirname));
1452 if (*dnp == '/')
1453 dnp++;
1454 else {
1455 return -1;
1457 if (strcmp (mypath, dnp) == 0) {
1458 memset (buf, 0, sizeof (struct stat));
1459 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1460 memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
1461 DEBUG (1, (" CURRENT:found in %s\n", current_info->dirname));
1462 return 0;
1465 DEBUG (3, ("'%s' not found in current_info '%s'\n", path,
1466 current_info->dirname));
1467 /* try to find this in the PREVIOUS listing */
1468 if (previous_info) {
1469 if (smbfs_search_dir_entry (previous_info->entries, mypath, buf) == 0)
1470 return 0;
1471 DEBUG (3, ("'%s' not found in previous_info '%s'\n", path,
1472 previous_info->dirname));
1474 /* try to find this in the SHARE listing */
1475 if (current_share_info) {
1476 if (smbfs_search_dir_entry (current_share_info->entries, mypath, buf) == 0)
1477 return 0;
1478 DEBUG (3, ("'%s' not found in share_info '%s'\n", path,
1479 current_share_info->dirname));
1481 /* try to find this in the SERVER listing */
1482 if (current_server_info) {
1483 if (smbfs_search_dir_entry (current_server_info->entries, mypath, buf) == 0)
1484 return 0;
1485 DEBUG (3, ("'%s' not found in server_info '%s'\n", path,
1486 current_server_info->dirname));
1488 /* nothing found. get stat file info from server */
1489 return smbfs_get_remote_stat (sc, path, buf);
1492 static int
1493 smbfs_chdir (struct vfs_class *me, const char *path)
1495 char *remote_dir;
1496 smbfs_connection *sc;
1498 (void) me;
1500 DEBUG (3, ("smbfs_chdir(path:%s)\n", path));
1501 if (!(remote_dir = smbfs_get_path (&sc, path)))
1502 return -1;
1503 g_free (remote_dir);
1505 return 0;
1508 static int
1509 smbfs_loaddir_by_name (struct vfs_class *me, const char *path)
1511 void *info;
1512 char *mypath, *p;
1514 mypath = g_strdup(path);
1515 p = strrchr(mypath, '/');
1517 if (p > mypath)
1518 *p = 0;
1519 DEBUG(6, ("smbfs_loaddir_by_name(%s)\n", mypath));
1520 smbfs_chdir(me, mypath);
1521 info = smbfs_opendir (me, mypath);
1522 g_free(mypath);
1523 if (!info)
1524 return -1;
1525 smbfs_readdir(info);
1526 smbfs_loaddir(info);
1527 return 0;
1530 static int
1531 smbfs_stat (struct vfs_class * me, const char *path, struct stat *buf)
1533 smbfs_connection *sc;
1534 pstring server_url;
1535 char *service, *pp, *at;
1536 const char *p;
1538 DEBUG (3, ("smbfs_stat(path:%s)\n", path));
1540 if (!current_info) {
1541 DEBUG (1, ("current_info = NULL: "));
1542 if (smbfs_loaddir_by_name (me, path) < 0)
1543 return -1;
1546 /* check if stating server */
1547 p = path;
1548 if (strncmp (p, URL_HEADER, HEADER_LEN)) {
1549 DEBUG (1, ("'%s' doesnt start with '%s' (length %d)\n",
1550 p, URL_HEADER, HEADER_LEN));
1551 return -1;
1554 p += HEADER_LEN;
1555 if (*p == '/')
1556 p++;
1558 pp = strchr (p, '/'); /* advance past next '/' */
1559 at = strchr (p, '@');
1560 pstrcpy (server_url, URL_HEADER);
1561 if (at && at < pp) { /* user@server */
1562 char *z = &(server_url[sizeof (server_url) - 1]);
1563 const char *s = p;
1565 at = &(server_url [HEADER_LEN]) + (at - p + 1);
1566 if (z > at)
1567 z = at;
1568 at = &(server_url [HEADER_LEN]);
1569 while (at < z)
1570 *at++ = *s++;
1571 *z = 0;
1573 pstrcat (server_url, current_bucket->host);
1575 if (!pp) {
1576 if (!current_info->server_list) {
1577 if (smbfs_loaddir_by_name (me, path) < 0)
1578 return -1;
1580 return smbfs_fake_server_stat (server_url, path, buf);
1583 if (!strchr (++pp, '/')) {
1584 return smbfs_fake_share_stat (server_url, path, buf);
1587 /* stating inside share at this point */
1588 if (!(service = smbfs_get_path (&sc, path))) /* connects if necessary */
1589 return -1;
1591 int hostlen = strlen (current_bucket->host);
1592 char *ppp = service + strlen (service) - hostlen;
1593 char *sp = server_url + strlen (server_url) - hostlen;
1595 if (strcmp (sp, ppp) == 0) {
1596 /* make server name appear as directory */
1597 DEBUG (1, ("smbfs_stat: showing server as directory\n"));
1598 memset (buf, 0, sizeof (struct stat));
1599 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1600 g_free (service);
1601 return 0;
1604 /* check if current_info is in share requested */
1605 p = service;
1606 pp = strchr (p, '/');
1607 if (pp) {
1608 p = ++pp; /* advance past server name */
1609 pp = strchr (p, '/');
1611 if (pp)
1612 *pp = 0; /* cut off everthing after service name */
1613 else
1614 p = IPC; /* browsing for services */
1615 pp = current_info->dirname;
1616 if (*pp == '/')
1617 pp++;
1618 if (strncmp (p, pp, strlen (p)) != 0) {
1619 DEBUG (6, ("desired '%s' is not loaded, we have '%s'\n", p, pp));
1620 if (smbfs_loaddir_by_name (me, path) < 0) {
1621 g_free (service);
1622 return -1;
1624 DEBUG (6, ("loaded dir: '%s'\n", current_info->dirname));
1626 g_free (service);
1627 /* stat dirs & files under shares now */
1628 return smbfs_get_stat_info (sc, path, buf);
1631 #define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */
1633 static off_t
1634 smbfs_lseek (void *data, off_t offset, int whence)
1636 smbfs_handle *info = (smbfs_handle *) data;
1637 size_t size;
1639 DEBUG (3,
1640 ("smbfs_lseek(info->nread => %d, offset => %d, whence => %d) \n",
1641 (int) info->nread, (int) offset, whence));
1643 switch (whence) {
1644 case SEEK_SET:
1645 info->nread = offset;
1646 break;
1647 case SEEK_CUR:
1648 info->nread += offset;
1649 break;
1650 case SEEK_END:
1651 if (!cli_qfileinfo (info->cli, info->fnum,
1652 NULL, &size, NULL, NULL, NULL,
1653 NULL, NULL) &&
1654 !cli_getattrE (info->cli, info->fnum,
1655 NULL, &size, NULL, NULL, NULL)) {
1656 errno = EINVAL;
1657 return -1;
1659 info->nread = size + offset;
1660 break;
1663 return info->nread;
1666 static int
1667 smbfs_mknod (struct vfs_class *me, const char *path, mode_t mode, dev_t dev)
1669 (void) me;
1671 DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%zu)\n", path, mode, dev));
1672 my_errno = EOPNOTSUPP;
1673 return -1;
1676 static int
1677 smbfs_mkdir (struct vfs_class * me, const char *path, mode_t mode)
1679 smbfs_connection *sc;
1680 char *remote_file;
1681 char *cpath;
1683 (void) me;
1685 DEBUG (3, ("smbfs_mkdir(path:%s, mode:%d)\n", path, (int) mode));
1686 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1687 return -1;
1688 g_free (remote_file);
1689 cpath = smbfs_convert_path (path, FALSE);
1691 if (!cli_mkdir (sc->cli, cpath)) {
1692 my_errno = cli_error (sc->cli, NULL, &err, NULL);
1693 message (D_ERROR, MSG_ERROR, _("Error %s creating directory %s"),
1694 cli_errstr (sc->cli), CNV_LANG (cpath));
1695 g_free (cpath);
1696 return -1;
1698 g_free (cpath);
1699 return 0;
1702 static int
1703 smbfs_rmdir (struct vfs_class *me, const char *path)
1705 smbfs_connection *sc;
1706 char *remote_file;
1707 char *cpath;
1709 (void) me;
1711 DEBUG(3, ("smbfs_rmdir(path:%s)\n", path));
1712 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1713 return -1;
1714 g_free (remote_file);
1715 cpath = smbfs_convert_path (path, FALSE);
1717 if (!cli_rmdir(sc->cli, cpath)) {
1718 my_errno = cli_error(sc->cli, NULL, &err, NULL);
1719 message (D_ERROR, MSG_ERROR, _("Error %s removing directory %s"),
1720 cli_errstr(sc->cli), CNV_LANG(cpath));
1721 g_free (cpath);
1722 return -1;
1725 g_free (cpath);
1726 return 0;
1729 static int
1730 smbfs_link (struct vfs_class *me, const char *p1, const char *p2)
1732 (void) me;
1734 DEBUG (3, ("smbfs_link(p1:%s, p2:%s)\n", p1, p2));
1735 my_errno = EOPNOTSUPP;
1736 return -1;
1739 static void
1740 smbfs_free (vfsid id)
1742 DEBUG (3, ("smbfs_free(%p)\n", id));
1743 smbfs_auth_free_all ();
1746 /* Gives up on a socket and reopens the connection, the child own the socket
1747 * now
1749 static void
1750 smbfs_forget (const char *path)
1752 char *host, *user, *p;
1753 int port;
1755 if (strncmp (path, URL_HEADER, HEADER_LEN))
1756 return;
1758 DEBUG (3, ("smbfs_forget(path:%s)\n", path));
1760 path += 6;
1761 if (path[0] == '/' && path[1] == '/')
1762 path += 2;
1764 if ((p = smbfs_get_host_and_username (&path, &host, &user, &port, NULL))) {
1765 size_t i;
1767 g_free (p);
1769 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1770 if (smbfs_connections[i].cli
1771 && (strcmp (host, smbfs_connections[i].host) == 0)
1772 && (strcmp (user, smbfs_connections[i].user) == 0)
1773 && (port == smbfs_connections[i].port)) {
1775 /* close socket: the child owns it now */
1776 cli_shutdown (smbfs_connections[i].cli);
1778 /* reopen the connection */
1779 smbfs_connections[i].cli =
1780 smbfs_do_connect (host, smbfs_connections[i].service);
1784 g_free (host);
1785 g_free (user);
1788 static int
1789 smbfs_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg)
1791 (void) me;
1792 (void) arg;
1794 DEBUG (3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path, ctlop));
1795 switch (ctlop) {
1796 case VFS_SETCTL_FORGET:
1797 smbfs_forget (path);
1798 return 0;
1800 return 0;
1804 static smbfs_handle *
1805 smbfs_open_readwrite (smbfs_handle *remote_handle, char *rname, int flags, mode_t mode)
1807 size_t size;
1809 (void) mode;
1811 if (flags & O_TRUNC) /* if it exists truncate to zero */
1812 DEBUG (3, ("smbfs_open: O_TRUNC\n"));
1814 remote_handle->fnum =
1815 #if 1 /* Don't play with flags, it is cli_open() headache */
1816 cli_open (remote_handle->cli, rname, flags, DENY_NONE);
1817 #else /* What's a reasons to has this code ? */
1818 cli_open (remote_handle->cli, rname, ((flags & O_CREAT)
1819 || (flags ==
1820 (O_WRONLY | O_APPEND))) ?
1821 flags : O_RDONLY, DENY_NONE);
1822 #endif
1823 if (remote_handle->fnum == -1) {
1824 message (D_ERROR, MSG_ERROR, _("%s opening remote file %s"),
1825 cli_errstr (remote_handle->cli), CNV_LANG (rname));
1826 DEBUG (1, ("smbfs_open(rname:%s) error:%s\n",
1827 rname, cli_errstr (remote_handle->cli)));
1828 my_errno = cli_error (remote_handle->cli, NULL, &err, NULL);
1829 return NULL;
1832 if (flags & O_CREAT)
1833 return remote_handle;
1835 if (!cli_qfileinfo (remote_handle->cli, remote_handle->fnum,
1836 &remote_handle->attr, &size, NULL, NULL, NULL, NULL,
1837 NULL)
1838 && !cli_getattrE (remote_handle->cli, remote_handle->fnum,
1839 &remote_handle->attr, &size, NULL, NULL, NULL)) {
1840 message (D_ERROR, MSG_ERROR, "getattrib: %s",
1841 cli_errstr (remote_handle->cli));
1842 DEBUG (1,
1843 ("smbfs_open(rname:%s) getattrib:%s\n", rname,
1844 cli_errstr (remote_handle->cli)));
1845 my_errno = cli_error (remote_handle->cli, NULL, &err, NULL);
1846 cli_close (remote_handle->cli, remote_handle->fnum);
1847 return NULL;
1850 if ((flags == (O_WRONLY | O_APPEND)) /* file.c:copy_file_file() -> do_append */
1851 && smbfs_lseek (remote_handle, 0, SEEK_END) == -1) {
1852 cli_close (remote_handle->cli, remote_handle->fnum);
1853 return NULL;
1856 return remote_handle;
1859 static void *
1860 smbfs_open (struct vfs_class *me, const char *file, int flags, mode_t mode)
1862 char *remote_file;
1863 void *ret;
1864 smbfs_connection *sc;
1865 smbfs_handle *remote_handle;
1867 (void) me;
1869 DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%o)\n", file, flags, mode));
1871 if (!(remote_file = smbfs_get_path (&sc, file)))
1872 return 0;
1874 remote_file = free_after(smbfs_convert_path (remote_file, FALSE), remote_file);
1876 remote_handle = g_new (smbfs_handle, 2);
1877 remote_handle->cli = sc->cli;
1878 remote_handle->nread = 0;
1880 ret = smbfs_open_readwrite (remote_handle, remote_file, flags, mode);
1882 g_free (remote_file);
1883 if (!ret)
1884 g_free (remote_handle);
1886 return ret;
1889 static int
1890 smbfs_unlink (struct vfs_class *me, const char *path)
1892 smbfs_connection *sc;
1893 char *remote_file;
1895 (void) me;
1897 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1898 return -1;
1900 remote_file = free_after(smbfs_convert_path (remote_file, FALSE), remote_file);
1902 if (!cli_unlink(sc->cli, remote_file)) {
1903 message (D_ERROR, MSG_ERROR, _("%s removing remote file %s"),
1904 cli_errstr(sc->cli), CNV_LANG(remote_file));
1905 g_free (remote_file);
1906 return -1;
1908 g_free (remote_file);
1909 return 0;
1912 static int
1913 smbfs_rename (struct vfs_class *me, const char *a, const char *b)
1915 smbfs_connection *sc;
1916 char *ra, *rb;
1917 int retval;
1919 (void) me;
1921 if ((ra = smbfs_get_path (&sc, a)) == 0)
1922 return -1;
1924 if ((rb = smbfs_get_path (&sc, b)) == 0) {
1925 g_free (ra);
1926 return -1;
1929 ra = free_after (smbfs_convert_path (ra, FALSE), ra);
1930 rb = free_after (smbfs_convert_path (rb, FALSE), rb);
1932 retval = cli_rename(sc->cli, ra, rb);
1934 g_free (ra);
1935 g_free (rb);
1937 if (!retval) {
1938 message (D_ERROR, MSG_ERROR, _("%s renaming files\n"),
1939 cli_errstr(sc->cli));
1940 return -1;
1942 return 0;
1945 static int
1946 smbfs_fstat (void *data, struct stat *buf)
1948 smbfs_handle *remote_handle = (smbfs_handle *)data;
1950 DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle->fnum));
1952 /* use left over from previous smbfs_get_remote_stat, if available */
1953 if (single_entry)
1954 memcpy(buf, &single_entry->my_stat, sizeof(struct stat));
1955 else { /* single_entry not set up: bug */
1956 my_errno = EFAULT;
1957 return -EFAULT;
1959 return 0;
1962 smb_authinfo *
1963 vfs_smb_authinfo_new (const char *host, const char *share, const char *domain,
1964 const char *user, const char *pass)
1966 smb_authinfo *auth;
1968 auth = g_try_new (struct smb_authinfo, 1);
1970 if (auth != NULL)
1972 auth->host = g_strdup (host);
1973 auth->share = g_strdup (share);
1974 auth->domain = g_strdup (domain);
1975 auth->user = g_strdup (user);
1976 auth->password = g_strdup (pass);
1979 return auth;
1982 void
1983 init_smbfs (void)
1985 tcp_init();
1987 vfs_smbfs_ops.name = "smbfs";
1988 vfs_smbfs_ops.prefix = "smb:";
1989 vfs_smbfs_ops.flags = VFSF_NOLINKS;
1990 vfs_smbfs_ops.init = smbfs_init;
1991 vfs_smbfs_ops.fill_names = smbfs_fill_names;
1992 vfs_smbfs_ops.open = smbfs_open;
1993 vfs_smbfs_ops.close = smbfs_close;
1994 vfs_smbfs_ops.read = smbfs_read;
1995 vfs_smbfs_ops.write = smbfs_write;
1996 vfs_smbfs_ops.opendir = smbfs_opendir;
1997 vfs_smbfs_ops.readdir = smbfs_readdir;
1998 vfs_smbfs_ops.closedir = smbfs_closedir;
1999 vfs_smbfs_ops.stat = smbfs_stat;
2000 vfs_smbfs_ops.lstat = smbfs_lstat;
2001 vfs_smbfs_ops.fstat = smbfs_fstat;
2002 vfs_smbfs_ops.chmod = smbfs_chmod;
2003 vfs_smbfs_ops.chown = smbfs_chown;
2004 vfs_smbfs_ops.utime = smbfs_utime;
2005 vfs_smbfs_ops.readlink = smbfs_readlink;
2006 vfs_smbfs_ops.symlink = smbfs_symlink;
2007 vfs_smbfs_ops.link = smbfs_link;
2008 vfs_smbfs_ops.unlink = smbfs_unlink;
2009 vfs_smbfs_ops.rename = smbfs_rename;
2010 vfs_smbfs_ops.chdir = smbfs_chdir;
2011 vfs_smbfs_ops.ferrno = smbfs_errno;
2012 vfs_smbfs_ops.lseek = smbfs_lseek;
2013 vfs_smbfs_ops.mknod = smbfs_mknod;
2014 vfs_smbfs_ops.free = smbfs_free;
2015 vfs_smbfs_ops.mkdir = smbfs_mkdir;
2016 vfs_smbfs_ops.rmdir = smbfs_rmdir;
2017 vfs_smbfs_ops.setctl = smbfs_setctl;
2018 vfs_register_class (&vfs_smbfs_ops);