* main.c (handle_args): Don't call mc_get_current_wd().
[midnight-commander.git] / vfs / smbfs.c
blob7a010bfaa7673e330c4f838b2954d36643a3b921
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 */
24 #include <config.h>
25 #include <stdio.h>
26 #include <sys/types.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 static mode_t myumask = 0755;
64 extern pstring global_myname;
65 static int smbfs_open_connections = 0;
66 static gboolean got_user = FALSE;
67 static gboolean got_pass = FALSE;
68 static pstring password;
69 static pstring username;
71 static struct _smbfs_connection {
72 struct cli_state *cli;
73 struct in_addr dest_ip;
74 BOOL have_ip;
75 char *host; /* server name */
76 char *service; /* share name */
77 char *domain;
78 char *user;
79 char *home;
80 char *password;
81 int port;
82 int name_type;
83 time_t last_use;
84 } smbfs_connections [SMBFS_MAX_CONNECTIONS];
85 /* unique to each connection */
87 static struct cli_state * smbfs_do_connect (const char *server, char *share);
89 typedef struct _smbfs_connection smbfs_connection;
90 static smbfs_connection *current_bucket;
92 typedef struct {
93 struct cli_state *cli;
94 int fnum;
95 off_t nread;
96 uint16 attr;
97 } smbfs_handle;
99 static GSList *auth_list;
101 static void
102 authinfo_free (struct smb_authinfo const *a)
104 g_free (a->host);
105 g_free (a->share);
106 g_free (a->domain);
107 g_free (a->user);
108 wipe_password (a->password);
111 static void
112 authinfo_free_all ()
114 if (auth_list) {
115 g_slist_foreach (auth_list, (GFunc)authinfo_free, 0);
116 g_slist_free (auth_list);
117 auth_list = 0;
121 static gint
122 authinfo_compare_host_and_share (gconstpointer _a, gconstpointer _b)
124 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
125 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
127 if (!a->host || !a->share || !b->host || !b->share)
128 return 1;
129 if (strcmp (a->host, b->host) != 0)
130 return 1;
131 if (strcmp (a->share, b->share) != 0)
132 return 1;
133 return 0;
136 static gint
137 authinfo_compare_host (gconstpointer _a, gconstpointer _b)
139 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
140 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
142 if (!a->host || !b->host)
143 return 1;
144 if (strcmp (a->host, b->host) != 0)
145 return 1;
146 if (strcmp (a->share, IPC) != 0)
147 return 1;
148 return 0;
151 static void
152 authinfo_add (const char *host, const char *share, const char *domain,
153 const char *user, const char *password)
155 struct smb_authinfo *auth = g_new (struct smb_authinfo, 1);
157 if (!auth)
158 return;
160 /* Don't check for NULL, g_strdup already does. */
161 auth->host = g_strdup (host);
162 auth->share = g_strdup (share);
163 auth->domain = g_strdup (domain);
164 auth->user = g_strdup (user);
165 auth->password = g_strdup (password);
166 auth_list = g_slist_prepend (auth_list, auth);
169 static void
170 authinfo_remove (const char *host, const char *share)
172 struct smb_authinfo data;
173 struct smb_authinfo *auth;
174 GSList *list;
176 data.host = g_strdup (host);
177 data.share = g_strdup (share);
178 list = g_slist_find_custom (auth_list,
179 &data,
180 authinfo_compare_host_and_share);
181 g_free (data.host);
182 g_free (data.share);
183 if (!list)
184 return;
185 auth = list->data;
186 auth_list = g_slist_remove (auth_list, auth);
187 authinfo_free (auth);
190 /* Set authentication information in bucket. Return 1 if successful, else 0 */
191 /* Information in auth_list overrides user if pass is NULL. */
192 /* bucket->host and bucket->service must be valid. */
193 static int
194 bucket_set_authinfo (smbfs_connection *bucket,
195 const char *domain, const char *user, const char *pass,
196 int fallback_to_host)
198 struct smb_authinfo data;
199 struct smb_authinfo *auth;
200 GSList *list;
202 if (domain && user && pass) {
203 g_free (bucket->domain);
204 g_free (bucket->user);
205 g_free (bucket->password);
206 bucket->domain = g_strdup (domain);
207 bucket->user = g_strdup (user);
208 bucket->password = g_strdup (pass);
209 authinfo_remove (bucket->host, bucket->service);
210 authinfo_add (bucket->host, bucket->service,
211 domain, user, pass);
212 return 1;
215 data.host = bucket->host;
216 data.share = bucket->service;
217 list = g_slist_find_custom (auth_list, &data, authinfo_compare_host_and_share);
218 if (!list && fallback_to_host)
219 list = g_slist_find_custom (auth_list, &data, authinfo_compare_host);
220 if (list) {
221 auth = list->data;
222 bucket->domain = g_strdup (auth->domain);
223 bucket->user = g_strdup (auth->user);
224 bucket->password = g_strdup (auth->password);
225 return 1;
228 if (got_pass) {
229 bucket->domain = g_strdup (lp_workgroup ());
230 bucket->user = g_strdup (got_user ? username : user);
231 bucket->password = g_strdup (password);
232 return 1;
235 auth = vfs_smb_get_authinfo (bucket->host,
236 bucket->service,
237 (domain ? domain : lp_workgroup ()),
238 user);
239 if (auth) {
240 g_free (bucket->domain);
241 g_free (bucket->user);
242 g_free (bucket->password);
243 bucket->domain = g_strdup (auth->domain);
244 bucket->user = g_strdup (auth->user);
245 bucket->password = g_strdup (auth->password);
246 authinfo_remove (bucket->host, bucket->service);
247 auth_list = g_slist_prepend (auth_list, auth);
248 return 1;
250 return 0;
253 void
254 smbfs_set_debug(int arg)
256 DEBUGLEVEL = arg;
259 /********************** The callbacks ******************************/
260 static int
261 smbfs_init(vfs *me)
263 char *servicesf = CONFIGDIR "/smb.conf";
265 DEBUGLEVEL = 0;
267 setup_logging("mc", True);
268 TimeInit();
269 charset_initialise();
271 DEBUG(3, ("smbfs_init(%s)\n", me->name));
273 if(!get_myname(myhostname,NULL))
274 DEBUG(0,("Failed to get my hostname.\n"));
276 if (!lp_load(servicesf,True,False,False))
277 DEBUG(0, ("Cannot load %s - run testparm to debug it\n", servicesf));
279 codepage_initialise(lp_client_code_page());
281 load_interfaces();
282 myumask = umask(0);
283 umask(myumask);
285 if (getenv("USER")) {
286 char *p;
287 pstrcpy(username, getenv("USER"));
288 got_user = TRUE;
289 DEBUG(3, ("smbfs_init(): $USER:%s\n", username));
290 if ((p = strchr(username, '%'))) {
291 *p = 0;
292 pstrcpy(password, p+1);
293 got_pass = TRUE;
294 memset(strchr(getenv("USER"), '%')+1, 'X', strlen(password));
295 DEBUG(3, ("smbfs_init(): $USER%%pass: %s%%%s\n",
296 username, password));
298 strupper(username);
300 if (getenv("PASSWD")) {
301 pstrcpy(password, getenv("PASSWD"));
302 got_pass = TRUE;
304 return 1;
307 static void
308 smbfs_fill_names (vfs *me, void (*func)(char *))
310 int i;
311 char *path;
312 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
313 if (smbfs_connections [i].cli) {
314 path = g_strconcat (URL_HEADER,
315 smbfs_connections[i].host,
316 "/", smbfs_connections[i].service,
317 NULL);
318 (*func)(path);
319 g_free (path);
324 #define CNV_LANG(s) dos_to_unix(s,False)
325 #define GNAL_VNC(s) unix_to_dos(s,False)
326 /* does same as do_get() in client.c */
327 /* called from vfs.c:1080, count = buffer size */
328 static int
329 smbfs_read (void *data, char *buffer, int count)
331 smbfs_handle *info = (smbfs_handle *) data;
332 int n;
334 DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%d)\n",
335 info->fnum, (int)info->nread, count));
336 n = cli_read(info->cli, info->fnum, buffer, info->nread, count);
337 if (n > 0)
338 info->nread += n;
339 return n;
342 static int
343 smbfs_write (void *data, char *buf, int nbyte)
345 smbfs_handle *info = (smbfs_handle *) data;
346 int n;
348 DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%d)\n",
349 info->fnum, (int)info->nread, nbyte));
350 n = cli_write(info->cli, info->fnum, 0, buf, info->nread, nbyte);
351 if (n > 0)
352 info->nread += n;
353 return n;
356 static int
357 smbfs_close (void *data)
359 smbfs_handle *info = (smbfs_handle *) data;
360 DEBUG(3, ("smbfs_close(fnum:%d)\n", info->fnum));
361 /* if imlementing archive_level: add rname to smbfs_handle
362 if (archive_level >= 2 && (inf->attr & aARCH)) {
363 cli_setatr(info->cli, rname, info->attr & ~(uint16)aARCH, 0);
364 } */
365 return (cli_close(info->cli, info->fnum) == True) ? 0 : -1;
368 static int
369 smbfs_errno (vfs *me)
371 DEBUG(3, ("smbfs_errno: %s\n", g_strerror(my_errno)));
372 return my_errno;
375 typedef struct dir_entry {
376 char *text;
377 struct dir_entry *next;
378 struct stat my_stat;
379 int merrno;
380 } dir_entry;
382 typedef struct {
383 gboolean server_list;
384 char *dirname;
385 char *path; /* the dir originally passed to smbfs_opendir */
386 smbfs_connection *conn;
387 dir_entry *entries;
388 dir_entry *current;
389 } opendir_info;
391 static opendir_info
392 *previous_info,
393 *current_info,
394 *current_share_info,
395 *current_server_info;
397 static gboolean first_direntry;
399 static dir_entry *
400 new_dir_entry (const char * name)
402 dir_entry *new_entry;
403 new_entry = g_new0 (dir_entry, 1);
404 new_entry->text = dos_to_unix (g_strdup (name), 1);
406 if (first_direntry) {
407 current_info->entries = new_entry;
408 first_direntry = FALSE;
409 } else {
410 current_info->current->next = new_entry;
412 current_info->current = new_entry;
414 return new_entry;
417 /* browse for shares on server */
418 static void
419 browsing_helper(const char *name, uint32 type, const char *comment)
421 char *typestr = "";
423 dir_entry *new_entry = new_dir_entry (name);
425 switch (type) {
426 case STYPE_DISKTREE:
427 typestr = "Disk";
428 /* show this as dir */
429 new_entry->my_stat.st_mode = S_IFDIR|S_IRUSR|S_IRGRP|S_IROTH;
430 break;
431 case STYPE_PRINTQ:
432 typestr = "Printer"; break;
433 case STYPE_DEVICE:
434 typestr = "Device"; break;
435 case STYPE_IPC:
436 typestr = "IPC"; break;
438 DEBUG(3, ("\t%-15.15s%-10.10s%s\n", name, typestr, comment));
441 static void
442 loaddir_helper(file_info *finfo, const char *mask)
444 dir_entry *new_entry;
445 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
446 #if 0 /* I want to see dot files */
447 if (finfo->mode & aHIDDEN)
448 return; /* don't bother with hidden files, "~$" screws up mc */
449 #endif
450 new_entry = new_dir_entry (finfo->name);
452 new_entry->my_stat.st_size = finfo->size;
453 new_entry->my_stat.st_mtime = finfo->mtime;
454 new_entry->my_stat.st_atime = finfo->atime;
455 new_entry->my_stat.st_ctime = finfo->ctime;
456 new_entry->my_stat.st_uid = finfo->uid;
457 new_entry->my_stat.st_gid = finfo->gid;
460 new_entry->my_stat.st_mode =
461 S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR|S_IWGRP|S_IWOTH;
463 /* if (finfo->mode & aVOLID); nothing similar in real world */
464 if (finfo->mode & aDIR)
465 new_entry->my_stat.st_mode |= S_IFDIR;
466 else
467 new_entry->my_stat.st_mode |= S_IFREG; /* if not dir, regular file? */
468 /* if (finfo->mode & aARCH); DOS archive */
469 /* if (finfo->mode & aHIDDEN); like a dot file? */
470 /* if (finfo->mode & aSYSTEM); like a kernel? */
471 if (finfo->mode & aRONLY)
472 new_entry->my_stat.st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
474 DEBUG(3, (" %-30s%7.7s%8.0f %s",
475 CNV_LANG(finfo->name),
476 attrib_string(finfo->mode),
477 (double)finfo->size,
478 asctime(LocalTime(&t))));
481 /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
482 static int
483 convert_path(char **remote_file, gboolean trailing_asterik)
485 char *p, *my_remote;
487 my_remote = *remote_file;
488 if (strncmp (my_remote, URL_HEADER, HEADER_LEN) == 0) { /* if passed directly */
489 my_remote += 6;
490 if (*my_remote == '/') /* from server browsing */
491 my_remote++;
492 p = strchr(my_remote, '/');
493 if (p)
494 my_remote = p+1; /* advance to end of server name */
497 if (*my_remote == '/')
498 my_remote++; /* strip off leading '/' */
499 p = strchr(my_remote, '/');
500 if (p)
501 my_remote = p; /* strip off share/service name */
502 /* create remote filename as understood by smb clientgen */
503 p = *remote_file = g_strconcat (my_remote, trailing_asterik ? "/*" : "", 0);
504 unix_to_dos (*remote_file, 1);
505 while ((p = strchr(p, '/')))
506 *p = '\\';
507 return 0;
510 static void
511 server_browsing_helper(const char *name, uint32 m, const char *comment)
513 dir_entry *new_entry = new_dir_entry (name);
515 /* show this as dir */
516 new_entry->my_stat.st_mode = S_IFDIR|S_IRUSR|S_IRGRP|S_IROTH;
518 DEBUG(3, ("\t%-16.16s %s\n", name, comment));
521 static BOOL
522 reconnect(smbfs_connection *conn, int *retries)
524 char *host;
525 DEBUG(3, ("RECONNECT\n"));
527 if (*(conn->host) == 0)
528 host = g_strdup(conn->cli->desthost); /* server browsing */
529 else
530 host = g_strdup(conn->host);
532 cli_shutdown(conn->cli);
534 if (!(conn->cli = smbfs_do_connect(host, conn->service))) {
535 message_2s (1, MSG_ERROR,
536 _(" reconnect to %s failed\n "), conn->host);
537 g_free(host);
538 return False;
540 g_free(host);
541 if (++(*retries) == 2)
542 return False;
543 return True;
546 static BOOL
547 smb_send(struct cli_state *cli)
549 size_t len;
550 size_t nwritten=0;
551 ssize_t ret;
553 len = smb_len(cli->outbuf) + 4;
555 while (nwritten < len) {
556 ret = write_socket(cli->fd, cli->outbuf+nwritten, len - nwritten);
557 if (ret <= 0 && errno == EPIPE)
558 return False;
559 nwritten += ret;
562 return True;
565 /****************************************************************************
566 See if server has cut us off by checking for EPIPE when writing.
567 Taken from cli_chkpath()
568 ****************************************************************************/
569 static BOOL
570 chkpath(struct cli_state *cli, char *path, BOOL send_only)
572 fstring path2;
573 char *p;
575 fstrcpy(path2,path);
576 unix_to_dos (path2, 1);
577 trim_string(path2,NULL,"\\");
578 if (!*path2) *path2 = '\\';
580 memset(cli->outbuf,'\0',smb_size);
581 set_message(cli->outbuf,0,4 + strlen(path2),True);
582 SCVAL(cli->outbuf,smb_com,SMBchkpth);
583 SSVAL(cli->outbuf,smb_tid,cli->cnum);
585 cli->rap_error = 0;
586 cli->nt_error = 0;
587 SSVAL(cli->outbuf,smb_pid,cli->pid);
588 SSVAL(cli->outbuf,smb_uid,cli->vuid);
589 SSVAL(cli->outbuf,smb_mid,cli->mid);
590 if (cli->protocol > PROTOCOL_CORE) {
591 SCVAL(cli->outbuf,smb_flg,0x8);
592 SSVAL(cli->outbuf,smb_flg2,0x1);
595 p = smb_buf(cli->outbuf);
596 *p++ = 4;
597 fstrcpy(p,path2);
599 if (!smb_send(cli)) {
600 DEBUG(3, ("chkpath: couldnt send\n"));
601 return False;
603 if (send_only) {
604 client_receive_smb(cli->fd, cli->inbuf, cli->timeout);
605 DEBUG(3, ("chkpath: send only OK\n"));
606 return True; /* just testing for EPIPE */
608 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout)) {
609 DEBUG(3, ("chkpath: receive error\n"));
610 return False;
612 if ((my_errno = cli_error(cli, NULL, NULL, NULL))) {
613 if (my_errno == 20 || my_errno == 13)
614 return True; /* ignore if 'not a directory' error */
615 DEBUG(3, ("chkpath: cli_error: %s\n", g_strerror(my_errno)));
616 return False;
619 return True;
622 /* #if 0 */
623 static int
624 fs (const char *text)
626 const char *p = text;
627 int count = 0;
629 while ((p = strchr(p, '/')) != NULL) {
630 count++;
631 p++;
633 if (count == 1)
634 return strlen(text);
635 return count;
637 /* #endif */
639 static int
640 smbfs_loaddir (opendir_info *smbfs_info)
642 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
643 int servlen = strlen(smbfs_info->conn->service);
644 char *my_dirname = smbfs_info->dirname;
646 DEBUG(3, ("smbfs_loaddir: dirname:%s\n", my_dirname));
647 first_direntry = TRUE;
649 if (current_info) {
650 DEBUG(3, ("smbfs_loaddir: new:'%s', cached:'%s'\n", my_dirname, current_info->dirname));
651 /* if new desired dir is longer than cached in current_info */
652 if (fs(my_dirname) > fs(current_info->dirname)) {
653 DEBUG(3, ("saving to previous_info\n"));
654 previous_info = current_info;
658 current_info = smbfs_info;
660 if (strcmp(my_dirname, "/") == 0) {
661 if (!strcmp(smbfs_info->path, URL_HEADER)) {
662 DEBUG(6, ("smbfs_loaddir: browsing %s\n", IPC));
663 /* browse for servers */
664 if (!cli_NetServerEnum(smbfs_info->conn->cli, smbfs_info->conn->domain,
665 SV_TYPE_ALL, server_browsing_helper))
666 return 0;
667 else
668 current_server_info = smbfs_info;
669 smbfs_info->server_list = TRUE;
670 } else {
671 /* browse for shares */
672 if (cli_RNetShareEnum(smbfs_info->conn->cli, browsing_helper) < 1)
673 return 0;
674 else
675 current_share_info = smbfs_info;
677 goto done;
680 /* do regular directory listing */
681 if(strncmp(smbfs_info->conn->service, my_dirname+1, servlen) == 0) {
682 /* strip share name from dir */
683 char *p = my_dirname = g_strdup(my_dirname + servlen);
684 *p = '/';
685 convert_path(&my_dirname, TRUE);
686 g_free (p);
687 } else
688 convert_path(&my_dirname, TRUE);
690 DEBUG(6, ("smbfs_loaddir: service: %s\n", smbfs_info->conn->service));
691 DEBUG(6, ("smbfs_loaddir: cli->share: %s\n", smbfs_info->conn->cli->share));
692 DEBUG(6, ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname));
693 /* do file listing: cli_list returns number of files */
694 if (cli_list(
695 smbfs_info->conn->cli, my_dirname, attribute, loaddir_helper) < 0) {
696 /* cli_list returns -1 if directory empty or cannot read socket */
697 my_errno = cli_error(smbfs_info->conn->cli, NULL, &err, NULL);
698 g_free (my_dirname);
699 return 0;
701 if (*(my_dirname) == 0)
702 smbfs_info->dirname = smbfs_info->conn->service;
703 g_free (my_dirname);
704 /* do_dskattr(); */
706 done:
707 /* current_info->parent = smbfs_info->dirname; */
709 smbfs_info->current = smbfs_info->entries;
710 return 1; /* 1 = ok */
713 #ifdef SMBFS_FREE_DIR
714 static void
715 smbfs_free_dir (dir_entry *de)
717 if (!de) return;
719 smbfs_free_dir (de->next);
720 g_free (de->text);
721 g_free (de);
723 #endif
725 /* Explanation:
726 * On some operating systems (Slowaris 2 for example)
727 * the d_name member is just a char long (Nice trick that break everything,
728 * so we need to set up some space for the filename.
730 static struct {
731 struct dirent dent;
732 #ifdef NEED_EXTRA_DIRENT_BUFFER
733 char extra_buffer [MC_MAXPATHLEN];
734 #endif
735 } smbfs_readdir_data;
738 /* The readdir routine loads the complete directory */
739 /* It's too slow to ask the server each time */
740 /* It now also sends the complete lstat information for each file */
741 static void *
742 smbfs_readdir (void *info)
744 static char * const dirent_dest = &(smbfs_readdir_data.dent.d_name [0]);
745 opendir_info *smbfs_info = (opendir_info *) info;
747 DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info->dirname));
749 if (!smbfs_info->entries)
750 if (!smbfs_loaddir (smbfs_info))
751 return NULL;
753 if (smbfs_info->current == 0) { /* reached end of dir entries */
754 DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
755 #ifdef SMBFS_FREE_DIR
756 smbfs_free_dir (smbfs_info->entries);
757 smbfs_info->entries = 0;
758 #endif
759 return NULL;
761 pstrcpy (dirent_dest, smbfs_info->current->text);
762 smbfs_info->current = smbfs_info->current->next;
764 #ifndef DIRENT_LENGTH_COMPUTED
765 smbfs_readdir_data.dent.d_namlen = strlen (smbfs_readdir_data.dent.d_name);
766 #endif
768 return &smbfs_readdir_data;
771 static int
772 smbfs_closedir (void *info)
774 opendir_info *smbfs_info = (opendir_info *) info;
775 /* dir_entry *p, *q; */
777 DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info->dirname));
778 /* CLOSE HERE */
780 /* for (p = smbfs_info->entries; p;){
781 q = p;
782 p = p->next;
783 g_free (q->text);
784 g_free (q);
786 g_free (info); */
787 return 0;
790 static int
791 smbfs_chmod (vfs *me, char *path, int mode)
793 DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path, mode));
794 /* my_errno = EOPNOTSUPP;
795 return -1; */ /* cant chmod on smb filesystem */
796 return 0; /* make mc happy */
799 static int
800 smbfs_chown (vfs *me, char *path, int owner, int group)
802 DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path, owner, group));
803 my_errno = EOPNOTSUPP; /* ready for your labotomy? */
804 return -1;
807 static int
808 smbfs_utime (vfs *me, char *path, struct utimbuf *times)
810 DEBUG(3, ("smbfs_utime(path:%s)\n", path));
811 my_errno = EOPNOTSUPP;
812 return -1;
815 static int
816 smbfs_readlink (vfs *me, char *path, char *buf, int size)
818 DEBUG(3, ("smbfs_readlink(path:%s, buf:%s, size:%d)\n", path, buf, size));
819 my_errno = EOPNOTSUPP;
820 return -1; /* no symlinks on smb filesystem? */
823 static int
824 smbfs_symlink (vfs *me, char *n1, char *n2)
826 DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1, n2));
827 my_errno = EOPNOTSUPP;
828 return -1; /* no symlinks on smb filesystem? */
831 /* Extract the hostname and username from the path */
832 /* path is in the form: hostname:user/remote-dir */
833 #if 0
834 static char *
835 smbfs_get_host_and_username
836 (char **path, char **host, char **user, int *port, char **pass)
838 /* char *p, *ret; */
839 char *ret;
841 ret = vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0);
843 #if 0
844 if ((p = strchr(*path, '@'))) /* user:pass@server */
845 *path = ++p; /* don't want user:pass@ in path */
846 if ((p = strchr(ret, '@'))) /* user:pass@server */
847 ret = ++p; /* don't want user:pass@ in path */
848 #endif
850 return ret;
852 #else
853 #define smbfs_get_host_and_username(path, host, user, port, pass) \
854 vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
855 #endif
856 /*****************************************************
857 return a connection to a SMB server
858 current_bucket needs to be set before calling
859 *******************************************************/
860 static struct cli_state *
861 smbfs_do_connect (const char *server, char *share)
863 struct cli_state *c;
864 struct nmb_name called, calling;
865 struct in_addr ip;
866 extern struct in_addr ipzero;
868 DEBUG(3, ("smbfs_do_connect(%s, %s)\n", server, share));
869 if (*share == '\\') {
870 server = share+2;
871 share = strchr(server,'\\');
872 if (!share) return NULL;
873 *share = 0;
874 share++;
877 make_nmb_name(&calling, global_myname, 0x0, "");
878 make_nmb_name(&called , server, current_bucket->name_type, "");
880 for (;;) {
882 ip = (current_bucket->have_ip) ? current_bucket->dest_ip : ipzero;
884 /* have to open a new connection */
885 if (!(c = cli_initialise(NULL))) {
886 my_errno = ENOMEM;
887 return NULL;
890 pwd_init(&(c->pwd)); /* should be moved into cli_initialise()? */
891 pwd_set_cleartext(&(c->pwd), current_bucket->password);
893 if ((cli_set_port(c, current_bucket->port) == 0) ||
894 !cli_connect(c, server, &ip)) {
895 DEBUG(1, ("Connection to %s failed\n", server));
896 break;
899 if (!cli_session_request(c, &calling, &called)) {
900 my_errno = cli_error(c, NULL, &err, NULL);
901 DEBUG(1, ("session request to %s failed\n", called.name));
902 cli_shutdown(c);
903 if (strcmp(called.name, "*SMBSERVER")) {
904 make_nmb_name(&called , "*SMBSERVER", 0x20, "");
905 continue;
907 return NULL;
910 DEBUG(3, (" session request ok\n"));
912 if (!cli_negprot(c)) {
913 DEBUG(1, ("protocol negotiation failed\n"));
914 break;
917 if (!cli_session_setup(c, current_bucket->user,
918 current_bucket->password, strlen(current_bucket->password),
919 current_bucket->password, strlen(current_bucket->password),
920 current_bucket->domain)) {
921 DEBUG(1,("session setup failed: %s\n", cli_errstr(c)));
922 authinfo_remove (server, share);
923 break;
926 if (*c->server_domain || *c->server_os || *c->server_type)
927 DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
928 c->server_domain,c->server_os,c->server_type));
930 DEBUG(3, (" session setup ok\n"));
932 if (!cli_send_tconX(c, share, "?????",
933 current_bucket->password, strlen(current_bucket->password)+1)) {
934 DEBUG(1,("%s: tree connect failed: %s\n", share, cli_errstr(c)));
935 break;
938 DEBUG(3, (" tconx ok\n"));
940 my_errno = 0;
941 return c;
944 my_errno = cli_error(c, NULL, &err, NULL);
945 cli_shutdown(c);
946 return NULL;
950 static int
951 get_master_browser(char **host)
953 int count;
954 struct in_addr *ip_list, bcast_addr;
955 extern struct in_addr ipzero;
957 /* does port = 137 for win95 master browser? */
958 int fd= open_socket_in( SOCK_DGRAM, 0, 3,
959 interpret_addr(lp_socket_address()), True );
960 if (fd == -1)
961 return 0;
962 set_socket_options(fd, "SO_BROADCAST");
963 ip_list = iface_bcast(ipzero);
964 bcast_addr = *ip_list;
965 if ((ip_list = name_query(fd, "\01\02__MSBROWSE__\02", 1, True,
966 True, bcast_addr, &count, NULL))) {
967 if (!count)
968 return 0;
969 /* just return first master browser */
970 *host = g_strdup(inet_ntoa(ip_list[0]));
971 return 1;
973 return 0;
976 static void
977 free_bucket (smbfs_connection *bucket)
979 g_free (bucket->host);
980 g_free (bucket->service);
981 g_free (bucket->domain);
982 g_free (bucket->user);
983 wipe_password (bucket->password);
984 if (bucket->home) g_free (bucket->home);
985 memset (bucket, 0, sizeof (smbfs_connection));
988 static smbfs_connection *
989 smbfs_get_free_bucket ()
991 int i;
993 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++)
994 if (!smbfs_connections [i].cli) return &smbfs_connections [i];
996 { /* search for most dormant connection */
997 int oldest = 0; /* index */
998 time_t oldest_time = smbfs_connections[0].last_use;
999 for (i = 1; i < SMBFS_MAX_CONNECTIONS; i++) {
1000 if (smbfs_connections[i].last_use < oldest_time) {
1001 oldest_time = smbfs_connections[i].last_use;
1002 oldest = i;
1005 cli_shutdown(smbfs_connections[oldest].cli);
1006 free_bucket (&smbfs_connections[oldest]);
1007 return &smbfs_connections[oldest];
1010 /* This can't happend, since we have checked for max connections before */
1011 vfs_die("Internal error: smbfs_get_free_bucket");
1012 return 0; /* shut up, stupid gcc */
1015 /* This routine keeps track of open connections */
1016 /* Returns a connected socket to host */
1017 static smbfs_connection *
1018 smbfs_open_link(char *host, char *path, const char *user, int *port, char *this_pass)
1020 int i;
1021 smbfs_connection *bucket;
1022 pstring service;
1023 struct in_addr *dest_ip = NULL;
1025 DEBUG(3, ("smbfs_open_link(host:%s, path:%s)\n", host, path));
1027 if (strcmp(host, path) == 0) /* if host & path are same: */
1028 pstrcpy(service, IPC); /* setup for browse */
1029 else { /* get share name from path, path starts with server name */
1030 char *p;
1031 if ((p = strchr(path, '/'))) /* get share aka */
1032 pstrcpy(service, ++p); /* service name from path */
1033 else
1034 pstrcpy(service, "");
1035 /* now check for trailing directory/filenames */
1036 p = strchr(service, '/');
1037 if (p)
1038 *p = 0; /* cut off dir/files: sharename only */
1040 DEBUG(6, ("smbfs_open_link: service from path:%s\n", service));
1043 if (got_user)
1044 user = username; /* global from getenv */
1046 /* Is the link actually open? */
1047 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1048 if (!smbfs_connections[i].cli)
1049 continue;
1050 if ((strcmp (host, smbfs_connections [i].host) == 0) &&
1051 (strcmp (user, smbfs_connections [i].user) == 0) &&
1052 (strcmp (service, smbfs_connections [i].service) == 0)) {
1053 int retries = 0;
1054 BOOL inshare = (*host != 0 && *path != 0 && strchr(path, '/'));
1055 /* check if this connection has died */
1056 while (!chkpath(smbfs_connections[i].cli, "\\", !inshare)) {
1057 if (!reconnect(&smbfs_connections[i], &retries))
1058 return 0;
1060 DEBUG(6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i));
1061 current_bucket = &smbfs_connections[i];
1062 smbfs_connections [i].last_use = time(NULL);
1063 return &smbfs_connections [i];
1065 /* connection not found, find if we have ip for new connection */
1066 if (strcmp (host, smbfs_connections [i].host) == 0)
1067 dest_ip = &smbfs_connections[i].cli->dest_ip;
1070 /* make new connection */
1071 bucket = smbfs_get_free_bucket ();
1072 bucket->name_type = 0x20;
1073 bucket->home = 0;
1074 bucket->port = *port;
1075 bucket->have_ip = False;
1076 if (dest_ip) {
1077 bucket->have_ip = True;
1078 bucket->dest_ip = *dest_ip;
1080 current_bucket = bucket;
1082 bucket->user = g_strdup(user);
1083 bucket->service = g_strdup (service);
1085 if (!(*host)) { /* if blank host name, browse for servers */
1086 if (!get_master_browser(&host)) /* set host to ip of master browser */
1087 return 0; /* couldnt find master browser? */
1088 g_free (host);
1089 bucket->host = g_strdup(""); /* blank host means master browser */
1090 } else
1091 bucket->host = g_strdup(host);
1093 if (!bucket_set_authinfo (bucket,
1094 0, /* domain currently not used */
1095 user,
1096 this_pass,
1098 return 0;
1100 /* connect to share */
1101 while (!(bucket->cli = smbfs_do_connect(host, service))) {
1103 if (my_errno != EPERM)
1104 return 0;
1105 message_1s (1, MSG_ERROR,
1106 _(" Authentication failed "));
1108 /* authentication failed, try again */
1109 authinfo_remove (bucket->host, bucket->service);
1110 if (!bucket_set_authinfo (bucket,
1111 bucket->domain,
1112 bucket->user,
1115 return 0;
1119 smbfs_open_connections++;
1120 DEBUG(3, ("smbfs_open_link:smbfs_open_connections: %d\n",
1121 smbfs_open_connections));
1122 return bucket;
1125 static char *
1126 smbfs_get_path(smbfs_connection **sc, char *path)
1128 char *user, *host, *remote_path, *pass;
1129 int port = SMB_PORT;
1131 DEBUG(3, ("smbfs_get_path(%s)\n", path));
1132 if (strncmp (path, URL_HEADER, HEADER_LEN))
1133 return NULL;
1134 path += HEADER_LEN;
1136 if (*path == '/') /* '/' leading server name */
1137 path++; /* probably came from server browsing */
1139 if ((remote_path = smbfs_get_host_and_username(
1140 &path, &host, &user, &port, &pass)))
1141 if ((*sc = smbfs_open_link (host, path, user, &port, pass)) == NULL){
1142 g_free (remote_path);
1143 remote_path = NULL;
1145 g_free (host);
1146 g_free (user);
1147 if (pass) wipe_password (pass);
1149 if (!remote_path) return NULL;
1151 /* NOTE: tildes are deprecated. See ftpfs.c */
1153 int f = !strcmp( remote_path, "/~" );
1154 if (f || !strncmp( remote_path, "/~/", 3 )) {
1155 char *s;
1156 s = concat_dir_and_file( (*sc)->home, remote_path +3-f );
1157 g_free (remote_path);
1158 return s;
1161 return remote_path;
1164 #if 0
1165 static int
1166 is_error (int result, int errno_num)
1168 if (!(result == -1))
1169 return my_errno = 0;
1170 else
1171 my_errno = errno_num;
1172 return 1;
1174 #endif
1176 static void *
1177 smbfs_opendir (vfs *me, char *dirname)
1179 opendir_info *smbfs_info;
1180 smbfs_connection *sc;
1181 char *remote_dir;
1183 DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname));
1185 if (!(remote_dir = smbfs_get_path (&sc, dirname)))
1186 return NULL;
1188 /* FIXME: where freed? */
1189 smbfs_info = g_new (opendir_info, 1);
1190 smbfs_info->server_list = FALSE;
1191 smbfs_info->path = g_strdup(dirname); /* keep original */
1192 smbfs_info->dirname = remote_dir;
1193 smbfs_info->conn = sc;
1194 smbfs_info->entries = 0;
1195 smbfs_info->current = 0;
1197 return smbfs_info;
1200 static int
1201 fake_server_stat(const char *server_url, const char *path, struct stat *buf)
1203 dir_entry *dentry;
1204 char *p;
1206 if ((p = strrchr(path, '/')))
1207 path = p + 1; /* advance until last '/' */
1209 if (!current_info->entries) {
1210 if (!smbfs_loaddir(current_info)); /* browse host */
1211 return -1;
1214 if (current_info->server_list == True) {
1215 dentry = current_info->entries;
1216 DEBUG(4, ("fake stat for SERVER \"%s\"\n", path));
1217 while (dentry) {
1218 if (strcmp(dentry->text, path) == 0) {
1219 DEBUG(4, ("fake_server_stat: %s:%4o\n",
1220 dentry->text, dentry->my_stat.st_mode));
1221 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1222 return 0;
1224 dentry = dentry->next;
1227 my_errno = ENOENT;
1228 return -1;
1231 static int
1232 fake_share_stat(const char *server_url, const char *path, struct stat *buf)
1234 dir_entry *dentry;
1235 if (strlen(path) < strlen(server_url))
1236 return -1;
1237 path += strlen(server_url); /* we only want share name */
1238 path++;
1240 if (*path == '/') /* '/' leading server name */
1241 path++; /* probably came from server browsing */
1243 if (!current_share_info->entries) {
1244 if (!smbfs_loaddir(current_share_info)); /* browse host */
1245 return -1;
1247 dentry = current_share_info->entries;
1248 DEBUG(3, ("fake_share_stat: %s on %s\n", path, server_url));
1249 while (dentry) {
1250 if (strcmp(dentry->text, path) == 0) {
1251 DEBUG(6, ("fake_share_stat: %s:%4o\n",
1252 dentry->text, dentry->my_stat.st_mode));
1253 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1254 return 0;
1256 dentry = dentry->next;
1258 my_errno = ENOENT;
1259 return -1;
1262 /* stat a single file, get_remote_stat callback */
1263 static dir_entry *single_entry;
1264 static void
1265 statfile_helper(file_info *finfo, const char *mask)
1267 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
1269 #if 0 /* single_entry is never freed now. And only my_stat is used */
1270 single_entry = g_new (dir_entry, 1);
1272 single_entry->text = dos_to_unix (g_strdup (finfo->name), 1);
1274 single_entry->next = 0;
1275 #endif
1276 if (!single_entry)
1277 single_entry = g_new0 (dir_entry, 1);
1279 single_entry->my_stat.st_size = finfo->size;
1280 single_entry->my_stat.st_mtime = finfo->mtime;
1281 single_entry->my_stat.st_atime = finfo->atime;
1282 single_entry->my_stat.st_ctime = finfo->ctime;
1283 single_entry->my_stat.st_uid = finfo->uid;
1284 single_entry->my_stat.st_gid = finfo->gid;
1286 single_entry->my_stat.st_mode =
1287 S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR|S_IWGRP|S_IWOTH;
1289 /* if (finfo->mode & aVOLID); nothing similar in real world */
1290 if (finfo->mode & aDIR)
1291 single_entry->my_stat.st_mode |= S_IFDIR;
1292 else
1293 single_entry->my_stat.st_mode |= S_IFREG;/* if not dir, regular file? */
1294 /* if (finfo->mode & aARCH); DOS archive */
1295 /* if (finfo->mode & aHIDDEN); like a dot file? */
1296 /* if (finfo->mode & aSYSTEM); like a kernel? */
1297 if (finfo->mode & aRONLY)
1298 single_entry->my_stat.st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
1300 DEBUG(6, (" %-30s%7.7s%8.0f %s",
1301 CNV_LANG(finfo->name),
1302 attrib_string(finfo->mode),
1303 (double)finfo->size,
1304 asctime(LocalTime(&t))));
1307 /* stat a single file */
1308 static int
1309 get_remote_stat(smbfs_connection *sc, char *path, struct stat *buf)
1311 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
1312 char *mypath = path;
1314 DEBUG(3, ("get_remote_stat(): mypath:%s\n", mypath));
1316 convert_path(&mypath, FALSE);
1318 if (cli_list(sc->cli, mypath, attribute, statfile_helper) < 1) {
1319 my_errno = ENOENT;
1320 g_free (mypath);
1321 return -1; /* cli_list returns number of files */
1324 memcpy(buf, &single_entry->my_stat, sizeof(struct stat));
1326 /* dont free here, use for smbfs_fstat() */
1327 /* g_free(single_entry->text);
1328 g_free(single_entry); */
1329 g_free (mypath);
1330 return 0;
1333 static int
1334 search_dir_entry (dir_entry *dentry, const char *text, struct stat *buf)
1336 while (dentry) {
1337 if (strcmp(text, dentry->text) == 0) {
1338 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1339 memcpy(&single_entry->my_stat, &dentry->my_stat,
1340 sizeof(struct stat));
1341 return 0;
1343 dentry = dentry->next;
1345 return -1;
1348 static int
1349 get_stat_info (smbfs_connection *sc, char *path, struct stat *buf)
1351 char *p;
1352 #if 0
1353 dir_entry *dentry = current_info->entries;
1354 #endif
1355 const char *mypath = path;
1357 mypath++; /* cut off leading '/' */
1358 if ((p = strrchr(mypath, '/')))
1359 mypath = p + 1; /* advance until last file/dir name */
1360 DEBUG(3, ("get_stat_info: mypath:%s, current_info->dirname:%s\n",
1361 mypath, current_info->dirname));
1362 #if 0
1363 if (!dentry) {
1364 DEBUG(1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n", current_info->dirname, path));
1365 return -1;
1367 #endif
1368 if (!single_entry) /* when found, this will be written too */
1369 single_entry = g_new (dir_entry, 1);
1370 if (search_dir_entry(current_info->entries, mypath, buf) == 0) {
1371 return 0;
1373 /* now try to identify mypath as PARENT dir */
1375 char *mdp;
1376 char *mydir;
1377 mdp = mydir = g_strdup(current_info->dirname);
1378 if ((p = strrchr(mydir, '/')))
1379 *p = 0; /* advance util last '/' */
1380 if ((p = strrchr(mydir, '/')))
1381 mydir = p + 1; /* advance util last '/' */
1382 if (strcmp(mydir, mypath) == 0) { /* fake a stat for ".." */
1383 memset(buf, 0, sizeof(struct stat));
1384 buf->st_mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH;
1385 memcpy(&single_entry->my_stat, buf, sizeof(struct stat));
1386 g_free(mdp);
1387 DEBUG(1, (" PARENT:found in %s\n", current_info->dirname));
1388 return 0;
1390 g_free(mdp);
1392 /* now try to identify as CURRENT dir? */
1394 char *dnp = current_info->dirname;
1395 DEBUG(6, ("get_stat_info: is %s current dir? this dir is: %s\n",
1396 mypath, current_info->dirname));
1397 if (*dnp == '/')
1398 dnp++;
1399 else {
1400 return -1;
1402 if (strcmp(mypath, dnp) == 0) {
1403 memset(buf, 0, sizeof(struct stat));
1404 buf->st_mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH;
1405 memcpy(&single_entry->my_stat, buf, sizeof(struct stat));
1406 DEBUG(1, (" CURRENT:found in %s\n", current_info->dirname));
1407 return 0;
1410 DEBUG(3, ("'%s' not found in current_info '%s'\n", path, current_info->dirname));
1411 /* try to find this in the PREVIOUS listing */
1412 if (previous_info) {
1413 if (search_dir_entry(previous_info->entries, mypath, buf) == 0)
1414 return 0;
1415 DEBUG(3, ("'%s' not found in previous_info '%s'\n", path, previous_info->dirname));
1417 /* try to find this in the SHARE listing */
1418 if (current_share_info && search_dir_entry(current_share_info->entries, mypath, buf) == 0)
1419 return 0;
1420 DEBUG(3, ("'%s' not found in share_info '%s'\n", path, current_share_info->dirname));
1421 /* try to find this in the SERVER listing */
1422 if (current_server_info && search_dir_entry(current_server_info->entries, mypath, buf) == 0)
1423 return 0;
1424 DEBUG(3, ("'%s' not found in server_info '%s'\n", path, current_server_info->dirname));
1425 /* nothing found. get stat file info from server */
1426 return get_remote_stat(sc, path, buf);
1429 static int
1430 smbfs_chdir (vfs *me, char *path)
1432 char *remote_dir;
1433 smbfs_connection *sc;
1435 DEBUG(3, ("smbfs_chdir(path:%s)\n", path));
1436 if (!(remote_dir = smbfs_get_path (&sc, path)))
1437 return -1;
1438 g_free (remote_dir);
1440 return 0;
1443 static int
1444 loaddir(vfs *me, const char *path)
1446 void *info;
1447 char *mypath, *p;
1449 mypath = g_strdup(path);
1450 p = strrchr(mypath, '/');
1452 if (p > mypath)
1453 *p = 0;
1454 DEBUG(6, ("loaddir(%s)\n", mypath));
1455 smbfs_chdir(me, mypath);
1456 info = smbfs_opendir (me, mypath);
1457 g_free(mypath);
1458 if (!info)
1459 return -1;
1460 smbfs_readdir(info);
1461 smbfs_loaddir(info);
1462 return 0;
1465 static int
1466 smbfs_stat (vfs *me, char *path, struct stat *buf)
1468 char *remote_dir;
1469 smbfs_connection *sc;
1470 pstring server_url;
1471 char *service, *pp;
1472 const char *p;
1474 DEBUG(3, ("smbfs_stat(path:%s)\n", path));
1476 #if 0
1477 if (p = strchr(path, '@')) /* user:pass@server */
1478 path = ++p; /* don't want user:pass@ in path */
1479 #endif
1481 if (!current_info) {
1482 DEBUG(1, ("current_info = NULL: "));
1483 if (loaddir(me, path) < 0)
1484 return -1;
1487 pstrcpy(server_url, URL_HEADER);
1488 pstrcat(server_url, current_bucket->host);
1490 /* check if stating server */
1491 p = path;
1492 if (strncmp(p, URL_HEADER, HEADER_LEN)) {
1493 DEBUG(1, ("'%s' doesnt start with '%s' (length %d)\n",
1494 p, URL_HEADER, HEADER_LEN));
1495 return -1;
1498 p += HEADER_LEN;
1499 if (*p == '/')
1500 p++;
1501 pp = strchr(p, '/'); /* advance past next '/' */
1502 if (!pp) {
1503 if (!current_info->server_list) {
1504 if (loaddir(me, path) < 0)
1505 return -1;
1507 return fake_server_stat(server_url, path, buf);
1509 if (!strchr(++pp, '/')) {
1510 return fake_share_stat(server_url, path, buf);
1513 /* stating inside share at this point */
1514 if (!(remote_dir = smbfs_get_path (&sc, path))) /* connects if necessary */
1515 return -1;
1516 g_free (remote_dir);
1518 int hostlen = strlen(current_bucket->host);
1519 char *pp = path + strlen(path)-hostlen;
1520 char *sp = server_url + strlen(server_url)-hostlen;
1522 if (strcmp(sp, pp) == 0) {
1523 /* make server name appear as directory */
1524 DEBUG(1, ("smbfs_stat: showing server as directory\n"));
1525 memset(buf, 0, sizeof(struct stat));
1526 buf->st_mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH;
1527 return 0;
1530 /* check if current_info is in share requested */
1531 p = service = g_strdup(p);
1532 pp = strchr(p, '/');
1533 if (pp) {
1534 p = ++pp; /* advance past server name */
1535 pp = strchr(p, '/');
1537 if (pp)
1538 *pp = 0; /* cut off everthing after service name */
1539 else
1540 p = IPC; /* browsing for services */
1541 pp = current_info->dirname;
1542 if (*pp == '/');
1543 pp++;
1544 if (strncmp(p, pp, strlen(p)) != 0) {
1545 DEBUG(6, ("desired '%s' is not loaded, we have '%s'\n", p, pp));
1546 if (loaddir(me, path) < 0) {
1547 g_free (service);
1548 return -1;
1550 DEBUG(6, ("loaded dir: '%s'\n", current_info->dirname));
1552 g_free(service);
1553 /* stat dirs & files under shares now */
1554 return get_stat_info(sc, path, buf);
1557 #define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */
1559 static int
1560 smbfs_lseek (void *data, off_t offset, int whence)
1562 DEBUG(3, ("smbfs_lseek()\n"));
1563 my_errno = EOPNOTSUPP;
1564 return -1;
1567 static int
1568 smbfs_mknod (vfs *me, char *path, int mode, int dev)
1570 DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%d)\n", path, mode, dev));
1571 my_errno = EOPNOTSUPP;
1572 return -1;
1575 static int
1576 smbfs_mkdir (vfs *me, char *path, mode_t mode)
1578 smbfs_connection *sc;
1579 char *remote_file;
1581 DEBUG(3, ("smbfs_mkdir(path:%s, mode:%d)\n", path, mode));
1582 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1583 return -1;
1584 g_free (remote_file);
1585 convert_path(&path, FALSE);
1587 if (!cli_mkdir(sc->cli, path)) {
1588 my_errno = cli_error(sc->cli, NULL, &err, NULL);
1589 message_3s (1, MSG_ERROR, _(" %s mkdir'ing %s "),
1590 cli_errstr(sc->cli), CNV_LANG(path));
1591 g_free (path);
1592 return -1;
1594 g_free (path);
1595 return 0;
1598 static int
1599 smbfs_rmdir (vfs *me, char *path)
1601 smbfs_connection *sc;
1602 char *remote_file;
1604 DEBUG(3, ("smbfs_rmdir(path:%s)\n", path));
1605 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1606 return -1;
1607 g_free (remote_file);
1608 convert_path(&path, FALSE);
1610 if (!cli_rmdir(sc->cli, path)) {
1611 my_errno = cli_error(sc->cli, NULL, &err, NULL);
1612 message_3s (1, MSG_ERROR, _(" %s rmdir'ing %s "),
1613 cli_errstr(sc->cli), CNV_LANG(path));
1614 g_free (path);
1615 return -1;
1618 g_free (path);
1619 return 0;
1622 static int
1623 smbfs_link (vfs *me, char *p1, char *p2)
1625 DEBUG(3, ("smbfs_link(p1:%s, p2:%s)\n", p1, p2));
1626 my_errno = EOPNOTSUPP;
1627 return -1;
1630 /* We do not free anything right now: we free resources when we run
1631 * out of them
1633 static vfsid
1634 smbfs_getid (vfs *me, char *p, struct vfs_stamping **parent)
1636 *parent = NULL;
1637 DEBUG(3, ("smbfs_getid(p:%s)\n", p));
1639 return (vfsid) -1;
1642 static int
1643 smbfs_nothingisopen (vfsid id)
1645 DEBUG(3, ("smbfs_nothingisopen(%d)\n", (int)id));
1646 return 0;
1649 static void
1650 smbfs_free (vfsid id)
1652 DEBUG(3, ("smbfs_free(%d)\n", (int)id));
1653 /* FIXME: Should not be empty */
1654 authinfo_free_all ();
1657 /* Gives up on a socket and reopens the connection, the child own the socket
1658 * now
1660 static void
1661 my_forget (char *path)
1663 char *host, *user, *p;
1664 int port, i;
1666 if (strncmp (path, URL_HEADER, HEADER_LEN))
1667 return;
1669 DEBUG(3, ("my_forget(path:%s)\n", path));
1671 path += 6;
1672 if (path[0] == '/' && path[1] == '/')
1673 path += 2;
1675 if ((p = smbfs_get_host_and_username (&path, &host, &user, &port, NULL))) {
1676 g_free (p);
1677 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1678 if ((strcmp (host, smbfs_connections [i].host) == 0) &&
1679 (strcmp (user, smbfs_connections [i].user) == 0) &&
1680 (port == smbfs_connections [i].port)) {
1682 /* close socket: the child owns it now */
1683 cli_shutdown(smbfs_connections [i].cli);
1685 /* reopen the connection */
1686 smbfs_connections [i].cli =
1687 smbfs_do_connect(host, smbfs_connections[i].service);
1691 g_free (host);
1692 g_free (user);
1695 static int
1696 smbfs_setctl (vfs *me, char *path, int ctlop, char *arg)
1698 DEBUG(3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path, ctlop));
1699 switch (ctlop) {
1700 case MCCTL_FORGET_ABOUT:
1701 my_forget(path);
1702 return 0;
1704 return 0;
1707 static smbfs_handle *
1708 open_write (smbfs_handle *remote_handle, char *rname, int flags, int mode)
1710 if (flags & O_TRUNC) /* if it exists truncate to zero */
1711 DEBUG(3, ("open_write: O_TRUNC\n"));
1713 remote_handle->fnum = cli_open(remote_handle->cli, rname, flags, DENY_NONE);
1715 if (remote_handle->fnum == -1) {
1716 message_3s (1, MSG_ERROR, _(" %s opening remote file %s "),
1717 cli_errstr(remote_handle->cli), CNV_LANG(rname));
1718 DEBUG(1,("smbfs_open(rname:%s) error:%s\n",
1719 rname, cli_errstr(remote_handle->cli)));
1720 my_errno = cli_error(remote_handle->cli, NULL, &err, NULL);
1721 return NULL;
1724 return remote_handle;
1727 static smbfs_handle *
1728 open_read (smbfs_handle *remote_handle, char *rname, int flags, int mode)
1730 size_t size;
1732 remote_handle->fnum =
1733 cli_open(remote_handle->cli, rname, O_RDONLY, DENY_NONE);
1735 if (remote_handle->fnum == -1) {
1736 message_3s (1, MSG_ERROR, _(" %s opening remote file %s "),
1737 cli_errstr(remote_handle->cli), CNV_LANG(rname));
1738 DEBUG(1,("smbfs_open(rname:%s) error:%s\n",
1739 rname, cli_errstr(remote_handle->cli)));
1740 my_errno = cli_error(remote_handle->cli, NULL, &err, NULL);
1741 return NULL;
1744 if (!cli_qfileinfo(remote_handle->cli, remote_handle->fnum,
1745 &remote_handle->attr, &size, NULL, NULL, NULL, NULL, NULL) &&
1746 !cli_getattrE(remote_handle->cli, remote_handle->fnum,
1747 &remote_handle->attr, &size, NULL, NULL, NULL)) {
1748 message_2s (1, MSG_ERROR, " getattrib: %s ",
1749 cli_errstr(remote_handle->cli));
1750 DEBUG(1,("smbfs_open(rname:%s) getattrib:%s\n",
1751 rname, cli_errstr(remote_handle->cli)));
1752 my_errno = cli_error(remote_handle->cli, NULL, &err, NULL);
1753 return NULL;
1756 return remote_handle;
1759 static void *
1760 smbfs_open (vfs *me, char *file, int flags, int mode)
1762 char *remote_file, *p;
1763 void *ret;
1764 smbfs_connection *sc;
1765 smbfs_handle *remote_handle;
1767 DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%d)\n", file, flags, mode));
1769 if (!(remote_file = smbfs_get_path (&sc, file)))
1770 return 0;
1772 p = remote_file;
1773 convert_path(&remote_file, FALSE);
1774 g_free (p);
1776 remote_handle = g_new (smbfs_handle, 2);
1777 remote_handle->cli = sc->cli;
1778 remote_handle->nread = 0;
1780 if (flags & O_CREAT)
1781 ret = open_write(remote_handle, remote_file, flags, mode);
1782 else
1783 ret = open_read(remote_handle, remote_file, flags, mode);
1785 g_free (remote_file);
1787 return ret;
1790 static int
1791 smbfs_unlink (vfs *me, char *path)
1793 smbfs_connection *sc;
1794 char *remote_file, *p;
1796 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1797 return -1;
1799 p = remote_file;
1800 convert_path(&remote_file, FALSE);
1801 g_free (p);
1803 if (!cli_unlink(sc->cli, remote_file)) {
1804 message_3s (1, MSG_ERROR, _(" %s removing remote file %s "),
1805 cli_errstr(sc->cli), CNV_LANG(remote_file));
1806 g_free (remote_file);
1807 return -1;
1809 g_free (remote_file);
1810 return 0;
1813 static int
1814 smbfs_rename (vfs *me, char *a, char *b)
1816 smbfs_connection *sc;
1817 char *ra, *rb;
1818 char *p;
1819 int retval;
1821 if ((ra = smbfs_get_path (&sc, a)) == 0)
1822 return -1;
1824 if ((rb = smbfs_get_path (&sc, b)) == 0) {
1825 g_free (ra);
1826 return -1;
1829 p = ra;
1830 convert_path(&ra, FALSE);
1831 g_free (p);
1832 p = rb;
1833 convert_path(&rb, FALSE);
1834 g_free (p);
1836 retval = cli_rename(sc->cli, ra, rb);
1838 g_free (ra);
1839 g_free (rb);
1841 if (!retval) {
1842 message_2s (1, MSG_ERROR, _(" %s renaming files\n"),
1843 cli_errstr(sc->cli));
1844 return -1;
1846 return 0;
1849 static int
1850 smbfs_fstat (void *data, struct stat *buf)
1852 smbfs_handle *remote_handle = (smbfs_handle *)data;
1854 DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle->fnum));
1856 /* use left over from previous get_remote_stat, if available */
1857 if (single_entry)
1858 memcpy(buf, &single_entry->my_stat, sizeof(struct stat));
1859 else { /* single_entry not set up: bug */
1860 my_errno = EFAULT;
1861 return -EFAULT;
1863 return 0;
1866 vfs vfs_smbfs_ops = {
1867 NULL, /* This is place of next pointer */
1868 "smbfs",
1869 F_NET, /* flags */
1870 "smb:", /* prefix */
1871 NULL, /* data */
1872 0, /* errno */
1873 smbfs_init,
1874 NULL,
1875 smbfs_fill_names,
1876 NULL,
1878 smbfs_open,
1879 smbfs_close,
1880 smbfs_read,
1881 smbfs_write,
1883 smbfs_opendir,
1884 smbfs_readdir,
1885 smbfs_closedir,
1886 NULL,
1887 NULL,
1889 smbfs_stat,
1890 smbfs_lstat,
1891 smbfs_fstat,
1893 smbfs_chmod,
1894 smbfs_chown,
1895 smbfs_utime,
1897 smbfs_readlink,
1898 smbfs_symlink,
1899 smbfs_link,
1900 smbfs_unlink,
1902 smbfs_rename,
1903 smbfs_chdir,
1904 smbfs_errno,
1905 smbfs_lseek,
1906 smbfs_mknod,
1908 smbfs_getid,
1909 smbfs_nothingisopen,
1910 smbfs_free,
1912 NULL,
1913 NULL,
1915 smbfs_mkdir,
1916 smbfs_rmdir,
1917 NULL,
1918 smbfs_setctl
1920 MMAPNULL