* configure.in: Bump version to 4.5.55. Release mc-4.5.55.
[midnight-commander.git] / vfs / smbfs.c
blob96fde4b1e3ab9dbb9b60e3551cfe38a28d4dbf0f
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 smbfs_vfs_ops */
24 #include <stdio.h>
25 #include <sys/types.h>
27 #include <config.h>
28 #include "utilvfs.h"
29 #include "samba/include/config.h"
30 /* don't load crap in "samba/include/includes.h" we don't use and which
31 conflicts with definitions in other includes */
32 #undef HAVE_LIBREADLINE
33 #define NO_CONFIG_H
34 #define BOOL_DEFINED
35 #undef VERSION
36 #include "samba/include/includes.h"
38 #include <string.h>
40 #include "vfs.h"
41 #include "smbfs.h"
42 #include "tcputil.h"
43 #include "../src/dialog.h"
45 #define SMBFS_MAX_CONNECTIONS 16
46 static const char * const IPC = "IPC$";
47 static const char * const URL_HEADER = "/#smb:";
48 #define HEADER_LEN 6
50 static int my_errno;
51 static uint32 err;
53 /* stuff that is same with each connection */
54 extern int DEBUGLEVEL;
55 static pstring myhostname;
56 static mode_t myumask = 0755;
57 extern pstring global_myname;
58 static int smbfs_open_connections = 0;
59 static gboolean got_user = FALSE;
60 static gboolean got_pass = FALSE;
61 static pstring password;
62 static pstring username;
64 static struct _smbfs_connection {
65 struct cli_state *cli;
66 struct in_addr dest_ip;
67 BOOL have_ip;
68 char *host; /* server name */
69 char *service; /* share name */
70 char *domain;
71 char *user;
72 char *home;
73 char *password;
74 int port;
75 int name_type;
76 time_t last_use;
77 } smbfs_connections [SMBFS_MAX_CONNECTIONS];
78 /* unique to each connection */
80 typedef struct _smbfs_connection smbfs_connection;
81 static smbfs_connection *current_bucket;
83 typedef struct {
84 struct cli_state *cli;
85 int fnum;
86 off_t nread;
87 uint16 attr;
88 } smbfs_handle;
90 static GSList *auth_list;
92 static void
93 authinfo_free (struct smb_authinfo const *a)
95 g_free (a->host);
96 g_free (a->share);
97 g_free (a->domain);
98 g_free (a->user);
99 wipe_password (a->password);
102 static void
103 authinfo_free_all ()
105 if (auth_list) {
106 g_slist_foreach (auth_list, (GFunc)authinfo_free, 0);
107 g_slist_free (auth_list);
108 auth_list = 0;
112 static gint
113 authinfo_compare_host_and_share (gconstpointer _a, gconstpointer _b)
115 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
116 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
118 if (!a->host || !a->share || !b->host || !b->share)
119 return 1;
120 if (strcmp (a->host, b->host) != 0)
121 return 1;
122 if (strcmp (a->share, b->share) != 0)
123 return 1;
124 return 0;
127 static gint
128 authinfo_compare_host (gconstpointer _a, gconstpointer _b)
130 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
131 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
133 if (!a->host || !b->host)
134 return 1;
135 if (strcmp (a->host, b->host) != 0)
136 return 1;
137 if (strcmp (a->share, IPC) != 0)
138 return 1;
139 return 0;
142 static void
143 authinfo_add (const char *host, const char *share, const char *domain,
144 const char *user, const char *password)
146 struct smb_authinfo *auth = g_new (struct smb_authinfo, 1);
148 if (!auth)
149 return;
151 /* Don't check for NULL, g_strdup already does. */
152 auth->host = g_strdup (host);
153 auth->share = g_strdup (share);
154 auth->domain = g_strdup (domain);
155 auth->user = g_strdup (user);
156 auth->password = g_strdup (password);
157 auth_list = g_slist_prepend (auth_list, auth);
160 static void
161 authinfo_remove (const char *host, const char *share)
163 struct smb_authinfo data;
164 struct smb_authinfo *auth;
165 GSList *list;
167 data.host = g_strdup (host);
168 data.share = g_strdup (share);
169 list = g_slist_find_custom (auth_list,
170 &data,
171 authinfo_compare_host_and_share);
172 g_free (data.host);
173 g_free (data.share);
174 if (!list)
175 return;
176 auth = list->data;
177 auth_list = g_slist_remove (auth_list, auth);
178 authinfo_free (auth);
181 /* Set authentication information in bucket. Return 1 if successful, else 0 */
182 /* Information in auth_list overrides user if pass is NULL. */
183 /* bucket->host and bucket->service must be valid. */
184 static int
185 bucket_set_authinfo (smbfs_connection *bucket,
186 const char *domain, const char *user, const char *pass,
187 int fallback_to_host)
189 struct smb_authinfo data;
190 struct smb_authinfo *auth;
191 GSList *list;
193 if (domain && user && pass) {
194 g_free (bucket->domain);
195 g_free (bucket->user);
196 g_free (bucket->password);
197 bucket->domain = g_strdup (domain);
198 bucket->user = g_strdup (user);
199 bucket->password = g_strdup (pass);
200 authinfo_remove (bucket->host, bucket->service);
201 authinfo_add (bucket->host, bucket->service,
202 domain, user, pass);
203 return 1;
206 data.host = bucket->host;
207 data.share = bucket->service;
208 list = g_slist_find_custom (auth_list, &data, authinfo_compare_host_and_share);
209 if (!list && fallback_to_host)
210 list = g_slist_find_custom (auth_list, &data, authinfo_compare_host);
211 if (list) {
212 auth = list->data;
213 bucket->domain = g_strdup (auth->domain);
214 bucket->user = g_strdup (auth->user);
215 bucket->password = g_strdup (auth->password);
216 return 1;
219 if (got_pass) {
220 bucket->domain = g_strdup (lp_workgroup ());
221 bucket->user = g_strdup (got_user ? username : user);
222 bucket->password = g_strdup (password);
223 return 1;
226 auth = vfs_smb_get_authinfo (bucket->host,
227 bucket->service,
228 (domain ? domain : lp_workgroup ()),
229 user);
230 if (auth) {
231 g_free (bucket->domain);
232 g_free (bucket->user);
233 g_free (bucket->password);
234 bucket->domain = g_strdup (auth->domain);
235 bucket->user = g_strdup (auth->user);
236 bucket->password = g_strdup (auth->password);
237 authinfo_remove (bucket->host, bucket->service);
238 auth_list = g_slist_prepend (auth_list, auth);
239 return 1;
241 return 0;
244 void
245 smbfs_set_debug(int arg)
247 DEBUGLEVEL = arg;
250 /********************** The callbacks ******************************/
251 static int
252 smbfs_init(vfs *me)
254 char *servicesf = "/etc/smb.conf";
256 /* DEBUGLEVEL = 4; */
258 setup_logging("mc", True);
259 TimeInit();
260 charset_initialise();
262 DEBUG(3, ("smbfs_init(%s)\n", me->name));
264 if(!get_myname(myhostname,NULL))
265 DEBUG(0,("Failed to get my hostname.\n"));
267 if (!lp_load(servicesf,True,False,False))
268 DEBUG(0, ("Cannot load %s - run testparm to debug it\n", servicesf));
270 codepage_initialise(lp_client_code_page());
272 load_interfaces();
273 myumask = umask(0);
274 umask(myumask);
276 if (getenv("USER")) {
277 char *p;
278 pstrcpy(username, getenv("USER"));
279 got_user = TRUE;
280 DEBUG(3, ("smbfs_init(): $USER:%s\n", username));
281 if ((p = strchr(username, '%'))) {
282 *p = 0;
283 pstrcpy(password, p+1);
284 got_pass = TRUE;
285 memset(strchr(getenv("USER"), '%')+1, 'X', strlen(password));
286 DEBUG(3, ("smbfs_init(): $USER%%pass: %s%%%s\n",
287 username, password));
289 strupper(username);
291 if (getenv("PASSWD")) {
292 pstrcpy(password, getenv("PASSWD"));
293 got_pass = TRUE;
295 return 1;
298 static void
299 smbfs_fill_names (vfs *me, void (*func)(char *))
301 int i;
302 char *path;
303 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
304 if (smbfs_connections [i].cli) {
305 path = g_strconcat (URL_HEADER,
306 smbfs_connections[i].host,
307 "/", smbfs_connections[i].service,
308 NULL);
309 (*func)(path);
310 g_free (path);
315 #define CNV_LANG(s) dos_to_unix(s,False)
316 #define GNAL_VNC(s) unix_to_dos(s,False)
317 /* does same as do_get() in client.c */
318 /* called from vfs.c:1080, count = buffer size */
319 static int
320 smbfs_read (void *data, char *buffer, int count)
322 smbfs_handle *info = (smbfs_handle *) data;
323 int n;
325 DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%d)\n",
326 info->fnum, (int)info->nread, count));
327 n = cli_read(info->cli, info->fnum, buffer, info->nread, count);
328 if (n > 0)
329 info->nread += n;
330 return n;
333 static int
334 smbfs_write (void *data, char *buf, int nbyte)
336 smbfs_handle *info = (smbfs_handle *) data;
337 int n;
339 DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%d)\n",
340 info->fnum, (int)info->nread, nbyte));
341 n = cli_write(info->cli, info->fnum, 0, buf, info->nread, nbyte);
342 if (n > 0)
343 info->nread += n;
344 return n;
347 static int
348 smbfs_close (void *data)
350 smbfs_handle *info = (smbfs_handle *) data;
351 DEBUG(3, ("smbfs_close(fnum:%d)\n", info->fnum));
352 /* if imlementing archive_level: add rname to smbfs_handle
353 if (archive_level >= 2 && (inf->attr & aARCH)) {
354 cli_setatr(info->cli, rname, info->attr & ~(uint16)aARCH, 0);
355 } */
356 return (cli_close(info->cli, info->fnum) == True) ? 0 : -1;
359 static int
360 smbfs_errno (vfs *me)
362 DEBUG(3, ("smbfs_errno: %s\n", g_strerror(my_errno)));
363 return my_errno;
366 typedef struct dir_entry {
367 char *text;
368 struct dir_entry *next;
369 struct stat my_stat;
370 int merrno;
371 } dir_entry;
373 typedef struct {
374 gboolean server_list;
375 char *dirname;
376 char *path; /* the dir originally passed to smbfs_opendir */
377 smbfs_connection *conn;
378 dir_entry *entries;
379 dir_entry *current;
380 } opendir_info;
382 static opendir_info
383 *previous_info,
384 *current_info,
385 *current_share_info,
386 *current_server_info;
388 static gboolean first_direntry;
390 static dir_entry *
391 new_dir_entry (const char * name)
393 dir_entry *new_entry;
394 new_entry = g_new0 (dir_entry, 1);
395 new_entry->text = dos_to_unix (g_strdup (name), 1);
397 if (first_direntry) {
398 current_info->entries = new_entry;
399 first_direntry = FALSE;
400 } else {
401 current_info->current->next = new_entry;
403 current_info->current = new_entry;
405 return new_entry;
408 /* browse for shares on server */
409 static void
410 browsing_helper(const char *name, uint32 type, const char *comment)
412 char *typestr = "";
414 dir_entry *new_entry = new_dir_entry (name);
416 switch (type) {
417 case STYPE_DISKTREE:
418 typestr = "Disk";
419 /* show this as dir */
420 new_entry->my_stat.st_mode = S_IFDIR|S_IRUSR|S_IRGRP|S_IROTH;
421 break;
422 case STYPE_PRINTQ:
423 typestr = "Printer"; break;
424 case STYPE_DEVICE:
425 typestr = "Device"; break;
426 case STYPE_IPC:
427 typestr = "IPC"; break;
429 DEBUG(3, ("\t%-15.15s%-10.10s%s\n", name, typestr, comment));
432 static void
433 loaddir_helper(file_info *finfo, const char *mask)
435 dir_entry *new_entry;
436 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
437 #if 0 /* I want to see dot files */
438 if (finfo->mode & aHIDDEN)
439 return; /* don't bother with hidden files, "~$" screws up mc */
440 #endif
441 new_entry = new_dir_entry (finfo->name);
443 new_entry->my_stat.st_size = finfo->size;
444 new_entry->my_stat.st_mtime = finfo->mtime;
445 new_entry->my_stat.st_atime = finfo->atime;
446 new_entry->my_stat.st_ctime = finfo->ctime;
447 new_entry->my_stat.st_uid = finfo->uid;
448 new_entry->my_stat.st_gid = finfo->gid;
451 new_entry->my_stat.st_mode =
452 S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR|S_IWGRP|S_IWOTH;
454 /* if (finfo->mode & aVOLID); nothing similar in real world */
455 if (finfo->mode & aDIR)
456 new_entry->my_stat.st_mode |= S_IFDIR;
457 else
458 new_entry->my_stat.st_mode |= S_IFREG; /* if not dir, regular file? */
459 /* if (finfo->mode & aARCH); DOS archive */
460 /* if (finfo->mode & aHIDDEN); like a dot file? */
461 /* if (finfo->mode & aSYSTEM); like a kernel? */
462 if (finfo->mode & aRONLY)
463 new_entry->my_stat.st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
465 DEBUG(3, (" %-30s%7.7s%8.0f %s",
466 CNV_LANG(finfo->name),
467 attrib_string(finfo->mode),
468 (double)finfo->size,
469 asctime(LocalTime(&t))));
472 /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
473 static int
474 convert_path(char **remote_file, gboolean trailing_asterik)
476 char *p, *my_remote;
478 my_remote = *remote_file;
479 if (strncmp (my_remote, URL_HEADER, HEADER_LEN) == 0) { /* if passed directly */
480 my_remote += 6;
481 if (*my_remote == '/') /* from server browsing */
482 my_remote++;
483 p = strchr(my_remote, '/');
484 if (p)
485 my_remote = p+1; /* advance to end of server name */
488 if (*my_remote == '/')
489 my_remote++; /* strip off leading '/' */
490 p = strchr(my_remote, '/');
491 if (p)
492 my_remote = p; /* strip off share/service name */
493 /* create remote filename as understood by smb clientgen */
494 p = *remote_file = g_strconcat (my_remote, trailing_asterik ? "/*" : "", 0);
495 unix_to_dos (*remote_file, 1);
496 while ((p = strchr(p, '/')))
497 *p = '\\';
498 return 0;
501 static void
502 server_browsing_helper(const char *name, uint32 m, const char *comment)
504 dir_entry *new_entry = new_dir_entry (name);
506 /* show this as dir */
507 new_entry->my_stat.st_mode = S_IFDIR|S_IRUSR|S_IRGRP|S_IROTH;
509 DEBUG(3, ("\t%-16.16s %s\n", name, comment));
512 static BOOL
513 reconnect(smbfs_connection *conn, int *retries)
515 char *host;
516 DEBUG(3, ("RECONNECT\n"));
518 if (*(conn->host) == 0)
519 host = g_strdup(conn->cli->desthost); /* server browsing */
520 else
521 host = g_strdup(conn->host);
523 cli_shutdown(conn->cli);
525 if (!(conn->cli = do_connect(host, conn->service))) {
526 message_2s (1, MSG_ERROR,
527 _(" reconnect to %s failed\n "), conn->host);
528 g_free(host);
529 return False;
531 g_free(host);
532 if (++(*retries) == 2)
533 return False;
534 return True;
537 static BOOL
538 smb_send(struct cli_state *cli)
540 size_t len;
541 size_t nwritten=0;
542 ssize_t ret;
544 len = smb_len(cli->outbuf) + 4;
546 while (nwritten < len) {
547 ret = write_socket(cli->fd, cli->outbuf+nwritten, len - nwritten);
548 if (ret <= 0 && errno == EPIPE)
549 return False;
550 nwritten += ret;
553 return True;
556 /****************************************************************************
557 See if server has cut us off by checking for EPIPE when writing.
558 Taken from cli_chkpath()
559 ****************************************************************************/
560 static BOOL
561 chkpath(struct cli_state *cli, char *path, BOOL send_only)
563 fstring path2;
564 char *p;
566 fstrcpy(path2,path);
567 unix_to_dos (path2, 1);
568 trim_string(path2,NULL,"\\");
569 if (!*path2) *path2 = '\\';
571 memset(cli->outbuf,'\0',smb_size);
572 set_message(cli->outbuf,0,4 + strlen(path2),True);
573 SCVAL(cli->outbuf,smb_com,SMBchkpth);
574 SSVAL(cli->outbuf,smb_tid,cli->cnum);
576 cli->rap_error = 0;
577 cli->nt_error = 0;
578 SSVAL(cli->outbuf,smb_pid,cli->pid);
579 SSVAL(cli->outbuf,smb_uid,cli->vuid);
580 SSVAL(cli->outbuf,smb_mid,cli->mid);
581 if (cli->protocol > PROTOCOL_CORE) {
582 SCVAL(cli->outbuf,smb_flg,0x8);
583 SSVAL(cli->outbuf,smb_flg2,0x1);
586 p = smb_buf(cli->outbuf);
587 *p++ = 4;
588 fstrcpy(p,path2);
590 if (!smb_send(cli)) {
591 DEBUG(3, ("chkpath: couldnt send\n"));
592 return False;
594 if (send_only) {
595 client_receive_smb(cli->fd, cli->inbuf, cli->timeout);
596 DEBUG(3, ("chkpath: send only OK\n"));
597 return True; /* just testing for EPIPE */
599 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout)) {
600 DEBUG(3, ("chkpath: receive error\n"));
601 return False;
603 if ((my_errno = cli_error(cli, NULL, NULL, NULL))) {
604 if (my_errno == 20 || my_errno == 13)
605 return True; /* ignore if 'not a directory' error */
606 DEBUG(3, ("chkpath: cli_error: %s\n", g_strerror(my_errno)));
607 return False;
610 return True;
613 /* #if 0 */
614 static int
615 fs (const char *text)
617 const char *p = text;
618 int count = 0;
620 while ((p = strchr(p, '/')) != NULL) {
621 count++;
622 p++;
624 if (count == 1)
625 return strlen(text);
626 return count;
628 /* #endif */
630 static int
631 smbfs_loaddir (opendir_info *smbfs_info)
633 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
634 int servlen = strlen(smbfs_info->conn->service);
635 char *my_dirname = smbfs_info->dirname;
637 DEBUG(3, ("smbfs_loaddir: dirname:%s\n", my_dirname));
638 first_direntry = TRUE;
640 if (current_info) {
641 DEBUG(3, ("smbfs_loaddir: new:'%s', cached:'%s'\n", my_dirname, current_info->dirname));
642 /* if new desired dir is longer than cached in current_info */
643 if (fs(my_dirname) > fs(current_info->dirname)) {
644 DEBUG(3, ("saving to previous_info\n"));
645 previous_info = current_info;
649 current_info = smbfs_info;
651 if (strcmp(my_dirname, "/") == 0) {
652 if (!strcmp(smbfs_info->path, URL_HEADER)) {
653 DEBUG(6, ("smbfs_loaddir: browsing %s\n", IPC));
654 /* browse for servers */
655 if (!cli_NetServerEnum(smbfs_info->conn->cli, smbfs_info->conn->domain,
656 SV_TYPE_ALL, server_browsing_helper))
657 return 0;
658 else
659 current_server_info = smbfs_info;
660 smbfs_info->server_list = TRUE;
661 } else {
662 /* browse for shares */
663 if (cli_RNetShareEnum(smbfs_info->conn->cli, browsing_helper) < 1)
664 return 0;
665 else
666 current_share_info = smbfs_info;
668 goto done;
671 /* do regular directory listing */
672 if(strncmp(smbfs_info->conn->service, my_dirname+1, servlen) == 0) {
673 /* strip share name from dir */
674 char *p = my_dirname = g_strdup(my_dirname + servlen);
675 *p = '/';
676 convert_path(&my_dirname, TRUE);
677 g_free (p);
678 } else
679 convert_path(&my_dirname, TRUE);
681 DEBUG(6, ("smbfs_loaddir: service: %s\n", smbfs_info->conn->service));
682 DEBUG(6, ("smbfs_loaddir: cli->share: %s\n", smbfs_info->conn->cli->share));
683 DEBUG(6, ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname));
684 /* do file listing: cli_list returns number of files */
685 if (cli_list(
686 smbfs_info->conn->cli, my_dirname, attribute, loaddir_helper) < 0) {
687 /* cli_list returns -1 if directory empty or cannot read socket */
688 my_errno = cli_error(smbfs_info->conn->cli, NULL, &err, NULL);
689 g_free (my_dirname);
690 return 0;
692 if (*(my_dirname) == 0)
693 smbfs_info->dirname = smbfs_info->conn->service;
694 g_free (my_dirname);
695 /* do_dskattr(); */
697 done:
698 /* current_info->parent = smbfs_info->dirname; */
700 smbfs_info->current = smbfs_info->entries;
701 return 1; /* 1 = ok */
704 #ifdef SMBFS_FREE_DIR
705 static void
706 smbfs_free_dir (dir_entry *de)
708 if (!de) return;
710 smbfs_free_dir (de->next);
711 g_free (de->text);
712 g_free (de);
714 #endif
716 /* Explanation:
717 * On some operating systems (Slowaris 2 for example)
718 * the d_name member is just a char long (Nice trick that break everything,
719 * so we need to set up some space for the filename.
721 static struct {
722 struct dirent dent;
723 #ifdef NEED_EXTRA_DIRENT_BUFFER
724 char extra_buffer [MC_MAXPATHLEN];
725 #endif
726 } smbfs_readdir_data;
729 /* The readdir routine loads the complete directory */
730 /* It's too slow to ask the server each time */
731 /* It now also sends the complete lstat information for each file */
732 static void *
733 smbfs_readdir (void *info)
735 static char * const dirent_dest = &(smbfs_readdir_data.dent.d_name [0]);
736 opendir_info *smbfs_info = (opendir_info *) info;
738 DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info->dirname));
740 if (!smbfs_info->entries)
741 if (!smbfs_loaddir (smbfs_info))
742 return NULL;
744 if (smbfs_info->current == 0) { /* reached end of dir entries */
745 DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
746 #ifdef SMBFS_FREE_DIR
747 smbfs_free_dir (smbfs_info->entries);
748 smbfs_info->entries = 0;
749 #endif
750 return NULL;
752 pstrcpy (dirent_dest, smbfs_info->current->text);
753 smbfs_info->current = smbfs_info->current->next;
755 #ifndef DIRENT_LENGTH_COMPUTED
756 smbfs_readdir_data.dent.d_namlen = strlen (smbfs_readdir_data.dent.d_name);
757 #endif
759 return &smbfs_readdir_data;
762 static int
763 smbfs_closedir (void *info)
765 opendir_info *smbfs_info = (opendir_info *) info;
766 /* dir_entry *p, *q; */
768 DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info->dirname));
769 /* CLOSE HERE */
771 /* for (p = smbfs_info->entries; p;){
772 q = p;
773 p = p->next;
774 g_free (q->text);
775 g_free (q);
777 g_free (info); */
778 return 0;
781 static int
782 smbfs_chmod (vfs *me, char *path, int mode)
784 DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path, mode));
785 /* my_errno = EOPNOTSUPP;
786 return -1; */ /* cant chmod on smb filesystem */
787 return 0; /* make mc happy */
790 static int
791 smbfs_chown (vfs *me, char *path, int owner, int group)
793 DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path, owner, group));
794 my_errno = EOPNOTSUPP; /* ready for your labotomy? */
795 return -1;
798 static int
799 smbfs_utime (vfs *me, char *path, struct utimbuf *times)
801 DEBUG(3, ("smbfs_utime(path:%s)\n", path));
802 my_errno = EOPNOTSUPP;
803 return -1;
806 static int
807 smbfs_readlink (vfs *me, char *path, char *buf, int size)
809 DEBUG(3, ("smbfs_readlink(path:%s, buf:%s, size:%d)\n", path, buf, size));
810 my_errno = EOPNOTSUPP;
811 return -1; /* no symlinks on smb filesystem? */
814 static int
815 smbfs_symlink (vfs *me, char *n1, char *n2)
817 DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1, n2));
818 my_errno = EOPNOTSUPP;
819 return -1; /* no symlinks on smb filesystem? */
822 /* Extract the hostname and username from the path */
823 /* path is in the form: hostname:user/remote-dir */
824 #if 0
825 static char *
826 smbfs_get_host_and_username
827 (char **path, char **host, char **user, int *port, char **pass)
829 /* char *p, *ret; */
830 char *ret;
832 ret = vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0);
834 #if 0
835 if ((p = strchr(*path, '@'))) /* user:pass@server */
836 *path = ++p; /* don't want user:pass@ in path */
837 if ((p = strchr(ret, '@'))) /* user:pass@server */
838 ret = ++p; /* don't want user:pass@ in path */
839 #endif
841 return ret;
843 #else
844 #define smbfs_get_host_and_username(path, host, user, port, pass) \
845 vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
846 #endif
847 /*****************************************************
848 return a connection to a SMB server
849 current_bucket needs to be set before calling
850 *******************************************************/
851 struct cli_state *
852 do_connect (char *server, char *share)
854 struct cli_state *c;
855 struct nmb_name called, calling;
856 struct in_addr ip;
857 extern struct in_addr ipzero;
859 DEBUG(3, ("do_connect(%s, %s)\n", server, share));
860 if (*share == '\\') {
861 server = share+2;
862 share = strchr(server,'\\');
863 if (!share) return NULL;
864 *share = 0;
865 share++;
868 ip = ipzero;
870 make_nmb_name(&calling, global_myname, 0x0, "");
871 make_nmb_name(&called , server, current_bucket->name_type, "");
873 for (;;) {
875 ip = ipzero;
876 if (current_bucket->have_ip) ip = current_bucket->dest_ip;
878 /* have to open a new connection */
879 if (!(c = cli_initialise(NULL))) {
880 my_errno = ENOMEM;
881 return NULL;
884 pwd_init(&(c->pwd)); /* should be moved into cli_initialise()? */
885 pwd_set_cleartext(&(c->pwd), current_bucket->password);
887 if ((cli_set_port(c, current_bucket->port) == 0) ||
888 !cli_connect(c, server, &ip)) {
889 DEBUG(1, ("Connection to %s failed\n", server));
890 break;
893 if (!cli_session_request(c, &calling, &called)) {
894 my_errno = cli_error(c, NULL, &err, NULL);
895 DEBUG(1, ("session request to %s failed\n", called.name));
896 cli_shutdown(c);
897 if (strcmp(called.name, "*SMBSERVER")) {
898 make_nmb_name(&called , "*SMBSERVER", 0x20, "");
899 continue;
901 return NULL;
904 DEBUG(3, (" session request ok\n"));
906 if (!cli_negprot(c)) {
907 DEBUG(1, ("protocol negotiation failed\n"));
908 break;
911 if (!cli_session_setup(c, current_bucket->user,
912 current_bucket->password, strlen(current_bucket->password),
913 current_bucket->password, strlen(current_bucket->password),
914 current_bucket->domain)) {
915 DEBUG(1,("session setup failed: %s\n", cli_errstr(c)));
916 authinfo_remove (server, share);
917 break;
920 if (*c->server_domain || *c->server_os || *c->server_type)
921 DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
922 c->server_domain,c->server_os,c->server_type));
924 DEBUG(3, (" session setup ok\n"));
926 if (!cli_send_tconX(c, share, "?????",
927 current_bucket->password, strlen(current_bucket->password)+1)) {
928 DEBUG(1,("%s: tree connect failed: %s\n", share, cli_errstr(c)));
929 break;
932 DEBUG(3, (" tconx ok\n"));
934 my_errno = 0;
935 return c;
938 my_errno = cli_error(c, NULL, &err, NULL);
939 cli_shutdown(c);
940 return NULL;
944 static int
945 get_master_browser(char **host)
947 int count;
948 struct in_addr *ip_list, bcast_addr, ipzero;
949 /* does port = 137 for win95 master browser? */
950 int fd= open_socket_in( SOCK_DGRAM, 0, 3,
951 interpret_addr(lp_socket_address()), True );
952 if (fd == -1)
953 return 0;
954 set_socket_options(fd, "SO_BROADCAST");
955 ip_list = iface_bcast(ipzero);
956 bcast_addr = *ip_list;
957 if ((ip_list = name_query(fd, "\01\02__MSBROWSE__\02", 1, True,
958 True, bcast_addr, &count, NULL))) {
959 if (!count)
960 return 0;
961 /* just return first master browser */
962 *host = g_strdup(inet_ntoa(ip_list[0]));
963 return 1;
965 return 0;
968 static void
969 free_bucket (smbfs_connection *bucket)
971 g_free (bucket->host);
972 g_free (bucket->service);
973 g_free (bucket->domain);
974 g_free (bucket->user);
975 wipe_password (bucket->password);
976 g_free (bucket->home);
977 bzero (bucket, sizeof (smbfs_connection));
980 static smbfs_connection *
981 smbfs_get_free_bucket ()
983 int i;
985 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++)
986 if (!smbfs_connections [i].cli) return &smbfs_connections [i];
988 { /* search for most dormant connection */
989 int oldest = 0; /* index */
990 time_t oldest_time = time(NULL);
991 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
992 if (smbfs_connections[i].last_use < oldest_time) {
993 oldest_time = smbfs_connections[i].last_use;
994 oldest = i;
997 cli_shutdown(smbfs_connections[oldest].cli);
998 free_bucket (&smbfs_connections[oldest]);
999 return &smbfs_connections[oldest];
1002 /* This can't happend, since we have checked for max connections before */
1003 vfs_die("Internal error: smbfs_get_free_bucket");
1004 return 0; /* shut up, stupid gcc */
1007 /* This routine keeps track of open connections */
1008 /* Returns a connected socket to host */
1009 static smbfs_connection *
1010 smbfs_open_link(char *host, char *path, const char *user, int *port, char *this_pass)
1012 int i;
1013 smbfs_connection *bucket;
1014 pstring service;
1015 struct in_addr *dest_ip = NULL;
1017 DEBUG(3, ("smbfs_open_link(host:%s, path:%s)\n", host, path));
1019 if (strcmp(host, path) == 0) /* if host & path are same: */
1020 pstrcpy(service, IPC); /* setup for browse */
1021 else { /* get share name from path, path starts with server name */
1022 char *p;
1023 if ((p = strchr(path, '/'))) /* get share aka */
1024 pstrcpy(service, ++p); /* service name from path */
1025 else
1026 pstrcpy(service, "");
1027 /* now check for trailing directory/filenames */
1028 p = strchr(service, '/');
1029 if (p)
1030 *p = 0; /* cut off dir/files: sharename only */
1032 DEBUG(6, ("smbfs_open_link: service from path:%s\n", service));
1035 if (got_user)
1036 user = username; /* global from getenv */
1038 /* Is the link actually open? */
1039 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1040 if (!smbfs_connections[i].cli)
1041 continue;
1042 if ((strcmp (host, smbfs_connections [i].host) == 0) &&
1043 (strcmp (user, smbfs_connections [i].user) == 0) &&
1044 (strcmp (service, smbfs_connections [i].service) == 0)) {
1045 int retries = 0;
1046 BOOL inshare = (*host != 0 && *path != 0 && strchr(path, '/'));
1047 /* check if this connection has died */
1048 while (!chkpath(smbfs_connections[i].cli, "\\", !inshare)) {
1049 if (!reconnect(&smbfs_connections[i], &retries))
1050 return 0;
1052 DEBUG(6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i));
1053 current_bucket = &smbfs_connections[i];
1054 smbfs_connections [i].last_use = time(NULL);
1055 return &smbfs_connections [i];
1057 /* connection not found, find if we have ip for new connection */
1058 if (strcmp (host, smbfs_connections [i].host) == 0)
1059 dest_ip = &smbfs_connections[i].cli->dest_ip;
1062 /* make new connection */
1063 bucket = smbfs_get_free_bucket ();
1064 bucket->name_type = 0x20;
1065 bucket->home = 0;
1066 bucket->port = *port;
1067 bucket->have_ip = False;
1068 if (dest_ip) {
1069 bucket->have_ip = True;
1070 bucket->dest_ip = *dest_ip;
1072 current_bucket = bucket;
1074 bucket->user = g_strdup(user);
1075 bucket->service = g_strdup (service);
1077 if (!(*host)) { /* if blank host name, browse for servers */
1078 if (!get_master_browser(&host)) /* set host to ip of master browser */
1079 return 0; /* couldnt find master browser? */
1080 g_free (host);
1081 bucket->host = g_strdup(""); /* blank host means master browser */
1082 } else
1083 bucket->host = g_strdup(host);
1085 if (!bucket_set_authinfo (bucket,
1086 0, /* domain currently not used */
1087 user,
1088 this_pass,
1090 return 0;
1092 /* connect to share */
1093 while (!(bucket->cli = do_connect(host, service))) {
1095 if (my_errno != EPERM)
1096 return 0;
1097 message_1s (1, MSG_ERROR,
1098 _(" Authentication failed "));
1100 /* authentication failed, try again */
1101 authinfo_remove (bucket->host, bucket->service);
1102 if (!bucket_set_authinfo (bucket,
1103 bucket->domain,
1104 bucket->user,
1107 return 0;
1111 smbfs_open_connections++;
1112 DEBUG(3, ("smbfs_open_link:smbfs_open_connections: %d\n",
1113 smbfs_open_connections));
1114 return bucket;
1117 static char *
1118 smbfs_get_path(smbfs_connection **sc, char *path)
1120 char *user, *host, *remote_path, *pass;
1121 int port = SMB_PORT;
1123 DEBUG(3, ("smbfs_get_path(%s)\n", path));
1124 if (strncmp (path, URL_HEADER, HEADER_LEN))
1125 return NULL;
1126 path += HEADER_LEN;
1128 if (*path == '/') /* '/' leading server name */
1129 path++; /* probably came from server browsing */
1131 if ((remote_path = smbfs_get_host_and_username(
1132 &path, &host, &user, &port, &pass)))
1133 if ((*sc = smbfs_open_link (host, path, user, &port, pass)) == NULL){
1134 g_free (remote_path);
1135 remote_path = NULL;
1137 g_free (host);
1138 g_free (user);
1139 if (pass) wipe_password (pass);
1141 if (!remote_path) return NULL;
1143 /* NOTE: tildes are deprecated. See ftpfs.c */
1145 int f = !strcmp( remote_path, "/~" );
1146 if (f || !strncmp( remote_path, "/~/", 3 )) {
1147 char *s;
1148 s = concat_dir_and_file( (*sc)->home, remote_path +3-f );
1149 g_free (remote_path);
1150 return s;
1153 return remote_path;
1156 #if 0
1157 static int
1158 is_error (int result, int errno_num)
1160 if (!(result == -1))
1161 return my_errno = 0;
1162 else
1163 my_errno = errno_num;
1164 return 1;
1166 #endif
1168 static void *
1169 smbfs_opendir (vfs *me, char *dirname)
1171 opendir_info *smbfs_info;
1172 smbfs_connection *sc;
1173 char *remote_dir;
1175 DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname));
1177 if (!(remote_dir = smbfs_get_path (&sc, dirname)))
1178 return NULL;
1180 /* FIXME: where freed? */
1181 smbfs_info = g_new (opendir_info, 1);
1182 smbfs_info->server_list = FALSE;
1183 smbfs_info->path = g_strdup(dirname); /* keep original */
1184 smbfs_info->dirname = remote_dir;
1185 smbfs_info->conn = sc;
1186 smbfs_info->entries = 0;
1187 smbfs_info->current = 0;
1189 return smbfs_info;
1192 static int
1193 fake_server_stat(char *server_url, const char *path, struct stat *buf)
1195 dir_entry *dentry;
1196 char *p;
1198 if ((p = strrchr(path, '/')))
1199 path = p + 1; /* advance until last '/' */
1201 if (!current_info->entries) {
1202 if (!smbfs_loaddir(current_info)); /* browse host */
1203 return -1;
1205 dentry = current_info->entries;
1206 DEBUG(4, ("fake stat for SERVER \"%s\"\n", path));
1207 while (dentry) {
1208 if (strcmp(dentry->text, path) == 0) {
1209 DEBUG(4, ("fake_server_stat: %s:%d\n",
1210 dentry->text, dentry->my_stat.st_mode));
1211 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1212 return 0;
1214 dentry = dentry->next;
1216 my_errno = ENOENT;
1217 return -1;
1220 static int
1221 fake_share_stat(char *server_url, char *path, struct stat *buf)
1223 dir_entry *dentry;
1224 if (strlen(path) < strlen(server_url))
1225 return -1;
1226 path += strlen(server_url); /* we only want share name */
1227 path++;
1229 if (*path == '/') /* '/' leading server name */
1230 path++; /* probably came from server browsing */
1232 if (!current_share_info->entries) {
1233 if (!smbfs_loaddir(current_share_info)); /* browse host */
1234 return -1;
1236 dentry = current_share_info->entries;
1237 DEBUG(3, ("fake_share_stat: %s on %s\n", path, server_url));
1238 while (dentry) {
1239 if (strcmp(dentry->text, path) == 0) {
1240 DEBUG(6, ("fake_share_stat: %s:%d\n",
1241 dentry->text, dentry->my_stat.st_mode));
1242 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1243 return 0;
1245 dentry = dentry->next;
1247 my_errno = ENOENT;
1248 return -1;
1251 /* stat a single file, get_remote_stat callback */
1252 static dir_entry *single_entry;
1253 static void
1254 statfile_helper(file_info *finfo, const char *mask)
1256 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
1258 #if 0 /* single_entry is never freed now. And only my_stat is used */
1259 single_entry = g_new (dir_entry, 1);
1261 single_entry->text = dos_to_unix (g_strdup (finfo->name), 1);
1263 single_entry->next = 0;
1264 #endif
1265 if (!single_entry)
1266 single_entry = g_new0 (dir_entry, 1);
1268 single_entry->my_stat.st_size = finfo->size;
1269 single_entry->my_stat.st_mtime = finfo->mtime;
1270 single_entry->my_stat.st_atime = finfo->atime;
1271 single_entry->my_stat.st_ctime = finfo->ctime;
1272 single_entry->my_stat.st_uid = finfo->uid;
1273 single_entry->my_stat.st_gid = finfo->gid;
1275 single_entry->my_stat.st_mode =
1276 S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR|S_IWGRP|S_IWOTH;
1278 /* if (finfo->mode & aVOLID); nothing similar in real world */
1279 if (finfo->mode & aDIR)
1280 single_entry->my_stat.st_mode |= S_IFDIR;
1281 else
1282 single_entry->my_stat.st_mode |= S_IFREG;/* if not dir, regular file? */
1283 /* if (finfo->mode & aARCH); DOS archive */
1284 /* if (finfo->mode & aHIDDEN); like a dot file? */
1285 /* if (finfo->mode & aSYSTEM); like a kernel? */
1286 if (finfo->mode & aRONLY)
1287 single_entry->my_stat.st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
1289 DEBUG(6, (" %-30s%7.7s%8.0f %s",
1290 CNV_LANG(finfo->name),
1291 attrib_string(finfo->mode),
1292 (double)finfo->size,
1293 asctime(LocalTime(&t))));
1296 /* stat a single file */
1297 static int
1298 get_remote_stat(smbfs_connection *sc, char *path, struct stat *buf)
1300 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
1301 char *mypath = path;
1303 DEBUG(3, ("get_remote_stat(): mypath:%s\n", mypath));
1305 convert_path(&mypath, FALSE);
1307 if (cli_list(sc->cli, mypath, attribute, statfile_helper) < 1) {
1308 my_errno = ENOENT;
1309 g_free (mypath);
1310 return -1; /* cli_list returns number of files */
1313 memcpy(buf, &single_entry->my_stat, sizeof(struct stat));
1315 /* dont free here, use for smbfs_fstat() */
1316 /* g_free(single_entry->text);
1317 g_free(single_entry); */
1318 g_free (mypath);
1319 return 0;
1322 static int
1323 search_dir_entry (dir_entry *dentry, const char *text, struct stat *buf)
1325 while (dentry) {
1326 if (strcmp(text, dentry->text) == 0) {
1327 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1328 memcpy(&single_entry->my_stat, &dentry->my_stat,
1329 sizeof(struct stat));
1330 return 0;
1332 dentry = dentry->next;
1334 return -1;
1337 static int
1338 get_stat_info (smbfs_connection *sc, char *path, struct stat *buf)
1340 char *p;
1341 #if 0
1342 dir_entry *dentry = current_info->entries;
1343 #endif
1344 const char *mypath = path;
1346 mypath++; /* cut off leading '/' */
1347 if ((p = strrchr(mypath, '/')))
1348 mypath = p + 1; /* advance until last file/dir name */
1349 DEBUG(3, ("get_stat_info: mypath:%s, current_info->dirname:%s\n",
1350 mypath, current_info->dirname));
1351 #if 0
1352 if (!dentry) {
1353 DEBUG(1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n", current_info->dirname, path));
1354 return -1;
1356 #endif
1357 if (!single_entry) /* when found, this will be written too */
1358 single_entry = g_new (dir_entry, 1);
1359 if (search_dir_entry(current_info->entries, mypath, buf) == 0) {
1360 return 0;
1362 /* now try to identify mypath as PARENT dir */
1364 char *mdp;
1365 char *mydir;
1366 mdp = mydir = g_strdup(current_info->dirname);
1367 if ((p = strrchr(mydir, '/')))
1368 *p = 0; /* advance util last '/' */
1369 if ((p = strrchr(mydir, '/')))
1370 mydir = p + 1; /* advance util last '/' */
1371 if (strcmp(mydir, mypath) == 0) { /* fake a stat for ".." */
1372 bzero(buf, sizeof(struct stat));
1373 buf->st_mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH;
1374 memcpy(&single_entry->my_stat, buf, sizeof(struct stat));
1375 g_free(mdp);
1376 DEBUG(1, (" PARENT:found in %s\n", current_info->dirname));
1377 return 0;
1379 g_free(mdp);
1381 /* now try to identify as CURRENT dir? */
1383 char *dnp = current_info->dirname;
1384 DEBUG(6, ("get_stat_info: is %s current dir? this dir is: %s\n",
1385 mypath, current_info->dirname));
1386 if (*dnp == '/')
1387 dnp++;
1388 else {
1389 return -1;
1391 if (strcmp(mypath, dnp) == 0) {
1392 bzero(buf, sizeof(struct stat));
1393 buf->st_mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH;
1394 memcpy(&single_entry->my_stat, buf, sizeof(struct stat));
1395 DEBUG(1, (" CURRENT:found in %s\n", current_info->dirname));
1396 return 0;
1399 DEBUG(3, ("'%s' not found in current_info '%s'\n", path, current_info->dirname));
1400 /* try to find this in the PREVIOUS listing */
1401 if (previous_info) {
1402 if (search_dir_entry(previous_info->entries, mypath, buf) == 0)
1403 return 0;
1404 DEBUG(3, ("'%s' not found in previous_info '%s'\n", path, previous_info->dirname));
1406 /* try to find this in the SHARE listing */
1407 if (current_share_info && 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, current_share_info->dirname));
1410 /* try to find this in the SERVER listing */
1411 if (current_server_info && search_dir_entry(current_server_info->entries, mypath, buf) == 0)
1412 return 0;
1413 DEBUG(3, ("'%s' not found in server_info '%s'\n", path, current_server_info->dirname));
1414 /* nothing found. get stat file info from server */
1415 return get_remote_stat(sc, path, buf);
1418 static int
1419 smbfs_chdir (vfs *me, char *path)
1421 char *remote_dir;
1422 smbfs_connection *sc;
1424 DEBUG(3, ("smbfs_chdir(path:%s)\n", path));
1425 if (!(remote_dir = smbfs_get_path (&sc, path)))
1426 return -1;
1427 g_free (remote_dir);
1429 return 0;
1432 static int
1433 loaddir(vfs *me, const char *path)
1435 void *info;
1436 char *mypath, *p;
1437 mypath = g_strdup(path);
1438 p = mypath;
1439 if (*p == '/')
1440 p++;
1441 while (strchr(p, '/'))
1442 p++;
1443 if (p-mypath > 1)
1444 *--p = 0;
1445 DEBUG(6, ("loaddir(%s)\n", mypath));
1446 smbfs_chdir(me, mypath);
1447 info = smbfs_opendir (me, mypath);
1448 g_free(mypath);
1449 if (!info)
1450 return -1;
1451 smbfs_readdir(info);
1452 smbfs_loaddir(info);
1453 return 0;
1456 static int
1457 smbfs_stat (vfs *me, char *path, struct stat *buf)
1459 char *remote_dir;
1460 smbfs_connection *sc;
1461 pstring server_url;
1462 char *service, *pp;
1463 const char *p;
1465 DEBUG(3, ("smbfs_stat(path:%s)\n", path));
1467 #if 0
1468 if (p = strchr(path, '@')) /* user:pass@server */
1469 path = ++p; /* don't want user:pass@ in path */
1470 #endif
1472 if (!current_info) {
1473 DEBUG(1, ("current_info = NULL: "));
1474 if (loaddir(me, path) < 0)
1475 return -1;
1478 pstrcpy(server_url, URL_HEADER);
1479 pstrcat(server_url, current_bucket->host);
1481 /* check if stating server */
1482 p = path;
1483 if (strncmp(p, URL_HEADER, HEADER_LEN)) {
1484 DEBUG(1, ("'%s' doesnt start with '%s' (length %d)\n",
1485 p, URL_HEADER, HEADER_LEN));
1486 return -1;
1489 p += HEADER_LEN;
1490 if (*p == '/')
1491 p++;
1492 p = strchr(p, '/'); /* advance past next '/' */
1493 if (!p) {
1494 if (!current_info->server_list) {
1495 if (loaddir(me, path) < 0)
1496 return -1;
1498 return fake_server_stat(server_url, path, buf);
1500 if (!strchr(++p, '/')) {
1501 return fake_share_stat(server_url, path, buf);
1504 /* stating inside share at this point */
1505 if (!(remote_dir = smbfs_get_path (&sc, path))) /* connects if necessary */
1506 return -1;
1507 g_free (remote_dir);
1509 char *sp, *pp = path;
1510 int hostlen = strlen(current_bucket->host);
1511 pp += strlen(path)-hostlen;
1512 sp = server_url;
1513 sp += strlen(server_url)-hostlen;
1514 if (strcmp(sp, pp) == 0) {
1515 /* make server name appear as directory */
1516 DEBUG(1, ("smbfs_stat: showing server as directory\n"));
1517 bzero(buf, sizeof(struct stat));
1518 buf->st_mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH;
1519 return 0;
1522 /* check if current_info is in share requested */
1523 p = path;
1524 if (strncmp(p, URL_HEADER, HEADER_LEN) == 0)
1525 p += HEADER_LEN;
1526 if (*p == '/')
1527 p++;
1528 p = service = g_strdup(p);
1529 pp = strchr(p, '/');
1530 if (pp)
1531 p = ++pp; /* advance past server name */
1532 pp = strchr(p, '/');
1533 if (pp)
1534 *pp = 0; /* cut off everthing after service name */
1535 else
1536 p = IPC; /* browsing for services */
1537 pp = current_info->dirname;
1538 if (*pp == '/');
1539 pp++;
1540 if (strncmp(p, pp, strlen(p)) != 0) {
1541 DEBUG(6, ("desired '%s' is not loaded, we have '%s'\n", p, pp));
1542 if (loaddir(me, path) < 0) {
1543 g_free (service);
1544 return -1;
1546 DEBUG(6, ("loaded dir: '%s'\n", current_info->dirname));
1548 g_free(service);
1549 /* stat dirs & files under shares now */
1550 return get_stat_info(sc, path, buf);
1553 static int
1554 smbfs_lstat (vfs *me, char *path, struct stat *buf)
1556 DEBUG(4, ("smbfs_lstat(path:%s)\n", path));
1557 return smbfs_stat(me, path, buf); /* no symlinks on smb filesystem? */
1560 static int
1561 smbfs_lseek (void *data, off_t offset, int whence)
1563 DEBUG(3, ("smbfs_lseek()\n"));
1564 my_errno = EOPNOTSUPP;
1565 return -1;
1568 static int
1569 smbfs_mknod (vfs *me, char *path, int mode, int dev)
1571 DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%d)\n", path, mode, dev));
1572 my_errno = EOPNOTSUPP;
1573 return -1;
1576 static int
1577 smbfs_mkdir (vfs *me, char *path, mode_t mode)
1579 smbfs_connection *sc;
1580 char *remote_file;
1582 DEBUG(3, ("smbfs_mkdir(path:%s, mode:%d)\n", path, mode));
1583 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1584 return -1;
1585 g_free (remote_file);
1586 convert_path(&path, FALSE);
1588 if (!cli_mkdir(sc->cli, path)) {
1589 my_errno = cli_error(sc->cli, NULL, &err, NULL);
1590 message_3s (1, MSG_ERROR, _(" %s mkdir'ing %s "),
1591 cli_errstr(sc->cli), CNV_LANG(path));
1592 g_free (path);
1593 return -1;
1595 g_free (path);
1596 return 0;
1599 static int
1600 smbfs_rmdir (vfs *me, char *path)
1602 smbfs_connection *sc;
1603 char *remote_file;
1605 DEBUG(3, ("smbfs_rmdir(path:%s)\n", path));
1606 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1607 return -1;
1608 g_free (remote_file);
1609 convert_path(&path, FALSE);
1611 if (!cli_rmdir(sc->cli, path)) {
1612 my_errno = cli_error(sc->cli, NULL, &err, NULL);
1613 message_3s (1, MSG_ERROR, _(" %s rmdir'ing %s "),
1614 cli_errstr(sc->cli), CNV_LANG(path));
1615 g_free (path);
1616 return -1;
1619 g_free (path);
1620 return 0;
1623 static int
1624 smbfs_link (vfs *me, char *p1, char *p2)
1626 DEBUG(3, ("smbfs_link(p1:%s, p2:%s)\n", p1, p2));
1627 my_errno = EOPNOTSUPP;
1628 return -1;
1631 /* We do not free anything right now: we free resources when we run
1632 * out of them
1634 static vfsid
1635 smbfs_getid (vfs *me, char *p, struct vfs_stamping **parent)
1637 *parent = NULL;
1638 DEBUG(3, ("smbfs_getid(p:%s)\n", p));
1640 return (vfsid) -1;
1643 static int
1644 smbfs_nothingisopen (vfsid id)
1646 DEBUG(3, ("smbfs_nothingisopen(%d)\n", (int)id));
1647 return 0;
1650 static void
1651 smbfs_free (vfsid id)
1653 DEBUG(3, ("smbfs_free(%d)\n", (int)id));
1654 /* FIXME: Should not be empty */
1655 authinfo_free_all ();
1658 /* Gives up on a socket and reopens the connection, the child own the socket
1659 * now
1661 static void
1662 my_forget (char *path)
1664 char *host, *user, *p;
1665 int port, i;
1667 if (strncmp (path, URL_HEADER, HEADER_LEN))
1668 return;
1670 DEBUG(3, ("my_forget(path:%s)\n", path));
1672 path += 6;
1673 if (path[0] == '/' && path[1] == '/')
1674 path += 2;
1676 if ((p = smbfs_get_host_and_username (&path, &host, &user, &port, NULL))) {
1677 g_free (p);
1678 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1679 if ((strcmp (host, smbfs_connections [i].host) == 0) &&
1680 (strcmp (user, smbfs_connections [i].user) == 0) &&
1681 (port == smbfs_connections [i].port)) {
1683 /* close socket: the child owns it now */
1684 cli_shutdown(smbfs_connections [i].cli);
1686 /* reopen the connection */
1687 smbfs_connections [i].cli =
1688 do_connect(host, smbfs_connections[i].service);
1692 g_free (host);
1693 g_free (user);
1696 static int
1697 smbfs_setctl (vfs *me, char *path, int ctlop, char *arg)
1699 DEBUG(3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path, ctlop));
1700 switch (ctlop) {
1701 case MCCTL_FORGET_ABOUT:
1702 my_forget(path);
1703 return 0;
1705 return 0;
1708 static smbfs_handle *
1709 open_write (smbfs_handle *remote_handle, char *rname, int flags, int mode)
1711 if (flags & O_TRUNC) /* if it exists truncate to zero */
1712 DEBUG(3, ("open_write: O_TRUNC\n"));
1714 remote_handle->fnum = cli_open(remote_handle->cli, rname, flags, DENY_NONE);
1716 if (remote_handle->fnum == -1) {
1717 message_3s (1, MSG_ERROR, _(" %s opening remote file %s "),
1718 cli_errstr(remote_handle->cli), CNV_LANG(rname));
1719 DEBUG(1,("smbfs_open(rname:%s) error:%s\n",
1720 rname, cli_errstr(remote_handle->cli)));
1721 my_errno = cli_error(remote_handle->cli, NULL, &err, NULL);
1722 return NULL;
1725 return remote_handle;
1728 static smbfs_handle *
1729 open_read (smbfs_handle *remote_handle, char *rname, int flags, int mode)
1731 size_t size;
1733 remote_handle->fnum =
1734 cli_open(remote_handle->cli, rname, O_RDONLY, DENY_NONE);
1736 if (remote_handle->fnum == -1) {
1737 message_3s (1, MSG_ERROR, _(" %s opening remote file %s "),
1738 cli_errstr(remote_handle->cli), CNV_LANG(rname));
1739 DEBUG(1,("smbfs_open(rname:%s) error:%s\n",
1740 rname, cli_errstr(remote_handle->cli)));
1741 my_errno = cli_error(remote_handle->cli, NULL, &err, NULL);
1742 return NULL;
1745 if (!cli_qfileinfo(remote_handle->cli, remote_handle->fnum,
1746 &remote_handle->attr, &size, NULL, NULL, NULL, NULL, NULL) &&
1747 !cli_getattrE(remote_handle->cli, remote_handle->fnum,
1748 &remote_handle->attr, &size, NULL, NULL, NULL)) {
1749 message_2s (1, MSG_ERROR, " getattrib: %s ",
1750 cli_errstr(remote_handle->cli));
1751 DEBUG(1,("smbfs_open(rname:%s) getattrib:%s\n",
1752 rname, cli_errstr(remote_handle->cli)));
1753 my_errno = cli_error(remote_handle->cli, NULL, &err, NULL);
1754 return NULL;
1757 return remote_handle;
1760 static void *
1761 smbfs_open (vfs *me, char *file, int flags, int mode)
1763 char *remote_file, *p;
1764 void *ret;
1765 smbfs_connection *sc;
1766 smbfs_handle *remote_handle;
1768 DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%d)\n", file, flags, mode));
1770 if (!(remote_file = smbfs_get_path (&sc, file)))
1771 return 0;
1773 p = remote_file;
1774 convert_path(&remote_file, FALSE);
1775 g_free (p);
1777 remote_handle = g_new (smbfs_handle, 2);
1778 remote_handle->cli = sc->cli;
1779 remote_handle->nread = 0;
1781 if (flags & O_CREAT)
1782 ret = open_write(remote_handle, remote_file, flags, mode);
1783 else
1784 ret = open_read(remote_handle, remote_file, flags, mode);
1786 g_free (remote_file);
1788 return ret;
1791 static int
1792 smbfs_unlink (vfs *me, char *path)
1794 smbfs_connection *sc;
1795 char *remote_file, *p;
1797 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1798 return -1;
1800 p = remote_file;
1801 convert_path(&remote_file, FALSE);
1802 g_free (p);
1804 if (!cli_unlink(sc->cli, remote_file)) {
1805 message_3s (1, MSG_ERROR, _(" %s removing remote file %s "),
1806 cli_errstr(sc->cli), CNV_LANG(remote_file));
1807 g_free (remote_file);
1808 return -1;
1810 g_free (remote_file);
1811 return 0;
1814 static int
1815 smbfs_rename (vfs *me, char *a, char *b)
1817 smbfs_connection *sc;
1818 char *ra, *rb;
1819 char *p;
1820 int retval;
1822 if ((ra = smbfs_get_path (&sc, a)) == 0)
1823 return -1;
1825 if ((rb = smbfs_get_path (&sc, b)) == 0) {
1826 g_free (ra);
1827 return -1;
1830 p = ra;
1831 convert_path(&ra, FALSE);
1832 g_free (p);
1833 p = rb;
1834 convert_path(&rb, FALSE);
1835 g_free (p);
1837 retval = cli_rename(sc->cli, ra, rb);
1839 g_free (ra);
1840 g_free (rb);
1842 if (!retval) {
1843 message_2s (1, MSG_ERROR, _(" %s renaming files\n"),
1844 cli_errstr(sc->cli));
1845 return -1;
1847 return 0;
1850 static int
1851 smbfs_fstat (void *data, struct stat *buf)
1853 smbfs_handle *remote_handle = (smbfs_handle *)data;
1855 DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle->fnum));
1857 /* use left over from previous get_remote_stat, if available */
1858 if (single_entry)
1859 memcpy(buf, &single_entry->my_stat, sizeof(struct stat));
1860 else { /* single_entry not set up: bug */
1861 my_errno = EFAULT;
1862 return -EFAULT;
1864 return 0;
1867 vfs vfs_smbfs_ops = {
1868 NULL, /* This is place of next pointer */
1869 "smbfs",
1870 F_NET, /* flags */
1871 "smb:", /* prefix */
1872 NULL, /* data */
1873 0, /* errno */
1874 smbfs_init,
1875 NULL,
1876 smbfs_fill_names,
1877 NULL,
1879 smbfs_open,
1880 smbfs_close,
1881 smbfs_read,
1882 smbfs_write,
1884 smbfs_opendir,
1885 smbfs_readdir,
1886 smbfs_closedir,
1887 NULL,
1888 NULL,
1890 smbfs_stat,
1891 smbfs_lstat,
1892 smbfs_fstat,
1894 smbfs_chmod,
1895 smbfs_chown,
1896 smbfs_utime,
1898 smbfs_readlink,
1899 smbfs_symlink,
1900 smbfs_link,
1901 smbfs_unlink,
1903 smbfs_rename,
1904 smbfs_chdir,
1905 smbfs_errno,
1906 smbfs_lseek,
1907 smbfs_mknod,
1909 smbfs_getid,
1910 smbfs_nothingisopen,
1911 smbfs_free,
1913 NULL,
1914 NULL,
1916 smbfs_mkdir,
1917 smbfs_rmdir,
1918 NULL,
1919 smbfs_setctl
1921 MMAPNULL