Special Option translated...
[midnight-commander.git] / vfs / smbfs.c
blobeff2c4a21799a7985f782e0e0516d2d046c27b32
1 /* Virtual File System: Midnight Commander file system.
3 Copyright (C) 1995, 1996, 1997 The Free Software Foundation
5 Written by Wayne Roberts <wroberts1@home.com>
7 $Id$
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* Namespace: exports vfs_smbfs_ops, smbfs_set_debug(), smbfs_set_debugf() */
24 #include <config.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #undef USE_NCURSES /* Don't include *curses.h */
28 #include "utilvfs.h"
30 #undef PACKAGE_BUGREPORT
31 #undef PACKAGE_NAME
32 #undef PACKAGE_STRING
33 #undef PACKAGE_TARNAME
34 #undef PACKAGE_VERSION
36 #include "samba/include/config.h"
37 /* don't load crap in "samba/include/includes.h" we don't use and which
38 conflicts with definitions in other includes */
39 #undef HAVE_LIBREADLINE
40 #define NO_CONFIG_H
41 #define BOOL_DEFINED
42 #undef VERSION
44 #include "samba/include/includes.h"
46 #include <string.h>
48 #include "vfs.h"
49 #include "smbfs.h"
50 #include "../src/dialog.h"
52 #define SMBFS_MAX_CONNECTIONS 16
53 static const char * const IPC = "IPC$";
54 static const char * const URL_HEADER = "/#smb:";
55 #define HEADER_LEN 6
57 static int my_errno;
58 static uint32 err;
60 /* stuff that is same with each connection */
61 extern int DEBUGLEVEL;
62 extern pstring myhostname;
63 extern pstring global_myname;
64 static int smbfs_open_connections = 0;
65 static gboolean got_user = FALSE;
66 static gboolean got_pass = FALSE;
67 static pstring password;
68 static pstring username;
70 static struct _smbfs_connection {
71 struct cli_state *cli;
72 struct in_addr dest_ip;
73 BOOL have_ip;
74 char *host; /* server name */
75 char *service; /* share name */
76 char *domain;
77 char *user;
78 char *home;
79 char *password;
80 int port;
81 int name_type;
82 time_t last_use;
83 } smbfs_connections [SMBFS_MAX_CONNECTIONS];
84 /* unique to each connection */
86 static struct cli_state * smbfs_do_connect (const char *server, char *share);
88 typedef struct _smbfs_connection smbfs_connection;
89 static smbfs_connection *current_bucket;
91 typedef struct {
92 struct cli_state *cli;
93 int fnum;
94 off_t nread;
95 uint16 attr;
96 } smbfs_handle;
98 static GSList *auth_list;
100 static void
101 authinfo_free (struct smb_authinfo const *a)
103 g_free (a->host);
104 g_free (a->share);
105 g_free (a->domain);
106 g_free (a->user);
107 wipe_password (a->password);
110 static void
111 authinfo_free_all ()
113 if (auth_list) {
114 g_slist_foreach (auth_list, (GFunc)authinfo_free, 0);
115 g_slist_free (auth_list);
116 auth_list = 0;
120 static gint
121 authinfo_compare_host_and_share (gconstpointer _a, gconstpointer _b)
123 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
124 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
126 if (!a->host || !a->share || !b->host || !b->share)
127 return 1;
128 if (strcmp (a->host, b->host) != 0)
129 return 1;
130 if (strcmp (a->share, b->share) != 0)
131 return 1;
132 return 0;
135 static gint
136 authinfo_compare_host (gconstpointer _a, gconstpointer _b)
138 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
139 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
141 if (!a->host || !b->host)
142 return 1;
143 if (strcmp (a->host, b->host) != 0)
144 return 1;
145 if (strcmp (a->share, IPC) != 0)
146 return 1;
147 return 0;
150 static void
151 authinfo_add (const char *host, const char *share, const char *domain,
152 const char *user, const char *password)
154 struct smb_authinfo *auth = g_new (struct smb_authinfo, 1);
156 if (!auth)
157 return;
159 /* Don't check for NULL, g_strdup already does. */
160 auth->host = g_strdup (host);
161 auth->share = g_strdup (share);
162 auth->domain = g_strdup (domain);
163 auth->user = g_strdup (user);
164 auth->password = g_strdup (password);
165 auth_list = g_slist_prepend (auth_list, auth);
168 static void
169 authinfo_remove (const char *host, const char *share)
171 struct smb_authinfo data;
172 struct smb_authinfo *auth;
173 GSList *list;
175 data.host = g_strdup (host);
176 data.share = g_strdup (share);
177 list = g_slist_find_custom (auth_list,
178 &data,
179 authinfo_compare_host_and_share);
180 g_free (data.host);
181 g_free (data.share);
182 if (!list)
183 return;
184 auth = list->data;
185 auth_list = g_slist_remove (auth_list, auth);
186 authinfo_free (auth);
189 /* Set authentication information in bucket. Return 1 if successful, else 0 */
190 /* Information in auth_list overrides user if pass is NULL. */
191 /* bucket->host and bucket->service must be valid. */
192 static int
193 bucket_set_authinfo (smbfs_connection *bucket,
194 const char *domain, const char *user, const char *pass,
195 int fallback_to_host)
197 struct smb_authinfo data;
198 struct smb_authinfo *auth;
199 GSList *list;
201 if (domain && user && pass) {
202 g_free (bucket->domain);
203 g_free (bucket->user);
204 g_free (bucket->password);
205 bucket->domain = g_strdup (domain);
206 bucket->user = g_strdup (user);
207 bucket->password = g_strdup (pass);
208 authinfo_remove (bucket->host, bucket->service);
209 authinfo_add (bucket->host, bucket->service,
210 domain, user, pass);
211 return 1;
214 data.host = bucket->host;
215 data.share = bucket->service;
216 list = g_slist_find_custom (auth_list, &data, authinfo_compare_host_and_share);
217 if (!list && fallback_to_host)
218 list = g_slist_find_custom (auth_list, &data, authinfo_compare_host);
219 if (list) {
220 auth = list->data;
221 bucket->domain = g_strdup (auth->domain);
222 bucket->user = g_strdup (auth->user);
223 bucket->password = g_strdup (auth->password);
224 return 1;
227 if (got_pass) {
228 bucket->domain = g_strdup (lp_workgroup ());
229 bucket->user = g_strdup (got_user ? username : user);
230 bucket->password = g_strdup (password);
231 return 1;
234 auth = vfs_smb_get_authinfo (bucket->host,
235 bucket->service,
236 (domain ? domain : lp_workgroup ()),
237 user);
238 if (auth) {
239 g_free (bucket->domain);
240 g_free (bucket->user);
241 g_free (bucket->password);
242 bucket->domain = g_strdup (auth->domain);
243 bucket->user = g_strdup (auth->user);
244 bucket->password = g_strdup (auth->password);
245 authinfo_remove (bucket->host, bucket->service);
246 auth_list = g_slist_prepend (auth_list, auth);
247 return 1;
249 return 0;
252 void
253 smbfs_set_debug (int arg)
255 DEBUGLEVEL = arg;
258 void
259 smbfs_set_debugf (const char *filename)
261 extern pstring debugf;
262 extern FILE *dbf;
263 if (DEBUGLEVEL > 0) {
264 FILE *outfile = fopen (filename, "w");
265 if (outfile) {
266 setup_logging ("", True); /* No needs for timestamp for each message */
267 dbf = outfile;
268 setbuf (dbf, NULL);
269 pstrcpy (debugf, filename);
274 /********************** The callbacks ******************************/
275 static int
276 smbfs_init (vfs * me)
278 char *servicesf = CONFIGDIR PATH_SEP_STR "smb.conf";
280 /* DEBUGLEVEL = 4; */
282 TimeInit ();
283 charset_initialise ();
285 DEBUG (3, ("smbfs_init(%s)\n", me->name));
287 if (!get_myname (myhostname, NULL))
288 DEBUG (0, ("Failed to get my hostname.\n"));
290 if (!lp_load (servicesf, True, False, False))
291 DEBUG (0, ("Cannot load %s - run testparm to debug it\n", servicesf));
293 codepage_initialise (lp_client_code_page ());
295 load_interfaces ();
297 if (getenv ("USER")) {
298 char *p;
300 pstrcpy (username, getenv ("USER"));
301 got_user = TRUE;
302 DEBUG (3, ("smbfs_init(): $USER:%s\n", username));
303 if ((p = strchr (username, '%'))) {
304 *p = 0;
305 pstrcpy (password, p + 1);
306 got_pass = TRUE;
307 memset (strchr (getenv ("USER"), '%') + 1, 'X', strlen (password));
308 DEBUG (3, ("smbfs_init(): $USER%%pass: %s%%%s\n",
309 username, password));
311 strupper (username);
313 if (getenv ("PASSWD")) {
314 pstrcpy (password, getenv ("PASSWD"));
315 got_pass = TRUE;
317 return 1;
320 static void
321 smbfs_fill_names (vfs *me, void (*func)(char *))
323 int i;
324 char *path;
325 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
326 if (smbfs_connections [i].cli) {
327 path = g_strconcat (URL_HEADER,
328 smbfs_connections[i].host,
329 "/", smbfs_connections[i].service,
330 NULL);
331 (*func)(path);
332 g_free (path);
337 #define CNV_LANG(s) dos_to_unix(s,False)
338 #define GNAL_VNC(s) unix_to_dos(s,False)
339 /* does same as do_get() in client.c */
340 /* called from vfs.c:1080, count = buffer size */
341 static int
342 smbfs_read (void *data, char *buffer, int count)
344 smbfs_handle *info = (smbfs_handle *) data;
345 int n;
347 DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%d)\n",
348 info->fnum, (int)info->nread, count));
349 n = cli_read(info->cli, info->fnum, buffer, info->nread, count);
350 if (n > 0)
351 info->nread += n;
352 return n;
355 static int
356 smbfs_write (void *data, char *buf, int nbyte)
358 smbfs_handle *info = (smbfs_handle *) data;
359 int n;
361 DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%d)\n",
362 info->fnum, (int)info->nread, nbyte));
363 n = cli_write(info->cli, info->fnum, 0, buf, info->nread, nbyte);
364 if (n > 0)
365 info->nread += n;
366 return n;
369 static int
370 smbfs_close (void *data)
372 smbfs_handle *info = (smbfs_handle *) data;
373 DEBUG (3, ("smbfs_close(fnum:%d)\n", info->fnum));
375 /* FIXME: Why too different cli have the same outbuf
376 * if file is copied to share
378 if (info->cli->outbuf == NULL) {
379 my_errno = EINVAL;
380 return -1;
382 #if 0
383 /* if imlementing archive_level: add rname to smbfs_handle */
384 if (archive_level >= 2 && (inf->attr & aARCH)) {
385 cli_setatr (info->cli, rname, info->attr & ~(uint16) aARCH, 0);
387 #endif
388 return (cli_close (info->cli, info->fnum) == True) ? 0 : -1;
391 static int
392 smbfs_errno (vfs *me)
394 DEBUG(3, ("smbfs_errno: %s\n", g_strerror(my_errno)));
395 return my_errno;
398 typedef struct dir_entry {
399 char *text;
400 struct dir_entry *next;
401 struct stat my_stat;
402 int merrno;
403 } dir_entry;
405 typedef struct {
406 gboolean server_list;
407 char *dirname;
408 char *path; /* the dir originally passed to smbfs_opendir */
409 smbfs_connection *conn;
410 dir_entry *entries;
411 dir_entry *current;
412 } opendir_info;
414 static opendir_info
415 *previous_info,
416 *current_info,
417 *current_share_info,
418 *current_server_info;
420 static gboolean first_direntry;
422 static dir_entry *
423 new_dir_entry (const char * name)
425 dir_entry *new_entry;
426 new_entry = g_new0 (dir_entry, 1);
427 new_entry->text = dos_to_unix (g_strdup (name), 1);
429 if (first_direntry) {
430 current_info->entries = new_entry;
431 first_direntry = FALSE;
432 } else {
433 current_info->current->next = new_entry;
435 current_info->current = new_entry;
437 return new_entry;
440 /* browse for shares on server */
441 static void
442 browsing_helper (const char *name, uint32 type, const char *comment, void *state)
444 char *typestr = "";
446 dir_entry *new_entry = new_dir_entry (name);
448 switch (type) {
449 case STYPE_DISKTREE:
450 typestr = "Disk";
451 /* show this as dir */
452 new_entry->my_stat.st_mode =
453 S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
454 S_IXOTH;
455 break;
456 case STYPE_PRINTQ:
457 typestr = "Printer";
458 break;
459 case STYPE_DEVICE:
460 typestr = "Device";
461 break;
462 case STYPE_IPC:
463 typestr = "IPC";
464 break;
466 DEBUG (3, ("\t%-15.15s%-10.10s%s\n", name, typestr, comment));
469 static void
470 loaddir_helper (file_info * finfo, const char *mask, void *entry)
472 dir_entry *new_entry = (dir_entry *) entry;
473 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
474 #if 0 /* I want to see dot files */
475 if (finfo->mode & aHIDDEN)
476 return; /* don't bother with hidden files, "~$" screws up mc */
477 #endif
478 if (!entry)
479 new_entry = new_dir_entry (finfo->name);
481 new_entry->my_stat.st_size = finfo->size;
482 new_entry->my_stat.st_mtime = finfo->mtime;
483 new_entry->my_stat.st_atime = finfo->atime;
484 new_entry->my_stat.st_ctime = finfo->ctime;
485 new_entry->my_stat.st_uid = finfo->uid;
486 new_entry->my_stat.st_gid = finfo->gid;
488 new_entry->my_stat.st_mode = /* rw-rw-rw */
489 S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH;
491 /* if (finfo->mode & aVOLID); nothing similar in real world */
492 if (finfo->mode & aDIR)
493 new_entry->my_stat.st_mode |= /* drwxrwxrwx */
494 S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
495 else
496 new_entry->my_stat.st_mode |= S_IFREG; /* if not dir, regular file? */
497 /* if (finfo->mode & aARCH); DOS archive */
498 /* if (finfo->mode & aHIDDEN); like a dot file? */
499 /* if (finfo->mode & aSYSTEM); like a kernel? */
500 if (finfo->mode & aRONLY)
501 new_entry->my_stat.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
503 DEBUG (entry ? 3 : 6, (" %-30s%7.7s%8.0f %s",
504 CNV_LANG (finfo->name),
505 attrib_string (finfo->mode),
506 (double) finfo->size,
507 asctime (LocalTime (&t))));
510 /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
511 static int
512 convert_path(char **remote_file, gboolean trailing_asterik)
514 char *p, *my_remote;
516 my_remote = *remote_file;
517 if (strncmp (my_remote, URL_HEADER, HEADER_LEN) == 0) { /* if passed directly */
518 my_remote += 6;
519 if (*my_remote == '/') /* from server browsing */
520 my_remote++;
521 p = strchr(my_remote, '/');
522 if (p)
523 my_remote = p+1; /* advance to end of server name */
526 if (*my_remote == '/')
527 my_remote++; /* strip off leading '/' */
528 p = strchr(my_remote, '/');
529 if (p)
530 my_remote = p; /* strip off share/service name */
531 /* create remote filename as understood by smb clientgen */
532 p = *remote_file = g_strconcat (my_remote, trailing_asterik ? "/*" : "", 0);
533 unix_to_dos (*remote_file, 1);
534 while ((p = strchr(p, '/')))
535 *p = '\\';
536 return 0;
539 static void
540 server_browsing_helper (const char *name, uint32 m, const char *comment, void *state)
542 dir_entry *new_entry = new_dir_entry (name);
544 /* show this as dir */
545 new_entry->my_stat.st_mode =
546 S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP | S_IXOTH;
548 DEBUG (3, ("\t%-16.16s %s\n", name, comment));
551 static BOOL
552 reconnect(smbfs_connection *conn, int *retries)
554 char *host;
555 DEBUG(3, ("RECONNECT\n"));
557 if (*(conn->host) == 0)
558 host = g_strdup(conn->cli->desthost); /* server browsing */
559 else
560 host = g_strdup(conn->host);
562 cli_shutdown(conn->cli);
564 if (!(conn->cli = smbfs_do_connect(host, conn->service))) {
565 message_2s (1, MSG_ERROR,
566 _(" reconnect to %s failed\n "), conn->host);
567 g_free(host);
568 return False;
570 g_free(host);
571 if (++(*retries) == 2)
572 return False;
573 return True;
576 static BOOL
577 smb_send(struct cli_state *cli)
579 size_t len;
580 size_t nwritten=0;
581 ssize_t ret;
583 len = smb_len(cli->outbuf) + 4;
585 while (nwritten < len) {
586 ret = write_socket(cli->fd, cli->outbuf+nwritten, len - nwritten);
587 if (ret <= 0 && errno == EPIPE)
588 return False;
589 nwritten += ret;
592 return True;
595 /****************************************************************************
596 See if server has cut us off by checking for EPIPE when writing.
597 Taken from cli_chkpath()
598 ****************************************************************************/
599 static BOOL
600 chkpath(struct cli_state *cli, char *path, BOOL send_only)
602 fstring path2;
603 char *p;
605 fstrcpy(path2,path);
606 unix_to_dos (path2, 1);
607 trim_string(path2,NULL,"\\");
608 if (!*path2) *path2 = '\\';
610 memset(cli->outbuf,'\0',smb_size);
611 set_message(cli->outbuf,0,4 + strlen(path2),True);
612 SCVAL(cli->outbuf,smb_com,SMBchkpth);
613 SSVAL(cli->outbuf,smb_tid,cli->cnum);
615 cli->rap_error = 0;
616 cli->nt_error = 0;
617 SSVAL(cli->outbuf,smb_pid,cli->pid);
618 SSVAL(cli->outbuf,smb_uid,cli->vuid);
619 SSVAL(cli->outbuf,smb_mid,cli->mid);
620 if (cli->protocol > PROTOCOL_CORE) {
621 SCVAL(cli->outbuf,smb_flg,0x8);
622 SSVAL(cli->outbuf,smb_flg2,0x1);
625 p = smb_buf(cli->outbuf);
626 *p++ = 4;
627 fstrcpy(p,path2);
629 if (!smb_send(cli)) {
630 DEBUG(3, ("chkpath: couldnt send\n"));
631 return False;
633 if (send_only) {
634 client_receive_smb(cli->fd, cli->inbuf, cli->timeout);
635 DEBUG(3, ("chkpath: send only OK\n"));
636 return True; /* just testing for EPIPE */
638 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout)) {
639 DEBUG(3, ("chkpath: receive error\n"));
640 return False;
642 if ((my_errno = cli_error(cli, NULL, NULL, NULL))) {
643 if (my_errno == 20 || my_errno == 13)
644 return True; /* ignore if 'not a directory' error */
645 DEBUG(3, ("chkpath: cli_error: %s\n", g_strerror(my_errno)));
646 return False;
649 return True;
652 #if 1
653 static int
654 fs (const char *text)
656 const char *p = text;
657 int count = 0;
659 while ((p = strchr(p, '/')) != NULL) {
660 count++;
661 p++;
663 if (count == 1)
664 return strlen(text);
665 return count;
667 #endif
669 static int
670 smbfs_loaddir (opendir_info *smbfs_info)
672 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
673 int servlen = strlen(smbfs_info->conn->service);
674 char *my_dirname = smbfs_info->dirname;
676 DEBUG(3, ("smbfs_loaddir: dirname:%s\n", my_dirname));
677 first_direntry = TRUE;
679 if (current_info) {
680 DEBUG(3, ("smbfs_loaddir: new:'%s', cached:'%s'\n", my_dirname, current_info->dirname));
681 /* if new desired dir is longer than cached in current_info */
682 if (fs(my_dirname) > fs(current_info->dirname)) {
683 DEBUG(3, ("saving to previous_info\n"));
684 previous_info = current_info;
688 current_info = smbfs_info;
690 if (strcmp(my_dirname, "/") == 0) {
691 if (!strcmp(smbfs_info->path, URL_HEADER)) {
692 DEBUG(6, ("smbfs_loaddir: browsing %s\n", IPC));
693 /* browse for servers */
694 if (!cli_NetServerEnum(smbfs_info->conn->cli, smbfs_info->conn->domain,
695 SV_TYPE_ALL, server_browsing_helper, NULL))
696 return 0;
697 else
698 current_server_info = smbfs_info;
699 smbfs_info->server_list = TRUE;
700 } else {
701 /* browse for shares */
702 if (cli_RNetShareEnum(smbfs_info->conn->cli, browsing_helper, NULL) < 1)
703 return 0;
704 else
705 current_share_info = smbfs_info;
707 goto done;
710 /* do regular directory listing */
711 if(strncmp(smbfs_info->conn->service, my_dirname+1, servlen) == 0) {
712 /* strip share name from dir */
713 char *p = my_dirname = g_strdup(my_dirname + servlen);
714 *p = '/';
715 convert_path(&my_dirname, TRUE);
716 g_free (p);
717 } else
718 convert_path(&my_dirname, TRUE);
720 DEBUG(6, ("smbfs_loaddir: service: %s\n", smbfs_info->conn->service));
721 DEBUG(6, ("smbfs_loaddir: cli->share: %s\n", smbfs_info->conn->cli->share));
722 DEBUG(6, ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname));
723 /* do file listing: cli_list returns number of files */
724 if (cli_list(
725 smbfs_info->conn->cli, my_dirname, attribute, loaddir_helper, NULL) < 0) {
726 /* cli_list returns -1 if directory empty or cannot read socket */
727 my_errno = cli_error(smbfs_info->conn->cli, NULL, &err, NULL);
728 g_free (my_dirname);
729 return 0;
731 if (*(my_dirname) == 0)
732 smbfs_info->dirname = smbfs_info->conn->service;
733 g_free (my_dirname);
734 /* do_dskattr(); */
736 done:
737 /* current_info->parent = smbfs_info->dirname; */
739 smbfs_info->current = smbfs_info->entries;
740 return 1; /* 1 = ok */
743 #ifdef SMBFS_FREE_DIR
744 static void
745 smbfs_free_dir (dir_entry *de)
747 if (!de) return;
749 smbfs_free_dir (de->next);
750 g_free (de->text);
751 g_free (de);
753 #endif
756 /* The readdir routine loads the complete directory */
757 /* It's too slow to ask the server each time */
758 /* It now also sends the complete lstat information for each file */
759 static void *
760 smbfs_readdir(void *info)
762 static union vfs_dirent smbfs_readdir_data;
763 static char *const dirent_dest = smbfs_readdir_data.dent.d_name;
764 opendir_info *smbfs_info = (opendir_info *) info;
766 DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info->dirname));
768 if (!smbfs_info->entries)
769 if (!smbfs_loaddir(smbfs_info))
770 return NULL;
772 if (smbfs_info->current == 0) { /* reached end of dir entries */
773 DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
774 #ifdef SMBFS_FREE_DIR
775 smbfs_free_dir(smbfs_info->entries);
776 smbfs_info->entries = 0;
777 #endif
778 return NULL;
780 strncpy(dirent_dest, smbfs_info->current->text, MC_MAXPATHLEN);
781 dirent_dest[MC_MAXPATHLEN] = 0;
782 smbfs_info->current = smbfs_info->current->next;
784 compute_namelen(&smbfs_readdir_data.dent);
786 return &smbfs_readdir_data;
789 static int
790 smbfs_closedir (void *info)
792 opendir_info *smbfs_info = (opendir_info *) info;
793 /* dir_entry *p, *q; */
795 DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info->dirname));
796 /* CLOSE HERE */
798 /* for (p = smbfs_info->entries; p;){
799 q = p;
800 p = p->next;
801 g_free (q->text);
802 g_free (q);
804 g_free (info); */
805 return 0;
808 static int
809 smbfs_chmod (vfs *me, char *path, int mode)
811 DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path, mode));
812 /* my_errno = EOPNOTSUPP;
813 return -1; */ /* cant chmod on smb filesystem */
814 return 0; /* make mc happy */
817 static int
818 smbfs_chown (vfs *me, char *path, int owner, int group)
820 DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path, owner, group));
821 my_errno = EOPNOTSUPP; /* ready for your labotomy? */
822 return -1;
825 static int
826 smbfs_utime (vfs *me, char *path, struct utimbuf *times)
828 DEBUG(3, ("smbfs_utime(path:%s)\n", path));
829 my_errno = EOPNOTSUPP;
830 return -1;
833 static int
834 smbfs_readlink (vfs *me, char *path, char *buf, int size)
836 DEBUG(3, ("smbfs_readlink(path:%s, buf:%s, size:%d)\n", path, buf, size));
837 my_errno = EOPNOTSUPP;
838 return -1; /* no symlinks on smb filesystem? */
841 static int
842 smbfs_symlink (vfs *me, char *n1, char *n2)
844 DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1, n2));
845 my_errno = EOPNOTSUPP;
846 return -1; /* no symlinks on smb filesystem? */
849 /* Extract the hostname and username from the path */
850 /* path is in the form: hostname:user/remote-dir */
851 #if 0
852 static char *
853 smbfs_get_host_and_username
854 (char **path, char **host, char **user, int *port, char **pass)
856 /* char *p, *ret; */
857 char *ret;
859 ret = vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0);
861 #if 0
862 if ((p = strchr(*path, '@'))) /* user:pass@server */
863 *path = ++p; /* don't want user:pass@ in path */
864 if ((p = strchr(ret, '@'))) /* user:pass@server */
865 ret = ++p; /* don't want user:pass@ in path */
866 #endif
868 return ret;
870 #else
871 #define smbfs_get_host_and_username(path, host, user, port, pass) \
872 vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
873 #endif
874 /*****************************************************
875 return a connection to a SMB server
876 current_bucket needs to be set before calling
877 *******************************************************/
878 static struct cli_state *
879 smbfs_do_connect (const char *server, char *share)
881 struct cli_state *c;
882 struct nmb_name called, calling;
883 struct in_addr ip;
884 extern struct in_addr ipzero;
886 DEBUG(3, ("smbfs_do_connect(%s, %s)\n", server, share));
887 if (*share == '\\') {
888 server = share+2;
889 share = strchr(server,'\\');
890 if (!share) return NULL;
891 *share = 0;
892 share++;
895 make_nmb_name(&calling, global_myname, 0x0);
896 make_nmb_name(&called , server, current_bucket->name_type);
898 for (;;) {
900 ip = (current_bucket->have_ip) ? current_bucket->dest_ip : ipzero;
902 /* have to open a new connection */
903 if (!(c = cli_initialise(NULL))) {
904 my_errno = ENOMEM;
905 return NULL;
908 pwd_init(&(c->pwd)); /* should be moved into cli_initialise()? */
909 pwd_set_cleartext(&(c->pwd), current_bucket->password);
911 if ((cli_set_port(c, current_bucket->port) == 0) ||
912 !cli_connect(c, server, &ip)) {
913 DEBUG(1, ("Connection to %s failed\n", server));
914 break;
917 if (!cli_session_request(c, &calling, &called)) {
918 my_errno = cli_error(c, NULL, &err, NULL);
919 DEBUG(1, ("session request to %s failed\n", called.name));
920 cli_shutdown(c);
921 if (strcmp(called.name, "*SMBSERVER")) {
922 make_nmb_name(&called , "*SMBSERVER", 0x20);
923 continue;
925 return NULL;
928 DEBUG(3, (" session request ok\n"));
930 if (!cli_negprot(c)) {
931 DEBUG(1, ("protocol negotiation failed\n"));
932 break;
935 if (!cli_session_setup(c, current_bucket->user,
936 current_bucket->password, strlen(current_bucket->password),
937 current_bucket->password, strlen(current_bucket->password),
938 current_bucket->domain)) {
939 DEBUG(1,("session setup failed: %s\n", cli_errstr(c)));
940 authinfo_remove (server, share);
941 break;
944 if (*c->server_domain || *c->server_os || *c->server_type)
945 DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
946 c->server_domain,c->server_os,c->server_type));
948 DEBUG(3, (" session setup ok\n"));
950 if (!cli_send_tconX(c, share, "?????",
951 current_bucket->password, strlen(current_bucket->password)+1)) {
952 DEBUG(1,("%s: tree connect failed: %s\n", share, cli_errstr(c)));
953 break;
956 DEBUG(3, (" tconx ok\n"));
958 my_errno = 0;
959 return c;
962 my_errno = cli_error(c, NULL, &err, NULL);
963 cli_shutdown(c);
964 return NULL;
968 static int
969 get_master_browser(char **host)
971 int count;
972 struct in_addr *ip_list, bcast_addr;
973 extern struct in_addr ipzero;
975 /* does port = 137 for win95 master browser? */
976 int fd= open_socket_in( SOCK_DGRAM, 0, 3,
977 interpret_addr(lp_socket_address()), True );
978 if (fd == -1)
979 return 0;
980 set_socket_options(fd, "SO_BROADCAST");
981 ip_list = iface_bcast(ipzero);
982 bcast_addr = *ip_list;
983 if ((ip_list = name_query(fd, "\01\02__MSBROWSE__\02", 1, True,
984 True, bcast_addr, &count, NULL))) {
985 if (!count)
986 return 0;
987 /* just return first master browser */
988 *host = g_strdup(inet_ntoa(ip_list[0]));
989 return 1;
991 return 0;
994 static void
995 free_bucket (smbfs_connection *bucket)
997 g_free (bucket->host);
998 g_free (bucket->service);
999 g_free (bucket->domain);
1000 g_free (bucket->user);
1001 wipe_password (bucket->password);
1002 if (bucket->home) g_free (bucket->home);
1003 memset (bucket, 0, sizeof (smbfs_connection));
1006 static smbfs_connection *
1007 smbfs_get_free_bucket ()
1009 int i;
1011 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++)
1012 if (!smbfs_connections [i].cli) return &smbfs_connections [i];
1014 { /* search for most dormant connection */
1015 int oldest = 0; /* index */
1016 time_t oldest_time = smbfs_connections[0].last_use;
1017 for (i = 1; i < SMBFS_MAX_CONNECTIONS; i++) {
1018 if (smbfs_connections[i].last_use < oldest_time) {
1019 oldest_time = smbfs_connections[i].last_use;
1020 oldest = i;
1023 cli_shutdown(smbfs_connections[oldest].cli);
1024 free_bucket (&smbfs_connections[oldest]);
1025 return &smbfs_connections[oldest];
1028 /* This can't happend, since we have checked for max connections before */
1029 vfs_die("Internal error: smbfs_get_free_bucket");
1030 return 0; /* shut up, stupid gcc */
1033 /* This routine keeps track of open connections */
1034 /* Returns a connected socket to host */
1035 static smbfs_connection *
1036 smbfs_open_link(char *host, char *path, const char *user, int *port, char *this_pass)
1038 int i;
1039 smbfs_connection *bucket;
1040 pstring service;
1041 struct in_addr *dest_ip = NULL;
1043 DEBUG(3, ("smbfs_open_link(host:%s, path:%s)\n", host, path));
1045 if (strcmp(host, path) == 0) /* if host & path are same: */
1046 pstrcpy(service, IPC); /* setup for browse */
1047 else { /* get share name from path, path starts with server name */
1048 char *p;
1049 if ((p = strchr(path, '/'))) /* get share aka */
1050 pstrcpy(service, ++p); /* service name from path */
1051 else
1052 pstrcpy(service, "");
1053 /* now check for trailing directory/filenames */
1054 p = strchr(service, '/');
1055 if (p)
1056 *p = 0; /* cut off dir/files: sharename only */
1058 DEBUG(6, ("smbfs_open_link: service from path:%s\n", service));
1061 if (got_user)
1062 user = username; /* global from getenv */
1064 /* Is the link actually open? */
1065 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1066 if (!smbfs_connections[i].cli)
1067 continue;
1068 if ((strcmp (host, smbfs_connections [i].host) == 0) &&
1069 (strcmp (user, smbfs_connections [i].user) == 0) &&
1070 (strcmp (service, smbfs_connections [i].service) == 0)) {
1071 int retries = 0;
1072 BOOL inshare = (*host != 0 && *path != 0 && strchr(path, '/'));
1073 /* check if this connection has died */
1074 while (!chkpath(smbfs_connections[i].cli, "\\", !inshare)) {
1075 if (!reconnect(&smbfs_connections[i], &retries))
1076 return 0;
1078 DEBUG(6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i));
1079 current_bucket = &smbfs_connections[i];
1080 smbfs_connections [i].last_use = time(NULL);
1081 return &smbfs_connections [i];
1083 /* connection not found, find if we have ip for new connection */
1084 if (strcmp (host, smbfs_connections [i].host) == 0)
1085 dest_ip = &smbfs_connections[i].cli->dest_ip;
1088 /* make new connection */
1089 bucket = smbfs_get_free_bucket ();
1090 bucket->name_type = 0x20;
1091 bucket->home = 0;
1092 bucket->port = *port;
1093 bucket->have_ip = False;
1094 if (dest_ip) {
1095 bucket->have_ip = True;
1096 bucket->dest_ip = *dest_ip;
1098 current_bucket = bucket;
1100 bucket->user = g_strdup(user);
1101 bucket->service = g_strdup (service);
1103 if (!(*host)) { /* if blank host name, browse for servers */
1104 if (!get_master_browser(&host)) /* set host to ip of master browser */
1105 return 0; /* couldnt find master browser? */
1106 g_free (host);
1107 bucket->host = g_strdup(""); /* blank host means master browser */
1108 } else
1109 bucket->host = g_strdup(host);
1111 if (!bucket_set_authinfo (bucket,
1112 0, /* domain currently not used */
1113 user,
1114 this_pass,
1116 return 0;
1118 /* connect to share */
1119 while (!(bucket->cli = smbfs_do_connect(host, service))) {
1121 if (my_errno != EPERM)
1122 return 0;
1123 message_1s (1, MSG_ERROR,
1124 _(" Authentication failed "));
1126 /* authentication failed, try again */
1127 authinfo_remove (bucket->host, bucket->service);
1128 if (!bucket_set_authinfo (bucket,
1129 bucket->domain,
1130 bucket->user,
1133 return 0;
1137 smbfs_open_connections++;
1138 DEBUG(3, ("smbfs_open_link:smbfs_open_connections: %d\n",
1139 smbfs_open_connections));
1140 return bucket;
1143 static char *
1144 smbfs_get_path(smbfs_connection **sc, char *path)
1146 char *user, *host, *remote_path, *pass;
1147 int port = SMB_PORT;
1149 DEBUG(3, ("smbfs_get_path(%s)\n", path));
1150 if (strncmp (path, URL_HEADER, HEADER_LEN))
1151 return NULL;
1152 path += HEADER_LEN;
1154 if (*path == '/') /* '/' leading server name */
1155 path++; /* probably came from server browsing */
1157 if ((remote_path = smbfs_get_host_and_username(
1158 &path, &host, &user, &port, &pass)))
1159 if ((*sc = smbfs_open_link (host, path, user, &port, pass)) == NULL){
1160 g_free (remote_path);
1161 remote_path = NULL;
1163 g_free (host);
1164 g_free (user);
1165 if (pass) wipe_password (pass);
1167 if (!remote_path) return NULL;
1169 /* NOTE: tildes are deprecated. See ftpfs.c */
1171 int f = !strcmp( remote_path, "/~" );
1172 if (f || !strncmp( remote_path, "/~/", 3 )) {
1173 char *s;
1174 s = concat_dir_and_file( (*sc)->home, remote_path +3-f );
1175 g_free (remote_path);
1176 return s;
1179 return remote_path;
1182 #if 0
1183 static int
1184 is_error (int result, int errno_num)
1186 if (!(result == -1))
1187 return my_errno = 0;
1188 else
1189 my_errno = errno_num;
1190 return 1;
1192 #endif
1194 static void *
1195 smbfs_opendir (vfs *me, char *dirname)
1197 opendir_info *smbfs_info;
1198 smbfs_connection *sc;
1199 char *remote_dir;
1201 DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname));
1203 if (!(remote_dir = smbfs_get_path (&sc, dirname)))
1204 return NULL;
1206 /* FIXME: where freed? */
1207 smbfs_info = g_new (opendir_info, 1);
1208 smbfs_info->server_list = FALSE;
1209 smbfs_info->path = g_strdup(dirname); /* keep original */
1210 smbfs_info->dirname = remote_dir;
1211 smbfs_info->conn = sc;
1212 smbfs_info->entries = 0;
1213 smbfs_info->current = 0;
1215 return smbfs_info;
1218 static int
1219 fake_server_stat(const char *server_url, const char *path, struct stat *buf)
1221 dir_entry *dentry;
1222 char *p;
1224 if ((p = strrchr(path, '/')))
1225 path = p + 1; /* advance until last '/' */
1227 if (!current_info->entries) {
1228 if (!smbfs_loaddir(current_info)); /* browse host */
1229 return -1;
1232 if (current_info->server_list == True) {
1233 dentry = current_info->entries;
1234 DEBUG(4, ("fake stat for SERVER \"%s\"\n", path));
1235 while (dentry) {
1236 if (strcmp(dentry->text, path) == 0) {
1237 DEBUG(4, ("fake_server_stat: %s:%4o\n",
1238 dentry->text, dentry->my_stat.st_mode));
1239 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1240 return 0;
1242 dentry = dentry->next;
1245 my_errno = ENOENT;
1246 return -1;
1249 static int
1250 fake_share_stat(const char *server_url, const char *path, struct stat *buf)
1252 dir_entry *dentry;
1253 if (strlen(path) < strlen(server_url))
1254 return -1;
1255 path += strlen(server_url); /* we only want share name */
1256 path++;
1258 if (*path == '/') /* '/' leading server name */
1259 path++; /* probably came from server browsing */
1261 if (!current_share_info->entries) {
1262 if (!smbfs_loaddir(current_share_info)); /* browse host */
1263 return -1;
1265 dentry = current_share_info->entries;
1266 DEBUG(3, ("fake_share_stat: %s on %s\n", path, server_url));
1267 while (dentry) {
1268 if (strcmp(dentry->text, path) == 0) {
1269 DEBUG(6, ("fake_share_stat: %s:%4o\n",
1270 dentry->text, dentry->my_stat.st_mode));
1271 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1272 return 0;
1274 dentry = dentry->next;
1276 my_errno = ENOENT;
1277 return -1;
1280 /* stat a single file, get_remote_stat callback */
1281 static dir_entry *single_entry;
1283 /* stat a single file */
1284 static int
1285 get_remote_stat (smbfs_connection * sc, char *path, struct stat *buf)
1287 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
1288 char *mypath = path;
1290 DEBUG (3, ("get_remote_stat(): mypath:%s\n", mypath));
1292 convert_path (&mypath, FALSE);
1294 #if 0 /* single_entry is never free()d now. And only my_stat is used */
1295 single_entry = g_new (dir_entry, 1);
1297 single_entry->text = dos_to_unix (g_strdup (finfo->name), 1);
1299 single_entry->next = 0;
1300 #endif
1301 if (!single_entry)
1302 single_entry = g_new0 (dir_entry, 1);
1304 if (cli_list
1305 (sc->cli, mypath, attribute, loaddir_helper, single_entry) < 1) {
1306 my_errno = ENOENT;
1307 g_free (mypath);
1308 return -1; /* cli_list returns number of files */
1311 memcpy (buf, &single_entry->my_stat, sizeof (struct stat));
1313 /* don't free here, use for smbfs_fstat() */
1314 /* g_free(single_entry->text);
1315 g_free(single_entry); */
1316 g_free (mypath);
1317 return 0;
1320 static int
1321 search_dir_entry (dir_entry *dentry, const char *text, struct stat *buf)
1323 while (dentry) {
1324 if (strcmp(text, dentry->text) == 0) {
1325 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1326 memcpy(&single_entry->my_stat, &dentry->my_stat,
1327 sizeof(struct stat));
1328 return 0;
1330 dentry = dentry->next;
1332 return -1;
1335 static int
1336 get_stat_info (smbfs_connection * sc, char *path, struct stat *buf)
1338 char *p;
1339 #if 0
1340 dir_entry *dentry = current_info->entries;
1341 #endif
1342 const char *mypath = path;
1344 mypath++; /* cut off leading '/' */
1345 if ((p = strrchr (mypath, '/')))
1346 mypath = p + 1; /* advance until last file/dir name */
1347 DEBUG (3, ("get_stat_info: mypath:%s, current_info->dirname:%s\n",
1348 mypath, current_info->dirname));
1349 #if 0
1350 if (!dentry) {
1351 DEBUG (1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n",
1352 current_info->dirname, path));
1353 return -1;
1355 #endif
1356 if (!single_entry) /* when found, this will be written too */
1357 single_entry = g_new (dir_entry, 1);
1358 if (search_dir_entry (current_info->entries, mypath, buf) == 0) {
1359 return 0;
1361 /* now try to identify mypath as PARENT dir */
1363 char *mdp;
1364 char *mydir;
1365 mdp = mydir = g_strdup (current_info->dirname);
1366 if ((p = strrchr (mydir, '/')))
1367 *p = 0; /* advance util last '/' */
1368 if ((p = strrchr (mydir, '/')))
1369 mydir = p + 1; /* advance util last '/' */
1370 if (strcmp (mydir, mypath) == 0) { /* fake a stat for ".." */
1371 memset (buf, 0, sizeof (struct stat));
1372 buf->st_mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH;
1373 memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
1374 g_free (mdp);
1375 DEBUG (1, (" PARENT:found in %s\n", current_info->dirname));
1376 return 0;
1378 g_free (mdp);
1380 /* now try to identify as CURRENT dir? */
1382 char *dnp = current_info->dirname;
1383 DEBUG (6, ("get_stat_info: is %s current dir? this dir is: %s\n",
1384 mypath, current_info->dirname));
1385 if (*dnp == '/')
1386 dnp++;
1387 else {
1388 return -1;
1390 if (strcmp (mypath, dnp) == 0) {
1391 memset (buf, 0, sizeof (struct stat));
1392 buf->st_mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH;
1393 memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
1394 DEBUG (1, (" CURRENT:found in %s\n", current_info->dirname));
1395 return 0;
1398 DEBUG (3, ("'%s' not found in current_info '%s'\n", path,
1399 current_info->dirname));
1400 /* try to find this in the PREVIOUS listing */
1401 if (previous_info) {
1402 if (search_dir_entry (previous_info->entries, mypath, buf) == 0)
1403 return 0;
1404 DEBUG (3, ("'%s' not found in previous_info '%s'\n", path,
1405 previous_info->dirname));
1407 /* try to find this in the SHARE listing */
1408 if (current_share_info) {
1409 if (search_dir_entry (current_share_info->entries, mypath, buf) == 0)
1410 return 0;
1411 DEBUG (3, ("'%s' not found in share_info '%s'\n", path,
1412 current_share_info->dirname));
1414 /* try to find this in the SERVER listing */
1415 if (current_server_info) {
1416 if (search_dir_entry (current_server_info->entries, mypath, buf) == 0)
1417 return 0;
1418 DEBUG (3, ("'%s' not found in server_info '%s'\n", path,
1419 current_server_info->dirname));
1421 /* nothing found. get stat file info from server */
1422 return get_remote_stat (sc, path, buf);
1425 static int
1426 smbfs_chdir (vfs *me, char *path)
1428 char *remote_dir;
1429 smbfs_connection *sc;
1431 DEBUG(3, ("smbfs_chdir(path:%s)\n", path));
1432 if (!(remote_dir = smbfs_get_path (&sc, path)))
1433 return -1;
1434 g_free (remote_dir);
1436 return 0;
1439 static int
1440 loaddir(vfs *me, const char *path)
1442 void *info;
1443 char *mypath, *p;
1445 mypath = g_strdup(path);
1446 p = strrchr(mypath, '/');
1448 if (p > mypath)
1449 *p = 0;
1450 DEBUG(6, ("loaddir(%s)\n", mypath));
1451 smbfs_chdir(me, mypath);
1452 info = smbfs_opendir (me, mypath);
1453 g_free(mypath);
1454 if (!info)
1455 return -1;
1456 smbfs_readdir(info);
1457 smbfs_loaddir(info);
1458 return 0;
1461 static int
1462 smbfs_stat (vfs *me, char *path, struct stat *buf)
1464 char *remote_dir;
1465 smbfs_connection *sc;
1466 pstring server_url;
1467 char *service, *pp;
1468 const char *p;
1470 DEBUG(3, ("smbfs_stat(path:%s)\n", path));
1472 #if 0
1473 if (p = strchr(path, '@')) /* user:pass@server */
1474 path = ++p; /* don't want user:pass@ in path */
1475 #endif
1477 if (!current_info) {
1478 DEBUG(1, ("current_info = NULL: "));
1479 if (loaddir(me, path) < 0)
1480 return -1;
1483 pstrcpy(server_url, URL_HEADER);
1484 pstrcat(server_url, current_bucket->host);
1486 /* check if stating server */
1487 p = path;
1488 if (strncmp(p, URL_HEADER, HEADER_LEN)) {
1489 DEBUG(1, ("'%s' doesnt start with '%s' (length %d)\n",
1490 p, URL_HEADER, HEADER_LEN));
1491 return -1;
1494 p += HEADER_LEN;
1495 if (*p == '/')
1496 p++;
1497 pp = strchr(p, '/'); /* advance past next '/' */
1498 if (!pp) {
1499 if (!current_info->server_list) {
1500 if (loaddir(me, path) < 0)
1501 return -1;
1503 return fake_server_stat(server_url, path, buf);
1505 if (!strchr(++pp, '/')) {
1506 return fake_share_stat(server_url, path, buf);
1509 /* stating inside share at this point */
1510 if (!(remote_dir = smbfs_get_path (&sc, path))) /* connects if necessary */
1511 return -1;
1512 g_free (remote_dir);
1514 int hostlen = strlen(current_bucket->host);
1515 char *pp = path + strlen(path)-hostlen;
1516 char *sp = server_url + strlen(server_url)-hostlen;
1518 if (strcmp(sp, pp) == 0) {
1519 /* make server name appear as directory */
1520 DEBUG(1, ("smbfs_stat: showing server as directory\n"));
1521 memset(buf, 0, sizeof(struct stat));
1522 buf->st_mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH;
1523 return 0;
1526 /* check if current_info is in share requested */
1527 p = service = g_strdup(p);
1528 pp = strchr(p, '/');
1529 if (pp) {
1530 p = ++pp; /* advance past server name */
1531 pp = strchr(p, '/');
1533 if (pp)
1534 *pp = 0; /* cut off everthing after service name */
1535 else
1536 p = IPC; /* browsing for services */
1537 pp = current_info->dirname;
1538 if (*pp == '/');
1539 pp++;
1540 if (strncmp(p, pp, strlen(p)) != 0) {
1541 DEBUG(6, ("desired '%s' is not loaded, we have '%s'\n", p, pp));
1542 if (loaddir(me, path) < 0) {
1543 g_free (service);
1544 return -1;
1546 DEBUG(6, ("loaded dir: '%s'\n", current_info->dirname));
1548 g_free(service);
1549 /* stat dirs & files under shares now */
1550 return get_stat_info(sc, path, buf);
1553 #define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */
1555 static int
1556 smbfs_lseek (void *data, off_t offset, int whence)
1558 DEBUG(3, ("smbfs_lseek()\n"));
1559 my_errno = EOPNOTSUPP;
1560 return -1;
1563 static int
1564 smbfs_mknod (vfs *me, char *path, int mode, int dev)
1566 DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%d)\n", path, mode, dev));
1567 my_errno = EOPNOTSUPP;
1568 return -1;
1571 static int
1572 smbfs_mkdir (vfs *me, char *path, mode_t mode)
1574 smbfs_connection *sc;
1575 char *remote_file;
1577 DEBUG(3, ("smbfs_mkdir(path:%s, mode:%d)\n", path, mode));
1578 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1579 return -1;
1580 g_free (remote_file);
1581 convert_path(&path, FALSE);
1583 if (!cli_mkdir(sc->cli, path)) {
1584 my_errno = cli_error(sc->cli, NULL, &err, NULL);
1585 message_3s (1, MSG_ERROR, _(" Error %s creating directory %s "),
1586 cli_errstr(sc->cli), CNV_LANG(path));
1587 g_free (path);
1588 return -1;
1590 g_free (path);
1591 return 0;
1594 static int
1595 smbfs_rmdir (vfs *me, char *path)
1597 smbfs_connection *sc;
1598 char *remote_file;
1600 DEBUG(3, ("smbfs_rmdir(path:%s)\n", path));
1601 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1602 return -1;
1603 g_free (remote_file);
1604 convert_path(&path, FALSE);
1606 if (!cli_rmdir(sc->cli, path)) {
1607 my_errno = cli_error(sc->cli, NULL, &err, NULL);
1608 message_3s (1, MSG_ERROR, _(" Error %s removing directory %s "),
1609 cli_errstr(sc->cli), CNV_LANG(path));
1610 g_free (path);
1611 return -1;
1614 g_free (path);
1615 return 0;
1618 static int
1619 smbfs_link (vfs *me, char *p1, char *p2)
1621 DEBUG(3, ("smbfs_link(p1:%s, p2:%s)\n", p1, p2));
1622 my_errno = EOPNOTSUPP;
1623 return -1;
1626 /* We do not free anything right now: we free resources when we run
1627 * out of them
1629 static vfsid
1630 smbfs_getid (vfs *me, char *p, struct vfs_stamping **parent)
1632 *parent = NULL;
1633 DEBUG(3, ("smbfs_getid(p:%s)\n", p));
1635 return (vfsid) -1;
1638 static int
1639 smbfs_nothingisopen (vfsid id)
1641 DEBUG(3, ("smbfs_nothingisopen(%d)\n", (int)id));
1642 return 0;
1645 static void
1646 smbfs_free (vfsid id)
1648 DEBUG(3, ("smbfs_free(%d)\n", (int)id));
1649 /* FIXME: Should not be empty */
1650 authinfo_free_all ();
1653 /* Gives up on a socket and reopens the connection, the child own the socket
1654 * now
1656 static void
1657 my_forget (char *path)
1659 char *host, *user, *p;
1660 int port, i;
1662 if (strncmp (path, URL_HEADER, HEADER_LEN))
1663 return;
1665 DEBUG(3, ("my_forget(path:%s)\n", path));
1667 path += 6;
1668 if (path[0] == '/' && path[1] == '/')
1669 path += 2;
1671 if ((p = smbfs_get_host_and_username (&path, &host, &user, &port, NULL))) {
1672 g_free (p);
1673 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1674 if ((strcmp (host, smbfs_connections [i].host) == 0) &&
1675 (strcmp (user, smbfs_connections [i].user) == 0) &&
1676 (port == smbfs_connections [i].port)) {
1678 /* close socket: the child owns it now */
1679 cli_shutdown(smbfs_connections [i].cli);
1681 /* reopen the connection */
1682 smbfs_connections [i].cli =
1683 smbfs_do_connect(host, smbfs_connections[i].service);
1687 g_free (host);
1688 g_free (user);
1691 static int
1692 smbfs_setctl (vfs *me, char *path, int ctlop, char *arg)
1694 DEBUG(3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path, ctlop));
1695 switch (ctlop) {
1696 case MCCTL_FORGET_ABOUT:
1697 my_forget(path);
1698 return 0;
1700 return 0;
1703 static smbfs_handle *
1704 open_write (smbfs_handle *remote_handle, char *rname, int flags, int mode)
1706 if (flags & O_TRUNC) /* if it exists truncate to zero */
1707 DEBUG(3, ("open_write: O_TRUNC\n"));
1709 remote_handle->fnum = cli_open(remote_handle->cli, rname, flags, DENY_NONE);
1711 if (remote_handle->fnum == -1) {
1712 message_3s (1, MSG_ERROR, _(" %s opening remote file %s "),
1713 cli_errstr(remote_handle->cli), CNV_LANG(rname));
1714 DEBUG(1,("smbfs_open(rname:%s) error:%s\n",
1715 rname, cli_errstr(remote_handle->cli)));
1716 my_errno = cli_error(remote_handle->cli, NULL, &err, NULL);
1717 return NULL;
1720 return remote_handle;
1723 static smbfs_handle *
1724 open_read (smbfs_handle *remote_handle, char *rname, int flags, int mode)
1726 size_t size;
1728 remote_handle->fnum =
1729 cli_open(remote_handle->cli, rname, O_RDONLY, DENY_NONE);
1731 if (remote_handle->fnum == -1) {
1732 message_3s (1, MSG_ERROR, _(" %s opening remote file %s "),
1733 cli_errstr(remote_handle->cli), CNV_LANG(rname));
1734 DEBUG(1,("smbfs_open(rname:%s) error:%s\n",
1735 rname, cli_errstr(remote_handle->cli)));
1736 my_errno = cli_error(remote_handle->cli, NULL, &err, NULL);
1737 return NULL;
1740 if (!cli_qfileinfo(remote_handle->cli, remote_handle->fnum,
1741 &remote_handle->attr, &size, NULL, NULL, NULL, NULL, NULL) &&
1742 !cli_getattrE(remote_handle->cli, remote_handle->fnum,
1743 &remote_handle->attr, &size, NULL, NULL, NULL)) {
1744 message_2s (1, MSG_ERROR, " getattrib: %s ",
1745 cli_errstr(remote_handle->cli));
1746 DEBUG(1,("smbfs_open(rname:%s) getattrib:%s\n",
1747 rname, cli_errstr(remote_handle->cli)));
1748 my_errno = cli_error(remote_handle->cli, NULL, &err, NULL);
1749 return NULL;
1752 return remote_handle;
1755 static void *
1756 smbfs_open (vfs *me, char *file, int flags, int mode)
1758 char *remote_file, *p;
1759 void *ret;
1760 smbfs_connection *sc;
1761 smbfs_handle *remote_handle;
1763 DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%d)\n", file, flags, mode));
1765 if (!(remote_file = smbfs_get_path (&sc, file)))
1766 return 0;
1768 p = remote_file;
1769 convert_path(&remote_file, FALSE);
1770 g_free (p);
1772 remote_handle = g_new (smbfs_handle, 2);
1773 remote_handle->cli = sc->cli;
1774 remote_handle->nread = 0;
1776 if (flags & O_CREAT)
1777 ret = open_write(remote_handle, remote_file, flags, mode);
1778 else
1779 ret = open_read(remote_handle, remote_file, flags, mode);
1781 g_free (remote_file);
1783 return ret;
1786 static int
1787 smbfs_unlink (vfs *me, char *path)
1789 smbfs_connection *sc;
1790 char *remote_file, *p;
1792 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1793 return -1;
1795 p = remote_file;
1796 convert_path(&remote_file, FALSE);
1797 g_free (p);
1799 if (!cli_unlink(sc->cli, remote_file)) {
1800 message_3s (1, MSG_ERROR, _(" %s removing remote file %s "),
1801 cli_errstr(sc->cli), CNV_LANG(remote_file));
1802 g_free (remote_file);
1803 return -1;
1805 g_free (remote_file);
1806 return 0;
1809 static int
1810 smbfs_rename (vfs *me, char *a, char *b)
1812 smbfs_connection *sc;
1813 char *ra, *rb;
1814 char *p;
1815 int retval;
1817 if ((ra = smbfs_get_path (&sc, a)) == 0)
1818 return -1;
1820 if ((rb = smbfs_get_path (&sc, b)) == 0) {
1821 g_free (ra);
1822 return -1;
1825 p = ra;
1826 convert_path(&ra, FALSE);
1827 g_free (p);
1828 p = rb;
1829 convert_path(&rb, FALSE);
1830 g_free (p);
1832 retval = cli_rename(sc->cli, ra, rb);
1834 g_free (ra);
1835 g_free (rb);
1837 if (!retval) {
1838 message_2s (1, MSG_ERROR, _(" %s renaming files\n"),
1839 cli_errstr(sc->cli));
1840 return -1;
1842 return 0;
1845 static int
1846 smbfs_fstat (void *data, struct stat *buf)
1848 smbfs_handle *remote_handle = (smbfs_handle *)data;
1850 DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle->fnum));
1852 /* use left over from previous get_remote_stat, if available */
1853 if (single_entry)
1854 memcpy(buf, &single_entry->my_stat, sizeof(struct stat));
1855 else { /* single_entry not set up: bug */
1856 my_errno = EFAULT;
1857 return -EFAULT;
1859 return 0;
1862 vfs vfs_smbfs_ops = {
1863 NULL, /* This is place of next pointer */
1864 "smbfs",
1865 F_NET, /* flags */
1866 "smb:", /* prefix */
1867 NULL, /* data */
1868 0, /* errno */
1869 smbfs_init,
1870 NULL,
1871 smbfs_fill_names,
1872 NULL,
1874 smbfs_open,
1875 smbfs_close,
1876 smbfs_read,
1877 smbfs_write,
1879 smbfs_opendir,
1880 smbfs_readdir,
1881 smbfs_closedir,
1882 NULL,
1883 NULL,
1885 smbfs_stat,
1886 smbfs_lstat,
1887 smbfs_fstat,
1889 smbfs_chmod,
1890 smbfs_chown,
1891 smbfs_utime,
1893 smbfs_readlink,
1894 smbfs_symlink,
1895 smbfs_link,
1896 smbfs_unlink,
1898 smbfs_rename,
1899 smbfs_chdir,
1900 smbfs_errno,
1901 smbfs_lseek,
1902 smbfs_mknod,
1904 smbfs_getid,
1905 smbfs_nothingisopen,
1906 smbfs_free,
1908 NULL,
1909 NULL,
1911 smbfs_mkdir,
1912 smbfs_rmdir,
1913 NULL,
1914 smbfs_setctl
1916 MMAPNULL