fix: incorret draw files in 8-bit codeset after recode
[midnight-commander.git] / vfs / smbfs.c
blob30431e5e7a23ac88ae20493d4155ffd69ae6d333
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>
26 #include <stdio.h>
27 #include <sys/types.h>
29 #undef USE_NCURSES /* Don't include *curses.h */
30 #undef USE_NCURSESW
31 #include "../src/global.h"
32 #include "../src/tty.h" /* enable/disable interrupt key */
33 #include "../src/wtools.h" /* message() */
34 #include "../src/main.h" /* print_vfs_message */
35 #include "utilvfs.h"
37 #undef PACKAGE_BUGREPORT
38 #undef PACKAGE_NAME
39 #undef PACKAGE_STRING
40 #undef PACKAGE_TARNAME
41 #undef PACKAGE_VERSION
43 #include "samba/include/config.h"
44 /* don't load crap in "samba/include/includes.h" we don't use and which
45 conflicts with definitions in other includes */
46 #undef HAVE_LIBREADLINE
47 #define NO_CONFIG_H
48 #undef VERSION
50 #include "samba/include/includes.h"
52 #include <string.h>
54 #include "vfs.h"
55 #include "vfs-impl.h"
56 #include "smbfs.h"
58 #define SMBFS_MAX_CONNECTIONS 16
59 static const char * const IPC = "IPC$";
60 static const char * const URL_HEADER = "/#smb:";
61 #define HEADER_LEN 6
63 static int my_errno;
64 static uint32 err;
66 /* stuff that is same with each connection */
67 extern int DEBUGLEVEL;
68 extern pstring myhostname;
69 extern struct in_addr ipzero;
70 static mode_t myumask = 0755;
71 extern pstring global_myname;
72 static int smbfs_open_connections = 0;
73 static gboolean got_user = FALSE;
74 static gboolean got_pass = FALSE;
75 static pstring password;
76 static pstring username;
77 static struct vfs_class vfs_smbfs_ops;
79 static struct _smbfs_connection {
80 struct cli_state *cli;
81 struct in_addr dest_ip;
82 BOOL have_ip;
83 char *host; /* server name */
84 char *service; /* share name */
85 char *domain;
86 char *user;
87 char *home;
88 char *password;
89 int port;
90 int name_type;
91 time_t last_use;
92 } smbfs_connections [SMBFS_MAX_CONNECTIONS];
93 /* unique to each connection */
95 /* modifies *share */
96 static struct cli_state * smbfs_do_connect (const char *server, char *share);
98 typedef struct _smbfs_connection smbfs_connection;
99 static smbfs_connection *current_bucket;
101 typedef struct {
102 struct cli_state *cli;
103 int fnum;
104 off_t nread;
105 uint16 attr;
106 } smbfs_handle;
108 static GSList *auth_list;
110 /* this function allows you to write:
111 * char *s = g_strdup("hello, world");
112 * s = free_after(g_strconcat(s, s, (char *)0), s);
114 static inline char *
115 free_after (char *result, char *string_to_free)
117 g_free(string_to_free);
118 return result;
122 static void
123 smbfs_auth_free (struct smb_authinfo const *a)
125 g_free (a->host);
126 g_free (a->share);
127 g_free (a->domain);
128 g_free (a->user);
129 wipe_password (a->password);
132 static void
133 smbfs_auth_free_all (void)
135 if (auth_list) {
136 g_slist_foreach (auth_list, (GFunc)smbfs_auth_free, 0);
137 g_slist_free (auth_list);
138 auth_list = 0;
142 static gint
143 smbfs_auth_cmp_host_and_share (gconstpointer _a, gconstpointer _b)
145 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
146 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
148 if (!a->host || !a->share || !b->host || !b->share)
149 return 1;
150 if (strcmp (a->host, b->host) != 0)
151 return 1;
152 if (strcmp (a->share, b->share) != 0)
153 return 1;
154 return 0;
157 static gint
158 smbfs_auth_cmp_host (gconstpointer _a, gconstpointer _b)
160 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
161 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
163 if (!a->host || !b->host)
164 return 1;
165 if (strcmp (a->host, b->host) != 0)
166 return 1;
167 if (strcmp (a->share, IPC) != 0)
168 return 1;
169 return 0;
172 static void
173 smbfs_auth_add (const char *host, const char *share, const char *domain,
174 const char *user, const char *password)
176 struct smb_authinfo *auth = g_new (struct smb_authinfo, 1);
178 if (!auth)
179 return;
181 /* Don't check for NULL, g_strdup already does. */
182 auth->host = g_strdup (host);
183 auth->share = g_strdup (share);
184 auth->domain = g_strdup (domain);
185 auth->user = g_strdup (user);
186 auth->password = g_strdup (password);
187 auth_list = g_slist_prepend (auth_list, auth);
190 static void
191 smbfs_auth_remove (const char *host, const char *share)
193 struct smb_authinfo data;
194 struct smb_authinfo *auth;
195 GSList *list;
197 data.host = g_strdup (host);
198 data.share = g_strdup (share);
199 list = g_slist_find_custom (auth_list,
200 &data,
201 smbfs_auth_cmp_host_and_share);
202 g_free (data.host);
203 g_free (data.share);
204 if (!list)
205 return;
206 auth = list->data;
207 auth_list = g_slist_remove (auth_list, auth);
208 smbfs_auth_free (auth);
211 /* Set authentication information in bucket. Return 1 if successful, else 0 */
212 /* Information in auth_list overrides user if pass is NULL. */
213 /* bucket->host and bucket->service must be valid. */
214 static int
215 smbfs_bucket_set_authinfo (smbfs_connection *bucket,
216 const char *domain, const char *user, const char *pass,
217 int fallback_to_host)
219 struct smb_authinfo data;
220 struct smb_authinfo *auth;
221 GSList *list;
223 if (domain && user && pass) {
224 g_free (bucket->domain);
225 g_free (bucket->user);
226 g_free (bucket->password);
227 bucket->domain = g_strdup (domain);
228 bucket->user = g_strdup (user);
229 bucket->password = g_strdup (pass);
230 smbfs_auth_remove (bucket->host, bucket->service);
231 smbfs_auth_add (bucket->host, bucket->service,
232 domain, user, pass);
233 return 1;
236 data.host = bucket->host;
237 data.share = bucket->service;
238 list = g_slist_find_custom (auth_list, &data, smbfs_auth_cmp_host_and_share);
239 if (!list && fallback_to_host)
240 list = g_slist_find_custom (auth_list, &data, smbfs_auth_cmp_host);
241 if (list) {
242 auth = list->data;
243 bucket->domain = g_strdup (auth->domain);
244 bucket->user = g_strdup (auth->user);
245 bucket->password = g_strdup (auth->password);
246 return 1;
249 if (got_pass) {
250 bucket->domain = g_strdup (lp_workgroup ());
251 bucket->user = g_strdup (got_user ? username : user);
252 bucket->password = g_strdup (password);
253 return 1;
256 auth = vfs_smb_get_authinfo (bucket->host,
257 bucket->service,
258 (domain ? domain : lp_workgroup ()),
259 user);
260 if (auth) {
261 g_free (bucket->domain);
262 g_free (bucket->user);
263 g_free (bucket->password);
264 bucket->domain = g_strdup (auth->domain);
265 bucket->user = g_strdup (auth->user);
266 bucket->password = g_strdup (auth->password);
267 smbfs_auth_remove (bucket->host, bucket->service);
268 auth_list = g_slist_prepend (auth_list, auth);
269 return 1;
271 return 0;
274 void
275 smbfs_set_debug (int arg)
277 DEBUGLEVEL = arg;
280 void
281 smbfs_set_debugf (const char *filename)
283 extern pstring debugf;
284 extern FILE *dbf;
285 if (DEBUGLEVEL > 0) {
286 FILE *outfile = fopen (filename, "w");
287 if (outfile) {
288 setup_logging ("", True); /* No needs for timestamp for each message */
289 dbf = outfile;
290 setbuf (dbf, NULL);
291 pstrcpy (debugf, filename);
296 /********************** The callbacks ******************************/
297 static int
298 smbfs_init (struct vfs_class * me)
300 const char *servicesf = CONFIGDIR PATH_SEP_STR "smb.conf";
302 /* DEBUGLEVEL = 4; */
304 TimeInit ();
305 charset_initialise ();
307 DEBUG (3, ("smbfs_init(%s)\n", me->name));
309 if (!get_myname (myhostname, NULL))
310 DEBUG (0, ("Failed to get my hostname.\n"));
312 if (!lp_load (servicesf, True, False, False))
313 DEBUG (0, ("Cannot load %s - run testparm to debug it\n", servicesf));
315 codepage_initialise (lp_client_code_page ());
317 load_interfaces ();
319 myumask = umask (0);
320 umask (myumask);
321 myumask = ~myumask;
323 if (getenv ("USER")) {
324 char *p;
326 pstrcpy (username, getenv ("USER"));
327 got_user = TRUE;
328 DEBUG (3, ("smbfs_init(): $USER:%s\n", username));
329 if ((p = strchr (username, '%'))) {
330 *p = 0;
331 pstrcpy (password, p + 1);
332 got_pass = TRUE;
333 memset (strchr (getenv ("USER"), '%') + 1, 'X', strlen (password));
334 DEBUG (3, ("smbfs_init(): $USER%%pass: %s%%%s\n",
335 username, password));
337 strupper (username);
339 if (getenv ("PASSWD")) {
340 pstrcpy (password, getenv ("PASSWD"));
341 got_pass = TRUE;
343 return 1;
346 static void
347 smbfs_fill_names (struct vfs_class *me, fill_names_f func)
349 int i;
350 char *path;
352 (void) me;
354 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
355 if (smbfs_connections [i].cli) {
356 path = g_strconcat (URL_HEADER,
357 smbfs_connections[i].user, "@",
358 smbfs_connections[i].host,
359 "/", smbfs_connections[i].service,
360 NULL);
361 (*func)(path);
362 g_free (path);
367 #define CNV_LANG(s) dos_to_unix(s,False)
368 #define GNAL_VNC(s) unix_to_dos(s,False)
369 /* does same as do_get() in client.c */
370 /* called from vfs.c:1080, count = buffer size */
371 static ssize_t
372 smbfs_read (void *data, char *buffer, int count)
374 smbfs_handle *info = (smbfs_handle *) data;
375 int n;
377 DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%d)\n",
378 info->fnum, (int)info->nread, count));
379 n = cli_read(info->cli, info->fnum, buffer, info->nread, count);
380 if (n > 0)
381 info->nread += n;
382 return n;
385 static ssize_t
386 smbfs_write (void *data, const char *buf, int nbyte)
388 smbfs_handle *info = (smbfs_handle *) data;
389 int n;
391 DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%d)\n",
392 info->fnum, (int)info->nread, nbyte));
393 n = cli_write(info->cli, info->fnum, 0, buf, info->nread, nbyte);
394 if (n > 0)
395 info->nread += n;
396 return n;
399 static int
400 smbfs_close (void *data)
402 smbfs_handle *info = (smbfs_handle *) data;
403 DEBUG (3, ("smbfs_close(fnum:%d)\n", info->fnum));
405 /* FIXME: Why too different cli have the same outbuf
406 * if file is copied to share
408 if (info->cli->outbuf == NULL) {
409 my_errno = EINVAL;
410 return -1;
412 #if 0
413 /* if imlementing archive_level: add rname to smbfs_handle */
414 if (archive_level >= 2 && (inf->attr & aARCH)) {
415 cli_setatr (info->cli, rname, info->attr & ~(uint16) aARCH, 0);
417 #endif
418 return (cli_close (info->cli, info->fnum) == True) ? 0 : -1;
421 static int
422 smbfs_errno (struct vfs_class *me)
424 (void) me;
426 DEBUG(3, ("smbfs_errno: %s\n", g_strerror(my_errno)));
427 return my_errno;
430 typedef struct dir_entry {
431 char *text;
432 struct dir_entry *next;
433 struct stat my_stat;
434 int merrno;
435 } dir_entry;
437 typedef struct {
438 gboolean server_list;
439 char *dirname;
440 char *path; /* the dir originally passed to smbfs_opendir */
441 smbfs_connection *conn;
442 dir_entry *entries;
443 dir_entry *current;
444 } opendir_info;
446 static opendir_info
447 *previous_info,
448 *current_info,
449 *current_share_info,
450 *current_server_info;
452 static gboolean first_direntry;
454 static dir_entry *
455 smbfs_new_dir_entry (const char *name)
457 static int inode_counter;
458 dir_entry *new_entry;
459 new_entry = g_new0 (dir_entry, 1);
460 new_entry->text = dos_to_unix (g_strdup (name), 1);
462 if (first_direntry) {
463 current_info->entries = new_entry;
464 first_direntry = FALSE;
465 } else {
466 current_info->current->next = new_entry;
468 current_info->current = new_entry;
469 new_entry->my_stat.st_ino = inode_counter++;
471 return new_entry;
474 /* browse for shares on server */
475 static void
476 smbfs_browsing_helper (const char *name, uint32 type, const char *comment, void *state)
478 const char *typestr = "";
479 dir_entry *new_entry = smbfs_new_dir_entry (name);
481 (void) state;
483 switch (type) {
484 case STYPE_DISKTREE:
485 typestr = "Disk";
486 /* show this as dir */
487 new_entry->my_stat.st_mode =
488 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
489 S_IXOTH) & myumask;
490 break;
491 case STYPE_PRINTQ:
492 typestr = "Printer";
493 break;
494 case STYPE_DEVICE:
495 typestr = "Device";
496 break;
497 case STYPE_IPC:
498 typestr = "IPC";
499 break;
501 DEBUG (3, ("\t%-15.15s%-10.10s%s\n", name, typestr, comment));
504 static void
505 smbfs_loaddir_helper (file_info * finfo, const char *mask, void *entry)
507 dir_entry *new_entry = (dir_entry *) entry;
508 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
510 (void) mask;
512 #if 0 /* I want to see dot files */
513 if (finfo->mode & aHIDDEN)
514 return; /* don't bother with hidden files, "~$" screws up mc */
515 #endif
516 if (!entry)
517 new_entry = smbfs_new_dir_entry (finfo->name);
519 new_entry->my_stat.st_size = finfo->size;
520 new_entry->my_stat.st_mtime = finfo->mtime;
521 new_entry->my_stat.st_atime = finfo->atime;
522 new_entry->my_stat.st_ctime = finfo->ctime;
523 new_entry->my_stat.st_uid = finfo->uid;
524 new_entry->my_stat.st_gid = finfo->gid;
526 new_entry->my_stat.st_mode = /* rw-rw-rw */
527 S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH;
529 /* if (finfo->mode & aVOLID); nothing similar in real world */
530 if (finfo->mode & aDIR)
531 new_entry->my_stat.st_mode |= /* drwxrwxrwx */
532 S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
533 else
534 new_entry->my_stat.st_mode |= S_IFREG; /* if not dir, regular file? */
535 /* if (finfo->mode & aARCH); DOS archive */
536 /* if (finfo->mode & aHIDDEN); like a dot file? */
537 /* if (finfo->mode & aSYSTEM); like a kernel? */
538 if (finfo->mode & aRONLY)
539 new_entry->my_stat.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
540 new_entry->my_stat.st_mode &= myumask;
542 DEBUG (entry ? 3 : 6, (" %-30s%7.7s%8.0f %s",
543 CNV_LANG (finfo->name),
544 attrib_string (finfo->mode),
545 (double) finfo->size,
546 asctime (LocalTime (&t))));
549 /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
550 static char *
551 smbfs_convert_path (const char *remote_file, gboolean trailing_asterik)
553 const char *p, *my_remote;
554 char *result;
556 my_remote = remote_file;
557 if (strncmp (my_remote, URL_HEADER, HEADER_LEN) == 0) { /* if passed directly */
558 my_remote += HEADER_LEN;
559 if (*my_remote == '/') /* from server browsing */
560 my_remote++;
561 p = strchr(my_remote, '/');
562 if (p)
563 my_remote = p+1; /* advance to end of server name */
566 if (*my_remote == '/')
567 my_remote++; /* strip off leading '/' */
568 p = strchr(my_remote, '/');
569 if (p)
570 my_remote = p; /* strip off share/service name */
571 /* create remote filename as understood by smb clientgen */
572 result = g_strconcat (my_remote, trailing_asterik ? "/*" : "", (char *) NULL);
573 unix_to_dos (result, /* inplace = */ 1); /* code page conversion */
574 str_replace(result, '/', '\\');
575 return result;
578 static void
579 smbfs_srv_browsing_helper (const char *name, uint32 m, const char *comment,
580 void *state)
582 dir_entry *new_entry = smbfs_new_dir_entry (name);
584 (void) m;
585 (void) state;
587 /* show this as dir */
588 new_entry->my_stat.st_mode =
589 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
590 S_IXOTH) & myumask;
592 DEBUG (3, ("\t%-16.16s %s\n", name, comment));
595 static BOOL
596 smbfs_reconnect(smbfs_connection *conn, int *retries)
598 char *host;
599 DEBUG(3, ("RECONNECT\n"));
601 if (*(conn->host) == 0)
602 host = g_strdup(conn->cli->desthost); /* server browsing */
603 else
604 host = g_strdup(conn->host);
606 cli_shutdown(conn->cli);
608 if (!(conn->cli = smbfs_do_connect(host, conn->service))) {
609 message (D_ERROR, MSG_ERROR,
610 _(" reconnect to %s failed\n "), conn->host);
611 g_free(host);
612 return False;
614 g_free(host);
615 if (++(*retries) == 2)
616 return False;
617 return True;
620 static BOOL
621 smbfs_send(struct cli_state *cli)
623 size_t len;
624 size_t nwritten=0;
625 ssize_t ret;
627 len = smb_len(cli->outbuf) + 4;
629 while (nwritten < len) {
630 ret = write_socket(cli->fd, cli->outbuf+nwritten, len - nwritten);
631 if (ret <= 0) {
632 if (errno == EPIPE)
633 return False;
634 } else
635 nwritten += ret;
638 return True;
641 /****************************************************************************
642 See if server has cut us off by checking for EPIPE when writing.
643 Taken from cli_chkpath()
644 ****************************************************************************/
645 static BOOL
646 smbfs_chkpath(struct cli_state *cli, const char *path, BOOL send_only)
648 fstring path2;
649 char *p;
651 fstrcpy(path2,path);
652 unix_to_dos (path2, 1);
653 trim_string(path2,NULL,"\\");
654 if (!*path2) *path2 = '\\';
656 memset(cli->outbuf,'\0',smb_size);
657 set_message(cli->outbuf,0,4 + strlen(path2),True);
658 SCVAL(cli->outbuf,smb_com,SMBchkpth);
659 SSVAL(cli->outbuf,smb_tid,cli->cnum);
661 cli->rap_error = 0;
662 cli->nt_error = 0;
663 SSVAL(cli->outbuf,smb_pid,cli->pid);
664 SSVAL(cli->outbuf,smb_uid,cli->vuid);
665 SSVAL(cli->outbuf,smb_mid,cli->mid);
666 if (cli->protocol > PROTOCOL_CORE) {
667 SCVAL(cli->outbuf,smb_flg,0x8);
668 SSVAL(cli->outbuf,smb_flg2,0x1);
671 p = smb_buf(cli->outbuf);
672 *p++ = 4;
673 fstrcpy(p,path2);
675 if (!smbfs_send(cli)) {
676 DEBUG(3, ("smbfs_chkpath: couldnt send\n"));
677 return False;
679 if (send_only) {
680 client_receive_smb(cli->fd, cli->inbuf, cli->timeout);
681 DEBUG(3, ("smbfs_chkpath: send only OK\n"));
682 return True; /* just testing for EPIPE */
684 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout)) {
685 DEBUG(3, ("smbfs_chkpath: receive error\n"));
686 return False;
688 if ((my_errno = cli_error(cli, NULL, NULL, NULL))) {
689 if (my_errno == 20 || my_errno == 13)
690 return True; /* ignore if 'not a directory' error */
691 DEBUG(3, ("smbfs_chkpath: cli_error: %s\n", g_strerror(my_errno)));
692 return False;
695 return True;
698 #if 1
699 static int
700 smbfs_fs (const char *text)
702 const char *p = text;
703 int count = 0;
705 while ((p = strchr(p, '/')) != NULL) {
706 count++;
707 p++;
709 if (count == 1)
710 return strlen(text);
711 return count;
713 #endif
715 static int
716 smbfs_loaddir (opendir_info *smbfs_info)
718 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
719 int servlen = strlen (smbfs_info->conn->service);
720 const char *info_dirname = smbfs_info->dirname;
721 char *my_dirname;
723 DEBUG (3, ("smbfs_loaddir: dirname:%s\n", info_dirname));
724 first_direntry = TRUE;
726 if (current_info) {
727 DEBUG (3,
728 ("smbfs_loaddir: new:'%s', cached:'%s'\n", info_dirname,
729 current_info->dirname));
730 /* if new desired dir is longer than cached in current_info */
731 if (smbfs_fs (info_dirname) > smbfs_fs (current_info->dirname)) {
732 DEBUG (3, ("saving to previous_info\n"));
733 previous_info = current_info;
737 current_info = smbfs_info;
739 if (strcmp (info_dirname, "/") == 0) {
740 if (!strcmp (smbfs_info->path, URL_HEADER)) {
741 DEBUG (6, ("smbfs_loaddir: browsing %s\n", IPC));
742 /* browse for servers */
743 if (!cli_NetServerEnum
744 (smbfs_info->conn->cli, smbfs_info->conn->domain,
745 SV_TYPE_ALL, smbfs_srv_browsing_helper, NULL))
746 return 0;
747 else
748 current_server_info = smbfs_info;
749 smbfs_info->server_list = TRUE;
750 } else {
751 /* browse for shares */
752 if (cli_RNetShareEnum
753 (smbfs_info->conn->cli, smbfs_browsing_helper, NULL) < 1)
754 return 0;
755 else
756 current_share_info = smbfs_info;
758 goto done;
761 /* do regular directory listing */
762 if (strncmp (smbfs_info->conn->service, info_dirname + 1, servlen) == 0) {
763 /* strip share name from dir */
764 my_dirname = g_strdup (info_dirname + servlen);
765 *my_dirname = '/';
766 my_dirname = free_after(smbfs_convert_path (my_dirname, TRUE), my_dirname);
767 } else
768 my_dirname = smbfs_convert_path (info_dirname, TRUE);
770 DEBUG (6, ("smbfs_loaddir: service: %s\n", smbfs_info->conn->service));
771 DEBUG (6,
772 ("smbfs_loaddir: cli->share: %s\n",
773 smbfs_info->conn->cli->share));
774 DEBUG (6,
775 ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname));
776 /* do file listing: cli_list returns number of files */
777 if (cli_list
778 (smbfs_info->conn->cli, my_dirname, attribute,
779 smbfs_loaddir_helper, NULL) < 0) {
780 /* cli_list returns -1 if directory empty or cannot read socket */
781 my_errno = cli_error (smbfs_info->conn->cli, NULL, &err, NULL);
782 g_free (my_dirname);
783 return 0;
785 if (*(my_dirname) == 0)
786 smbfs_info->dirname = smbfs_info->conn->service;
787 g_free (my_dirname);
788 /* do_dskattr(); */
790 done:
791 /* current_info->parent = smbfs_info->dirname; */
793 smbfs_info->current = smbfs_info->entries;
794 return 1; /* 1 = ok */
797 #ifdef SMBFS_FREE_DIR
798 static void
799 smbfs_free_dir (dir_entry *de)
801 if (!de) return;
803 smbfs_free_dir (de->next);
804 g_free (de->text);
805 g_free (de);
807 #endif
810 /* The readdir routine loads the complete directory */
811 /* It's too slow to ask the server each time */
812 /* It now also sends the complete lstat information for each file */
813 static void *
814 smbfs_readdir(void *info)
816 static union vfs_dirent smbfs_readdir_data;
817 static char *const dirent_dest = smbfs_readdir_data.dent.d_name;
818 opendir_info *smbfs_info = (opendir_info *) info;
820 DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info->dirname));
822 if (!smbfs_info->entries)
823 if (!smbfs_loaddir(smbfs_info))
824 return NULL;
826 if (smbfs_info->current == 0) { /* reached end of dir entries */
827 DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
828 #ifdef SMBFS_FREE_DIR
829 smbfs_free_dir(smbfs_info->entries);
830 smbfs_info->entries = 0;
831 #endif
832 return NULL;
834 g_strlcpy(dirent_dest, smbfs_info->current->text, MC_MAXPATHLEN);
835 smbfs_info->current = smbfs_info->current->next;
837 compute_namelen(&smbfs_readdir_data.dent);
839 return &smbfs_readdir_data;
842 static int
843 smbfs_closedir (void *info)
845 opendir_info *smbfs_info = (opendir_info *) info;
846 /* dir_entry *p, *q; */
848 DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info->dirname));
849 /* CLOSE HERE */
851 /* for (p = smbfs_info->entries; p;){
852 q = p;
853 p = p->next;
854 g_free (q->text);
855 g_free (q);
857 g_free (info); */
858 return 0;
861 static int
862 smbfs_chmod (struct vfs_class *me, const char *path, int mode)
864 (void) me;
866 DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path, mode));
867 /* my_errno = EOPNOTSUPP;
868 return -1; */ /* cannot chmod on smb filesystem */
869 return 0; /* make mc happy */
872 static int
873 smbfs_chown (struct vfs_class *me, const char *path, int owner, int group)
875 (void) me;
877 DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path, owner, group));
878 my_errno = EOPNOTSUPP; /* ready for your labotomy? */
879 return -1;
882 static int
883 smbfs_utime (struct vfs_class *me, const char *path, struct utimbuf *times)
885 (void) me;
886 (void) times;
888 DEBUG(3, ("smbfs_utime(path:%s)\n", path));
889 my_errno = EOPNOTSUPP;
890 return -1;
893 static int
894 smbfs_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
896 (void) me;
898 DEBUG (3,
899 ("smbfs_readlink(path:%s, buf:%s, size:%d)\n", path, buf,
900 (int) size));
901 my_errno = EOPNOTSUPP;
902 return -1; /* no symlinks on smb filesystem? */
905 static int
906 smbfs_symlink (struct vfs_class *me, const char *n1, const char *n2)
908 (void) me;
910 DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1, n2));
911 my_errno = EOPNOTSUPP;
912 return -1; /* no symlinks on smb filesystem? */
915 /* Extract the hostname and username from the path */
916 /* path is in the form: [user@]hostname/share/remote-dir */
917 #define smbfs_get_host_and_username(path, host, user, port, pass) \
918 vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
920 /*****************************************************
921 return a connection to a SMB server
922 current_bucket needs to be set before calling
923 *******************************************************/
924 static struct cli_state *
925 smbfs_do_connect (const char *server, char *share)
927 struct cli_state *c;
928 struct nmb_name called, calling;
929 struct in_addr ip;
931 DEBUG(3, ("smbfs_do_connect(%s, %s)\n", server, share));
932 if (*share == '\\') {
933 server = share+2;
934 share = strchr(server,'\\');
935 if (!share) return NULL;
936 *share = 0;
937 share++;
940 make_nmb_name(&calling, global_myname, 0x0);
941 make_nmb_name(&called , server, current_bucket->name_type);
943 for (;;) {
945 ip = (current_bucket->have_ip) ? current_bucket->dest_ip : ipzero;
947 /* have to open a new connection */
948 if (!(c = cli_initialise(NULL))) {
949 my_errno = ENOMEM;
950 return NULL;
953 pwd_init(&(c->pwd)); /* should be moved into cli_initialise()? */
954 pwd_set_cleartext(&(c->pwd), current_bucket->password);
956 if ((cli_set_port(c, current_bucket->port) == 0) ||
957 !cli_connect(c, server, &ip)) {
958 DEBUG(1, ("Connection to %s failed\n", server));
959 break;
962 if (!cli_session_request(c, &calling, &called)) {
963 my_errno = cli_error(c, NULL, &err, NULL);
964 DEBUG(1, ("session request to %s failed\n", called.name));
965 cli_shutdown(c);
966 if (strcmp(called.name, "*SMBSERVER")) {
967 make_nmb_name(&called , "*SMBSERVER", 0x20);
968 continue;
970 return NULL;
973 DEBUG(3, (" session request ok\n"));
975 if (!cli_negprot(c)) {
976 DEBUG(1, ("protocol negotiation failed\n"));
977 break;
980 if (!cli_session_setup(c, current_bucket->user,
981 current_bucket->password, strlen(current_bucket->password),
982 current_bucket->password, strlen(current_bucket->password),
983 current_bucket->domain)) {
984 DEBUG(1,("session setup failed: %s\n", cli_errstr(c)));
985 smbfs_auth_remove (server, share);
986 break;
989 if (*c->server_domain || *c->server_os || *c->server_type)
990 DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
991 c->server_domain,c->server_os,c->server_type));
993 DEBUG(3, (" session setup ok\n"));
995 if (!cli_send_tconX(c, share, "?????",
996 current_bucket->password, strlen(current_bucket->password)+1)) {
997 DEBUG(1,("%s: tree connect failed: %s\n", share, cli_errstr(c)));
998 break;
1001 DEBUG(3, (" tconx ok\n"));
1003 my_errno = 0;
1004 return c;
1007 my_errno = cli_error(c, NULL, &err, NULL);
1008 cli_shutdown(c);
1009 return NULL;
1013 static int
1014 smbfs_get_master_browser(char **host)
1016 static char so_broadcast[] = "SO_BROADCAST";
1017 int count;
1018 struct in_addr *ip_list, bcast_addr;
1020 /* does port = 137 for win95 master browser? */
1021 int fd= open_socket_in( SOCK_DGRAM, 0, 3,
1022 interpret_addr(lp_socket_address()), True );
1023 if (fd == -1)
1024 return 0;
1025 set_socket_options(fd, so_broadcast);
1026 ip_list = iface_bcast(ipzero);
1027 bcast_addr = *ip_list;
1028 if ((ip_list = name_query(fd, "\01\02__MSBROWSE__\02", 1, True,
1029 True, bcast_addr, &count, NULL))) {
1030 if (!count)
1031 return 0;
1032 /* just return first master browser */
1033 *host = g_strdup(inet_ntoa(ip_list[0]));
1034 return 1;
1036 return 0;
1039 static void
1040 smbfs_free_bucket (smbfs_connection *bucket)
1042 g_free (bucket->host);
1043 g_free (bucket->service);
1044 g_free (bucket->domain);
1045 g_free (bucket->user);
1046 wipe_password (bucket->password);
1047 g_free (bucket->home);
1048 memset (bucket, 0, sizeof (smbfs_connection));
1051 static smbfs_connection *
1052 smbfs_get_free_bucket (void)
1054 int i;
1056 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++)
1057 if (!smbfs_connections [i].cli) return &smbfs_connections [i];
1059 { /* search for most dormant connection */
1060 int oldest = 0; /* index */
1061 time_t oldest_time = smbfs_connections[0].last_use;
1062 for (i = 1; i < SMBFS_MAX_CONNECTIONS; i++) {
1063 if (smbfs_connections[i].last_use < oldest_time) {
1064 oldest_time = smbfs_connections[i].last_use;
1065 oldest = i;
1068 cli_shutdown(smbfs_connections[oldest].cli);
1069 smbfs_free_bucket (&smbfs_connections[oldest]);
1070 return &smbfs_connections[oldest];
1073 /* This can't happend, since we have checked for max connections before */
1074 vfs_die("Internal error: smbfs_get_free_bucket");
1075 return 0; /* shut up, stupid gcc */
1078 /* This routine keeps track of open connections */
1079 /* Returns a connected socket to host */
1080 static smbfs_connection *
1081 smbfs_open_link (char *host, char *path, const char *user, int *port,
1082 char *this_pass)
1084 int i;
1085 smbfs_connection *bucket;
1086 pstring service;
1087 struct in_addr *dest_ip = NULL;
1089 DEBUG (3, ("smbfs_open_link(host:%s, path:%s)\n", host, path));
1091 if (strcmp (host, path) == 0) /* if host & path are same: */
1092 pstrcpy (service, IPC); /* setup for browse */
1093 else { /* get share name from path, path starts with server name */
1094 char *p;
1095 if ((p = strchr (path, '/'))) /* get share aka */
1096 pstrcpy (service, ++p); /* service name from path */
1097 else
1098 pstrcpy (service, "");
1099 /* now check for trailing directory/filenames */
1100 p = strchr (service, '/');
1101 if (p)
1102 *p = 0; /* cut off dir/files: sharename only */
1103 if (!*service)
1104 pstrcpy (service, IPC); /* setup for browse */
1105 DEBUG (6, ("smbfs_open_link: service from path:%s\n", service));
1108 if (got_user)
1109 user = username; /* global from getenv */
1111 /* Is the link actually open? */
1112 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1113 if (!smbfs_connections[i].cli)
1114 continue;
1115 if ((strcmp (host, smbfs_connections[i].host) == 0) &&
1116 (strcmp (user, smbfs_connections[i].user) == 0) &&
1117 (strcmp (service, smbfs_connections[i].service) == 0)) {
1118 int retries = 0;
1119 BOOL inshare = (*host != 0 && *path != 0 && strchr (path, '/'));
1120 /* check if this connection has died */
1121 while (!smbfs_chkpath (smbfs_connections[i].cli, "\\", !inshare)) {
1122 if (!smbfs_reconnect (&smbfs_connections[i], &retries))
1123 return 0;
1125 DEBUG (6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i));
1126 current_bucket = &smbfs_connections[i];
1127 smbfs_connections[i].last_use = time (NULL);
1128 return &smbfs_connections[i];
1130 /* connection not found, find if we have ip for new connection */
1131 if (strcmp (host, smbfs_connections[i].host) == 0)
1132 dest_ip = &smbfs_connections[i].cli->dest_ip;
1135 /* make new connection */
1136 bucket = smbfs_get_free_bucket ();
1137 bucket->name_type = 0x20;
1138 bucket->home = 0;
1139 bucket->port = *port;
1140 bucket->have_ip = False;
1141 if (dest_ip) {
1142 bucket->have_ip = True;
1143 bucket->dest_ip = *dest_ip;
1145 current_bucket = bucket;
1147 bucket->user = g_strdup (user);
1148 bucket->service = g_strdup (service);
1150 if (!(*host)) { /* if blank host name, browse for servers */
1151 if (!smbfs_get_master_browser (&host)) /* set host to ip of master browser */
1152 return 0; /* could not find master browser? */
1153 g_free (host);
1154 bucket->host = g_strdup (""); /* blank host means master browser */
1155 } else
1156 bucket->host = g_strdup (host);
1158 if (!smbfs_bucket_set_authinfo (bucket, 0, /* domain currently not used */
1159 user, this_pass, 1))
1160 return 0;
1162 /* connect to share */
1163 while (!(bucket->cli = smbfs_do_connect (host, service))) {
1165 if (my_errno != EPERM)
1166 return 0;
1167 message (D_ERROR, MSG_ERROR, _(" Authentication failed "));
1169 /* authentication failed, try again */
1170 smbfs_auth_remove (bucket->host, bucket->service);
1171 if (!smbfs_bucket_set_authinfo (bucket, bucket->domain, bucket->user, 0, 0))
1172 return 0;
1176 smbfs_open_connections++;
1177 DEBUG (3, ("smbfs_open_link:smbfs_open_connections: %d\n",
1178 smbfs_open_connections));
1179 return bucket;
1182 static char *
1183 smbfs_get_path (smbfs_connection ** sc, const char *path)
1185 char *user, *host, *remote_path, *pass;
1186 int port = SMB_PORT;
1188 DEBUG (3, ("smbfs_get_path(%s)\n", path));
1189 if (strncmp (path, URL_HEADER, HEADER_LEN))
1190 return NULL;
1191 path += HEADER_LEN;
1193 if (*path == '/') /* '/' leading server name */
1194 path++; /* probably came from server browsing */
1196 if ((remote_path =
1197 smbfs_get_host_and_username (&path, &host, &user, &port, &pass)))
1198 if ((*sc =
1199 smbfs_open_link (host, remote_path, user, &port, pass)) == NULL) {
1200 g_free (remote_path);
1201 remote_path = NULL;
1203 g_free (host);
1204 g_free (user);
1205 if (pass)
1206 wipe_password (pass);
1208 if (!remote_path)
1209 return NULL;
1211 /* NOTE: tildes are deprecated. See ftpfs.c */
1213 int f = !strcmp (remote_path, "/~");
1214 if (f || !strncmp (remote_path, "/~/", 3)) {
1215 char *s;
1216 s = concat_dir_and_file ((*sc)->home, remote_path + 3 - f);
1217 g_free (remote_path);
1218 return s;
1221 return remote_path;
1224 #if 0
1225 static int
1226 is_error (int result, int errno_num)
1228 if (!(result == -1))
1229 return my_errno = 0;
1230 else
1231 my_errno = errno_num;
1232 return 1;
1234 #endif
1236 static void *
1237 smbfs_opendir (struct vfs_class *me, const char *dirname)
1239 opendir_info *smbfs_info;
1240 smbfs_connection *sc;
1241 char *remote_dir;
1243 (void) me;
1245 DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname));
1247 if (!(remote_dir = smbfs_get_path (&sc, dirname)))
1248 return NULL;
1250 /* FIXME: where freed? */
1251 smbfs_info = g_new (opendir_info, 1);
1252 smbfs_info->server_list = FALSE;
1253 smbfs_info->path = g_strdup(dirname); /* keep original */
1254 smbfs_info->dirname = remote_dir;
1255 smbfs_info->conn = sc;
1256 smbfs_info->entries = 0;
1257 smbfs_info->current = 0;
1259 return smbfs_info;
1262 static int
1263 smbfs_fake_server_stat (const char *server_url, const char *path, struct stat *buf)
1265 dir_entry *dentry;
1266 const char *p;
1268 (void) server_url;
1270 if ((p = strrchr (path, '/')))
1271 path = p + 1; /* advance until last '/' */
1273 if (!current_info->entries) {
1274 if (!smbfs_loaddir (current_info)) /* browse host */
1275 return -1;
1278 if (current_info->server_list == True) {
1279 dentry = current_info->entries;
1280 DEBUG (4, ("fake stat for SERVER \"%s\"\n", path));
1281 while (dentry) {
1282 if (strcmp (dentry->text, path) == 0) {
1283 DEBUG (4, ("smbfs_fake_server_stat: %s:%4o\n",
1284 dentry->text, (int)dentry->my_stat.st_mode));
1285 memcpy (buf, &dentry->my_stat, sizeof (struct stat));
1286 return 0;
1288 dentry = dentry->next;
1291 my_errno = ENOENT;
1292 return -1;
1295 static int
1296 smbfs_fake_share_stat (const char *server_url, const char *path, struct stat *buf)
1298 dir_entry *dentry;
1299 if (strlen (path) < strlen (server_url))
1300 return -1;
1302 if (!current_share_info) { /* Server was not stat()ed */
1303 /* Make sure there is such share at server */
1304 smbfs_connection *sc;
1305 char *p;
1306 p = smbfs_get_path (&sc, path);
1307 g_free (p);
1308 if (p) {
1309 memset (buf, 0, sizeof (*buf));
1310 /* show this as dir */
1311 buf->st_mode =
1312 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
1313 S_IXOTH) & myumask;
1314 return 0;
1316 return -1;
1319 path += strlen (server_url); /* we only want share name */
1320 path++;
1322 if (*path == '/') /* '/' leading server name */
1323 path++; /* probably came from server browsing */
1325 if (!current_share_info->entries) {
1326 if (!smbfs_loaddir (current_share_info)) /* browse host */
1327 return -1;
1329 dentry = current_share_info->entries;
1330 DEBUG (3, ("smbfs_fake_share_stat: %s on %s\n", path, server_url));
1331 while (dentry) {
1332 if (strcmp (dentry->text, path) == 0) {
1333 DEBUG (6, ("smbfs_fake_share_stat: %s:%4o\n",
1334 dentry->text, (int) dentry->my_stat.st_mode));
1335 memcpy (buf, &dentry->my_stat, sizeof (struct stat));
1336 return 0;
1338 dentry = dentry->next;
1340 my_errno = ENOENT;
1341 return -1;
1344 /* stat a single file, smbfs_get_remote_stat callback */
1345 static dir_entry *single_entry;
1347 /* stat a single file */
1348 static int
1349 smbfs_get_remote_stat (smbfs_connection * sc, const char *path, struct stat *buf)
1351 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
1352 char *mypath;
1354 DEBUG (3, ("smbfs_get_remote_stat(): mypath:%s\n", path));
1356 mypath = smbfs_convert_path (path, FALSE);
1358 #if 0 /* single_entry is never free()d now. And only my_stat is used */
1359 single_entry = g_new (dir_entry, 1);
1361 single_entry->text = dos_to_unix (g_strdup (finfo->name), 1);
1363 single_entry->next = 0;
1364 #endif
1365 if (!single_entry)
1366 single_entry = g_new0 (dir_entry, 1);
1368 if (cli_list
1369 (sc->cli, mypath, attribute, smbfs_loaddir_helper, single_entry) < 1) {
1370 my_errno = ENOENT;
1371 g_free (mypath);
1372 return -1; /* cli_list returns number of files */
1375 memcpy (buf, &single_entry->my_stat, sizeof (struct stat));
1377 /* don't free here, use for smbfs_fstat() */
1378 /* g_free(single_entry->text);
1379 g_free(single_entry); */
1380 g_free (mypath);
1381 return 0;
1384 static int
1385 smbfs_search_dir_entry (dir_entry *dentry, const char *text, struct stat *buf)
1387 while (dentry) {
1388 if (strcmp(text, dentry->text) == 0) {
1389 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1390 memcpy(&single_entry->my_stat, &dentry->my_stat,
1391 sizeof(struct stat));
1392 return 0;
1394 dentry = dentry->next;
1396 return -1;
1399 static int
1400 smbfs_get_stat_info (smbfs_connection * sc, const char *path, struct stat *buf)
1402 char *p;
1403 #if 0
1404 dir_entry *dentry = current_info->entries;
1405 #endif
1406 const char *mypath = path;
1408 mypath++; /* cut off leading '/' */
1409 if ((p = strrchr (mypath, '/')))
1410 mypath = p + 1; /* advance until last file/dir name */
1411 DEBUG (3, ("smbfs_get_stat_info: mypath:%s, current_info->dirname:%s\n",
1412 mypath, current_info->dirname));
1413 #if 0
1414 if (!dentry) {
1415 DEBUG (1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n",
1416 current_info->dirname, path));
1417 return -1;
1419 #endif
1420 if (!single_entry) /* when found, this will be written too */
1421 single_entry = g_new (dir_entry, 1);
1422 if (smbfs_search_dir_entry (current_info->entries, mypath, buf) == 0) {
1423 return 0;
1425 /* now try to identify mypath as PARENT dir */
1427 char *mdp;
1428 char *mydir;
1429 mdp = mydir = g_strdup (current_info->dirname);
1430 if ((p = strrchr (mydir, '/')))
1431 *p = 0; /* advance util last '/' */
1432 if ((p = strrchr (mydir, '/')))
1433 mydir = p + 1; /* advance util last '/' */
1434 if (strcmp (mydir, mypath) == 0) { /* fake a stat for ".." */
1435 memset (buf, 0, sizeof (struct stat));
1436 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1437 memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
1438 g_free (mdp);
1439 DEBUG (1, (" PARENT:found in %s\n", current_info->dirname));
1440 return 0;
1442 g_free (mdp);
1444 /* now try to identify as CURRENT dir? */
1446 char *dnp = current_info->dirname;
1447 DEBUG (6, ("smbfs_get_stat_info: is %s current dir? this dir is: %s\n",
1448 mypath, current_info->dirname));
1449 if (*dnp == '/')
1450 dnp++;
1451 else {
1452 return -1;
1454 if (strcmp (mypath, dnp) == 0) {
1455 memset (buf, 0, sizeof (struct stat));
1456 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1457 memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
1458 DEBUG (1, (" CURRENT:found in %s\n", current_info->dirname));
1459 return 0;
1462 DEBUG (3, ("'%s' not found in current_info '%s'\n", path,
1463 current_info->dirname));
1464 /* try to find this in the PREVIOUS listing */
1465 if (previous_info) {
1466 if (smbfs_search_dir_entry (previous_info->entries, mypath, buf) == 0)
1467 return 0;
1468 DEBUG (3, ("'%s' not found in previous_info '%s'\n", path,
1469 previous_info->dirname));
1471 /* try to find this in the SHARE listing */
1472 if (current_share_info) {
1473 if (smbfs_search_dir_entry (current_share_info->entries, mypath, buf) == 0)
1474 return 0;
1475 DEBUG (3, ("'%s' not found in share_info '%s'\n", path,
1476 current_share_info->dirname));
1478 /* try to find this in the SERVER listing */
1479 if (current_server_info) {
1480 if (smbfs_search_dir_entry (current_server_info->entries, mypath, buf) == 0)
1481 return 0;
1482 DEBUG (3, ("'%s' not found in server_info '%s'\n", path,
1483 current_server_info->dirname));
1485 /* nothing found. get stat file info from server */
1486 return smbfs_get_remote_stat (sc, path, buf);
1489 static int
1490 smbfs_chdir (struct vfs_class *me, const char *path)
1492 char *remote_dir;
1493 smbfs_connection *sc;
1495 (void) me;
1497 DEBUG (3, ("smbfs_chdir(path:%s)\n", path));
1498 if (!(remote_dir = smbfs_get_path (&sc, path)))
1499 return -1;
1500 g_free (remote_dir);
1502 return 0;
1505 static int
1506 smbfs_loaddir_by_name (struct vfs_class *me, const char *path)
1508 void *info;
1509 char *mypath, *p;
1511 mypath = g_strdup(path);
1512 p = strrchr(mypath, '/');
1514 if (p > mypath)
1515 *p = 0;
1516 DEBUG(6, ("smbfs_loaddir_by_name(%s)\n", mypath));
1517 smbfs_chdir(me, mypath);
1518 info = smbfs_opendir (me, mypath);
1519 g_free(mypath);
1520 if (!info)
1521 return -1;
1522 smbfs_readdir(info);
1523 smbfs_loaddir(info);
1524 return 0;
1527 static int
1528 smbfs_stat (struct vfs_class * me, const char *path, struct stat *buf)
1530 smbfs_connection *sc;
1531 pstring server_url;
1532 char *service, *pp, *at;
1533 const char *p;
1535 DEBUG (3, ("smbfs_stat(path:%s)\n", path));
1537 if (!current_info) {
1538 DEBUG (1, ("current_info = NULL: "));
1539 if (smbfs_loaddir_by_name (me, path) < 0)
1540 return -1;
1543 /* check if stating server */
1544 p = path;
1545 if (strncmp (p, URL_HEADER, HEADER_LEN)) {
1546 DEBUG (1, ("'%s' doesnt start with '%s' (length %d)\n",
1547 p, URL_HEADER, HEADER_LEN));
1548 return -1;
1551 p += HEADER_LEN;
1552 if (*p == '/')
1553 p++;
1555 pp = strchr (p, '/'); /* advance past next '/' */
1556 at = strchr (p, '@');
1557 pstrcpy (server_url, URL_HEADER);
1558 if (at && at < pp) { /* user@server */
1559 char *z = &(server_url[sizeof (server_url) - 1]);
1560 const char *s = p;
1562 at = &(server_url [HEADER_LEN]) + (at - p + 1);
1563 if (z > at)
1564 z = at;
1565 at = &(server_url [HEADER_LEN]);
1566 while (at < z)
1567 *at++ = *s++;
1568 *z = 0;
1570 pstrcat (server_url, current_bucket->host);
1572 if (!pp) {
1573 if (!current_info->server_list) {
1574 if (smbfs_loaddir_by_name (me, path) < 0)
1575 return -1;
1577 return smbfs_fake_server_stat (server_url, path, buf);
1580 if (!strchr (++pp, '/')) {
1581 return smbfs_fake_share_stat (server_url, path, buf);
1584 /* stating inside share at this point */
1585 if (!(service = smbfs_get_path (&sc, path))) /* connects if necessary */
1586 return -1;
1588 int hostlen = strlen (current_bucket->host);
1589 char *ppp = service + strlen (service) - hostlen;
1590 char *sp = server_url + strlen (server_url) - hostlen;
1592 if (strcmp (sp, ppp) == 0) {
1593 /* make server name appear as directory */
1594 DEBUG (1, ("smbfs_stat: showing server as directory\n"));
1595 memset (buf, 0, sizeof (struct stat));
1596 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1597 g_free (service);
1598 return 0;
1601 /* check if current_info is in share requested */
1602 p = service;
1603 pp = strchr (p, '/');
1604 if (pp) {
1605 p = ++pp; /* advance past server name */
1606 pp = strchr (p, '/');
1608 if (pp)
1609 *pp = 0; /* cut off everthing after service name */
1610 else
1611 p = IPC; /* browsing for services */
1612 pp = current_info->dirname;
1613 if (*pp == '/')
1614 pp++;
1615 if (strncmp (p, pp, strlen (p)) != 0) {
1616 DEBUG (6, ("desired '%s' is not loaded, we have '%s'\n", p, pp));
1617 if (smbfs_loaddir_by_name (me, path) < 0) {
1618 g_free (service);
1619 return -1;
1621 DEBUG (6, ("loaded dir: '%s'\n", current_info->dirname));
1623 g_free (service);
1624 /* stat dirs & files under shares now */
1625 return smbfs_get_stat_info (sc, path, buf);
1628 #define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */
1630 static off_t
1631 smbfs_lseek (void *data, off_t offset, int whence)
1633 smbfs_handle *info = (smbfs_handle *) data;
1634 size_t size;
1636 DEBUG (3,
1637 ("smbfs_lseek(info->nread => %d, offset => %d, whence => %d) \n",
1638 (int) info->nread, (int) offset, whence));
1640 switch (whence) {
1641 case SEEK_SET:
1642 info->nread = offset;
1643 break;
1644 case SEEK_CUR:
1645 info->nread += offset;
1646 break;
1647 case SEEK_END:
1648 if (!cli_qfileinfo (info->cli, info->fnum,
1649 NULL, &size, NULL, NULL, NULL,
1650 NULL, NULL) &&
1651 !cli_getattrE (info->cli, info->fnum,
1652 NULL, &size, NULL, NULL, NULL)) {
1653 errno = EINVAL;
1654 return -1;
1656 info->nread = size + offset;
1657 break;
1660 return info->nread;
1663 static int
1664 smbfs_mknod (struct vfs_class *me, const char *path, int mode, int dev)
1666 (void) me;
1668 DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%d)\n", path, mode, dev));
1669 my_errno = EOPNOTSUPP;
1670 return -1;
1673 static int
1674 smbfs_mkdir (struct vfs_class * me, const char *path, mode_t mode)
1676 smbfs_connection *sc;
1677 char *remote_file;
1678 char *cpath;
1680 (void) me;
1682 DEBUG (3, ("smbfs_mkdir(path:%s, mode:%d)\n", path, (int) mode));
1683 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1684 return -1;
1685 g_free (remote_file);
1686 cpath = smbfs_convert_path (path, FALSE);
1688 if (!cli_mkdir (sc->cli, cpath)) {
1689 my_errno = cli_error (sc->cli, NULL, &err, NULL);
1690 message (D_ERROR, MSG_ERROR, _(" Error %s creating directory %s "),
1691 cli_errstr (sc->cli), CNV_LANG (cpath));
1692 g_free (cpath);
1693 return -1;
1695 g_free (cpath);
1696 return 0;
1699 static int
1700 smbfs_rmdir (struct vfs_class *me, const char *path)
1702 smbfs_connection *sc;
1703 char *remote_file;
1704 char *cpath;
1706 (void) me;
1708 DEBUG(3, ("smbfs_rmdir(path:%s)\n", path));
1709 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1710 return -1;
1711 g_free (remote_file);
1712 cpath = smbfs_convert_path (path, FALSE);
1714 if (!cli_rmdir(sc->cli, cpath)) {
1715 my_errno = cli_error(sc->cli, NULL, &err, NULL);
1716 message (D_ERROR, MSG_ERROR, _(" Error %s removing directory %s "),
1717 cli_errstr(sc->cli), CNV_LANG(cpath));
1718 g_free (cpath);
1719 return -1;
1722 g_free (cpath);
1723 return 0;
1726 static int
1727 smbfs_link (struct vfs_class *me, const char *p1, const char *p2)
1729 (void) me;
1731 DEBUG (3, ("smbfs_link(p1:%s, p2:%s)\n", p1, p2));
1732 my_errno = EOPNOTSUPP;
1733 return -1;
1736 static void
1737 smbfs_free (vfsid id)
1739 DEBUG (3, ("smbfs_free(%p)\n", id));
1740 smbfs_auth_free_all ();
1743 /* Gives up on a socket and reopens the connection, the child own the socket
1744 * now
1746 static void
1747 smbfs_forget (const char *path)
1749 char *host, *user, *p;
1750 int port, i;
1752 if (strncmp (path, URL_HEADER, HEADER_LEN))
1753 return;
1755 DEBUG (3, ("smbfs_forget(path:%s)\n", path));
1757 path += 6;
1758 if (path[0] == '/' && path[1] == '/')
1759 path += 2;
1761 if ((p = smbfs_get_host_and_username (&path, &host, &user, &port, NULL))) {
1762 g_free (p);
1763 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1764 if (smbfs_connections[i].cli
1765 && (strcmp (host, smbfs_connections[i].host) == 0)
1766 && (strcmp (user, smbfs_connections[i].user) == 0)
1767 && (port == smbfs_connections[i].port)) {
1769 /* close socket: the child owns it now */
1770 cli_shutdown (smbfs_connections[i].cli);
1772 /* reopen the connection */
1773 smbfs_connections[i].cli =
1774 smbfs_do_connect (host, smbfs_connections[i].service);
1778 g_free (host);
1779 g_free (user);
1782 static int
1783 smbfs_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg)
1785 (void) me;
1786 (void) arg;
1788 DEBUG (3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path, ctlop));
1789 switch (ctlop) {
1790 case VFS_SETCTL_FORGET:
1791 smbfs_forget (path);
1792 return 0;
1794 return 0;
1798 static smbfs_handle *
1799 smbfs_open_readwrite (smbfs_handle *remote_handle, char *rname, int flags, int mode)
1801 size_t size;
1803 (void) mode;
1805 if (flags & O_TRUNC) /* if it exists truncate to zero */
1806 DEBUG (3, ("smbfs_open: O_TRUNC\n"));
1808 remote_handle->fnum =
1809 #if 1 /* Don't play with flags, it is cli_open() headache */
1810 cli_open (remote_handle->cli, rname, flags, DENY_NONE);
1811 #else /* What's a reasons to has this code ? */
1812 cli_open (remote_handle->cli, rname, ((flags & O_CREAT)
1813 || (flags ==
1814 (O_WRONLY | O_APPEND))) ?
1815 flags : O_RDONLY, DENY_NONE);
1816 #endif
1817 if (remote_handle->fnum == -1) {
1818 message (D_ERROR, MSG_ERROR, _(" %s opening remote file %s "),
1819 cli_errstr (remote_handle->cli), CNV_LANG (rname));
1820 DEBUG (1, ("smbfs_open(rname:%s) error:%s\n",
1821 rname, cli_errstr (remote_handle->cli)));
1822 my_errno = cli_error (remote_handle->cli, NULL, &err, NULL);
1823 return NULL;
1826 if (flags & O_CREAT)
1827 return remote_handle;
1829 if (!cli_qfileinfo (remote_handle->cli, remote_handle->fnum,
1830 &remote_handle->attr, &size, NULL, NULL, NULL, NULL,
1831 NULL)
1832 && !cli_getattrE (remote_handle->cli, remote_handle->fnum,
1833 &remote_handle->attr, &size, NULL, NULL, NULL)) {
1834 message (D_ERROR, MSG_ERROR, " getattrib: %s ",
1835 cli_errstr (remote_handle->cli));
1836 DEBUG (1,
1837 ("smbfs_open(rname:%s) getattrib:%s\n", rname,
1838 cli_errstr (remote_handle->cli)));
1839 my_errno = cli_error (remote_handle->cli, NULL, &err, NULL);
1840 cli_close (remote_handle->cli, remote_handle->fnum);
1841 return NULL;
1844 if ((flags == (O_WRONLY | O_APPEND)) /* file.c:copy_file_file() -> do_append */
1845 && smbfs_lseek (remote_handle, 0, SEEK_END) == -1) {
1846 cli_close (remote_handle->cli, remote_handle->fnum);
1847 return NULL;
1850 return remote_handle;
1853 static void *
1854 smbfs_open (struct vfs_class *me, const char *file, int flags, int mode)
1856 char *remote_file;
1857 void *ret;
1858 smbfs_connection *sc;
1859 smbfs_handle *remote_handle;
1861 (void) me;
1863 DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%o)\n", file, flags, mode));
1865 if (!(remote_file = smbfs_get_path (&sc, file)))
1866 return 0;
1868 remote_file = free_after(smbfs_convert_path (remote_file, FALSE), remote_file);
1870 remote_handle = g_new (smbfs_handle, 2);
1871 remote_handle->cli = sc->cli;
1872 remote_handle->nread = 0;
1874 ret = smbfs_open_readwrite (remote_handle, remote_file, flags, mode);
1876 g_free (remote_file);
1877 if (!ret)
1878 g_free (remote_handle);
1880 return ret;
1883 static int
1884 smbfs_unlink (struct vfs_class *me, const char *path)
1886 smbfs_connection *sc;
1887 char *remote_file;
1889 (void) me;
1891 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1892 return -1;
1894 remote_file = free_after(smbfs_convert_path (remote_file, FALSE), remote_file);
1896 if (!cli_unlink(sc->cli, remote_file)) {
1897 message (D_ERROR, MSG_ERROR, _(" %s removing remote file %s "),
1898 cli_errstr(sc->cli), CNV_LANG(remote_file));
1899 g_free (remote_file);
1900 return -1;
1902 g_free (remote_file);
1903 return 0;
1906 static int
1907 smbfs_rename (struct vfs_class *me, const char *a, const char *b)
1909 smbfs_connection *sc;
1910 char *ra, *rb;
1911 int retval;
1913 (void) me;
1915 if ((ra = smbfs_get_path (&sc, a)) == 0)
1916 return -1;
1918 if ((rb = smbfs_get_path (&sc, b)) == 0) {
1919 g_free (ra);
1920 return -1;
1923 ra = free_after (smbfs_convert_path (ra, FALSE), ra);
1924 rb = free_after (smbfs_convert_path (rb, FALSE), rb);
1926 retval = cli_rename(sc->cli, ra, rb);
1928 g_free (ra);
1929 g_free (rb);
1931 if (!retval) {
1932 message (D_ERROR, MSG_ERROR, _(" %s renaming files\n"),
1933 cli_errstr(sc->cli));
1934 return -1;
1936 return 0;
1939 static int
1940 smbfs_fstat (void *data, struct stat *buf)
1942 smbfs_handle *remote_handle = (smbfs_handle *)data;
1944 DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle->fnum));
1946 /* use left over from previous smbfs_get_remote_stat, if available */
1947 if (single_entry)
1948 memcpy(buf, &single_entry->my_stat, sizeof(struct stat));
1949 else { /* single_entry not set up: bug */
1950 my_errno = EFAULT;
1951 return -EFAULT;
1953 return 0;
1956 void
1957 init_smbfs (void)
1959 vfs_smbfs_ops.name = "smbfs";
1960 vfs_smbfs_ops.prefix = "smb:";
1961 vfs_smbfs_ops.flags = VFSF_NOLINKS;
1962 vfs_smbfs_ops.init = smbfs_init;
1963 vfs_smbfs_ops.fill_names = smbfs_fill_names;
1964 vfs_smbfs_ops.open = smbfs_open;
1965 vfs_smbfs_ops.close = smbfs_close;
1966 vfs_smbfs_ops.read = smbfs_read;
1967 vfs_smbfs_ops.write = smbfs_write;
1968 vfs_smbfs_ops.opendir = smbfs_opendir;
1969 vfs_smbfs_ops.readdir = smbfs_readdir;
1970 vfs_smbfs_ops.closedir = smbfs_closedir;
1971 vfs_smbfs_ops.stat = smbfs_stat;
1972 vfs_smbfs_ops.lstat = smbfs_lstat;
1973 vfs_smbfs_ops.fstat = smbfs_fstat;
1974 vfs_smbfs_ops.chmod = smbfs_chmod;
1975 vfs_smbfs_ops.chown = smbfs_chown;
1976 vfs_smbfs_ops.utime = smbfs_utime;
1977 vfs_smbfs_ops.readlink = smbfs_readlink;
1978 vfs_smbfs_ops.symlink = smbfs_symlink;
1979 vfs_smbfs_ops.link = smbfs_link;
1980 vfs_smbfs_ops.unlink = smbfs_unlink;
1981 vfs_smbfs_ops.rename = smbfs_rename;
1982 vfs_smbfs_ops.chdir = smbfs_chdir;
1983 vfs_smbfs_ops.ferrno = smbfs_errno;
1984 vfs_smbfs_ops.lseek = smbfs_lseek;
1985 vfs_smbfs_ops.mknod = smbfs_mknod;
1986 vfs_smbfs_ops.free = smbfs_free;
1987 vfs_smbfs_ops.mkdir = smbfs_mkdir;
1988 vfs_smbfs_ops.rmdir = smbfs_rmdir;
1989 vfs_smbfs_ops.setctl = smbfs_setctl;
1990 vfs_register_class (&vfs_smbfs_ops);