small fix...
[midnight-commander.git] / vfs / smbfs.c
blob90c6e7a00a844a0eb92076460f48d85b397851f7
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 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License
9 as published by the Free Software Foundation; either version 2 of
10 the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* Namespace: exports vfs_smbfs_ops, smbfs_set_debug(), smbfs_set_debugf() */
22 #include <config.h>
23 #include <stdio.h>
24 #include <sys/types.h>
26 #undef USE_NCURSES /* Don't include *curses.h */
27 #include "utilvfs.h"
29 #undef PACKAGE_BUGREPORT
30 #undef PACKAGE_NAME
31 #undef PACKAGE_STRING
32 #undef PACKAGE_TARNAME
33 #undef PACKAGE_VERSION
35 #include "samba/include/config.h"
36 /* don't load crap in "samba/include/includes.h" we don't use and which
37 conflicts with definitions in other includes */
38 #undef HAVE_LIBREADLINE
39 #define NO_CONFIG_H
40 #undef VERSION
42 #include "samba/include/includes.h"
44 #include <string.h>
46 #include "vfs.h"
47 #include "smbfs.h"
48 #include "../src/dialog.h"
50 #define SMBFS_MAX_CONNECTIONS 16
51 static const char * const IPC = "IPC$";
52 static const char * const URL_HEADER = "/#smb:";
53 #define HEADER_LEN 6
55 static int my_errno;
56 static uint32 err;
58 /* stuff that is same with each connection */
59 extern int DEBUGLEVEL;
60 extern pstring myhostname;
61 extern pstring global_myname;
62 static int smbfs_open_connections = 0;
63 static gboolean got_user = FALSE;
64 static gboolean got_pass = FALSE;
65 static pstring password;
66 static pstring username;
68 static struct _smbfs_connection {
69 struct cli_state *cli;
70 struct in_addr dest_ip;
71 BOOL have_ip;
72 char *host; /* server name */
73 char *service; /* share name */
74 char *domain;
75 char *user;
76 char *home;
77 char *password;
78 int port;
79 int name_type;
80 time_t last_use;
81 } smbfs_connections [SMBFS_MAX_CONNECTIONS];
82 /* unique to each connection */
84 static struct cli_state * smbfs_do_connect (const char *server, char *share);
86 typedef struct _smbfs_connection smbfs_connection;
87 static smbfs_connection *current_bucket;
89 typedef struct {
90 struct cli_state *cli;
91 int fnum;
92 off_t nread;
93 uint16 attr;
94 } smbfs_handle;
96 static GSList *auth_list;
98 static void
99 authinfo_free (struct smb_authinfo const *a)
101 g_free (a->host);
102 g_free (a->share);
103 g_free (a->domain);
104 g_free (a->user);
105 wipe_password (a->password);
108 static void
109 authinfo_free_all ()
111 if (auth_list) {
112 g_slist_foreach (auth_list, (GFunc)authinfo_free, 0);
113 g_slist_free (auth_list);
114 auth_list = 0;
118 static gint
119 authinfo_compare_host_and_share (gconstpointer _a, gconstpointer _b)
121 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
122 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
124 if (!a->host || !a->share || !b->host || !b->share)
125 return 1;
126 if (strcmp (a->host, b->host) != 0)
127 return 1;
128 if (strcmp (a->share, b->share) != 0)
129 return 1;
130 return 0;
133 static gint
134 authinfo_compare_host (gconstpointer _a, gconstpointer _b)
136 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
137 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
139 if (!a->host || !b->host)
140 return 1;
141 if (strcmp (a->host, b->host) != 0)
142 return 1;
143 if (strcmp (a->share, IPC) != 0)
144 return 1;
145 return 0;
148 static void
149 authinfo_add (const char *host, const char *share, const char *domain,
150 const char *user, const char *password)
152 struct smb_authinfo *auth = g_new (struct smb_authinfo, 1);
154 if (!auth)
155 return;
157 /* Don't check for NULL, g_strdup already does. */
158 auth->host = g_strdup (host);
159 auth->share = g_strdup (share);
160 auth->domain = g_strdup (domain);
161 auth->user = g_strdup (user);
162 auth->password = g_strdup (password);
163 auth_list = g_slist_prepend (auth_list, auth);
166 static void
167 authinfo_remove (const char *host, const char *share)
169 struct smb_authinfo data;
170 struct smb_authinfo *auth;
171 GSList *list;
173 data.host = g_strdup (host);
174 data.share = g_strdup (share);
175 list = g_slist_find_custom (auth_list,
176 &data,
177 authinfo_compare_host_and_share);
178 g_free (data.host);
179 g_free (data.share);
180 if (!list)
181 return;
182 auth = list->data;
183 auth_list = g_slist_remove (auth_list, auth);
184 authinfo_free (auth);
187 /* Set authentication information in bucket. Return 1 if successful, else 0 */
188 /* Information in auth_list overrides user if pass is NULL. */
189 /* bucket->host and bucket->service must be valid. */
190 static int
191 bucket_set_authinfo (smbfs_connection *bucket,
192 const char *domain, const char *user, const char *pass,
193 int fallback_to_host)
195 struct smb_authinfo data;
196 struct smb_authinfo *auth;
197 GSList *list;
199 if (domain && user && pass) {
200 g_free (bucket->domain);
201 g_free (bucket->user);
202 g_free (bucket->password);
203 bucket->domain = g_strdup (domain);
204 bucket->user = g_strdup (user);
205 bucket->password = g_strdup (pass);
206 authinfo_remove (bucket->host, bucket->service);
207 authinfo_add (bucket->host, bucket->service,
208 domain, user, pass);
209 return 1;
212 data.host = bucket->host;
213 data.share = bucket->service;
214 list = g_slist_find_custom (auth_list, &data, authinfo_compare_host_and_share);
215 if (!list && fallback_to_host)
216 list = g_slist_find_custom (auth_list, &data, authinfo_compare_host);
217 if (list) {
218 auth = list->data;
219 bucket->domain = g_strdup (auth->domain);
220 bucket->user = g_strdup (auth->user);
221 bucket->password = g_strdup (auth->password);
222 return 1;
225 if (got_pass) {
226 bucket->domain = g_strdup (lp_workgroup ());
227 bucket->user = g_strdup (got_user ? username : user);
228 bucket->password = g_strdup (password);
229 return 1;
232 auth = vfs_smb_get_authinfo (bucket->host,
233 bucket->service,
234 (domain ? domain : lp_workgroup ()),
235 user);
236 if (auth) {
237 g_free (bucket->domain);
238 g_free (bucket->user);
239 g_free (bucket->password);
240 bucket->domain = g_strdup (auth->domain);
241 bucket->user = g_strdup (auth->user);
242 bucket->password = g_strdup (auth->password);
243 authinfo_remove (bucket->host, bucket->service);
244 auth_list = g_slist_prepend (auth_list, auth);
245 return 1;
247 return 0;
250 void
251 smbfs_set_debug (int arg)
253 DEBUGLEVEL = arg;
256 void
257 smbfs_set_debugf (const char *filename)
259 extern pstring debugf;
260 extern FILE *dbf;
261 if (DEBUGLEVEL > 0) {
262 FILE *outfile = fopen (filename, "w");
263 if (outfile) {
264 setup_logging ("", True); /* No needs for timestamp for each message */
265 dbf = outfile;
266 setbuf (dbf, NULL);
267 pstrcpy (debugf, filename);
272 /********************** The callbacks ******************************/
273 static int
274 smbfs_init (vfs * me)
276 char *servicesf = CONFIGDIR PATH_SEP_STR "smb.conf";
278 /* DEBUGLEVEL = 4; */
280 TimeInit ();
281 charset_initialise ();
283 DEBUG (3, ("smbfs_init(%s)\n", me->name));
285 if (!get_myname (myhostname, NULL))
286 DEBUG (0, ("Failed to get my hostname.\n"));
288 if (!lp_load (servicesf, True, False, False))
289 DEBUG (0, ("Cannot load %s - run testparm to debug it\n", servicesf));
291 codepage_initialise (lp_client_code_page ());
293 load_interfaces ();
295 if (getenv ("USER")) {
296 char *p;
298 pstrcpy (username, getenv ("USER"));
299 got_user = TRUE;
300 DEBUG (3, ("smbfs_init(): $USER:%s\n", username));
301 if ((p = strchr (username, '%'))) {
302 *p = 0;
303 pstrcpy (password, p + 1);
304 got_pass = TRUE;
305 memset (strchr (getenv ("USER"), '%') + 1, 'X', strlen (password));
306 DEBUG (3, ("smbfs_init(): $USER%%pass: %s%%%s\n",
307 username, password));
309 strupper (username);
311 if (getenv ("PASSWD")) {
312 pstrcpy (password, getenv ("PASSWD"));
313 got_pass = TRUE;
315 return 1;
318 static void
319 smbfs_fill_names (vfs *me, void (*func)(char *))
321 int i;
322 char *path;
323 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
324 if (smbfs_connections [i].cli) {
325 path = g_strconcat (URL_HEADER,
326 smbfs_connections[i].host,
327 "/", smbfs_connections[i].service,
328 NULL);
329 (*func)(path);
330 g_free (path);
335 #define CNV_LANG(s) dos_to_unix(s,False)
336 #define GNAL_VNC(s) unix_to_dos(s,False)
337 /* does same as do_get() in client.c */
338 /* called from vfs.c:1080, count = buffer size */
339 static int
340 smbfs_read (void *data, char *buffer, int count)
342 smbfs_handle *info = (smbfs_handle *) data;
343 int n;
345 DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%d)\n",
346 info->fnum, (int)info->nread, count));
347 n = cli_read(info->cli, info->fnum, buffer, info->nread, count);
348 if (n > 0)
349 info->nread += n;
350 return n;
353 static int
354 smbfs_write (void *data, char *buf, int nbyte)
356 smbfs_handle *info = (smbfs_handle *) data;
357 int n;
359 DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%d)\n",
360 info->fnum, (int)info->nread, nbyte));
361 n = cli_write(info->cli, info->fnum, 0, buf, info->nread, nbyte);
362 if (n > 0)
363 info->nread += n;
364 return n;
367 static int
368 smbfs_close (void *data)
370 smbfs_handle *info = (smbfs_handle *) data;
371 DEBUG (3, ("smbfs_close(fnum:%d)\n", info->fnum));
373 /* FIXME: Why too different cli have the same outbuf
374 * if file is copied to share
376 if (info->cli->outbuf == NULL) {
377 my_errno = EINVAL;
378 return -1;
380 #if 0
381 /* if imlementing archive_level: add rname to smbfs_handle */
382 if (archive_level >= 2 && (inf->attr & aARCH)) {
383 cli_setatr (info->cli, rname, info->attr & ~(uint16) aARCH, 0);
385 #endif
386 return (cli_close (info->cli, info->fnum) == True) ? 0 : -1;
389 static int
390 smbfs_errno (vfs *me)
392 DEBUG(3, ("smbfs_errno: %s\n", g_strerror(my_errno)));
393 return my_errno;
396 typedef struct dir_entry {
397 char *text;
398 struct dir_entry *next;
399 struct stat my_stat;
400 int merrno;
401 } dir_entry;
403 typedef struct {
404 gboolean server_list;
405 char *dirname;
406 char *path; /* the dir originally passed to smbfs_opendir */
407 smbfs_connection *conn;
408 dir_entry *entries;
409 dir_entry *current;
410 } opendir_info;
412 static opendir_info
413 *previous_info,
414 *current_info,
415 *current_share_info,
416 *current_server_info;
418 static gboolean first_direntry;
420 static dir_entry *
421 new_dir_entry (const char * name)
423 dir_entry *new_entry;
424 new_entry = g_new0 (dir_entry, 1);
425 new_entry->text = dos_to_unix (g_strdup (name), 1);
427 if (first_direntry) {
428 current_info->entries = new_entry;
429 first_direntry = FALSE;
430 } else {
431 current_info->current->next = new_entry;
433 current_info->current = new_entry;
435 return new_entry;
438 /* browse for shares on server */
439 static void
440 browsing_helper (const char *name, uint32 type, const char *comment, void *state)
442 char *typestr = "";
444 dir_entry *new_entry = new_dir_entry (name);
446 switch (type) {
447 case STYPE_DISKTREE:
448 typestr = "Disk";
449 /* show this as dir */
450 new_entry->my_stat.st_mode =
451 S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
452 S_IXOTH;
453 break;
454 case STYPE_PRINTQ:
455 typestr = "Printer";
456 break;
457 case STYPE_DEVICE:
458 typestr = "Device";
459 break;
460 case STYPE_IPC:
461 typestr = "IPC";
462 break;
464 DEBUG (3, ("\t%-15.15s%-10.10s%s\n", name, typestr, comment));
467 static void
468 loaddir_helper (file_info * finfo, const char *mask, void *entry)
470 dir_entry *new_entry = (dir_entry *) entry;
471 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
472 #if 0 /* I want to see dot files */
473 if (finfo->mode & aHIDDEN)
474 return; /* don't bother with hidden files, "~$" screws up mc */
475 #endif
476 if (!entry)
477 new_entry = new_dir_entry (finfo->name);
479 new_entry->my_stat.st_size = finfo->size;
480 new_entry->my_stat.st_mtime = finfo->mtime;
481 new_entry->my_stat.st_atime = finfo->atime;
482 new_entry->my_stat.st_ctime = finfo->ctime;
483 new_entry->my_stat.st_uid = finfo->uid;
484 new_entry->my_stat.st_gid = finfo->gid;
486 new_entry->my_stat.st_mode = /* rw-rw-rw */
487 S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH;
489 /* if (finfo->mode & aVOLID); nothing similar in real world */
490 if (finfo->mode & aDIR)
491 new_entry->my_stat.st_mode |= /* drwxrwxrwx */
492 S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
493 else
494 new_entry->my_stat.st_mode |= S_IFREG; /* if not dir, regular file? */
495 /* if (finfo->mode & aARCH); DOS archive */
496 /* if (finfo->mode & aHIDDEN); like a dot file? */
497 /* if (finfo->mode & aSYSTEM); like a kernel? */
498 if (finfo->mode & aRONLY)
499 new_entry->my_stat.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
501 DEBUG (entry ? 3 : 6, (" %-30s%7.7s%8.0f %s",
502 CNV_LANG (finfo->name),
503 attrib_string (finfo->mode),
504 (double) finfo->size,
505 asctime (LocalTime (&t))));
508 /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
509 static int
510 convert_path(char **remote_file, gboolean trailing_asterik)
512 char *p, *my_remote;
514 my_remote = *remote_file;
515 if (strncmp (my_remote, URL_HEADER, HEADER_LEN) == 0) { /* if passed directly */
516 my_remote += 6;
517 if (*my_remote == '/') /* from server browsing */
518 my_remote++;
519 p = strchr(my_remote, '/');
520 if (p)
521 my_remote = p+1; /* advance to end of server name */
524 if (*my_remote == '/')
525 my_remote++; /* strip off leading '/' */
526 p = strchr(my_remote, '/');
527 if (p)
528 my_remote = p; /* strip off share/service name */
529 /* create remote filename as understood by smb clientgen */
530 p = *remote_file = g_strconcat (my_remote, trailing_asterik ? "/*" : "", 0);
531 unix_to_dos (*remote_file, 1);
532 while ((p = strchr(p, '/')))
533 *p = '\\';
534 return 0;
537 static void
538 server_browsing_helper (const char *name, uint32 m, const char *comment, void *state)
540 dir_entry *new_entry = new_dir_entry (name);
542 /* show this as dir */
543 new_entry->my_stat.st_mode =
544 S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP | S_IXOTH;
546 DEBUG (3, ("\t%-16.16s %s\n", name, comment));
549 static BOOL
550 reconnect(smbfs_connection *conn, int *retries)
552 char *host;
553 DEBUG(3, ("RECONNECT\n"));
555 if (*(conn->host) == 0)
556 host = g_strdup(conn->cli->desthost); /* server browsing */
557 else
558 host = g_strdup(conn->host);
560 cli_shutdown(conn->cli);
562 if (!(conn->cli = smbfs_do_connect(host, conn->service))) {
563 message_2s (1, MSG_ERROR,
564 _(" reconnect to %s failed\n "), conn->host);
565 g_free(host);
566 return False;
568 g_free(host);
569 if (++(*retries) == 2)
570 return False;
571 return True;
574 static BOOL
575 smb_send(struct cli_state *cli)
577 size_t len;
578 size_t nwritten=0;
579 ssize_t ret;
581 len = smb_len(cli->outbuf) + 4;
583 while (nwritten < len) {
584 ret = write_socket(cli->fd, cli->outbuf+nwritten, len - nwritten);
585 if (ret <= 0 && errno == EPIPE)
586 return False;
587 nwritten += ret;
590 return True;
593 /****************************************************************************
594 See if server has cut us off by checking for EPIPE when writing.
595 Taken from cli_chkpath()
596 ****************************************************************************/
597 static BOOL
598 chkpath(struct cli_state *cli, char *path, BOOL send_only)
600 fstring path2;
601 char *p;
603 fstrcpy(path2,path);
604 unix_to_dos (path2, 1);
605 trim_string(path2,NULL,"\\");
606 if (!*path2) *path2 = '\\';
608 memset(cli->outbuf,'\0',smb_size);
609 set_message(cli->outbuf,0,4 + strlen(path2),True);
610 SCVAL(cli->outbuf,smb_com,SMBchkpth);
611 SSVAL(cli->outbuf,smb_tid,cli->cnum);
613 cli->rap_error = 0;
614 cli->nt_error = 0;
615 SSVAL(cli->outbuf,smb_pid,cli->pid);
616 SSVAL(cli->outbuf,smb_uid,cli->vuid);
617 SSVAL(cli->outbuf,smb_mid,cli->mid);
618 if (cli->protocol > PROTOCOL_CORE) {
619 SCVAL(cli->outbuf,smb_flg,0x8);
620 SSVAL(cli->outbuf,smb_flg2,0x1);
623 p = smb_buf(cli->outbuf);
624 *p++ = 4;
625 fstrcpy(p,path2);
627 if (!smb_send(cli)) {
628 DEBUG(3, ("chkpath: couldnt send\n"));
629 return False;
631 if (send_only) {
632 client_receive_smb(cli->fd, cli->inbuf, cli->timeout);
633 DEBUG(3, ("chkpath: send only OK\n"));
634 return True; /* just testing for EPIPE */
636 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout)) {
637 DEBUG(3, ("chkpath: receive error\n"));
638 return False;
640 if ((my_errno = cli_error(cli, NULL, NULL, NULL))) {
641 if (my_errno == 20 || my_errno == 13)
642 return True; /* ignore if 'not a directory' error */
643 DEBUG(3, ("chkpath: cli_error: %s\n", g_strerror(my_errno)));
644 return False;
647 return True;
650 #if 1
651 static int
652 fs (const char *text)
654 const char *p = text;
655 int count = 0;
657 while ((p = strchr(p, '/')) != NULL) {
658 count++;
659 p++;
661 if (count == 1)
662 return strlen(text);
663 return count;
665 #endif
667 static int
668 smbfs_loaddir (opendir_info *smbfs_info)
670 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
671 int servlen = strlen(smbfs_info->conn->service);
672 char *my_dirname = smbfs_info->dirname;
674 DEBUG(3, ("smbfs_loaddir: dirname:%s\n", my_dirname));
675 first_direntry = TRUE;
677 if (current_info) {
678 DEBUG(3, ("smbfs_loaddir: new:'%s', cached:'%s'\n", my_dirname, current_info->dirname));
679 /* if new desired dir is longer than cached in current_info */
680 if (fs(my_dirname) > fs(current_info->dirname)) {
681 DEBUG(3, ("saving to previous_info\n"));
682 previous_info = current_info;
686 current_info = smbfs_info;
688 if (strcmp(my_dirname, "/") == 0) {
689 if (!strcmp(smbfs_info->path, URL_HEADER)) {
690 DEBUG(6, ("smbfs_loaddir: browsing %s\n", IPC));
691 /* browse for servers */
692 if (!cli_NetServerEnum(smbfs_info->conn->cli, smbfs_info->conn->domain,
693 SV_TYPE_ALL, server_browsing_helper, NULL))
694 return 0;
695 else
696 current_server_info = smbfs_info;
697 smbfs_info->server_list = TRUE;
698 } else {
699 /* browse for shares */
700 if (cli_RNetShareEnum(smbfs_info->conn->cli, browsing_helper, NULL) < 1)
701 return 0;
702 else
703 current_share_info = smbfs_info;
705 goto done;
708 /* do regular directory listing */
709 if(strncmp(smbfs_info->conn->service, my_dirname+1, servlen) == 0) {
710 /* strip share name from dir */
711 char *p = my_dirname = g_strdup(my_dirname + servlen);
712 *p = '/';
713 convert_path(&my_dirname, TRUE);
714 g_free (p);
715 } else
716 convert_path(&my_dirname, TRUE);
718 DEBUG(6, ("smbfs_loaddir: service: %s\n", smbfs_info->conn->service));
719 DEBUG(6, ("smbfs_loaddir: cli->share: %s\n", smbfs_info->conn->cli->share));
720 DEBUG(6, ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname));
721 /* do file listing: cli_list returns number of files */
722 if (cli_list(
723 smbfs_info->conn->cli, my_dirname, attribute, loaddir_helper, NULL) < 0) {
724 /* cli_list returns -1 if directory empty or cannot read socket */
725 my_errno = cli_error(smbfs_info->conn->cli, NULL, &err, NULL);
726 g_free (my_dirname);
727 return 0;
729 if (*(my_dirname) == 0)
730 smbfs_info->dirname = smbfs_info->conn->service;
731 g_free (my_dirname);
732 /* do_dskattr(); */
734 done:
735 /* current_info->parent = smbfs_info->dirname; */
737 smbfs_info->current = smbfs_info->entries;
738 return 1; /* 1 = ok */
741 #ifdef SMBFS_FREE_DIR
742 static void
743 smbfs_free_dir (dir_entry *de)
745 if (!de) return;
747 smbfs_free_dir (de->next);
748 g_free (de->text);
749 g_free (de);
751 #endif
754 /* The readdir routine loads the complete directory */
755 /* It's too slow to ask the server each time */
756 /* It now also sends the complete lstat information for each file */
757 static void *
758 smbfs_readdir(void *info)
760 static union vfs_dirent smbfs_readdir_data;
761 static char *const dirent_dest = smbfs_readdir_data.dent.d_name;
762 opendir_info *smbfs_info = (opendir_info *) info;
764 DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info->dirname));
766 if (!smbfs_info->entries)
767 if (!smbfs_loaddir(smbfs_info))
768 return NULL;
770 if (smbfs_info->current == 0) { /* reached end of dir entries */
771 DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
772 #ifdef SMBFS_FREE_DIR
773 smbfs_free_dir(smbfs_info->entries);
774 smbfs_info->entries = 0;
775 #endif
776 return NULL;
778 strncpy(dirent_dest, smbfs_info->current->text, MC_MAXPATHLEN);
779 dirent_dest[MC_MAXPATHLEN] = 0;
780 smbfs_info->current = smbfs_info->current->next;
782 compute_namelen(&smbfs_readdir_data.dent);
784 return &smbfs_readdir_data;
787 static int
788 smbfs_closedir (void *info)
790 opendir_info *smbfs_info = (opendir_info *) info;
791 /* dir_entry *p, *q; */
793 DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info->dirname));
794 /* CLOSE HERE */
796 /* for (p = smbfs_info->entries; p;){
797 q = p;
798 p = p->next;
799 g_free (q->text);
800 g_free (q);
802 g_free (info); */
803 return 0;
806 static int
807 smbfs_chmod (vfs *me, char *path, int mode)
809 DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path, mode));
810 /* my_errno = EOPNOTSUPP;
811 return -1; */ /* cant chmod on smb filesystem */
812 return 0; /* make mc happy */
815 static int
816 smbfs_chown (vfs *me, char *path, int owner, int group)
818 DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path, owner, group));
819 my_errno = EOPNOTSUPP; /* ready for your labotomy? */
820 return -1;
823 static int
824 smbfs_utime (vfs *me, char *path, struct utimbuf *times)
826 DEBUG(3, ("smbfs_utime(path:%s)\n", path));
827 my_errno = EOPNOTSUPP;
828 return -1;
831 static int
832 smbfs_readlink (vfs *me, char *path, char *buf, int size)
834 DEBUG(3, ("smbfs_readlink(path:%s, buf:%s, size:%d)\n", path, buf, size));
835 my_errno = EOPNOTSUPP;
836 return -1; /* no symlinks on smb filesystem? */
839 static int
840 smbfs_symlink (vfs *me, char *n1, char *n2)
842 DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1, n2));
843 my_errno = EOPNOTSUPP;
844 return -1; /* no symlinks on smb filesystem? */
847 /* Extract the hostname and username from the path */
848 /* path is in the form: hostname:user/remote-dir */
849 #if 0
850 static char *
851 smbfs_get_host_and_username
852 (char **path, char **host, char **user, int *port, char **pass)
854 /* char *p, *ret; */
855 char *ret;
857 ret = vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0);
859 #if 0
860 if ((p = strchr(*path, '@'))) /* user:pass@server */
861 *path = ++p; /* don't want user:pass@ in path */
862 if ((p = strchr(ret, '@'))) /* user:pass@server */
863 ret = ++p; /* don't want user:pass@ in path */
864 #endif
866 return ret;
868 #else
869 #define smbfs_get_host_and_username(path, host, user, port, pass) \
870 vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
871 #endif
872 /*****************************************************
873 return a connection to a SMB server
874 current_bucket needs to be set before calling
875 *******************************************************/
876 static struct cli_state *
877 smbfs_do_connect (const char *server, char *share)
879 struct cli_state *c;
880 struct nmb_name called, calling;
881 struct in_addr ip;
882 extern struct in_addr ipzero;
884 DEBUG(3, ("smbfs_do_connect(%s, %s)\n", server, share));
885 if (*share == '\\') {
886 server = share+2;
887 share = strchr(server,'\\');
888 if (!share) return NULL;
889 *share = 0;
890 share++;
893 make_nmb_name(&calling, global_myname, 0x0);
894 make_nmb_name(&called , server, current_bucket->name_type);
896 for (;;) {
898 ip = (current_bucket->have_ip) ? current_bucket->dest_ip : ipzero;
900 /* have to open a new connection */
901 if (!(c = cli_initialise(NULL))) {
902 my_errno = ENOMEM;
903 return NULL;
906 pwd_init(&(c->pwd)); /* should be moved into cli_initialise()? */
907 pwd_set_cleartext(&(c->pwd), current_bucket->password);
909 if ((cli_set_port(c, current_bucket->port) == 0) ||
910 !cli_connect(c, server, &ip)) {
911 DEBUG(1, ("Connection to %s failed\n", server));
912 break;
915 if (!cli_session_request(c, &calling, &called)) {
916 my_errno = cli_error(c, NULL, &err, NULL);
917 DEBUG(1, ("session request to %s failed\n", called.name));
918 cli_shutdown(c);
919 if (strcmp(called.name, "*SMBSERVER")) {
920 make_nmb_name(&called , "*SMBSERVER", 0x20);
921 continue;
923 return NULL;
926 DEBUG(3, (" session request ok\n"));
928 if (!cli_negprot(c)) {
929 DEBUG(1, ("protocol negotiation failed\n"));
930 break;
933 if (!cli_session_setup(c, current_bucket->user,
934 current_bucket->password, strlen(current_bucket->password),
935 current_bucket->password, strlen(current_bucket->password),
936 current_bucket->domain)) {
937 DEBUG(1,("session setup failed: %s\n", cli_errstr(c)));
938 authinfo_remove (server, share);
939 break;
942 if (*c->server_domain || *c->server_os || *c->server_type)
943 DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
944 c->server_domain,c->server_os,c->server_type));
946 DEBUG(3, (" session setup ok\n"));
948 if (!cli_send_tconX(c, share, "?????",
949 current_bucket->password, strlen(current_bucket->password)+1)) {
950 DEBUG(1,("%s: tree connect failed: %s\n", share, cli_errstr(c)));
951 break;
954 DEBUG(3, (" tconx ok\n"));
956 my_errno = 0;
957 return c;
960 my_errno = cli_error(c, NULL, &err, NULL);
961 cli_shutdown(c);
962 return NULL;
966 static int
967 get_master_browser(char **host)
969 int count;
970 struct in_addr *ip_list, bcast_addr;
971 extern struct in_addr ipzero;
973 /* does port = 137 for win95 master browser? */
974 int fd= open_socket_in( SOCK_DGRAM, 0, 3,
975 interpret_addr(lp_socket_address()), True );
976 if (fd == -1)
977 return 0;
978 set_socket_options(fd, "SO_BROADCAST");
979 ip_list = iface_bcast(ipzero);
980 bcast_addr = *ip_list;
981 if ((ip_list = name_query(fd, "\01\02__MSBROWSE__\02", 1, True,
982 True, bcast_addr, &count, NULL))) {
983 if (!count)
984 return 0;
985 /* just return first master browser */
986 *host = g_strdup(inet_ntoa(ip_list[0]));
987 return 1;
989 return 0;
992 static void
993 free_bucket (smbfs_connection *bucket)
995 g_free (bucket->host);
996 g_free (bucket->service);
997 g_free (bucket->domain);
998 g_free (bucket->user);
999 wipe_password (bucket->password);
1000 if (bucket->home) g_free (bucket->home);
1001 memset (bucket, 0, sizeof (smbfs_connection));
1004 static smbfs_connection *
1005 smbfs_get_free_bucket ()
1007 int i;
1009 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++)
1010 if (!smbfs_connections [i].cli) return &smbfs_connections [i];
1012 { /* search for most dormant connection */
1013 int oldest = 0; /* index */
1014 time_t oldest_time = smbfs_connections[0].last_use;
1015 for (i = 1; i < SMBFS_MAX_CONNECTIONS; i++) {
1016 if (smbfs_connections[i].last_use < oldest_time) {
1017 oldest_time = smbfs_connections[i].last_use;
1018 oldest = i;
1021 cli_shutdown(smbfs_connections[oldest].cli);
1022 free_bucket (&smbfs_connections[oldest]);
1023 return &smbfs_connections[oldest];
1026 /* This can't happend, since we have checked for max connections before */
1027 vfs_die("Internal error: smbfs_get_free_bucket");
1028 return 0; /* shut up, stupid gcc */
1031 /* This routine keeps track of open connections */
1032 /* Returns a connected socket to host */
1033 static smbfs_connection *
1034 smbfs_open_link(char *host, char *path, const char *user, int *port, char *this_pass)
1036 int i;
1037 smbfs_connection *bucket;
1038 pstring service;
1039 struct in_addr *dest_ip = NULL;
1041 DEBUG(3, ("smbfs_open_link(host:%s, path:%s)\n", host, path));
1043 if (strcmp(host, path) == 0) /* if host & path are same: */
1044 pstrcpy(service, IPC); /* setup for browse */
1045 else { /* get share name from path, path starts with server name */
1046 char *p;
1047 if ((p = strchr(path, '/'))) /* get share aka */
1048 pstrcpy(service, ++p); /* service name from path */
1049 else
1050 pstrcpy(service, "");
1051 /* now check for trailing directory/filenames */
1052 p = strchr(service, '/');
1053 if (p)
1054 *p = 0; /* cut off dir/files: sharename only */
1056 DEBUG(6, ("smbfs_open_link: service from path:%s\n", service));
1059 if (got_user)
1060 user = username; /* global from getenv */
1062 /* Is the link actually open? */
1063 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1064 if (!smbfs_connections[i].cli)
1065 continue;
1066 if ((strcmp (host, smbfs_connections [i].host) == 0) &&
1067 (strcmp (user, smbfs_connections [i].user) == 0) &&
1068 (strcmp (service, smbfs_connections [i].service) == 0)) {
1069 int retries = 0;
1070 BOOL inshare = (*host != 0 && *path != 0 && strchr(path, '/'));
1071 /* check if this connection has died */
1072 while (!chkpath(smbfs_connections[i].cli, "\\", !inshare)) {
1073 if (!reconnect(&smbfs_connections[i], &retries))
1074 return 0;
1076 DEBUG(6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i));
1077 current_bucket = &smbfs_connections[i];
1078 smbfs_connections [i].last_use = time(NULL);
1079 return &smbfs_connections [i];
1081 /* connection not found, find if we have ip for new connection */
1082 if (strcmp (host, smbfs_connections [i].host) == 0)
1083 dest_ip = &smbfs_connections[i].cli->dest_ip;
1086 /* make new connection */
1087 bucket = smbfs_get_free_bucket ();
1088 bucket->name_type = 0x20;
1089 bucket->home = 0;
1090 bucket->port = *port;
1091 bucket->have_ip = False;
1092 if (dest_ip) {
1093 bucket->have_ip = True;
1094 bucket->dest_ip = *dest_ip;
1096 current_bucket = bucket;
1098 bucket->user = g_strdup(user);
1099 bucket->service = g_strdup (service);
1101 if (!(*host)) { /* if blank host name, browse for servers */
1102 if (!get_master_browser(&host)) /* set host to ip of master browser */
1103 return 0; /* couldnt find master browser? */
1104 g_free (host);
1105 bucket->host = g_strdup(""); /* blank host means master browser */
1106 } else
1107 bucket->host = g_strdup(host);
1109 if (!bucket_set_authinfo (bucket,
1110 0, /* domain currently not used */
1111 user,
1112 this_pass,
1114 return 0;
1116 /* connect to share */
1117 while (!(bucket->cli = smbfs_do_connect(host, service))) {
1119 if (my_errno != EPERM)
1120 return 0;
1121 message_1s (1, MSG_ERROR,
1122 _(" Authentication failed "));
1124 /* authentication failed, try again */
1125 authinfo_remove (bucket->host, bucket->service);
1126 if (!bucket_set_authinfo (bucket,
1127 bucket->domain,
1128 bucket->user,
1131 return 0;
1135 smbfs_open_connections++;
1136 DEBUG(3, ("smbfs_open_link:smbfs_open_connections: %d\n",
1137 smbfs_open_connections));
1138 return bucket;
1141 static char *
1142 smbfs_get_path(smbfs_connection **sc, char *path)
1144 char *user, *host, *remote_path, *pass;
1145 int port = SMB_PORT;
1147 DEBUG(3, ("smbfs_get_path(%s)\n", path));
1148 if (strncmp (path, URL_HEADER, HEADER_LEN))
1149 return NULL;
1150 path += HEADER_LEN;
1152 if (*path == '/') /* '/' leading server name */
1153 path++; /* probably came from server browsing */
1155 if ((remote_path = smbfs_get_host_and_username(
1156 &path, &host, &user, &port, &pass)))
1157 if ((*sc = smbfs_open_link (host, path, user, &port, pass)) == NULL){
1158 g_free (remote_path);
1159 remote_path = NULL;
1161 g_free (host);
1162 g_free (user);
1163 if (pass) wipe_password (pass);
1165 if (!remote_path) return NULL;
1167 /* NOTE: tildes are deprecated. See ftpfs.c */
1169 int f = !strcmp( remote_path, "/~" );
1170 if (f || !strncmp( remote_path, "/~/", 3 )) {
1171 char *s;
1172 s = concat_dir_and_file( (*sc)->home, remote_path +3-f );
1173 g_free (remote_path);
1174 return s;
1177 return remote_path;
1180 #if 0
1181 static int
1182 is_error (int result, int errno_num)
1184 if (!(result == -1))
1185 return my_errno = 0;
1186 else
1187 my_errno = errno_num;
1188 return 1;
1190 #endif
1192 static void *
1193 smbfs_opendir (vfs *me, char *dirname)
1195 opendir_info *smbfs_info;
1196 smbfs_connection *sc;
1197 char *remote_dir;
1199 DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname));
1201 if (!(remote_dir = smbfs_get_path (&sc, dirname)))
1202 return NULL;
1204 /* FIXME: where freed? */
1205 smbfs_info = g_new (opendir_info, 1);
1206 smbfs_info->server_list = FALSE;
1207 smbfs_info->path = g_strdup(dirname); /* keep original */
1208 smbfs_info->dirname = remote_dir;
1209 smbfs_info->conn = sc;
1210 smbfs_info->entries = 0;
1211 smbfs_info->current = 0;
1213 return smbfs_info;
1216 static int
1217 fake_server_stat(const char *server_url, const char *path, struct stat *buf)
1219 dir_entry *dentry;
1220 char *p;
1222 if ((p = strrchr(path, '/')))
1223 path = p + 1; /* advance until last '/' */
1225 if (!current_info->entries) {
1226 if (!smbfs_loaddir(current_info)); /* browse host */
1227 return -1;
1230 if (current_info->server_list == True) {
1231 dentry = current_info->entries;
1232 DEBUG(4, ("fake stat for SERVER \"%s\"\n", path));
1233 while (dentry) {
1234 if (strcmp(dentry->text, path) == 0) {
1235 DEBUG(4, ("fake_server_stat: %s:%4o\n",
1236 dentry->text, dentry->my_stat.st_mode));
1237 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1238 return 0;
1240 dentry = dentry->next;
1243 my_errno = ENOENT;
1244 return -1;
1247 static int
1248 fake_share_stat(const char *server_url, const char *path, struct stat *buf)
1250 dir_entry *dentry;
1251 if (strlen(path) < strlen(server_url))
1252 return -1;
1253 path += strlen(server_url); /* we only want share name */
1254 path++;
1256 if (*path == '/') /* '/' leading server name */
1257 path++; /* probably came from server browsing */
1259 if (!current_share_info->entries) {
1260 if (!smbfs_loaddir(current_share_info)); /* browse host */
1261 return -1;
1263 dentry = current_share_info->entries;
1264 DEBUG(3, ("fake_share_stat: %s on %s\n", path, server_url));
1265 while (dentry) {
1266 if (strcmp(dentry->text, path) == 0) {
1267 DEBUG(6, ("fake_share_stat: %s:%4o\n",
1268 dentry->text, dentry->my_stat.st_mode));
1269 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1270 return 0;
1272 dentry = dentry->next;
1274 my_errno = ENOENT;
1275 return -1;
1278 /* stat a single file, get_remote_stat callback */
1279 static dir_entry *single_entry;
1281 /* stat a single file */
1282 static int
1283 get_remote_stat (smbfs_connection * sc, char *path, struct stat *buf)
1285 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
1286 char *mypath = path;
1288 DEBUG (3, ("get_remote_stat(): mypath:%s\n", mypath));
1290 convert_path (&mypath, FALSE);
1292 #if 0 /* single_entry is never free()d now. And only my_stat is used */
1293 single_entry = g_new (dir_entry, 1);
1295 single_entry->text = dos_to_unix (g_strdup (finfo->name), 1);
1297 single_entry->next = 0;
1298 #endif
1299 if (!single_entry)
1300 single_entry = g_new0 (dir_entry, 1);
1302 if (cli_list
1303 (sc->cli, mypath, attribute, loaddir_helper, single_entry) < 1) {
1304 my_errno = ENOENT;
1305 g_free (mypath);
1306 return -1; /* cli_list returns number of files */
1309 memcpy (buf, &single_entry->my_stat, sizeof (struct stat));
1311 /* don't free here, use for smbfs_fstat() */
1312 /* g_free(single_entry->text);
1313 g_free(single_entry); */
1314 g_free (mypath);
1315 return 0;
1318 static int
1319 search_dir_entry (dir_entry *dentry, const char *text, struct stat *buf)
1321 while (dentry) {
1322 if (strcmp(text, dentry->text) == 0) {
1323 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1324 memcpy(&single_entry->my_stat, &dentry->my_stat,
1325 sizeof(struct stat));
1326 return 0;
1328 dentry = dentry->next;
1330 return -1;
1333 static int
1334 get_stat_info (smbfs_connection * sc, char *path, struct stat *buf)
1336 char *p;
1337 #if 0
1338 dir_entry *dentry = current_info->entries;
1339 #endif
1340 const char *mypath = path;
1342 mypath++; /* cut off leading '/' */
1343 if ((p = strrchr (mypath, '/')))
1344 mypath = p + 1; /* advance until last file/dir name */
1345 DEBUG (3, ("get_stat_info: mypath:%s, current_info->dirname:%s\n",
1346 mypath, current_info->dirname));
1347 #if 0
1348 if (!dentry) {
1349 DEBUG (1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n",
1350 current_info->dirname, path));
1351 return -1;
1353 #endif
1354 if (!single_entry) /* when found, this will be written too */
1355 single_entry = g_new (dir_entry, 1);
1356 if (search_dir_entry (current_info->entries, mypath, buf) == 0) {
1357 return 0;
1359 /* now try to identify mypath as PARENT dir */
1361 char *mdp;
1362 char *mydir;
1363 mdp = mydir = g_strdup (current_info->dirname);
1364 if ((p = strrchr (mydir, '/')))
1365 *p = 0; /* advance util last '/' */
1366 if ((p = strrchr (mydir, '/')))
1367 mydir = p + 1; /* advance util last '/' */
1368 if (strcmp (mydir, mypath) == 0) { /* fake a stat for ".." */
1369 memset (buf, 0, sizeof (struct stat));
1370 buf->st_mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH;
1371 memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
1372 g_free (mdp);
1373 DEBUG (1, (" PARENT:found in %s\n", current_info->dirname));
1374 return 0;
1376 g_free (mdp);
1378 /* now try to identify as CURRENT dir? */
1380 char *dnp = current_info->dirname;
1381 DEBUG (6, ("get_stat_info: is %s current dir? this dir is: %s\n",
1382 mypath, current_info->dirname));
1383 if (*dnp == '/')
1384 dnp++;
1385 else {
1386 return -1;
1388 if (strcmp (mypath, dnp) == 0) {
1389 memset (buf, 0, sizeof (struct stat));
1390 buf->st_mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH;
1391 memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
1392 DEBUG (1, (" CURRENT:found in %s\n", current_info->dirname));
1393 return 0;
1396 DEBUG (3, ("'%s' not found in current_info '%s'\n", path,
1397 current_info->dirname));
1398 /* try to find this in the PREVIOUS listing */
1399 if (previous_info) {
1400 if (search_dir_entry (previous_info->entries, mypath, buf) == 0)
1401 return 0;
1402 DEBUG (3, ("'%s' not found in previous_info '%s'\n", path,
1403 previous_info->dirname));
1405 /* try to find this in the SHARE listing */
1406 if (current_share_info) {
1407 if (search_dir_entry (current_share_info->entries, mypath, buf) == 0)
1408 return 0;
1409 DEBUG (3, ("'%s' not found in share_info '%s'\n", path,
1410 current_share_info->dirname));
1412 /* try to find this in the SERVER listing */
1413 if (current_server_info) {
1414 if (search_dir_entry (current_server_info->entries, mypath, buf) == 0)
1415 return 0;
1416 DEBUG (3, ("'%s' not found in server_info '%s'\n", path,
1417 current_server_info->dirname));
1419 /* nothing found. get stat file info from server */
1420 return get_remote_stat (sc, path, buf);
1423 static int
1424 smbfs_chdir (vfs *me, char *path)
1426 char *remote_dir;
1427 smbfs_connection *sc;
1429 DEBUG(3, ("smbfs_chdir(path:%s)\n", path));
1430 if (!(remote_dir = smbfs_get_path (&sc, path)))
1431 return -1;
1432 g_free (remote_dir);
1434 return 0;
1437 static int
1438 loaddir(vfs *me, const char *path)
1440 void *info;
1441 char *mypath, *p;
1443 mypath = g_strdup(path);
1444 p = strrchr(mypath, '/');
1446 if (p > mypath)
1447 *p = 0;
1448 DEBUG(6, ("loaddir(%s)\n", mypath));
1449 smbfs_chdir(me, mypath);
1450 info = smbfs_opendir (me, mypath);
1451 g_free(mypath);
1452 if (!info)
1453 return -1;
1454 smbfs_readdir(info);
1455 smbfs_loaddir(info);
1456 return 0;
1459 static int
1460 smbfs_stat (vfs *me, char *path, struct stat *buf)
1462 char *remote_dir;
1463 smbfs_connection *sc;
1464 pstring server_url;
1465 char *service, *pp;
1466 const char *p;
1468 DEBUG(3, ("smbfs_stat(path:%s)\n", path));
1470 #if 0
1471 if (p = strchr(path, '@')) /* user:pass@server */
1472 path = ++p; /* don't want user:pass@ in path */
1473 #endif
1475 if (!current_info) {
1476 DEBUG(1, ("current_info = NULL: "));
1477 if (loaddir(me, path) < 0)
1478 return -1;
1481 pstrcpy(server_url, URL_HEADER);
1482 pstrcat(server_url, current_bucket->host);
1484 /* check if stating server */
1485 p = path;
1486 if (strncmp(p, URL_HEADER, HEADER_LEN)) {
1487 DEBUG(1, ("'%s' doesnt start with '%s' (length %d)\n",
1488 p, URL_HEADER, HEADER_LEN));
1489 return -1;
1492 p += HEADER_LEN;
1493 if (*p == '/')
1494 p++;
1495 pp = strchr(p, '/'); /* advance past next '/' */
1496 if (!pp) {
1497 if (!current_info->server_list) {
1498 if (loaddir(me, path) < 0)
1499 return -1;
1501 return fake_server_stat(server_url, path, buf);
1503 if (!strchr(++pp, '/')) {
1504 return fake_share_stat(server_url, path, buf);
1507 /* stating inside share at this point */
1508 if (!(remote_dir = smbfs_get_path (&sc, path))) /* connects if necessary */
1509 return -1;
1510 g_free (remote_dir);
1512 int hostlen = strlen(current_bucket->host);
1513 char *pp = path + strlen(path)-hostlen;
1514 char *sp = server_url + strlen(server_url)-hostlen;
1516 if (strcmp(sp, pp) == 0) {
1517 /* make server name appear as directory */
1518 DEBUG(1, ("smbfs_stat: showing server as directory\n"));
1519 memset(buf, 0, sizeof(struct stat));
1520 buf->st_mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH;
1521 return 0;
1524 /* check if current_info is in share requested */
1525 p = service = g_strdup(p);
1526 pp = strchr(p, '/');
1527 if (pp) {
1528 p = ++pp; /* advance past server name */
1529 pp = strchr(p, '/');
1531 if (pp)
1532 *pp = 0; /* cut off everthing after service name */
1533 else
1534 p = IPC; /* browsing for services */
1535 pp = current_info->dirname;
1536 if (*pp == '/');
1537 pp++;
1538 if (strncmp(p, pp, strlen(p)) != 0) {
1539 DEBUG(6, ("desired '%s' is not loaded, we have '%s'\n", p, pp));
1540 if (loaddir(me, path) < 0) {
1541 g_free (service);
1542 return -1;
1544 DEBUG(6, ("loaded dir: '%s'\n", current_info->dirname));
1546 g_free(service);
1547 /* stat dirs & files under shares now */
1548 return get_stat_info(sc, path, buf);
1551 #define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */
1553 static int
1554 smbfs_lseek (void *data, off_t offset, int whence)
1556 DEBUG(3, ("smbfs_lseek()\n"));
1557 my_errno = EOPNOTSUPP;
1558 return -1;
1561 static int
1562 smbfs_mknod (vfs *me, char *path, int mode, int dev)
1564 DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%d)\n", path, mode, dev));
1565 my_errno = EOPNOTSUPP;
1566 return -1;
1569 static int
1570 smbfs_mkdir (vfs *me, char *path, mode_t mode)
1572 smbfs_connection *sc;
1573 char *remote_file;
1575 DEBUG(3, ("smbfs_mkdir(path:%s, mode:%d)\n", path, mode));
1576 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1577 return -1;
1578 g_free (remote_file);
1579 convert_path(&path, FALSE);
1581 if (!cli_mkdir(sc->cli, path)) {
1582 my_errno = cli_error(sc->cli, NULL, &err, NULL);
1583 message_3s (1, MSG_ERROR, _(" Error %s creating directory %s "),
1584 cli_errstr(sc->cli), CNV_LANG(path));
1585 g_free (path);
1586 return -1;
1588 g_free (path);
1589 return 0;
1592 static int
1593 smbfs_rmdir (vfs *me, char *path)
1595 smbfs_connection *sc;
1596 char *remote_file;
1598 DEBUG(3, ("smbfs_rmdir(path:%s)\n", path));
1599 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1600 return -1;
1601 g_free (remote_file);
1602 convert_path(&path, FALSE);
1604 if (!cli_rmdir(sc->cli, path)) {
1605 my_errno = cli_error(sc->cli, NULL, &err, NULL);
1606 message_3s (1, MSG_ERROR, _(" Error %s removing directory %s "),
1607 cli_errstr(sc->cli), CNV_LANG(path));
1608 g_free (path);
1609 return -1;
1612 g_free (path);
1613 return 0;
1616 static int
1617 smbfs_link (vfs *me, char *p1, char *p2)
1619 DEBUG(3, ("smbfs_link(p1:%s, p2:%s)\n", p1, p2));
1620 my_errno = EOPNOTSUPP;
1621 return -1;
1624 /* We do not free anything right now: we free resources when we run
1625 * out of them
1627 static vfsid
1628 smbfs_getid (vfs *me, char *p, struct vfs_stamping **parent)
1630 *parent = NULL;
1631 DEBUG(3, ("smbfs_getid(p:%s)\n", p));
1633 return (vfsid) -1;
1636 static int
1637 smbfs_nothingisopen (vfsid id)
1639 DEBUG(3, ("smbfs_nothingisopen(%d)\n", (int)id));
1640 return 0;
1643 static void
1644 smbfs_free (vfsid id)
1646 DEBUG(3, ("smbfs_free(%d)\n", (int)id));
1647 /* FIXME: Should not be empty */
1648 authinfo_free_all ();
1651 /* Gives up on a socket and reopens the connection, the child own the socket
1652 * now
1654 static void
1655 my_forget (char *path)
1657 char *host, *user, *p;
1658 int port, i;
1660 if (strncmp (path, URL_HEADER, HEADER_LEN))
1661 return;
1663 DEBUG(3, ("my_forget(path:%s)\n", path));
1665 path += 6;
1666 if (path[0] == '/' && path[1] == '/')
1667 path += 2;
1669 if ((p = smbfs_get_host_and_username (&path, &host, &user, &port, NULL))) {
1670 g_free (p);
1671 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1672 if ((strcmp (host, smbfs_connections [i].host) == 0) &&
1673 (strcmp (user, smbfs_connections [i].user) == 0) &&
1674 (port == smbfs_connections [i].port)) {
1676 /* close socket: the child owns it now */
1677 cli_shutdown(smbfs_connections [i].cli);
1679 /* reopen the connection */
1680 smbfs_connections [i].cli =
1681 smbfs_do_connect(host, smbfs_connections[i].service);
1685 g_free (host);
1686 g_free (user);
1689 static int
1690 smbfs_setctl (vfs *me, char *path, int ctlop, char *arg)
1692 DEBUG(3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path, ctlop));
1693 switch (ctlop) {
1694 case MCCTL_FORGET_ABOUT:
1695 my_forget(path);
1696 return 0;
1698 return 0;
1701 static smbfs_handle *
1702 open_write (smbfs_handle *remote_handle, char *rname, int flags, int mode)
1704 if (flags & O_TRUNC) /* if it exists truncate to zero */
1705 DEBUG(3, ("open_write: O_TRUNC\n"));
1707 remote_handle->fnum = cli_open(remote_handle->cli, rname, flags, DENY_NONE);
1709 if (remote_handle->fnum == -1) {
1710 message_3s (1, MSG_ERROR, _(" %s opening remote file %s "),
1711 cli_errstr(remote_handle->cli), CNV_LANG(rname));
1712 DEBUG(1,("smbfs_open(rname:%s) error:%s\n",
1713 rname, cli_errstr(remote_handle->cli)));
1714 my_errno = cli_error(remote_handle->cli, NULL, &err, NULL);
1715 return NULL;
1718 return remote_handle;
1721 static smbfs_handle *
1722 open_read (smbfs_handle *remote_handle, char *rname, int flags, int mode)
1724 size_t size;
1726 remote_handle->fnum =
1727 cli_open(remote_handle->cli, rname, O_RDONLY, DENY_NONE);
1729 if (remote_handle->fnum == -1) {
1730 message_3s (1, MSG_ERROR, _(" %s opening remote file %s "),
1731 cli_errstr(remote_handle->cli), CNV_LANG(rname));
1732 DEBUG(1,("smbfs_open(rname:%s) error:%s\n",
1733 rname, cli_errstr(remote_handle->cli)));
1734 my_errno = cli_error(remote_handle->cli, NULL, &err, NULL);
1735 return NULL;
1738 if (!cli_qfileinfo(remote_handle->cli, remote_handle->fnum,
1739 &remote_handle->attr, &size, NULL, NULL, NULL, NULL, NULL) &&
1740 !cli_getattrE(remote_handle->cli, remote_handle->fnum,
1741 &remote_handle->attr, &size, NULL, NULL, NULL)) {
1742 message_2s (1, MSG_ERROR, " getattrib: %s ",
1743 cli_errstr(remote_handle->cli));
1744 DEBUG(1,("smbfs_open(rname:%s) getattrib:%s\n",
1745 rname, cli_errstr(remote_handle->cli)));
1746 my_errno = cli_error(remote_handle->cli, NULL, &err, NULL);
1747 return NULL;
1750 return remote_handle;
1753 static void *
1754 smbfs_open (vfs *me, char *file, int flags, int mode)
1756 char *remote_file, *p;
1757 void *ret;
1758 smbfs_connection *sc;
1759 smbfs_handle *remote_handle;
1761 DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%d)\n", file, flags, mode));
1763 if (!(remote_file = smbfs_get_path (&sc, file)))
1764 return 0;
1766 p = remote_file;
1767 convert_path(&remote_file, FALSE);
1768 g_free (p);
1770 remote_handle = g_new (smbfs_handle, 2);
1771 remote_handle->cli = sc->cli;
1772 remote_handle->nread = 0;
1774 if (flags & O_CREAT)
1775 ret = open_write(remote_handle, remote_file, flags, mode);
1776 else
1777 ret = open_read(remote_handle, remote_file, flags, mode);
1779 g_free (remote_file);
1781 return ret;
1784 static int
1785 smbfs_unlink (vfs *me, char *path)
1787 smbfs_connection *sc;
1788 char *remote_file, *p;
1790 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1791 return -1;
1793 p = remote_file;
1794 convert_path(&remote_file, FALSE);
1795 g_free (p);
1797 if (!cli_unlink(sc->cli, remote_file)) {
1798 message_3s (1, MSG_ERROR, _(" %s removing remote file %s "),
1799 cli_errstr(sc->cli), CNV_LANG(remote_file));
1800 g_free (remote_file);
1801 return -1;
1803 g_free (remote_file);
1804 return 0;
1807 static int
1808 smbfs_rename (vfs *me, char *a, char *b)
1810 smbfs_connection *sc;
1811 char *ra, *rb;
1812 char *p;
1813 int retval;
1815 if ((ra = smbfs_get_path (&sc, a)) == 0)
1816 return -1;
1818 if ((rb = smbfs_get_path (&sc, b)) == 0) {
1819 g_free (ra);
1820 return -1;
1823 p = ra;
1824 convert_path(&ra, FALSE);
1825 g_free (p);
1826 p = rb;
1827 convert_path(&rb, FALSE);
1828 g_free (p);
1830 retval = cli_rename(sc->cli, ra, rb);
1832 g_free (ra);
1833 g_free (rb);
1835 if (!retval) {
1836 message_2s (1, MSG_ERROR, _(" %s renaming files\n"),
1837 cli_errstr(sc->cli));
1838 return -1;
1840 return 0;
1843 static int
1844 smbfs_fstat (void *data, struct stat *buf)
1846 smbfs_handle *remote_handle = (smbfs_handle *)data;
1848 DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle->fnum));
1850 /* use left over from previous get_remote_stat, if available */
1851 if (single_entry)
1852 memcpy(buf, &single_entry->my_stat, sizeof(struct stat));
1853 else { /* single_entry not set up: bug */
1854 my_errno = EFAULT;
1855 return -EFAULT;
1857 return 0;
1860 vfs vfs_smbfs_ops = {
1861 NULL, /* This is place of next pointer */
1862 "smbfs",
1863 F_NET, /* flags */
1864 "smb:", /* prefix */
1865 NULL, /* data */
1866 0, /* errno */
1867 smbfs_init,
1868 NULL,
1869 smbfs_fill_names,
1870 NULL,
1872 smbfs_open,
1873 smbfs_close,
1874 smbfs_read,
1875 smbfs_write,
1877 smbfs_opendir,
1878 smbfs_readdir,
1879 smbfs_closedir,
1880 NULL,
1881 NULL,
1883 smbfs_stat,
1884 smbfs_lstat,
1885 smbfs_fstat,
1887 smbfs_chmod,
1888 smbfs_chown,
1889 smbfs_utime,
1891 smbfs_readlink,
1892 smbfs_symlink,
1893 smbfs_link,
1894 smbfs_unlink,
1896 smbfs_rename,
1897 smbfs_chdir,
1898 smbfs_errno,
1899 smbfs_lseek,
1900 smbfs_mknod,
1902 smbfs_getid,
1903 smbfs_nothingisopen,
1904 smbfs_free,
1906 NULL,
1907 NULL,
1909 smbfs_mkdir,
1910 smbfs_rmdir,
1911 NULL,
1912 smbfs_setctl
1914 MMAPNULL