*** empty log message ***
[midnight-commander.git] / vfs / smbfs.c
blob12d62622d4258c003c170f794e1c0b026c65bba6
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 extern pstring global_myname;
64 static int smbfs_open_connections = 0;
65 static gboolean got_user = FALSE;
66 static gboolean got_pass = FALSE;
67 static pstring password;
68 static pstring username;
70 static struct _smbfs_connection {
71 struct cli_state *cli;
72 struct in_addr dest_ip;
73 BOOL have_ip;
74 char *host; /* server name */
75 char *service; /* share name */
76 char *domain;
77 char *user;
78 char *home;
79 char *password;
80 int port;
81 int name_type;
82 time_t last_use;
83 } smbfs_connections [SMBFS_MAX_CONNECTIONS];
84 /* unique to each connection */
86 static struct cli_state * smbfs_do_connect (const char *server, char *share);
88 typedef struct _smbfs_connection smbfs_connection;
89 static smbfs_connection *current_bucket;
91 typedef struct {
92 struct cli_state *cli;
93 int fnum;
94 off_t nread;
95 uint16 attr;
96 } smbfs_handle;
98 static GSList *auth_list;
100 static void
101 authinfo_free (struct smb_authinfo const *a)
103 g_free (a->host);
104 g_free (a->share);
105 g_free (a->domain);
106 g_free (a->user);
107 wipe_password (a->password);
110 static void
111 authinfo_free_all ()
113 if (auth_list) {
114 g_slist_foreach (auth_list, (GFunc)authinfo_free, 0);
115 g_slist_free (auth_list);
116 auth_list = 0;
120 static gint
121 authinfo_compare_host_and_share (gconstpointer _a, gconstpointer _b)
123 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
124 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
126 if (!a->host || !a->share || !b->host || !b->share)
127 return 1;
128 if (strcmp (a->host, b->host) != 0)
129 return 1;
130 if (strcmp (a->share, b->share) != 0)
131 return 1;
132 return 0;
135 static gint
136 authinfo_compare_host (gconstpointer _a, gconstpointer _b)
138 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
139 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
141 if (!a->host || !b->host)
142 return 1;
143 if (strcmp (a->host, b->host) != 0)
144 return 1;
145 if (strcmp (a->share, IPC) != 0)
146 return 1;
147 return 0;
150 static void
151 authinfo_add (const char *host, const char *share, const char *domain,
152 const char *user, const char *password)
154 struct smb_authinfo *auth = g_new (struct smb_authinfo, 1);
156 if (!auth)
157 return;
159 /* Don't check for NULL, g_strdup already does. */
160 auth->host = g_strdup (host);
161 auth->share = g_strdup (share);
162 auth->domain = g_strdup (domain);
163 auth->user = g_strdup (user);
164 auth->password = g_strdup (password);
165 auth_list = g_slist_prepend (auth_list, auth);
168 static void
169 authinfo_remove (const char *host, const char *share)
171 struct smb_authinfo data;
172 struct smb_authinfo *auth;
173 GSList *list;
175 data.host = g_strdup (host);
176 data.share = g_strdup (share);
177 list = g_slist_find_custom (auth_list,
178 &data,
179 authinfo_compare_host_and_share);
180 g_free (data.host);
181 g_free (data.share);
182 if (!list)
183 return;
184 auth = list->data;
185 auth_list = g_slist_remove (auth_list, auth);
186 authinfo_free (auth);
189 /* Set authentication information in bucket. Return 1 if successful, else 0 */
190 /* Information in auth_list overrides user if pass is NULL. */
191 /* bucket->host and bucket->service must be valid. */
192 static int
193 bucket_set_authinfo (smbfs_connection *bucket,
194 const char *domain, const char *user, const char *pass,
195 int fallback_to_host)
197 struct smb_authinfo data;
198 struct smb_authinfo *auth;
199 GSList *list;
201 if (domain && user && pass) {
202 g_free (bucket->domain);
203 g_free (bucket->user);
204 g_free (bucket->password);
205 bucket->domain = g_strdup (domain);
206 bucket->user = g_strdup (user);
207 bucket->password = g_strdup (pass);
208 authinfo_remove (bucket->host, bucket->service);
209 authinfo_add (bucket->host, bucket->service,
210 domain, user, pass);
211 return 1;
214 data.host = bucket->host;
215 data.share = bucket->service;
216 list = g_slist_find_custom (auth_list, &data, authinfo_compare_host_and_share);
217 if (!list && fallback_to_host)
218 list = g_slist_find_custom (auth_list, &data, authinfo_compare_host);
219 if (list) {
220 auth = list->data;
221 bucket->domain = g_strdup (auth->domain);
222 bucket->user = g_strdup (auth->user);
223 bucket->password = g_strdup (auth->password);
224 return 1;
227 if (got_pass) {
228 bucket->domain = g_strdup (lp_workgroup ());
229 bucket->user = g_strdup (got_user ? username : user);
230 bucket->password = g_strdup (password);
231 return 1;
234 auth = vfs_smb_get_authinfo (bucket->host,
235 bucket->service,
236 (domain ? domain : lp_workgroup ()),
237 user);
238 if (auth) {
239 g_free (bucket->domain);
240 g_free (bucket->user);
241 g_free (bucket->password);
242 bucket->domain = g_strdup (auth->domain);
243 bucket->user = g_strdup (auth->user);
244 bucket->password = g_strdup (auth->password);
245 authinfo_remove (bucket->host, bucket->service);
246 auth_list = g_slist_prepend (auth_list, auth);
247 return 1;
249 return 0;
252 void
253 smbfs_set_debug(int arg)
255 DEBUGLEVEL = arg;
258 /********************** The callbacks ******************************/
259 static int
260 smbfs_init (vfs * me)
262 char *servicesf = CONFIGDIR PATH_SEP_STR "smb.conf";
264 /* DEBUGLEVEL = 4; */
266 setup_logging ("mc", True);
267 TimeInit ();
268 charset_initialise ();
270 DEBUG (3, ("smbfs_init(%s)\n", me->name));
272 if (!get_myname (myhostname, NULL))
273 DEBUG (0, ("Failed to get my hostname.\n"));
275 if (!lp_load (servicesf, True, False, False))
276 DEBUG (0, ("Cannot load %s - run testparm to debug it\n", servicesf));
278 codepage_initialise (lp_client_code_page ());
280 load_interfaces ();
282 if (getenv ("USER")) {
283 char *p;
285 pstrcpy (username, getenv ("USER"));
286 got_user = TRUE;
287 DEBUG (3, ("smbfs_init(): $USER:%s\n", username));
288 if ((p = strchr (username, '%'))) {
289 *p = 0;
290 pstrcpy (password, p + 1);
291 got_pass = TRUE;
292 memset (strchr (getenv ("USER"), '%') + 1, 'X', strlen (password));
293 DEBUG (3, ("smbfs_init(): $USER%%pass: %s%%%s\n",
294 username, password));
296 strupper (username);
298 if (getenv ("PASSWD")) {
299 pstrcpy (password, getenv ("PASSWD"));
300 got_pass = TRUE;
302 return 1;
305 static void
306 smbfs_fill_names (vfs *me, void (*func)(char *))
308 int i;
309 char *path;
310 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
311 if (smbfs_connections [i].cli) {
312 path = g_strconcat (URL_HEADER,
313 smbfs_connections[i].host,
314 "/", smbfs_connections[i].service,
315 NULL);
316 (*func)(path);
317 g_free (path);
322 #define CNV_LANG(s) dos_to_unix(s,False)
323 #define GNAL_VNC(s) unix_to_dos(s,False)
324 /* does same as do_get() in client.c */
325 /* called from vfs.c:1080, count = buffer size */
326 static int
327 smbfs_read (void *data, char *buffer, int count)
329 smbfs_handle *info = (smbfs_handle *) data;
330 int n;
332 DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%d)\n",
333 info->fnum, (int)info->nread, count));
334 n = cli_read(info->cli, info->fnum, buffer, info->nread, count);
335 if (n > 0)
336 info->nread += n;
337 return n;
340 static int
341 smbfs_write (void *data, char *buf, int nbyte)
343 smbfs_handle *info = (smbfs_handle *) data;
344 int n;
346 DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%d)\n",
347 info->fnum, (int)info->nread, nbyte));
348 n = cli_write(info->cli, info->fnum, 0, buf, info->nread, nbyte);
349 if (n > 0)
350 info->nread += n;
351 return n;
354 static int
355 smbfs_close (void *data)
357 smbfs_handle *info = (smbfs_handle *) data;
358 DEBUG(3, ("smbfs_close(fnum:%d)\n", info->fnum));
359 /* if imlementing archive_level: add rname to smbfs_handle
360 if (archive_level >= 2 && (inf->attr & aARCH)) {
361 cli_setatr(info->cli, rname, info->attr & ~(uint16)aARCH, 0);
362 } */
363 return (cli_close(info->cli, info->fnum) == True) ? 0 : -1;
366 static int
367 smbfs_errno (vfs *me)
369 DEBUG(3, ("smbfs_errno: %s\n", g_strerror(my_errno)));
370 return my_errno;
373 typedef struct dir_entry {
374 char *text;
375 struct dir_entry *next;
376 struct stat my_stat;
377 int merrno;
378 } dir_entry;
380 typedef struct {
381 gboolean server_list;
382 char *dirname;
383 char *path; /* the dir originally passed to smbfs_opendir */
384 smbfs_connection *conn;
385 dir_entry *entries;
386 dir_entry *current;
387 } opendir_info;
389 static opendir_info
390 *previous_info,
391 *current_info,
392 *current_share_info,
393 *current_server_info;
395 static gboolean first_direntry;
397 static dir_entry *
398 new_dir_entry (const char * name)
400 dir_entry *new_entry;
401 new_entry = g_new0 (dir_entry, 1);
402 new_entry->text = dos_to_unix (g_strdup (name), 1);
404 if (first_direntry) {
405 current_info->entries = new_entry;
406 first_direntry = FALSE;
407 } else {
408 current_info->current->next = new_entry;
410 current_info->current = new_entry;
412 return new_entry;
415 /* browse for shares on server */
416 static void
417 browsing_helper(const char *name, uint32 type, const char *comment)
419 char *typestr = "";
421 dir_entry *new_entry = new_dir_entry (name);
423 switch (type) {
424 case STYPE_DISKTREE:
425 typestr = "Disk";
426 /* show this as dir */
427 new_entry->my_stat.st_mode = S_IFDIR|S_IRUSR|S_IRGRP|S_IROTH;
428 break;
429 case STYPE_PRINTQ:
430 typestr = "Printer"; break;
431 case STYPE_DEVICE:
432 typestr = "Device"; break;
433 case STYPE_IPC:
434 typestr = "IPC"; break;
436 DEBUG(3, ("\t%-15.15s%-10.10s%s\n", name, typestr, comment));
439 static void
440 loaddir_helper(file_info *finfo, const char *mask)
442 dir_entry *new_entry;
443 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
444 #if 0 /* I want to see dot files */
445 if (finfo->mode & aHIDDEN)
446 return; /* don't bother with hidden files, "~$" screws up mc */
447 #endif
448 new_entry = new_dir_entry (finfo->name);
450 new_entry->my_stat.st_size = finfo->size;
451 new_entry->my_stat.st_mtime = finfo->mtime;
452 new_entry->my_stat.st_atime = finfo->atime;
453 new_entry->my_stat.st_ctime = finfo->ctime;
454 new_entry->my_stat.st_uid = finfo->uid;
455 new_entry->my_stat.st_gid = finfo->gid;
458 new_entry->my_stat.st_mode =
459 S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR|S_IWGRP|S_IWOTH;
461 /* if (finfo->mode & aVOLID); nothing similar in real world */
462 if (finfo->mode & aDIR)
463 new_entry->my_stat.st_mode |= S_IFDIR;
464 else
465 new_entry->my_stat.st_mode |= S_IFREG; /* if not dir, regular file? */
466 /* if (finfo->mode & aARCH); DOS archive */
467 /* if (finfo->mode & aHIDDEN); like a dot file? */
468 /* if (finfo->mode & aSYSTEM); like a kernel? */
469 if (finfo->mode & aRONLY)
470 new_entry->my_stat.st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
472 DEBUG(3, (" %-30s%7.7s%8.0f %s",
473 CNV_LANG(finfo->name),
474 attrib_string(finfo->mode),
475 (double)finfo->size,
476 asctime(LocalTime(&t))));
479 /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
480 static int
481 convert_path(char **remote_file, gboolean trailing_asterik)
483 char *p, *my_remote;
485 my_remote = *remote_file;
486 if (strncmp (my_remote, URL_HEADER, HEADER_LEN) == 0) { /* if passed directly */
487 my_remote += 6;
488 if (*my_remote == '/') /* from server browsing */
489 my_remote++;
490 p = strchr(my_remote, '/');
491 if (p)
492 my_remote = p+1; /* advance to end of server name */
495 if (*my_remote == '/')
496 my_remote++; /* strip off leading '/' */
497 p = strchr(my_remote, '/');
498 if (p)
499 my_remote = p; /* strip off share/service name */
500 /* create remote filename as understood by smb clientgen */
501 p = *remote_file = g_strconcat (my_remote, trailing_asterik ? "/*" : "", 0);
502 unix_to_dos (*remote_file, 1);
503 while ((p = strchr(p, '/')))
504 *p = '\\';
505 return 0;
508 static void
509 server_browsing_helper(const char *name, uint32 m, const char *comment)
511 dir_entry *new_entry = new_dir_entry (name);
513 /* show this as dir */
514 new_entry->my_stat.st_mode = S_IFDIR|S_IRUSR|S_IRGRP|S_IROTH;
516 DEBUG(3, ("\t%-16.16s %s\n", name, comment));
519 static BOOL
520 reconnect(smbfs_connection *conn, int *retries)
522 char *host;
523 DEBUG(3, ("RECONNECT\n"));
525 if (*(conn->host) == 0)
526 host = g_strdup(conn->cli->desthost); /* server browsing */
527 else
528 host = g_strdup(conn->host);
530 cli_shutdown(conn->cli);
532 if (!(conn->cli = smbfs_do_connect(host, conn->service))) {
533 message_2s (1, MSG_ERROR,
534 _(" reconnect to %s failed\n "), conn->host);
535 g_free(host);
536 return False;
538 g_free(host);
539 if (++(*retries) == 2)
540 return False;
541 return True;
544 static BOOL
545 smb_send(struct cli_state *cli)
547 size_t len;
548 size_t nwritten=0;
549 ssize_t ret;
551 len = smb_len(cli->outbuf) + 4;
553 while (nwritten < len) {
554 ret = write_socket(cli->fd, cli->outbuf+nwritten, len - nwritten);
555 if (ret <= 0 && errno == EPIPE)
556 return False;
557 nwritten += ret;
560 return True;
563 /****************************************************************************
564 See if server has cut us off by checking for EPIPE when writing.
565 Taken from cli_chkpath()
566 ****************************************************************************/
567 static BOOL
568 chkpath(struct cli_state *cli, char *path, BOOL send_only)
570 fstring path2;
571 char *p;
573 fstrcpy(path2,path);
574 unix_to_dos (path2, 1);
575 trim_string(path2,NULL,"\\");
576 if (!*path2) *path2 = '\\';
578 memset(cli->outbuf,'\0',smb_size);
579 set_message(cli->outbuf,0,4 + strlen(path2),True);
580 SCVAL(cli->outbuf,smb_com,SMBchkpth);
581 SSVAL(cli->outbuf,smb_tid,cli->cnum);
583 cli->rap_error = 0;
584 cli->nt_error = 0;
585 SSVAL(cli->outbuf,smb_pid,cli->pid);
586 SSVAL(cli->outbuf,smb_uid,cli->vuid);
587 SSVAL(cli->outbuf,smb_mid,cli->mid);
588 if (cli->protocol > PROTOCOL_CORE) {
589 SCVAL(cli->outbuf,smb_flg,0x8);
590 SSVAL(cli->outbuf,smb_flg2,0x1);
593 p = smb_buf(cli->outbuf);
594 *p++ = 4;
595 fstrcpy(p,path2);
597 if (!smb_send(cli)) {
598 DEBUG(3, ("chkpath: couldnt send\n"));
599 return False;
601 if (send_only) {
602 client_receive_smb(cli->fd, cli->inbuf, cli->timeout);
603 DEBUG(3, ("chkpath: send only OK\n"));
604 return True; /* just testing for EPIPE */
606 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout)) {
607 DEBUG(3, ("chkpath: receive error\n"));
608 return False;
610 if ((my_errno = cli_error(cli, NULL, NULL, NULL))) {
611 if (my_errno == 20 || my_errno == 13)
612 return True; /* ignore if 'not a directory' error */
613 DEBUG(3, ("chkpath: cli_error: %s\n", g_strerror(my_errno)));
614 return False;
617 return True;
620 /* #if 0 */
621 static int
622 fs (const char *text)
624 const char *p = text;
625 int count = 0;
627 while ((p = strchr(p, '/')) != NULL) {
628 count++;
629 p++;
631 if (count == 1)
632 return strlen(text);
633 return count;
635 /* #endif */
637 static int
638 smbfs_loaddir (opendir_info *smbfs_info)
640 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
641 int servlen = strlen(smbfs_info->conn->service);
642 char *my_dirname = smbfs_info->dirname;
644 DEBUG(3, ("smbfs_loaddir: dirname:%s\n", my_dirname));
645 first_direntry = TRUE;
647 if (current_info) {
648 DEBUG(3, ("smbfs_loaddir: new:'%s', cached:'%s'\n", my_dirname, current_info->dirname));
649 /* if new desired dir is longer than cached in current_info */
650 if (fs(my_dirname) > fs(current_info->dirname)) {
651 DEBUG(3, ("saving to previous_info\n"));
652 previous_info = current_info;
656 current_info = smbfs_info;
658 if (strcmp(my_dirname, "/") == 0) {
659 if (!strcmp(smbfs_info->path, URL_HEADER)) {
660 DEBUG(6, ("smbfs_loaddir: browsing %s\n", IPC));
661 /* browse for servers */
662 if (!cli_NetServerEnum(smbfs_info->conn->cli, smbfs_info->conn->domain,
663 SV_TYPE_ALL, server_browsing_helper))
664 return 0;
665 else
666 current_server_info = smbfs_info;
667 smbfs_info->server_list = TRUE;
668 } else {
669 /* browse for shares */
670 if (cli_RNetShareEnum(smbfs_info->conn->cli, browsing_helper) < 1)
671 return 0;
672 else
673 current_share_info = smbfs_info;
675 goto done;
678 /* do regular directory listing */
679 if(strncmp(smbfs_info->conn->service, my_dirname+1, servlen) == 0) {
680 /* strip share name from dir */
681 char *p = my_dirname = g_strdup(my_dirname + servlen);
682 *p = '/';
683 convert_path(&my_dirname, TRUE);
684 g_free (p);
685 } else
686 convert_path(&my_dirname, TRUE);
688 DEBUG(6, ("smbfs_loaddir: service: %s\n", smbfs_info->conn->service));
689 DEBUG(6, ("smbfs_loaddir: cli->share: %s\n", smbfs_info->conn->cli->share));
690 DEBUG(6, ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname));
691 /* do file listing: cli_list returns number of files */
692 if (cli_list(
693 smbfs_info->conn->cli, my_dirname, attribute, loaddir_helper) < 0) {
694 /* cli_list returns -1 if directory empty or cannot read socket */
695 my_errno = cli_error(smbfs_info->conn->cli, NULL, &err, NULL);
696 g_free (my_dirname);
697 return 0;
699 if (*(my_dirname) == 0)
700 smbfs_info->dirname = smbfs_info->conn->service;
701 g_free (my_dirname);
702 /* do_dskattr(); */
704 done:
705 /* current_info->parent = smbfs_info->dirname; */
707 smbfs_info->current = smbfs_info->entries;
708 return 1; /* 1 = ok */
711 #ifdef SMBFS_FREE_DIR
712 static void
713 smbfs_free_dir (dir_entry *de)
715 if (!de) return;
717 smbfs_free_dir (de->next);
718 g_free (de->text);
719 g_free (de);
721 #endif
724 /* The readdir routine loads the complete directory */
725 /* It's too slow to ask the server each time */
726 /* It now also sends the complete lstat information for each file */
727 static void *
728 smbfs_readdir(void *info)
730 static union vfs_dirent smbfs_readdir_data;
731 static char *const dirent_dest = smbfs_readdir_data.dent.d_name;
732 opendir_info *smbfs_info = (opendir_info *) info;
734 DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info->dirname));
736 if (!smbfs_info->entries)
737 if (!smbfs_loaddir(smbfs_info))
738 return NULL;
740 if (smbfs_info->current == 0) { /* reached end of dir entries */
741 DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
742 #ifdef SMBFS_FREE_DIR
743 smbfs_free_dir(smbfs_info->entries);
744 smbfs_info->entries = 0;
745 #endif
746 return NULL;
748 strncpy(dirent_dest, smbfs_info->current->text, MC_MAXPATHLEN);
749 dirent_dest[MC_MAXPATHLEN] = 0;
750 smbfs_info->current = smbfs_info->current->next;
752 compute_namelen(&smbfs_readdir_data.dent);
754 return &smbfs_readdir_data;
757 static int
758 smbfs_closedir (void *info)
760 opendir_info *smbfs_info = (opendir_info *) info;
761 /* dir_entry *p, *q; */
763 DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info->dirname));
764 /* CLOSE HERE */
766 /* for (p = smbfs_info->entries; p;){
767 q = p;
768 p = p->next;
769 g_free (q->text);
770 g_free (q);
772 g_free (info); */
773 return 0;
776 static int
777 smbfs_chmod (vfs *me, char *path, int mode)
779 DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path, mode));
780 /* my_errno = EOPNOTSUPP;
781 return -1; */ /* cant chmod on smb filesystem */
782 return 0; /* make mc happy */
785 static int
786 smbfs_chown (vfs *me, char *path, int owner, int group)
788 DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path, owner, group));
789 my_errno = EOPNOTSUPP; /* ready for your labotomy? */
790 return -1;
793 static int
794 smbfs_utime (vfs *me, char *path, struct utimbuf *times)
796 DEBUG(3, ("smbfs_utime(path:%s)\n", path));
797 my_errno = EOPNOTSUPP;
798 return -1;
801 static int
802 smbfs_readlink (vfs *me, char *path, char *buf, int size)
804 DEBUG(3, ("smbfs_readlink(path:%s, buf:%s, size:%d)\n", path, buf, size));
805 my_errno = EOPNOTSUPP;
806 return -1; /* no symlinks on smb filesystem? */
809 static int
810 smbfs_symlink (vfs *me, char *n1, char *n2)
812 DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1, n2));
813 my_errno = EOPNOTSUPP;
814 return -1; /* no symlinks on smb filesystem? */
817 /* Extract the hostname and username from the path */
818 /* path is in the form: hostname:user/remote-dir */
819 #if 0
820 static char *
821 smbfs_get_host_and_username
822 (char **path, char **host, char **user, int *port, char **pass)
824 /* char *p, *ret; */
825 char *ret;
827 ret = vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0);
829 #if 0
830 if ((p = strchr(*path, '@'))) /* user:pass@server */
831 *path = ++p; /* don't want user:pass@ in path */
832 if ((p = strchr(ret, '@'))) /* user:pass@server */
833 ret = ++p; /* don't want user:pass@ in path */
834 #endif
836 return ret;
838 #else
839 #define smbfs_get_host_and_username(path, host, user, port, pass) \
840 vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
841 #endif
842 /*****************************************************
843 return a connection to a SMB server
844 current_bucket needs to be set before calling
845 *******************************************************/
846 static struct cli_state *
847 smbfs_do_connect (const char *server, char *share)
849 struct cli_state *c;
850 struct nmb_name called, calling;
851 struct in_addr ip;
852 extern struct in_addr ipzero;
854 DEBUG(3, ("smbfs_do_connect(%s, %s)\n", server, share));
855 if (*share == '\\') {
856 server = share+2;
857 share = strchr(server,'\\');
858 if (!share) return NULL;
859 *share = 0;
860 share++;
863 make_nmb_name(&calling, global_myname, 0x0, "");
864 make_nmb_name(&called , server, current_bucket->name_type, "");
866 for (;;) {
868 ip = (current_bucket->have_ip) ? current_bucket->dest_ip : ipzero;
870 /* have to open a new connection */
871 if (!(c = cli_initialise(NULL))) {
872 my_errno = ENOMEM;
873 return NULL;
876 pwd_init(&(c->pwd)); /* should be moved into cli_initialise()? */
877 pwd_set_cleartext(&(c->pwd), current_bucket->password);
879 if ((cli_set_port(c, current_bucket->port) == 0) ||
880 !cli_connect(c, server, &ip)) {
881 DEBUG(1, ("Connection to %s failed\n", server));
882 break;
885 if (!cli_session_request(c, &calling, &called)) {
886 my_errno = cli_error(c, NULL, &err, NULL);
887 DEBUG(1, ("session request to %s failed\n", called.name));
888 cli_shutdown(c);
889 if (strcmp(called.name, "*SMBSERVER")) {
890 make_nmb_name(&called , "*SMBSERVER", 0x20, "");
891 continue;
893 return NULL;
896 DEBUG(3, (" session request ok\n"));
898 if (!cli_negprot(c)) {
899 DEBUG(1, ("protocol negotiation failed\n"));
900 break;
903 if (!cli_session_setup(c, current_bucket->user,
904 current_bucket->password, strlen(current_bucket->password),
905 current_bucket->password, strlen(current_bucket->password),
906 current_bucket->domain)) {
907 DEBUG(1,("session setup failed: %s\n", cli_errstr(c)));
908 authinfo_remove (server, share);
909 break;
912 if (*c->server_domain || *c->server_os || *c->server_type)
913 DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
914 c->server_domain,c->server_os,c->server_type));
916 DEBUG(3, (" session setup ok\n"));
918 if (!cli_send_tconX(c, share, "?????",
919 current_bucket->password, strlen(current_bucket->password)+1)) {
920 DEBUG(1,("%s: tree connect failed: %s\n", share, cli_errstr(c)));
921 break;
924 DEBUG(3, (" tconx ok\n"));
926 my_errno = 0;
927 return c;
930 my_errno = cli_error(c, NULL, &err, NULL);
931 cli_shutdown(c);
932 return NULL;
936 static int
937 get_master_browser(char **host)
939 int count;
940 struct in_addr *ip_list, bcast_addr;
941 extern struct in_addr ipzero;
943 /* does port = 137 for win95 master browser? */
944 int fd= open_socket_in( SOCK_DGRAM, 0, 3,
945 interpret_addr(lp_socket_address()), True );
946 if (fd == -1)
947 return 0;
948 set_socket_options(fd, "SO_BROADCAST");
949 ip_list = iface_bcast(ipzero);
950 bcast_addr = *ip_list;
951 if ((ip_list = name_query(fd, "\01\02__MSBROWSE__\02", 1, True,
952 True, bcast_addr, &count, NULL))) {
953 if (!count)
954 return 0;
955 /* just return first master browser */
956 *host = g_strdup(inet_ntoa(ip_list[0]));
957 return 1;
959 return 0;
962 static void
963 free_bucket (smbfs_connection *bucket)
965 g_free (bucket->host);
966 g_free (bucket->service);
967 g_free (bucket->domain);
968 g_free (bucket->user);
969 wipe_password (bucket->password);
970 if (bucket->home) g_free (bucket->home);
971 memset (bucket, 0, sizeof (smbfs_connection));
974 static smbfs_connection *
975 smbfs_get_free_bucket ()
977 int i;
979 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++)
980 if (!smbfs_connections [i].cli) return &smbfs_connections [i];
982 { /* search for most dormant connection */
983 int oldest = 0; /* index */
984 time_t oldest_time = smbfs_connections[0].last_use;
985 for (i = 1; i < SMBFS_MAX_CONNECTIONS; i++) {
986 if (smbfs_connections[i].last_use < oldest_time) {
987 oldest_time = smbfs_connections[i].last_use;
988 oldest = i;
991 cli_shutdown(smbfs_connections[oldest].cli);
992 free_bucket (&smbfs_connections[oldest]);
993 return &smbfs_connections[oldest];
996 /* This can't happend, since we have checked for max connections before */
997 vfs_die("Internal error: smbfs_get_free_bucket");
998 return 0; /* shut up, stupid gcc */
1001 /* This routine keeps track of open connections */
1002 /* Returns a connected socket to host */
1003 static smbfs_connection *
1004 smbfs_open_link(char *host, char *path, const char *user, int *port, char *this_pass)
1006 int i;
1007 smbfs_connection *bucket;
1008 pstring service;
1009 struct in_addr *dest_ip = NULL;
1011 DEBUG(3, ("smbfs_open_link(host:%s, path:%s)\n", host, path));
1013 if (strcmp(host, path) == 0) /* if host & path are same: */
1014 pstrcpy(service, IPC); /* setup for browse */
1015 else { /* get share name from path, path starts with server name */
1016 char *p;
1017 if ((p = strchr(path, '/'))) /* get share aka */
1018 pstrcpy(service, ++p); /* service name from path */
1019 else
1020 pstrcpy(service, "");
1021 /* now check for trailing directory/filenames */
1022 p = strchr(service, '/');
1023 if (p)
1024 *p = 0; /* cut off dir/files: sharename only */
1026 DEBUG(6, ("smbfs_open_link: service from path:%s\n", service));
1029 if (got_user)
1030 user = username; /* global from getenv */
1032 /* Is the link actually open? */
1033 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1034 if (!smbfs_connections[i].cli)
1035 continue;
1036 if ((strcmp (host, smbfs_connections [i].host) == 0) &&
1037 (strcmp (user, smbfs_connections [i].user) == 0) &&
1038 (strcmp (service, smbfs_connections [i].service) == 0)) {
1039 int retries = 0;
1040 BOOL inshare = (*host != 0 && *path != 0 && strchr(path, '/'));
1041 /* check if this connection has died */
1042 while (!chkpath(smbfs_connections[i].cli, "\\", !inshare)) {
1043 if (!reconnect(&smbfs_connections[i], &retries))
1044 return 0;
1046 DEBUG(6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i));
1047 current_bucket = &smbfs_connections[i];
1048 smbfs_connections [i].last_use = time(NULL);
1049 return &smbfs_connections [i];
1051 /* connection not found, find if we have ip for new connection */
1052 if (strcmp (host, smbfs_connections [i].host) == 0)
1053 dest_ip = &smbfs_connections[i].cli->dest_ip;
1056 /* make new connection */
1057 bucket = smbfs_get_free_bucket ();
1058 bucket->name_type = 0x20;
1059 bucket->home = 0;
1060 bucket->port = *port;
1061 bucket->have_ip = False;
1062 if (dest_ip) {
1063 bucket->have_ip = True;
1064 bucket->dest_ip = *dest_ip;
1066 current_bucket = bucket;
1068 bucket->user = g_strdup(user);
1069 bucket->service = g_strdup (service);
1071 if (!(*host)) { /* if blank host name, browse for servers */
1072 if (!get_master_browser(&host)) /* set host to ip of master browser */
1073 return 0; /* couldnt find master browser? */
1074 g_free (host);
1075 bucket->host = g_strdup(""); /* blank host means master browser */
1076 } else
1077 bucket->host = g_strdup(host);
1079 if (!bucket_set_authinfo (bucket,
1080 0, /* domain currently not used */
1081 user,
1082 this_pass,
1084 return 0;
1086 /* connect to share */
1087 while (!(bucket->cli = smbfs_do_connect(host, service))) {
1089 if (my_errno != EPERM)
1090 return 0;
1091 message_1s (1, MSG_ERROR,
1092 _(" Authentication failed "));
1094 /* authentication failed, try again */
1095 authinfo_remove (bucket->host, bucket->service);
1096 if (!bucket_set_authinfo (bucket,
1097 bucket->domain,
1098 bucket->user,
1101 return 0;
1105 smbfs_open_connections++;
1106 DEBUG(3, ("smbfs_open_link:smbfs_open_connections: %d\n",
1107 smbfs_open_connections));
1108 return bucket;
1111 static char *
1112 smbfs_get_path(smbfs_connection **sc, char *path)
1114 char *user, *host, *remote_path, *pass;
1115 int port = SMB_PORT;
1117 DEBUG(3, ("smbfs_get_path(%s)\n", path));
1118 if (strncmp (path, URL_HEADER, HEADER_LEN))
1119 return NULL;
1120 path += HEADER_LEN;
1122 if (*path == '/') /* '/' leading server name */
1123 path++; /* probably came from server browsing */
1125 if ((remote_path = smbfs_get_host_and_username(
1126 &path, &host, &user, &port, &pass)))
1127 if ((*sc = smbfs_open_link (host, path, user, &port, pass)) == NULL){
1128 g_free (remote_path);
1129 remote_path = NULL;
1131 g_free (host);
1132 g_free (user);
1133 if (pass) wipe_password (pass);
1135 if (!remote_path) return NULL;
1137 /* NOTE: tildes are deprecated. See ftpfs.c */
1139 int f = !strcmp( remote_path, "/~" );
1140 if (f || !strncmp( remote_path, "/~/", 3 )) {
1141 char *s;
1142 s = concat_dir_and_file( (*sc)->home, remote_path +3-f );
1143 g_free (remote_path);
1144 return s;
1147 return remote_path;
1150 #if 0
1151 static int
1152 is_error (int result, int errno_num)
1154 if (!(result == -1))
1155 return my_errno = 0;
1156 else
1157 my_errno = errno_num;
1158 return 1;
1160 #endif
1162 static void *
1163 smbfs_opendir (vfs *me, char *dirname)
1165 opendir_info *smbfs_info;
1166 smbfs_connection *sc;
1167 char *remote_dir;
1169 DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname));
1171 if (!(remote_dir = smbfs_get_path (&sc, dirname)))
1172 return NULL;
1174 /* FIXME: where freed? */
1175 smbfs_info = g_new (opendir_info, 1);
1176 smbfs_info->server_list = FALSE;
1177 smbfs_info->path = g_strdup(dirname); /* keep original */
1178 smbfs_info->dirname = remote_dir;
1179 smbfs_info->conn = sc;
1180 smbfs_info->entries = 0;
1181 smbfs_info->current = 0;
1183 return smbfs_info;
1186 static int
1187 fake_server_stat(const char *server_url, const char *path, struct stat *buf)
1189 dir_entry *dentry;
1190 char *p;
1192 if ((p = strrchr(path, '/')))
1193 path = p + 1; /* advance until last '/' */
1195 if (!current_info->entries) {
1196 if (!smbfs_loaddir(current_info)); /* browse host */
1197 return -1;
1200 if (current_info->server_list == True) {
1201 dentry = current_info->entries;
1202 DEBUG(4, ("fake stat for SERVER \"%s\"\n", path));
1203 while (dentry) {
1204 if (strcmp(dentry->text, path) == 0) {
1205 DEBUG(4, ("fake_server_stat: %s:%4o\n",
1206 dentry->text, dentry->my_stat.st_mode));
1207 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1208 return 0;
1210 dentry = dentry->next;
1213 my_errno = ENOENT;
1214 return -1;
1217 static int
1218 fake_share_stat(const char *server_url, const char *path, struct stat *buf)
1220 dir_entry *dentry;
1221 if (strlen(path) < strlen(server_url))
1222 return -1;
1223 path += strlen(server_url); /* we only want share name */
1224 path++;
1226 if (*path == '/') /* '/' leading server name */
1227 path++; /* probably came from server browsing */
1229 if (!current_share_info->entries) {
1230 if (!smbfs_loaddir(current_share_info)); /* browse host */
1231 return -1;
1233 dentry = current_share_info->entries;
1234 DEBUG(3, ("fake_share_stat: %s on %s\n", path, server_url));
1235 while (dentry) {
1236 if (strcmp(dentry->text, path) == 0) {
1237 DEBUG(6, ("fake_share_stat: %s:%4o\n",
1238 dentry->text, dentry->my_stat.st_mode));
1239 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1240 return 0;
1242 dentry = dentry->next;
1244 my_errno = ENOENT;
1245 return -1;
1248 /* stat a single file, get_remote_stat callback */
1249 static dir_entry *single_entry;
1250 static void
1251 statfile_helper(file_info *finfo, const char *mask)
1253 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
1255 #if 0 /* single_entry is never freed now. And only my_stat is used */
1256 single_entry = g_new (dir_entry, 1);
1258 single_entry->text = dos_to_unix (g_strdup (finfo->name), 1);
1260 single_entry->next = 0;
1261 #endif
1262 if (!single_entry)
1263 single_entry = g_new0 (dir_entry, 1);
1265 single_entry->my_stat.st_size = finfo->size;
1266 single_entry->my_stat.st_mtime = finfo->mtime;
1267 single_entry->my_stat.st_atime = finfo->atime;
1268 single_entry->my_stat.st_ctime = finfo->ctime;
1269 single_entry->my_stat.st_uid = finfo->uid;
1270 single_entry->my_stat.st_gid = finfo->gid;
1272 single_entry->my_stat.st_mode =
1273 S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR|S_IWGRP|S_IWOTH;
1275 /* if (finfo->mode & aVOLID); nothing similar in real world */
1276 if (finfo->mode & aDIR)
1277 single_entry->my_stat.st_mode |= S_IFDIR;
1278 else
1279 single_entry->my_stat.st_mode |= S_IFREG;/* if not dir, regular file? */
1280 /* if (finfo->mode & aARCH); DOS archive */
1281 /* if (finfo->mode & aHIDDEN); like a dot file? */
1282 /* if (finfo->mode & aSYSTEM); like a kernel? */
1283 if (finfo->mode & aRONLY)
1284 single_entry->my_stat.st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
1286 DEBUG(6, (" %-30s%7.7s%8.0f %s",
1287 CNV_LANG(finfo->name),
1288 attrib_string(finfo->mode),
1289 (double)finfo->size,
1290 asctime(LocalTime(&t))));
1293 /* stat a single file */
1294 static int
1295 get_remote_stat(smbfs_connection *sc, char *path, struct stat *buf)
1297 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
1298 char *mypath = path;
1300 DEBUG(3, ("get_remote_stat(): mypath:%s\n", mypath));
1302 convert_path(&mypath, FALSE);
1304 if (cli_list(sc->cli, mypath, attribute, statfile_helper) < 1) {
1305 my_errno = ENOENT;
1306 g_free (mypath);
1307 return -1; /* cli_list returns number of files */
1310 memcpy(buf, &single_entry->my_stat, sizeof(struct stat));
1312 /* dont free here, use for smbfs_fstat() */
1313 /* g_free(single_entry->text);
1314 g_free(single_entry); */
1315 g_free (mypath);
1316 return 0;
1319 static int
1320 search_dir_entry (dir_entry *dentry, const char *text, struct stat *buf)
1322 while (dentry) {
1323 if (strcmp(text, dentry->text) == 0) {
1324 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1325 memcpy(&single_entry->my_stat, &dentry->my_stat,
1326 sizeof(struct stat));
1327 return 0;
1329 dentry = dentry->next;
1331 return -1;
1334 static int
1335 get_stat_info (smbfs_connection *sc, char *path, struct stat *buf)
1337 char *p;
1338 #if 0
1339 dir_entry *dentry = current_info->entries;
1340 #endif
1341 const char *mypath = path;
1343 mypath++; /* cut off leading '/' */
1344 if ((p = strrchr(mypath, '/')))
1345 mypath = p + 1; /* advance until last file/dir name */
1346 DEBUG(3, ("get_stat_info: mypath:%s, current_info->dirname:%s\n",
1347 mypath, current_info->dirname));
1348 #if 0
1349 if (!dentry) {
1350 DEBUG(1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n", current_info->dirname, path));
1351 return -1;
1353 #endif
1354 if (!single_entry) /* when found, this will be written too */
1355 single_entry = g_new (dir_entry, 1);
1356 if (search_dir_entry(current_info->entries, mypath, buf) == 0) {
1357 return 0;
1359 /* now try to identify mypath as PARENT dir */
1361 char *mdp;
1362 char *mydir;
1363 mdp = mydir = g_strdup(current_info->dirname);
1364 if ((p = strrchr(mydir, '/')))
1365 *p = 0; /* advance util last '/' */
1366 if ((p = strrchr(mydir, '/')))
1367 mydir = p + 1; /* advance util last '/' */
1368 if (strcmp(mydir, mypath) == 0) { /* fake a stat for ".." */
1369 memset(buf, 0, sizeof(struct stat));
1370 buf->st_mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH;
1371 memcpy(&single_entry->my_stat, buf, sizeof(struct stat));
1372 g_free(mdp);
1373 DEBUG(1, (" PARENT:found in %s\n", current_info->dirname));
1374 return 0;
1376 g_free(mdp);
1378 /* now try to identify as CURRENT dir? */
1380 char *dnp = current_info->dirname;
1381 DEBUG(6, ("get_stat_info: is %s current dir? this dir is: %s\n",
1382 mypath, current_info->dirname));
1383 if (*dnp == '/')
1384 dnp++;
1385 else {
1386 return -1;
1388 if (strcmp(mypath, dnp) == 0) {
1389 memset(buf, 0, sizeof(struct stat));
1390 buf->st_mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH;
1391 memcpy(&single_entry->my_stat, buf, sizeof(struct stat));
1392 DEBUG(1, (" CURRENT:found in %s\n", current_info->dirname));
1393 return 0;
1396 DEBUG(3, ("'%s' not found in current_info '%s'\n", path, current_info->dirname));
1397 /* try to find this in the PREVIOUS listing */
1398 if (previous_info) {
1399 if (search_dir_entry(previous_info->entries, mypath, buf) == 0)
1400 return 0;
1401 DEBUG(3, ("'%s' not found in previous_info '%s'\n", path, previous_info->dirname));
1403 /* try to find this in the SHARE listing */
1404 if (current_share_info && search_dir_entry(current_share_info->entries, mypath, buf) == 0)
1405 return 0;
1406 DEBUG(3, ("'%s' not found in share_info '%s'\n", path, current_share_info->dirname));
1407 /* try to find this in the SERVER listing */
1408 if (current_server_info && search_dir_entry(current_server_info->entries, mypath, buf) == 0)
1409 return 0;
1410 DEBUG(3, ("'%s' not found in server_info '%s'\n", path, current_server_info->dirname));
1411 /* nothing found. get stat file info from server */
1412 return get_remote_stat(sc, path, buf);
1415 static int
1416 smbfs_chdir (vfs *me, char *path)
1418 char *remote_dir;
1419 smbfs_connection *sc;
1421 DEBUG(3, ("smbfs_chdir(path:%s)\n", path));
1422 if (!(remote_dir = smbfs_get_path (&sc, path)))
1423 return -1;
1424 g_free (remote_dir);
1426 return 0;
1429 static int
1430 loaddir(vfs *me, const char *path)
1432 void *info;
1433 char *mypath, *p;
1435 mypath = g_strdup(path);
1436 p = strrchr(mypath, '/');
1438 if (p > mypath)
1439 *p = 0;
1440 DEBUG(6, ("loaddir(%s)\n", mypath));
1441 smbfs_chdir(me, mypath);
1442 info = smbfs_opendir (me, mypath);
1443 g_free(mypath);
1444 if (!info)
1445 return -1;
1446 smbfs_readdir(info);
1447 smbfs_loaddir(info);
1448 return 0;
1451 static int
1452 smbfs_stat (vfs *me, char *path, struct stat *buf)
1454 char *remote_dir;
1455 smbfs_connection *sc;
1456 pstring server_url;
1457 char *service, *pp;
1458 const char *p;
1460 DEBUG(3, ("smbfs_stat(path:%s)\n", path));
1462 #if 0
1463 if (p = strchr(path, '@')) /* user:pass@server */
1464 path = ++p; /* don't want user:pass@ in path */
1465 #endif
1467 if (!current_info) {
1468 DEBUG(1, ("current_info = NULL: "));
1469 if (loaddir(me, path) < 0)
1470 return -1;
1473 pstrcpy(server_url, URL_HEADER);
1474 pstrcat(server_url, current_bucket->host);
1476 /* check if stating server */
1477 p = path;
1478 if (strncmp(p, URL_HEADER, HEADER_LEN)) {
1479 DEBUG(1, ("'%s' doesnt start with '%s' (length %d)\n",
1480 p, URL_HEADER, HEADER_LEN));
1481 return -1;
1484 p += HEADER_LEN;
1485 if (*p == '/')
1486 p++;
1487 pp = strchr(p, '/'); /* advance past next '/' */
1488 if (!pp) {
1489 if (!current_info->server_list) {
1490 if (loaddir(me, path) < 0)
1491 return -1;
1493 return fake_server_stat(server_url, path, buf);
1495 if (!strchr(++pp, '/')) {
1496 return fake_share_stat(server_url, path, buf);
1499 /* stating inside share at this point */
1500 if (!(remote_dir = smbfs_get_path (&sc, path))) /* connects if necessary */
1501 return -1;
1502 g_free (remote_dir);
1504 int hostlen = strlen(current_bucket->host);
1505 char *pp = path + strlen(path)-hostlen;
1506 char *sp = server_url + strlen(server_url)-hostlen;
1508 if (strcmp(sp, pp) == 0) {
1509 /* make server name appear as directory */
1510 DEBUG(1, ("smbfs_stat: showing server as directory\n"));
1511 memset(buf, 0, sizeof(struct stat));
1512 buf->st_mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH;
1513 return 0;
1516 /* check if current_info is in share requested */
1517 p = service = g_strdup(p);
1518 pp = strchr(p, '/');
1519 if (pp) {
1520 p = ++pp; /* advance past server name */
1521 pp = strchr(p, '/');
1523 if (pp)
1524 *pp = 0; /* cut off everthing after service name */
1525 else
1526 p = IPC; /* browsing for services */
1527 pp = current_info->dirname;
1528 if (*pp == '/');
1529 pp++;
1530 if (strncmp(p, pp, strlen(p)) != 0) {
1531 DEBUG(6, ("desired '%s' is not loaded, we have '%s'\n", p, pp));
1532 if (loaddir(me, path) < 0) {
1533 g_free (service);
1534 return -1;
1536 DEBUG(6, ("loaded dir: '%s'\n", current_info->dirname));
1538 g_free(service);
1539 /* stat dirs & files under shares now */
1540 return get_stat_info(sc, path, buf);
1543 #define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */
1545 static int
1546 smbfs_lseek (void *data, off_t offset, int whence)
1548 DEBUG(3, ("smbfs_lseek()\n"));
1549 my_errno = EOPNOTSUPP;
1550 return -1;
1553 static int
1554 smbfs_mknod (vfs *me, char *path, int mode, int dev)
1556 DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%d)\n", path, mode, dev));
1557 my_errno = EOPNOTSUPP;
1558 return -1;
1561 static int
1562 smbfs_mkdir (vfs *me, char *path, mode_t mode)
1564 smbfs_connection *sc;
1565 char *remote_file;
1567 DEBUG(3, ("smbfs_mkdir(path:%s, mode:%d)\n", path, mode));
1568 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1569 return -1;
1570 g_free (remote_file);
1571 convert_path(&path, FALSE);
1573 if (!cli_mkdir(sc->cli, path)) {
1574 my_errno = cli_error(sc->cli, NULL, &err, NULL);
1575 message_3s (1, MSG_ERROR, _(" %s mkdir'ing %s "),
1576 cli_errstr(sc->cli), CNV_LANG(path));
1577 g_free (path);
1578 return -1;
1580 g_free (path);
1581 return 0;
1584 static int
1585 smbfs_rmdir (vfs *me, char *path)
1587 smbfs_connection *sc;
1588 char *remote_file;
1590 DEBUG(3, ("smbfs_rmdir(path:%s)\n", path));
1591 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1592 return -1;
1593 g_free (remote_file);
1594 convert_path(&path, FALSE);
1596 if (!cli_rmdir(sc->cli, path)) {
1597 my_errno = cli_error(sc->cli, NULL, &err, NULL);
1598 message_3s (1, MSG_ERROR, _(" %s rmdir'ing %s "),
1599 cli_errstr(sc->cli), CNV_LANG(path));
1600 g_free (path);
1601 return -1;
1604 g_free (path);
1605 return 0;
1608 static int
1609 smbfs_link (vfs *me, char *p1, char *p2)
1611 DEBUG(3, ("smbfs_link(p1:%s, p2:%s)\n", p1, p2));
1612 my_errno = EOPNOTSUPP;
1613 return -1;
1616 /* We do not free anything right now: we free resources when we run
1617 * out of them
1619 static vfsid
1620 smbfs_getid (vfs *me, char *p, struct vfs_stamping **parent)
1622 *parent = NULL;
1623 DEBUG(3, ("smbfs_getid(p:%s)\n", p));
1625 return (vfsid) -1;
1628 static int
1629 smbfs_nothingisopen (vfsid id)
1631 DEBUG(3, ("smbfs_nothingisopen(%d)\n", (int)id));
1632 return 0;
1635 static void
1636 smbfs_free (vfsid id)
1638 DEBUG(3, ("smbfs_free(%d)\n", (int)id));
1639 /* FIXME: Should not be empty */
1640 authinfo_free_all ();
1643 /* Gives up on a socket and reopens the connection, the child own the socket
1644 * now
1646 static void
1647 my_forget (char *path)
1649 char *host, *user, *p;
1650 int port, i;
1652 if (strncmp (path, URL_HEADER, HEADER_LEN))
1653 return;
1655 DEBUG(3, ("my_forget(path:%s)\n", path));
1657 path += 6;
1658 if (path[0] == '/' && path[1] == '/')
1659 path += 2;
1661 if ((p = smbfs_get_host_and_username (&path, &host, &user, &port, NULL))) {
1662 g_free (p);
1663 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1664 if ((strcmp (host, smbfs_connections [i].host) == 0) &&
1665 (strcmp (user, smbfs_connections [i].user) == 0) &&
1666 (port == smbfs_connections [i].port)) {
1668 /* close socket: the child owns it now */
1669 cli_shutdown(smbfs_connections [i].cli);
1671 /* reopen the connection */
1672 smbfs_connections [i].cli =
1673 smbfs_do_connect(host, smbfs_connections[i].service);
1677 g_free (host);
1678 g_free (user);
1681 static int
1682 smbfs_setctl (vfs *me, char *path, int ctlop, char *arg)
1684 DEBUG(3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path, ctlop));
1685 switch (ctlop) {
1686 case MCCTL_FORGET_ABOUT:
1687 my_forget(path);
1688 return 0;
1690 return 0;
1693 static smbfs_handle *
1694 open_write (smbfs_handle *remote_handle, char *rname, int flags, int mode)
1696 if (flags & O_TRUNC) /* if it exists truncate to zero */
1697 DEBUG(3, ("open_write: O_TRUNC\n"));
1699 remote_handle->fnum = cli_open(remote_handle->cli, rname, flags, DENY_NONE);
1701 if (remote_handle->fnum == -1) {
1702 message_3s (1, MSG_ERROR, _(" %s opening remote file %s "),
1703 cli_errstr(remote_handle->cli), CNV_LANG(rname));
1704 DEBUG(1,("smbfs_open(rname:%s) error:%s\n",
1705 rname, cli_errstr(remote_handle->cli)));
1706 my_errno = cli_error(remote_handle->cli, NULL, &err, NULL);
1707 return NULL;
1710 return remote_handle;
1713 static smbfs_handle *
1714 open_read (smbfs_handle *remote_handle, char *rname, int flags, int mode)
1716 size_t size;
1718 remote_handle->fnum =
1719 cli_open(remote_handle->cli, rname, O_RDONLY, DENY_NONE);
1721 if (remote_handle->fnum == -1) {
1722 message_3s (1, MSG_ERROR, _(" %s opening remote file %s "),
1723 cli_errstr(remote_handle->cli), CNV_LANG(rname));
1724 DEBUG(1,("smbfs_open(rname:%s) error:%s\n",
1725 rname, cli_errstr(remote_handle->cli)));
1726 my_errno = cli_error(remote_handle->cli, NULL, &err, NULL);
1727 return NULL;
1730 if (!cli_qfileinfo(remote_handle->cli, remote_handle->fnum,
1731 &remote_handle->attr, &size, NULL, NULL, NULL, NULL, NULL) &&
1732 !cli_getattrE(remote_handle->cli, remote_handle->fnum,
1733 &remote_handle->attr, &size, NULL, NULL, NULL)) {
1734 message_2s (1, MSG_ERROR, " getattrib: %s ",
1735 cli_errstr(remote_handle->cli));
1736 DEBUG(1,("smbfs_open(rname:%s) getattrib:%s\n",
1737 rname, cli_errstr(remote_handle->cli)));
1738 my_errno = cli_error(remote_handle->cli, NULL, &err, NULL);
1739 return NULL;
1742 return remote_handle;
1745 static void *
1746 smbfs_open (vfs *me, char *file, int flags, int mode)
1748 char *remote_file, *p;
1749 void *ret;
1750 smbfs_connection *sc;
1751 smbfs_handle *remote_handle;
1753 DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%d)\n", file, flags, mode));
1755 if (!(remote_file = smbfs_get_path (&sc, file)))
1756 return 0;
1758 p = remote_file;
1759 convert_path(&remote_file, FALSE);
1760 g_free (p);
1762 remote_handle = g_new (smbfs_handle, 2);
1763 remote_handle->cli = sc->cli;
1764 remote_handle->nread = 0;
1766 if (flags & O_CREAT)
1767 ret = open_write(remote_handle, remote_file, flags, mode);
1768 else
1769 ret = open_read(remote_handle, remote_file, flags, mode);
1771 g_free (remote_file);
1773 return ret;
1776 static int
1777 smbfs_unlink (vfs *me, char *path)
1779 smbfs_connection *sc;
1780 char *remote_file, *p;
1782 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1783 return -1;
1785 p = remote_file;
1786 convert_path(&remote_file, FALSE);
1787 g_free (p);
1789 if (!cli_unlink(sc->cli, remote_file)) {
1790 message_3s (1, MSG_ERROR, _(" %s removing remote file %s "),
1791 cli_errstr(sc->cli), CNV_LANG(remote_file));
1792 g_free (remote_file);
1793 return -1;
1795 g_free (remote_file);
1796 return 0;
1799 static int
1800 smbfs_rename (vfs *me, char *a, char *b)
1802 smbfs_connection *sc;
1803 char *ra, *rb;
1804 char *p;
1805 int retval;
1807 if ((ra = smbfs_get_path (&sc, a)) == 0)
1808 return -1;
1810 if ((rb = smbfs_get_path (&sc, b)) == 0) {
1811 g_free (ra);
1812 return -1;
1815 p = ra;
1816 convert_path(&ra, FALSE);
1817 g_free (p);
1818 p = rb;
1819 convert_path(&rb, FALSE);
1820 g_free (p);
1822 retval = cli_rename(sc->cli, ra, rb);
1824 g_free (ra);
1825 g_free (rb);
1827 if (!retval) {
1828 message_2s (1, MSG_ERROR, _(" %s renaming files\n"),
1829 cli_errstr(sc->cli));
1830 return -1;
1832 return 0;
1835 static int
1836 smbfs_fstat (void *data, struct stat *buf)
1838 smbfs_handle *remote_handle = (smbfs_handle *)data;
1840 DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle->fnum));
1842 /* use left over from previous get_remote_stat, if available */
1843 if (single_entry)
1844 memcpy(buf, &single_entry->my_stat, sizeof(struct stat));
1845 else { /* single_entry not set up: bug */
1846 my_errno = EFAULT;
1847 return -EFAULT;
1849 return 0;
1852 vfs vfs_smbfs_ops = {
1853 NULL, /* This is place of next pointer */
1854 "smbfs",
1855 F_NET, /* flags */
1856 "smb:", /* prefix */
1857 NULL, /* data */
1858 0, /* errno */
1859 smbfs_init,
1860 NULL,
1861 smbfs_fill_names,
1862 NULL,
1864 smbfs_open,
1865 smbfs_close,
1866 smbfs_read,
1867 smbfs_write,
1869 smbfs_opendir,
1870 smbfs_readdir,
1871 smbfs_closedir,
1872 NULL,
1873 NULL,
1875 smbfs_stat,
1876 smbfs_lstat,
1877 smbfs_fstat,
1879 smbfs_chmod,
1880 smbfs_chown,
1881 smbfs_utime,
1883 smbfs_readlink,
1884 smbfs_symlink,
1885 smbfs_link,
1886 smbfs_unlink,
1888 smbfs_rename,
1889 smbfs_chdir,
1890 smbfs_errno,
1891 smbfs_lseek,
1892 smbfs_mknod,
1894 smbfs_getid,
1895 smbfs_nothingisopen,
1896 smbfs_free,
1898 NULL,
1899 NULL,
1901 smbfs_mkdir,
1902 smbfs_rmdir,
1903 NULL,
1904 smbfs_setctl
1906 MMAPNULL