* syntax.c (get_args): Use in "args_size" argument instead of
[midnight-commander.git] / vfs / smbfs.c
blob47f3c702bd8b4135a9a81b8300e6a84049588958
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>, 1997
6 Andrew V. Samoilov <sav@bcs.zp.ua> 2002, 2003
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public License
10 as published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public
19 License along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* Namespace: exports init_smbfs, smbfs_set_debug(), smbfs_set_debugf() */
23 #include <config.h>
24 #include <stdio.h>
25 #include <sys/types.h>
27 #undef USE_NCURSES /* Don't include *curses.h */
28 #include "utilvfs.h"
30 #undef PACKAGE_BUGREPORT
31 #undef PACKAGE_NAME
32 #undef PACKAGE_STRING
33 #undef PACKAGE_TARNAME
34 #undef PACKAGE_VERSION
36 #include "samba/include/config.h"
37 /* don't load crap in "samba/include/includes.h" we don't use and which
38 conflicts with definitions in other includes */
39 #undef HAVE_LIBREADLINE
40 #define NO_CONFIG_H
41 #undef VERSION
43 #include "samba/include/includes.h"
45 #include <string.h>
47 #include "vfs.h"
48 #include "vfs-impl.h"
49 #include "smbfs.h"
51 #define SMBFS_MAX_CONNECTIONS 16
52 static const char * const IPC = "IPC$";
53 static const char * const URL_HEADER = "/#smb:";
54 #define HEADER_LEN 6
56 static int my_errno;
57 static uint32 err;
59 /* stuff that is same with each connection */
60 extern int DEBUGLEVEL;
61 extern pstring myhostname;
62 static mode_t myumask = 0755;
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;
69 static struct vfs_class vfs_smbfs_ops;
71 static struct _smbfs_connection {
72 struct cli_state *cli;
73 struct in_addr dest_ip;
74 BOOL have_ip;
75 char *host; /* server name */
76 char *service; /* share name */
77 char *domain;
78 char *user;
79 char *home;
80 char *password;
81 int port;
82 int name_type;
83 time_t last_use;
84 } smbfs_connections [SMBFS_MAX_CONNECTIONS];
85 /* unique to each connection */
87 /* modifies *share */
88 static struct cli_state * smbfs_do_connect (const char *server, char *share);
90 typedef struct _smbfs_connection smbfs_connection;
91 static smbfs_connection *current_bucket;
93 typedef struct {
94 struct cli_state *cli;
95 int fnum;
96 off_t nread;
97 uint16 attr;
98 } smbfs_handle;
100 static GSList *auth_list;
102 static inline BOOL dbghdr_wrapper (int level, const char *file, const char *func, int line)
104 return dbghdr (level, const_cast(char *, file), const_cast(char *, func), line);
106 #define dbghdr dbghdr_wrapper
108 static void
109 smbfs_auth_free (struct smb_authinfo const *a)
111 g_free (a->host);
112 g_free (a->share);
113 g_free (a->domain);
114 g_free (a->user);
115 wipe_password (a->password);
118 static void
119 smbfs_auth_free_all (void)
121 if (auth_list) {
122 g_slist_foreach (auth_list, (GFunc)smbfs_auth_free, 0);
123 g_slist_free (auth_list);
124 auth_list = 0;
128 static gint
129 smbfs_auth_cmp_host_and_share (gconstpointer _a, gconstpointer _b)
131 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
132 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
134 if (!a->host || !a->share || !b->host || !b->share)
135 return 1;
136 if (strcmp (a->host, b->host) != 0)
137 return 1;
138 if (strcmp (a->share, b->share) != 0)
139 return 1;
140 return 0;
143 static gint
144 smbfs_auth_cmp_host (gconstpointer _a, gconstpointer _b)
146 struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
147 struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
149 if (!a->host || !b->host)
150 return 1;
151 if (strcmp (a->host, b->host) != 0)
152 return 1;
153 if (strcmp (a->share, IPC) != 0)
154 return 1;
155 return 0;
158 static void
159 smbfs_auth_add (const char *host, const char *share, const char *domain,
160 const char *user, const char *password)
162 struct smb_authinfo *auth = g_new (struct smb_authinfo, 1);
164 if (!auth)
165 return;
167 /* Don't check for NULL, g_strdup already does. */
168 auth->host = g_strdup (host);
169 auth->share = g_strdup (share);
170 auth->domain = g_strdup (domain);
171 auth->user = g_strdup (user);
172 auth->password = g_strdup (password);
173 auth_list = g_slist_prepend (auth_list, auth);
176 static void
177 smbfs_auth_remove (const char *host, const char *share)
179 struct smb_authinfo data;
180 struct smb_authinfo *auth;
181 GSList *list;
183 data.host = g_strdup (host);
184 data.share = g_strdup (share);
185 list = g_slist_find_custom (auth_list,
186 &data,
187 smbfs_auth_cmp_host_and_share);
188 g_free (data.host);
189 g_free (data.share);
190 if (!list)
191 return;
192 auth = list->data;
193 auth_list = g_slist_remove (auth_list, auth);
194 smbfs_auth_free (auth);
197 /* Set authentication information in bucket. Return 1 if successful, else 0 */
198 /* Information in auth_list overrides user if pass is NULL. */
199 /* bucket->host and bucket->service must be valid. */
200 static int
201 smbfs_bucket_set_authinfo (smbfs_connection *bucket,
202 const char *domain, const char *user, const char *pass,
203 int fallback_to_host)
205 struct smb_authinfo data;
206 struct smb_authinfo *auth;
207 GSList *list;
209 if (domain && user && pass) {
210 g_free (bucket->domain);
211 g_free (bucket->user);
212 g_free (bucket->password);
213 bucket->domain = g_strdup (domain);
214 bucket->user = g_strdup (user);
215 bucket->password = g_strdup (pass);
216 smbfs_auth_remove (bucket->host, bucket->service);
217 smbfs_auth_add (bucket->host, bucket->service,
218 domain, user, pass);
219 return 1;
222 data.host = bucket->host;
223 data.share = bucket->service;
224 list = g_slist_find_custom (auth_list, &data, smbfs_auth_cmp_host_and_share);
225 if (!list && fallback_to_host)
226 list = g_slist_find_custom (auth_list, &data, smbfs_auth_cmp_host);
227 if (list) {
228 auth = list->data;
229 bucket->domain = g_strdup (auth->domain);
230 bucket->user = g_strdup (auth->user);
231 bucket->password = g_strdup (auth->password);
232 return 1;
235 if (got_pass) {
236 bucket->domain = g_strdup (lp_workgroup ());
237 bucket->user = g_strdup (got_user ? username : user);
238 bucket->password = g_strdup (password);
239 return 1;
242 auth = vfs_smb_get_authinfo (bucket->host,
243 bucket->service,
244 (domain ? domain : lp_workgroup ()),
245 user);
246 if (auth) {
247 g_free (bucket->domain);
248 g_free (bucket->user);
249 g_free (bucket->password);
250 bucket->domain = g_strdup (auth->domain);
251 bucket->user = g_strdup (auth->user);
252 bucket->password = g_strdup (auth->password);
253 smbfs_auth_remove (bucket->host, bucket->service);
254 auth_list = g_slist_prepend (auth_list, auth);
255 return 1;
257 return 0;
260 void
261 smbfs_set_debug (int arg)
263 DEBUGLEVEL = arg;
266 void
267 smbfs_set_debugf (const char *filename)
269 extern pstring debugf;
270 extern FILE *dbf;
271 if (DEBUGLEVEL > 0) {
272 FILE *outfile = fopen (filename, "w");
273 if (outfile) {
274 setup_logging (const_cast(char *, ""), True); /* No needs for timestamp for each message */
275 dbf = outfile;
276 setbuf (dbf, NULL);
277 pstrcpy (debugf, filename);
282 /********************** The callbacks ******************************/
283 static int
284 smbfs_init (struct vfs_class * me)
286 const char *servicesf = CONFIGDIR PATH_SEP_STR "smb.conf";
288 /* DEBUGLEVEL = 4; */
290 TimeInit ();
291 charset_initialise ();
293 DEBUG (3, ("smbfs_init(%s)\n", me->name));
295 if (!get_myname (myhostname, NULL))
296 DEBUG (0, ("Failed to get my hostname.\n"));
298 if (!lp_load (const_cast(char *, servicesf), True, False, False))
299 DEBUG (0, ("Cannot load %s - run testparm to debug it\n", servicesf));
301 codepage_initialise (lp_client_code_page ());
303 load_interfaces ();
305 myumask = umask (0);
306 umask (myumask);
307 myumask = ~myumask;
309 if (getenv ("USER")) {
310 char *p;
312 pstrcpy (username, getenv ("USER"));
313 got_user = TRUE;
314 DEBUG (3, ("smbfs_init(): $USER:%s\n", username));
315 if ((p = strchr (username, '%'))) {
316 *p = 0;
317 pstrcpy (password, p + 1);
318 got_pass = TRUE;
319 memset (strchr (getenv ("USER"), '%') + 1, 'X', strlen (password));
320 DEBUG (3, ("smbfs_init(): $USER%%pass: %s%%%s\n",
321 username, password));
323 strupper (username);
325 if (getenv ("PASSWD")) {
326 pstrcpy (password, getenv ("PASSWD"));
327 got_pass = TRUE;
329 return 1;
332 static void
333 smbfs_fill_names (struct vfs_class *me, fill_names_f func)
335 int i;
336 char *path;
337 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
338 if (smbfs_connections [i].cli) {
339 path = g_strconcat (URL_HEADER,
340 smbfs_connections[i].user, "@",
341 smbfs_connections[i].host,
342 "/", smbfs_connections[i].service,
343 NULL);
344 (*func)(path);
345 g_free (path);
350 #define CNV_LANG(s) dos_to_unix(s,False)
351 #define GNAL_VNC(s) unix_to_dos(s,False)
352 /* does same as do_get() in client.c */
353 /* called from vfs.c:1080, count = buffer size */
354 static int
355 smbfs_read (void *data, char *buffer, int count)
357 smbfs_handle *info = (smbfs_handle *) data;
358 int n;
360 DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%d)\n",
361 info->fnum, (int)info->nread, count));
362 n = cli_read(info->cli, info->fnum, buffer, info->nread, count);
363 if (n > 0)
364 info->nread += n;
365 return n;
368 static int
369 smbfs_write (void *data, const char *buf, int nbyte)
371 smbfs_handle *info = (smbfs_handle *) data;
372 int n;
374 DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%d)\n",
375 info->fnum, (int)info->nread, nbyte));
376 n = cli_write(info->cli, info->fnum, 0, const_cast(char *, buf), info->nread, nbyte);
377 if (n > 0)
378 info->nread += n;
379 return n;
382 static int
383 smbfs_close (void *data)
385 smbfs_handle *info = (smbfs_handle *) data;
386 DEBUG (3, ("smbfs_close(fnum:%d)\n", info->fnum));
388 /* FIXME: Why too different cli have the same outbuf
389 * if file is copied to share
391 if (info->cli->outbuf == NULL) {
392 my_errno = EINVAL;
393 return -1;
395 #if 0
396 /* if imlementing archive_level: add rname to smbfs_handle */
397 if (archive_level >= 2 && (inf->attr & aARCH)) {
398 cli_setatr (info->cli, rname, info->attr & ~(uint16) aARCH, 0);
400 #endif
401 return (cli_close (info->cli, info->fnum) == True) ? 0 : -1;
404 static int
405 smbfs_errno (struct vfs_class *me)
407 DEBUG(3, ("smbfs_errno: %s\n", g_strerror(my_errno)));
408 return my_errno;
411 typedef struct dir_entry {
412 char *text;
413 struct dir_entry *next;
414 struct stat my_stat;
415 int merrno;
416 } dir_entry;
418 typedef struct {
419 gboolean server_list;
420 char *dirname;
421 char *path; /* the dir originally passed to smbfs_opendir */
422 smbfs_connection *conn;
423 dir_entry *entries;
424 dir_entry *current;
425 } opendir_info;
427 static opendir_info
428 *previous_info,
429 *current_info,
430 *current_share_info,
431 *current_server_info;
433 static gboolean first_direntry;
435 static dir_entry *
436 smbfs_new_dir_entry (const char *name)
438 static int inode_counter;
439 dir_entry *new_entry;
440 new_entry = g_new0 (dir_entry, 1);
441 new_entry->text = dos_to_unix (g_strdup (name), 1);
443 if (first_direntry) {
444 current_info->entries = new_entry;
445 first_direntry = FALSE;
446 } else {
447 current_info->current->next = new_entry;
449 current_info->current = new_entry;
450 new_entry->my_stat.st_ino = inode_counter++;
452 return new_entry;
455 /* browse for shares on server */
456 static void
457 smbfs_browsing_helper (const char *name, uint32 type, const char *comment, void *state)
459 const char *typestr = "";
461 dir_entry *new_entry = smbfs_new_dir_entry (name);
463 switch (type) {
464 case STYPE_DISKTREE:
465 typestr = "Disk";
466 /* show this as dir */
467 new_entry->my_stat.st_mode =
468 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
469 S_IXOTH) & myumask;
470 break;
471 case STYPE_PRINTQ:
472 typestr = "Printer";
473 break;
474 case STYPE_DEVICE:
475 typestr = "Device";
476 break;
477 case STYPE_IPC:
478 typestr = "IPC";
479 break;
481 DEBUG (3, ("\t%-15.15s%-10.10s%s\n", name, typestr, comment));
484 static void
485 smbfs_loaddir_helper (file_info * finfo, const char *mask, void *entry)
487 dir_entry *new_entry = (dir_entry *) entry;
488 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
489 #if 0 /* I want to see dot files */
490 if (finfo->mode & aHIDDEN)
491 return; /* don't bother with hidden files, "~$" screws up mc */
492 #endif
493 if (!entry)
494 new_entry = smbfs_new_dir_entry (finfo->name);
496 new_entry->my_stat.st_size = finfo->size;
497 new_entry->my_stat.st_mtime = finfo->mtime;
498 new_entry->my_stat.st_atime = finfo->atime;
499 new_entry->my_stat.st_ctime = finfo->ctime;
500 new_entry->my_stat.st_uid = finfo->uid;
501 new_entry->my_stat.st_gid = finfo->gid;
503 new_entry->my_stat.st_mode = /* rw-rw-rw */
504 S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH;
506 /* if (finfo->mode & aVOLID); nothing similar in real world */
507 if (finfo->mode & aDIR)
508 new_entry->my_stat.st_mode |= /* drwxrwxrwx */
509 S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
510 else
511 new_entry->my_stat.st_mode |= S_IFREG; /* if not dir, regular file? */
512 /* if (finfo->mode & aARCH); DOS archive */
513 /* if (finfo->mode & aHIDDEN); like a dot file? */
514 /* if (finfo->mode & aSYSTEM); like a kernel? */
515 if (finfo->mode & aRONLY)
516 new_entry->my_stat.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
517 new_entry->my_stat.st_mode &= myumask;
519 DEBUG (entry ? 3 : 6, (" %-30s%7.7s%8.0f %s",
520 CNV_LANG (finfo->name),
521 attrib_string (finfo->mode),
522 (double) finfo->size,
523 asctime (LocalTime (&t))));
526 /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
527 static char *
528 smbfs_convert_path (const char *remote_file, gboolean trailing_asterik)
530 const char *p, *my_remote;
531 char *result;
533 my_remote = remote_file;
534 if (strncmp (my_remote, URL_HEADER, HEADER_LEN) == 0) { /* if passed directly */
535 my_remote += HEADER_LEN;
536 if (*my_remote == '/') /* from server browsing */
537 my_remote++;
538 p = strchr(my_remote, '/');
539 if (p)
540 my_remote = p+1; /* advance to end of server name */
543 if (*my_remote == '/')
544 my_remote++; /* strip off leading '/' */
545 p = strchr(my_remote, '/');
546 if (p)
547 my_remote = p; /* strip off share/service name */
548 /* create remote filename as understood by smb clientgen */
549 result = g_strconcat (my_remote, trailing_asterik ? "/*" : "", 0);
550 unix_to_dos (result, /* inplace = */ 1); /* code page conversion */
551 str_replace(result, '/', '\\');
552 return result;
555 static void
556 smbfs_srv_browsing_helper (const char *name, uint32 m, const char *comment,
557 void *state)
559 dir_entry *new_entry = smbfs_new_dir_entry (name);
561 /* show this as dir */
562 new_entry->my_stat.st_mode =
563 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
564 S_IXOTH) & myumask;
566 DEBUG (3, ("\t%-16.16s %s\n", name, comment));
569 static BOOL
570 smbfs_reconnect(smbfs_connection *conn, int *retries)
572 char *host;
573 DEBUG(3, ("RECONNECT\n"));
575 if (*(conn->host) == 0)
576 host = g_strdup(conn->cli->desthost); /* server browsing */
577 else
578 host = g_strdup(conn->host);
580 cli_shutdown(conn->cli);
582 if (!(conn->cli = smbfs_do_connect(host, conn->service))) {
583 message (1, MSG_ERROR,
584 _(" reconnect to %s failed\n "), conn->host);
585 g_free(host);
586 return False;
588 g_free(host);
589 if (++(*retries) == 2)
590 return False;
591 return True;
594 static BOOL
595 smbfs_send(struct cli_state *cli)
597 size_t len;
598 size_t nwritten=0;
599 ssize_t ret;
601 len = smb_len(cli->outbuf) + 4;
603 while (nwritten < len) {
604 ret = write_socket(cli->fd, cli->outbuf+nwritten, len - nwritten);
605 if (ret <= 0) {
606 if (errno == EPIPE)
607 return False;
608 } else
609 nwritten += ret;
612 return True;
615 /****************************************************************************
616 See if server has cut us off by checking for EPIPE when writing.
617 Taken from cli_chkpath()
618 ****************************************************************************/
619 static BOOL
620 smbfs_chkpath(struct cli_state *cli, const char *path, BOOL send_only)
622 fstring path2;
623 char *p;
625 fstrcpy(path2,path);
626 unix_to_dos (path2, 1);
627 trim_string(path2,NULL,"\\");
628 if (!*path2) *path2 = '\\';
630 memset(cli->outbuf,'\0',smb_size);
631 set_message(cli->outbuf,0,4 + strlen(path2),True);
632 SCVAL(cli->outbuf,smb_com,SMBchkpth);
633 SSVAL(cli->outbuf,smb_tid,cli->cnum);
635 cli->rap_error = 0;
636 cli->nt_error = 0;
637 SSVAL(cli->outbuf,smb_pid,cli->pid);
638 SSVAL(cli->outbuf,smb_uid,cli->vuid);
639 SSVAL(cli->outbuf,smb_mid,cli->mid);
640 if (cli->protocol > PROTOCOL_CORE) {
641 SCVAL(cli->outbuf,smb_flg,0x8);
642 SSVAL(cli->outbuf,smb_flg2,0x1);
645 p = smb_buf(cli->outbuf);
646 *p++ = 4;
647 fstrcpy(p,path2);
649 if (!smbfs_send(cli)) {
650 DEBUG(3, ("smbfs_chkpath: couldnt send\n"));
651 return False;
653 if (send_only) {
654 client_receive_smb(cli->fd, cli->inbuf, cli->timeout);
655 DEBUG(3, ("smbfs_chkpath: send only OK\n"));
656 return True; /* just testing for EPIPE */
658 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout)) {
659 DEBUG(3, ("smbfs_chkpath: receive error\n"));
660 return False;
662 if ((my_errno = cli_error(cli, NULL, NULL, NULL))) {
663 if (my_errno == 20 || my_errno == 13)
664 return True; /* ignore if 'not a directory' error */
665 DEBUG(3, ("smbfs_chkpath: cli_error: %s\n", g_strerror(my_errno)));
666 return False;
669 return True;
672 #if 1
673 static int
674 smbfs_fs (const char *text)
676 const char *p = text;
677 int count = 0;
679 while ((p = strchr(p, '/')) != NULL) {
680 count++;
681 p++;
683 if (count == 1)
684 return strlen(text);
685 return count;
687 #endif
689 static int
690 smbfs_loaddir (opendir_info *smbfs_info)
692 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
693 int servlen = strlen (smbfs_info->conn->service);
694 const char *info_dirname = smbfs_info->dirname;
695 char *my_dirname;
697 DEBUG (3, ("smbfs_loaddir: dirname:%s\n", info_dirname));
698 first_direntry = TRUE;
700 if (current_info) {
701 DEBUG (3,
702 ("smbfs_loaddir: new:'%s', cached:'%s'\n", info_dirname,
703 current_info->dirname));
704 /* if new desired dir is longer than cached in current_info */
705 if (smbfs_fs (info_dirname) > smbfs_fs (current_info->dirname)) {
706 DEBUG (3, ("saving to previous_info\n"));
707 previous_info = current_info;
711 current_info = smbfs_info;
713 if (strcmp (info_dirname, "/") == 0) {
714 if (!strcmp (smbfs_info->path, URL_HEADER)) {
715 DEBUG (6, ("smbfs_loaddir: browsing %s\n", IPC));
716 /* browse for servers */
717 if (!cli_NetServerEnum
718 (smbfs_info->conn->cli, smbfs_info->conn->domain,
719 SV_TYPE_ALL, smbfs_srv_browsing_helper, NULL))
720 return 0;
721 else
722 current_server_info = smbfs_info;
723 smbfs_info->server_list = TRUE;
724 } else {
725 /* browse for shares */
726 if (cli_RNetShareEnum
727 (smbfs_info->conn->cli, smbfs_browsing_helper, NULL) < 1)
728 return 0;
729 else
730 current_share_info = smbfs_info;
732 goto done;
735 /* do regular directory listing */
736 if (strncmp (smbfs_info->conn->service, info_dirname + 1, servlen) == 0) {
737 /* strip share name from dir */
738 my_dirname = g_strdup (info_dirname + servlen);
739 *my_dirname = '/';
740 my_dirname = free_after(smbfs_convert_path (my_dirname, TRUE), my_dirname);
741 } else
742 my_dirname = smbfs_convert_path (info_dirname, TRUE);
744 DEBUG (6, ("smbfs_loaddir: service: %s\n", smbfs_info->conn->service));
745 DEBUG (6,
746 ("smbfs_loaddir: cli->share: %s\n",
747 smbfs_info->conn->cli->share));
748 DEBUG (6,
749 ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname));
750 /* do file listing: cli_list returns number of files */
751 if (cli_list
752 (smbfs_info->conn->cli, my_dirname, attribute,
753 smbfs_loaddir_helper, NULL) < 0) {
754 /* cli_list returns -1 if directory empty or cannot read socket */
755 my_errno = cli_error (smbfs_info->conn->cli, NULL, &err, NULL);
756 g_free (my_dirname);
757 return 0;
759 if (*(my_dirname) == 0)
760 smbfs_info->dirname = smbfs_info->conn->service;
761 g_free (my_dirname);
762 /* do_dskattr(); */
764 done:
765 /* current_info->parent = smbfs_info->dirname; */
767 smbfs_info->current = smbfs_info->entries;
768 return 1; /* 1 = ok */
771 #ifdef SMBFS_FREE_DIR
772 static void
773 smbfs_free_dir (dir_entry *de)
775 if (!de) return;
777 smbfs_free_dir (de->next);
778 g_free (de->text);
779 g_free (de);
781 #endif
784 /* The readdir routine loads the complete directory */
785 /* It's too slow to ask the server each time */
786 /* It now also sends the complete lstat information for each file */
787 static void *
788 smbfs_readdir(void *info)
790 static union vfs_dirent smbfs_readdir_data;
791 static char *const dirent_dest = smbfs_readdir_data.dent.d_name;
792 opendir_info *smbfs_info = (opendir_info *) info;
794 DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info->dirname));
796 if (!smbfs_info->entries)
797 if (!smbfs_loaddir(smbfs_info))
798 return NULL;
800 if (smbfs_info->current == 0) { /* reached end of dir entries */
801 DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
802 #ifdef SMBFS_FREE_DIR
803 smbfs_free_dir(smbfs_info->entries);
804 smbfs_info->entries = 0;
805 #endif
806 return NULL;
808 g_strlcpy(dirent_dest, smbfs_info->current->text, MC_MAXPATHLEN);
809 smbfs_info->current = smbfs_info->current->next;
811 compute_namelen(&smbfs_readdir_data.dent);
813 return &smbfs_readdir_data;
816 static int
817 smbfs_closedir (void *info)
819 opendir_info *smbfs_info = (opendir_info *) info;
820 /* dir_entry *p, *q; */
822 DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info->dirname));
823 /* CLOSE HERE */
825 /* for (p = smbfs_info->entries; p;){
826 q = p;
827 p = p->next;
828 g_free (q->text);
829 g_free (q);
831 g_free (info); */
832 return 0;
835 static int
836 smbfs_chmod (struct vfs_class *me, const char *path, int mode)
838 DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path, mode));
839 /* my_errno = EOPNOTSUPP;
840 return -1; */ /* cannot chmod on smb filesystem */
841 return 0; /* make mc happy */
844 static int
845 smbfs_chown (struct vfs_class *me, const char *path, int owner, int group)
847 DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path, owner, group));
848 my_errno = EOPNOTSUPP; /* ready for your labotomy? */
849 return -1;
852 static int
853 smbfs_utime (struct vfs_class *me, const char *path, struct utimbuf *times)
855 DEBUG(3, ("smbfs_utime(path:%s)\n", path));
856 my_errno = EOPNOTSUPP;
857 return -1;
860 static int
861 smbfs_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
863 DEBUG (3,
864 ("smbfs_readlink(path:%s, buf:%s, size:%d)\n", path, buf,
865 (int) size));
866 my_errno = EOPNOTSUPP;
867 return -1; /* no symlinks on smb filesystem? */
870 static int
871 smbfs_symlink (struct vfs_class *me, const char *n1, const char *n2)
873 DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1, n2));
874 my_errno = EOPNOTSUPP;
875 return -1; /* no symlinks on smb filesystem? */
878 /* Extract the hostname and username from the path */
879 /* path is in the form: [user@]hostname/share/remote-dir */
880 #define smbfs_get_host_and_username(path, host, user, port, pass) \
881 vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
883 /*****************************************************
884 return a connection to a SMB server
885 current_bucket needs to be set before calling
886 *******************************************************/
887 static struct cli_state *
888 smbfs_do_connect (const char *server, char *share)
890 struct cli_state *c;
891 struct nmb_name called, calling;
892 struct in_addr ip;
893 extern struct in_addr ipzero;
895 DEBUG(3, ("smbfs_do_connect(%s, %s)\n", server, share));
896 if (*share == '\\') {
897 server = share+2;
898 share = strchr(server,'\\');
899 if (!share) return NULL;
900 *share = 0;
901 share++;
904 make_nmb_name(&calling, global_myname, 0x0);
905 make_nmb_name(&called , server, current_bucket->name_type);
907 for (;;) {
909 ip = (current_bucket->have_ip) ? current_bucket->dest_ip : ipzero;
911 /* have to open a new connection */
912 if (!(c = cli_initialise(NULL))) {
913 my_errno = ENOMEM;
914 return NULL;
917 pwd_init(&(c->pwd)); /* should be moved into cli_initialise()? */
918 pwd_set_cleartext(&(c->pwd), current_bucket->password);
920 if ((cli_set_port(c, current_bucket->port) == 0) ||
921 !cli_connect(c, server, &ip)) {
922 DEBUG(1, ("Connection to %s failed\n", server));
923 break;
926 if (!cli_session_request(c, &calling, &called)) {
927 my_errno = cli_error(c, NULL, &err, NULL);
928 DEBUG(1, ("session request to %s failed\n", called.name));
929 cli_shutdown(c);
930 if (strcmp(called.name, "*SMBSERVER")) {
931 make_nmb_name(&called , "*SMBSERVER", 0x20);
932 continue;
934 return NULL;
937 DEBUG(3, (" session request ok\n"));
939 if (!cli_negprot(c)) {
940 DEBUG(1, ("protocol negotiation failed\n"));
941 break;
944 if (!cli_session_setup(c, current_bucket->user,
945 current_bucket->password, strlen(current_bucket->password),
946 current_bucket->password, strlen(current_bucket->password),
947 current_bucket->domain)) {
948 DEBUG(1,("session setup failed: %s\n", cli_errstr(c)));
949 smbfs_auth_remove (server, share);
950 break;
953 if (*c->server_domain || *c->server_os || *c->server_type)
954 DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
955 c->server_domain,c->server_os,c->server_type));
957 DEBUG(3, (" session setup ok\n"));
959 if (!cli_send_tconX(c, share, const_cast(char *, "?????"),
960 current_bucket->password, strlen(current_bucket->password)+1)) {
961 DEBUG(1,("%s: tree connect failed: %s\n", share, cli_errstr(c)));
962 break;
965 DEBUG(3, (" tconx ok\n"));
967 my_errno = 0;
968 return c;
971 my_errno = cli_error(c, NULL, &err, NULL);
972 cli_shutdown(c);
973 return NULL;
977 static int
978 smbfs_get_master_browser(char **host)
980 int count;
981 struct in_addr *ip_list, bcast_addr;
982 extern struct in_addr ipzero;
984 /* does port = 137 for win95 master browser? */
985 int fd= open_socket_in( SOCK_DGRAM, 0, 3,
986 interpret_addr(lp_socket_address()), True );
987 if (fd == -1)
988 return 0;
989 set_socket_options(fd, const_cast(char *, "SO_BROADCAST"));
990 ip_list = iface_bcast(ipzero);
991 bcast_addr = *ip_list;
992 if ((ip_list = name_query(fd, "\01\02__MSBROWSE__\02", 1, True,
993 True, bcast_addr, &count, NULL))) {
994 if (!count)
995 return 0;
996 /* just return first master browser */
997 *host = g_strdup(inet_ntoa(ip_list[0]));
998 return 1;
1000 return 0;
1003 static void
1004 smbfs_free_bucket (smbfs_connection *bucket)
1006 g_free (bucket->host);
1007 g_free (bucket->service);
1008 g_free (bucket->domain);
1009 g_free (bucket->user);
1010 wipe_password (bucket->password);
1011 g_free (bucket->home);
1012 memset (bucket, 0, sizeof (smbfs_connection));
1015 static smbfs_connection *
1016 smbfs_get_free_bucket (void)
1018 int i;
1020 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++)
1021 if (!smbfs_connections [i].cli) return &smbfs_connections [i];
1023 { /* search for most dormant connection */
1024 int oldest = 0; /* index */
1025 time_t oldest_time = smbfs_connections[0].last_use;
1026 for (i = 1; i < SMBFS_MAX_CONNECTIONS; i++) {
1027 if (smbfs_connections[i].last_use < oldest_time) {
1028 oldest_time = smbfs_connections[i].last_use;
1029 oldest = i;
1032 cli_shutdown(smbfs_connections[oldest].cli);
1033 smbfs_free_bucket (&smbfs_connections[oldest]);
1034 return &smbfs_connections[oldest];
1037 /* This can't happend, since we have checked for max connections before */
1038 vfs_die("Internal error: smbfs_get_free_bucket");
1039 return 0; /* shut up, stupid gcc */
1042 /* This routine keeps track of open connections */
1043 /* Returns a connected socket to host */
1044 static smbfs_connection *
1045 smbfs_open_link (char *host, char *path, const char *user, int *port,
1046 char *this_pass)
1048 int i;
1049 smbfs_connection *bucket;
1050 pstring service;
1051 struct in_addr *dest_ip = NULL;
1053 DEBUG (3, ("smbfs_open_link(host:%s, path:%s)\n", host, path));
1055 if (strcmp (host, path) == 0) /* if host & path are same: */
1056 pstrcpy (service, IPC); /* setup for browse */
1057 else { /* get share name from path, path starts with server name */
1058 char *p;
1059 if ((p = strchr (path, '/'))) /* get share aka */
1060 pstrcpy (service, ++p); /* service name from path */
1061 else
1062 pstrcpy (service, "");
1063 /* now check for trailing directory/filenames */
1064 p = strchr (service, '/');
1065 if (p)
1066 *p = 0; /* cut off dir/files: sharename only */
1067 if (!*service)
1068 pstrcpy (service, IPC); /* setup for browse */
1069 DEBUG (6, ("smbfs_open_link: service from path:%s\n", service));
1072 if (got_user)
1073 user = username; /* global from getenv */
1075 /* Is the link actually open? */
1076 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1077 if (!smbfs_connections[i].cli)
1078 continue;
1079 if ((strcmp (host, smbfs_connections[i].host) == 0) &&
1080 (strcmp (user, smbfs_connections[i].user) == 0) &&
1081 (strcmp (service, smbfs_connections[i].service) == 0)) {
1082 int retries = 0;
1083 BOOL inshare = (*host != 0 && *path != 0 && strchr (path, '/'));
1084 /* check if this connection has died */
1085 while (!smbfs_chkpath (smbfs_connections[i].cli, "\\", !inshare)) {
1086 if (!smbfs_reconnect (&smbfs_connections[i], &retries))
1087 return 0;
1089 DEBUG (6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i));
1090 current_bucket = &smbfs_connections[i];
1091 smbfs_connections[i].last_use = time (NULL);
1092 return &smbfs_connections[i];
1094 /* connection not found, find if we have ip for new connection */
1095 if (strcmp (host, smbfs_connections[i].host) == 0)
1096 dest_ip = &smbfs_connections[i].cli->dest_ip;
1099 /* make new connection */
1100 bucket = smbfs_get_free_bucket ();
1101 bucket->name_type = 0x20;
1102 bucket->home = 0;
1103 bucket->port = *port;
1104 bucket->have_ip = False;
1105 if (dest_ip) {
1106 bucket->have_ip = True;
1107 bucket->dest_ip = *dest_ip;
1109 current_bucket = bucket;
1111 bucket->user = g_strdup (user);
1112 bucket->service = g_strdup (service);
1114 if (!(*host)) { /* if blank host name, browse for servers */
1115 if (!smbfs_get_master_browser (&host)) /* set host to ip of master browser */
1116 return 0; /* could not find master browser? */
1117 g_free (host);
1118 bucket->host = g_strdup (""); /* blank host means master browser */
1119 } else
1120 bucket->host = g_strdup (host);
1122 if (!smbfs_bucket_set_authinfo (bucket, 0, /* domain currently not used */
1123 user, this_pass, 1))
1124 return 0;
1126 /* connect to share */
1127 while (!(bucket->cli = smbfs_do_connect (host, service))) {
1129 if (my_errno != EPERM)
1130 return 0;
1131 message (1, MSG_ERROR, _(" Authentication failed "));
1133 /* authentication failed, try again */
1134 smbfs_auth_remove (bucket->host, bucket->service);
1135 if (!smbfs_bucket_set_authinfo (bucket, bucket->domain, bucket->user, 0, 0))
1136 return 0;
1140 smbfs_open_connections++;
1141 DEBUG (3, ("smbfs_open_link:smbfs_open_connections: %d\n",
1142 smbfs_open_connections));
1143 return bucket;
1146 static char *
1147 smbfs_get_path (smbfs_connection ** sc, const char *path)
1149 char *user, *host, *remote_path, *pass;
1150 int port = SMB_PORT;
1152 DEBUG (3, ("smbfs_get_path(%s)\n", path));
1153 if (strncmp (path, URL_HEADER, HEADER_LEN))
1154 return NULL;
1155 path += HEADER_LEN;
1157 if (*path == '/') /* '/' leading server name */
1158 path++; /* probably came from server browsing */
1160 if ((remote_path =
1161 smbfs_get_host_and_username (&path, &host, &user, &port, &pass)))
1162 if ((*sc =
1163 smbfs_open_link (host, remote_path, user, &port, pass)) == NULL) {
1164 g_free (remote_path);
1165 remote_path = NULL;
1167 g_free (host);
1168 g_free (user);
1169 if (pass)
1170 wipe_password (pass);
1172 if (!remote_path)
1173 return NULL;
1175 /* NOTE: tildes are deprecated. See ftpfs.c */
1177 int f = !strcmp (remote_path, "/~");
1178 if (f || !strncmp (remote_path, "/~/", 3)) {
1179 char *s;
1180 s = concat_dir_and_file ((*sc)->home, remote_path + 3 - f);
1181 g_free (remote_path);
1182 return s;
1185 return remote_path;
1188 #if 0
1189 static int
1190 is_error (int result, int errno_num)
1192 if (!(result == -1))
1193 return my_errno = 0;
1194 else
1195 my_errno = errno_num;
1196 return 1;
1198 #endif
1200 static void *
1201 smbfs_opendir (struct vfs_class *me, const char *dirname)
1203 opendir_info *smbfs_info;
1204 smbfs_connection *sc;
1205 char *remote_dir;
1207 DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname));
1209 if (!(remote_dir = smbfs_get_path (&sc, dirname)))
1210 return NULL;
1212 /* FIXME: where freed? */
1213 smbfs_info = g_new (opendir_info, 1);
1214 smbfs_info->server_list = FALSE;
1215 smbfs_info->path = g_strdup(dirname); /* keep original */
1216 smbfs_info->dirname = remote_dir;
1217 smbfs_info->conn = sc;
1218 smbfs_info->entries = 0;
1219 smbfs_info->current = 0;
1221 return smbfs_info;
1224 static int
1225 smbfs_fake_server_stat (const char *server_url, const char *path, struct stat *buf)
1227 dir_entry *dentry;
1228 const char *p;
1230 if ((p = strrchr (path, '/')))
1231 path = p + 1; /* advance until last '/' */
1233 if (!current_info->entries) {
1234 if (!smbfs_loaddir (current_info)) /* browse host */
1235 return -1;
1238 if (current_info->server_list == True) {
1239 dentry = current_info->entries;
1240 DEBUG (4, ("fake stat for SERVER \"%s\"\n", path));
1241 while (dentry) {
1242 if (strcmp (dentry->text, path) == 0) {
1243 DEBUG (4, ("smbfs_fake_server_stat: %s:%4o\n",
1244 dentry->text, (int)dentry->my_stat.st_mode));
1245 memcpy (buf, &dentry->my_stat, sizeof (struct stat));
1246 return 0;
1248 dentry = dentry->next;
1251 my_errno = ENOENT;
1252 return -1;
1255 static int
1256 smbfs_fake_share_stat (const char *server_url, const char *path, struct stat *buf)
1258 dir_entry *dentry;
1259 if (strlen (path) < strlen (server_url))
1260 return -1;
1262 if (!current_share_info) { /* Server was not stat()ed */
1263 /* Make sure there is such share at server */
1264 smbfs_connection *sc;
1265 char *p;
1266 p = smbfs_get_path (&sc, path);
1267 g_free (p);
1268 if (p) {
1269 memset (buf, 0, sizeof (*buf));
1270 /* show this as dir */
1271 buf->st_mode =
1272 (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
1273 S_IXOTH) & myumask;
1274 return 0;
1276 return -1;
1279 path += strlen (server_url); /* we only want share name */
1280 path++;
1282 if (*path == '/') /* '/' leading server name */
1283 path++; /* probably came from server browsing */
1285 if (!current_share_info->entries) {
1286 if (!smbfs_loaddir (current_share_info)) /* browse host */
1287 return -1;
1289 dentry = current_share_info->entries;
1290 DEBUG (3, ("smbfs_fake_share_stat: %s on %s\n", path, server_url));
1291 while (dentry) {
1292 if (strcmp (dentry->text, path) == 0) {
1293 DEBUG (6, ("smbfs_fake_share_stat: %s:%4o\n",
1294 dentry->text, (int) dentry->my_stat.st_mode));
1295 memcpy (buf, &dentry->my_stat, sizeof (struct stat));
1296 return 0;
1298 dentry = dentry->next;
1300 my_errno = ENOENT;
1301 return -1;
1304 /* stat a single file, smbfs_get_remote_stat callback */
1305 static dir_entry *single_entry;
1307 /* stat a single file */
1308 static int
1309 smbfs_get_remote_stat (smbfs_connection * sc, const char *path, struct stat *buf)
1311 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
1312 char *mypath;
1314 DEBUG (3, ("smbfs_get_remote_stat(): mypath:%s\n", path));
1316 mypath = smbfs_convert_path (path, FALSE);
1318 #if 0 /* single_entry is never free()d now. And only my_stat is used */
1319 single_entry = g_new (dir_entry, 1);
1321 single_entry->text = dos_to_unix (g_strdup (finfo->name), 1);
1323 single_entry->next = 0;
1324 #endif
1325 if (!single_entry)
1326 single_entry = g_new0 (dir_entry, 1);
1328 if (cli_list
1329 (sc->cli, mypath, attribute, smbfs_loaddir_helper, single_entry) < 1) {
1330 my_errno = ENOENT;
1331 g_free (mypath);
1332 return -1; /* cli_list returns number of files */
1335 memcpy (buf, &single_entry->my_stat, sizeof (struct stat));
1337 /* don't free here, use for smbfs_fstat() */
1338 /* g_free(single_entry->text);
1339 g_free(single_entry); */
1340 g_free (mypath);
1341 return 0;
1344 static int
1345 smbfs_search_dir_entry (dir_entry *dentry, const char *text, struct stat *buf)
1347 while (dentry) {
1348 if (strcmp(text, dentry->text) == 0) {
1349 memcpy(buf, &dentry->my_stat, sizeof(struct stat));
1350 memcpy(&single_entry->my_stat, &dentry->my_stat,
1351 sizeof(struct stat));
1352 return 0;
1354 dentry = dentry->next;
1356 return -1;
1359 static int
1360 smbfs_get_stat_info (smbfs_connection * sc, const char *path, struct stat *buf)
1362 char *p;
1363 #if 0
1364 dir_entry *dentry = current_info->entries;
1365 #endif
1366 const char *mypath = path;
1368 mypath++; /* cut off leading '/' */
1369 if ((p = strrchr (mypath, '/')))
1370 mypath = p + 1; /* advance until last file/dir name */
1371 DEBUG (3, ("smbfs_get_stat_info: mypath:%s, current_info->dirname:%s\n",
1372 mypath, current_info->dirname));
1373 #if 0
1374 if (!dentry) {
1375 DEBUG (1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n",
1376 current_info->dirname, path));
1377 return -1;
1379 #endif
1380 if (!single_entry) /* when found, this will be written too */
1381 single_entry = g_new (dir_entry, 1);
1382 if (smbfs_search_dir_entry (current_info->entries, mypath, buf) == 0) {
1383 return 0;
1385 /* now try to identify mypath as PARENT dir */
1387 char *mdp;
1388 char *mydir;
1389 mdp = mydir = g_strdup (current_info->dirname);
1390 if ((p = strrchr (mydir, '/')))
1391 *p = 0; /* advance util last '/' */
1392 if ((p = strrchr (mydir, '/')))
1393 mydir = p + 1; /* advance util last '/' */
1394 if (strcmp (mydir, mypath) == 0) { /* fake a stat for ".." */
1395 memset (buf, 0, sizeof (struct stat));
1396 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1397 memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
1398 g_free (mdp);
1399 DEBUG (1, (" PARENT:found in %s\n", current_info->dirname));
1400 return 0;
1402 g_free (mdp);
1404 /* now try to identify as CURRENT dir? */
1406 char *dnp = current_info->dirname;
1407 DEBUG (6, ("smbfs_get_stat_info: is %s current dir? this dir is: %s\n",
1408 mypath, current_info->dirname));
1409 if (*dnp == '/')
1410 dnp++;
1411 else {
1412 return -1;
1414 if (strcmp (mypath, dnp) == 0) {
1415 memset (buf, 0, sizeof (struct stat));
1416 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1417 memcpy (&single_entry->my_stat, buf, sizeof (struct stat));
1418 DEBUG (1, (" CURRENT:found in %s\n", current_info->dirname));
1419 return 0;
1422 DEBUG (3, ("'%s' not found in current_info '%s'\n", path,
1423 current_info->dirname));
1424 /* try to find this in the PREVIOUS listing */
1425 if (previous_info) {
1426 if (smbfs_search_dir_entry (previous_info->entries, mypath, buf) == 0)
1427 return 0;
1428 DEBUG (3, ("'%s' not found in previous_info '%s'\n", path,
1429 previous_info->dirname));
1431 /* try to find this in the SHARE listing */
1432 if (current_share_info) {
1433 if (smbfs_search_dir_entry (current_share_info->entries, mypath, buf) == 0)
1434 return 0;
1435 DEBUG (3, ("'%s' not found in share_info '%s'\n", path,
1436 current_share_info->dirname));
1438 /* try to find this in the SERVER listing */
1439 if (current_server_info) {
1440 if (smbfs_search_dir_entry (current_server_info->entries, mypath, buf) == 0)
1441 return 0;
1442 DEBUG (3, ("'%s' not found in server_info '%s'\n", path,
1443 current_server_info->dirname));
1445 /* nothing found. get stat file info from server */
1446 return smbfs_get_remote_stat (sc, path, buf);
1449 static int
1450 smbfs_chdir (struct vfs_class *me, const char *path)
1452 char *remote_dir;
1453 smbfs_connection *sc;
1455 DEBUG (3, ("smbfs_chdir(path:%s)\n", path));
1456 if (!(remote_dir = smbfs_get_path (&sc, path)))
1457 return -1;
1458 g_free (remote_dir);
1460 return 0;
1463 static int
1464 smbfs_loaddir_by_name (struct vfs_class *me, const char *path)
1466 void *info;
1467 char *mypath, *p;
1469 mypath = g_strdup(path);
1470 p = strrchr(mypath, '/');
1472 if (p > mypath)
1473 *p = 0;
1474 DEBUG(6, ("smbfs_loaddir_by_name(%s)\n", mypath));
1475 smbfs_chdir(me, mypath);
1476 info = smbfs_opendir (me, mypath);
1477 g_free(mypath);
1478 if (!info)
1479 return -1;
1480 smbfs_readdir(info);
1481 smbfs_loaddir(info);
1482 return 0;
1485 static int
1486 smbfs_stat (struct vfs_class * me, const char *path, struct stat *buf)
1488 smbfs_connection *sc;
1489 pstring server_url;
1490 char *service, *pp, *at;
1491 const char *p;
1493 DEBUG (3, ("smbfs_stat(path:%s)\n", path));
1495 if (!current_info) {
1496 DEBUG (1, ("current_info = NULL: "));
1497 if (smbfs_loaddir_by_name (me, path) < 0)
1498 return -1;
1501 /* check if stating server */
1502 p = path;
1503 if (strncmp (p, URL_HEADER, HEADER_LEN)) {
1504 DEBUG (1, ("'%s' doesnt start with '%s' (length %d)\n",
1505 p, URL_HEADER, HEADER_LEN));
1506 return -1;
1509 p += HEADER_LEN;
1510 if (*p == '/')
1511 p++;
1513 pp = strchr (p, '/'); /* advance past next '/' */
1514 at = strchr (p, '@');
1515 pstrcpy (server_url, URL_HEADER);
1516 if (at && at < pp) { /* user@server */
1517 char *z = &(server_url[sizeof (server_url) - 1]);
1518 const char *s = p;
1520 at = &(server_url [HEADER_LEN]) + (at - p + 1);
1521 if (z > at)
1522 z = at;
1523 at = &(server_url [HEADER_LEN]);
1524 while (at < z)
1525 *at++ = *s++;
1526 *z = 0;
1528 pstrcat (server_url, current_bucket->host);
1530 if (!pp) {
1531 if (!current_info->server_list) {
1532 if (smbfs_loaddir_by_name (me, path) < 0)
1533 return -1;
1535 return smbfs_fake_server_stat (server_url, path, buf);
1538 if (!strchr (++pp, '/')) {
1539 return smbfs_fake_share_stat (server_url, path, buf);
1542 /* stating inside share at this point */
1543 if (!(service = smbfs_get_path (&sc, path))) /* connects if necessary */
1544 return -1;
1546 int hostlen = strlen (current_bucket->host);
1547 char *ppp = service + strlen (service) - hostlen;
1548 char *sp = server_url + strlen (server_url) - hostlen;
1550 if (strcmp (sp, ppp) == 0) {
1551 /* make server name appear as directory */
1552 DEBUG (1, ("smbfs_stat: showing server as directory\n"));
1553 memset (buf, 0, sizeof (struct stat));
1554 buf->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) & myumask;
1555 g_free (service);
1556 return 0;
1559 /* check if current_info is in share requested */
1560 p = service;
1561 pp = strchr (p, '/');
1562 if (pp) {
1563 p = ++pp; /* advance past server name */
1564 pp = strchr (p, '/');
1566 if (pp)
1567 *pp = 0; /* cut off everthing after service name */
1568 else
1569 p = IPC; /* browsing for services */
1570 pp = current_info->dirname;
1571 if (*pp == '/')
1572 pp++;
1573 if (strncmp (p, pp, strlen (p)) != 0) {
1574 DEBUG (6, ("desired '%s' is not loaded, we have '%s'\n", p, pp));
1575 if (smbfs_loaddir_by_name (me, path) < 0) {
1576 g_free (service);
1577 return -1;
1579 DEBUG (6, ("loaded dir: '%s'\n", current_info->dirname));
1581 g_free (service);
1582 /* stat dirs & files under shares now */
1583 return smbfs_get_stat_info (sc, path, buf);
1586 #define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */
1588 static int
1589 smbfs_lseek (void *data, off_t offset, int whence)
1591 smbfs_handle *info = (smbfs_handle *) data;
1592 size_t size;
1594 DEBUG (3,
1595 ("smbfs_lseek(info->nread => %d, offset => %d, whence => %d) \n",
1596 (int) info->nread, (int) offset, whence));
1598 switch (whence) {
1599 case SEEK_SET:
1600 info->nread = offset;
1601 break;
1602 case SEEK_CUR:
1603 info->nread += offset;
1604 break;
1605 case SEEK_END:
1606 if (!cli_qfileinfo (info->cli, info->fnum,
1607 NULL, &size, NULL, NULL, NULL,
1608 NULL, NULL) &&
1609 !cli_getattrE (info->cli, info->fnum,
1610 NULL, &size, NULL, NULL, NULL)) {
1611 errno = EINVAL;
1612 return -1;
1614 info->nread = size + offset;
1615 break;
1618 return info->nread;
1621 static int
1622 smbfs_mknod (struct vfs_class *me, const char *path, int mode, int dev)
1624 DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%d)\n", path, mode, dev));
1625 my_errno = EOPNOTSUPP;
1626 return -1;
1629 static int
1630 smbfs_mkdir (struct vfs_class * me, const char *path, mode_t mode)
1632 smbfs_connection *sc;
1633 char *remote_file;
1634 char *cpath;
1636 DEBUG (3, ("smbfs_mkdir(path:%s, mode:%d)\n", path, (int) mode));
1637 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1638 return -1;
1639 g_free (remote_file);
1640 cpath = smbfs_convert_path (path, FALSE);
1642 if (!cli_mkdir (sc->cli, cpath)) {
1643 my_errno = cli_error (sc->cli, NULL, &err, NULL);
1644 message (1, MSG_ERROR, _(" Error %s creating directory %s "),
1645 cli_errstr (sc->cli), CNV_LANG (cpath));
1646 g_free (cpath);
1647 return -1;
1649 g_free (cpath);
1650 return 0;
1653 static int
1654 smbfs_rmdir (struct vfs_class *me, const char *path)
1656 smbfs_connection *sc;
1657 char *remote_file;
1658 char *cpath;
1660 DEBUG(3, ("smbfs_rmdir(path:%s)\n", path));
1661 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1662 return -1;
1663 g_free (remote_file);
1664 cpath = smbfs_convert_path (path, FALSE);
1666 if (!cli_rmdir(sc->cli, cpath)) {
1667 my_errno = cli_error(sc->cli, NULL, &err, NULL);
1668 message (1, MSG_ERROR, _(" Error %s removing directory %s "),
1669 cli_errstr(sc->cli), CNV_LANG(cpath));
1670 g_free (cpath);
1671 return -1;
1674 g_free (cpath);
1675 return 0;
1678 static int
1679 smbfs_link (struct vfs_class *me, const char *p1, const char *p2)
1681 DEBUG (3, ("smbfs_link(p1:%s, p2:%s)\n", p1, p2));
1682 my_errno = EOPNOTSUPP;
1683 return -1;
1686 static void
1687 smbfs_free (vfsid id)
1689 DEBUG (3, ("smbfs_free(%p)\n", id));
1690 smbfs_auth_free_all ();
1693 /* Gives up on a socket and reopens the connection, the child own the socket
1694 * now
1696 static void
1697 smbfs_forget (const char *path)
1699 char *host, *user, *p;
1700 int port, i;
1702 if (strncmp (path, URL_HEADER, HEADER_LEN))
1703 return;
1705 DEBUG (3, ("smbfs_forget(path:%s)\n", path));
1707 path += 6;
1708 if (path[0] == '/' && path[1] == '/')
1709 path += 2;
1711 if ((p = smbfs_get_host_and_username (&path, &host, &user, &port, NULL))) {
1712 g_free (p);
1713 for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
1714 if (smbfs_connections[i].cli
1715 && (strcmp (host, smbfs_connections[i].host) == 0)
1716 && (strcmp (user, smbfs_connections[i].user) == 0)
1717 && (port == smbfs_connections[i].port)) {
1719 /* close socket: the child owns it now */
1720 cli_shutdown (smbfs_connections[i].cli);
1722 /* reopen the connection */
1723 smbfs_connections[i].cli =
1724 smbfs_do_connect (host, smbfs_connections[i].service);
1728 g_free (host);
1729 g_free (user);
1732 static int
1733 smbfs_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg)
1735 DEBUG (3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path, ctlop));
1736 switch (ctlop) {
1737 case VFS_SETCTL_FORGET:
1738 smbfs_forget (path);
1739 return 0;
1741 return 0;
1745 static smbfs_handle *
1746 smbfs_open_readwrite (smbfs_handle *remote_handle, char *rname, int flags, int mode)
1748 size_t size;
1750 if (flags & O_TRUNC) /* if it exists truncate to zero */
1751 DEBUG (3, ("smbfs_open: O_TRUNC\n"));
1753 remote_handle->fnum =
1754 #if 1 /* Don't play with flags, it is cli_open() headache */
1755 cli_open (remote_handle->cli, rname, flags, DENY_NONE);
1756 #else /* What's a reasons to has this code ? */
1757 cli_open (remote_handle->cli, rname, ((flags & O_CREAT)
1758 || (flags ==
1759 (O_WRONLY | O_APPEND))) ?
1760 flags : O_RDONLY, DENY_NONE);
1761 #endif
1762 if (remote_handle->fnum == -1) {
1763 message (1, MSG_ERROR, _(" %s opening remote file %s "),
1764 cli_errstr (remote_handle->cli), CNV_LANG (rname));
1765 DEBUG (1, ("smbfs_open(rname:%s) error:%s\n",
1766 rname, cli_errstr (remote_handle->cli)));
1767 my_errno = cli_error (remote_handle->cli, NULL, &err, NULL);
1768 return NULL;
1771 if (flags & O_CREAT)
1772 return remote_handle;
1774 if (!cli_qfileinfo (remote_handle->cli, remote_handle->fnum,
1775 &remote_handle->attr, &size, NULL, NULL, NULL, NULL,
1776 NULL)
1777 && !cli_getattrE (remote_handle->cli, remote_handle->fnum,
1778 &remote_handle->attr, &size, NULL, NULL, NULL)) {
1779 message (1, MSG_ERROR, " getattrib: %s ",
1780 cli_errstr (remote_handle->cli));
1781 DEBUG (1,
1782 ("smbfs_open(rname:%s) getattrib:%s\n", rname,
1783 cli_errstr (remote_handle->cli)));
1784 my_errno = cli_error (remote_handle->cli, NULL, &err, NULL);
1785 cli_close (remote_handle->cli, remote_handle->fnum);
1786 return NULL;
1789 if ((flags == (O_WRONLY | O_APPEND)) /* file.c:copy_file_file() -> do_append */
1790 && smbfs_lseek (remote_handle, 0, SEEK_END) == -1) {
1791 cli_close (remote_handle->cli, remote_handle->fnum);
1792 return NULL;
1795 return remote_handle;
1798 static void *
1799 smbfs_open (struct vfs_class *me, const char *file, int flags, int mode)
1801 char *remote_file;
1802 void *ret;
1803 smbfs_connection *sc;
1804 smbfs_handle *remote_handle;
1806 DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%o)\n", file, flags, mode));
1808 if (!(remote_file = smbfs_get_path (&sc, file)))
1809 return 0;
1811 remote_file = free_after(smbfs_convert_path (remote_file, FALSE), remote_file);
1813 remote_handle = g_new (smbfs_handle, 2);
1814 remote_handle->cli = sc->cli;
1815 remote_handle->nread = 0;
1817 ret = smbfs_open_readwrite (remote_handle, remote_file, flags, mode);
1819 g_free (remote_file);
1820 if (!ret)
1821 g_free (remote_handle);
1823 return ret;
1826 static int
1827 smbfs_unlink (struct vfs_class *me, const char *path)
1829 smbfs_connection *sc;
1830 char *remote_file;
1832 if ((remote_file = smbfs_get_path (&sc, path)) == 0)
1833 return -1;
1835 remote_file = free_after(smbfs_convert_path (remote_file, FALSE), remote_file);
1837 if (!cli_unlink(sc->cli, remote_file)) {
1838 message (1, MSG_ERROR, _(" %s removing remote file %s "),
1839 cli_errstr(sc->cli), CNV_LANG(remote_file));
1840 g_free (remote_file);
1841 return -1;
1843 g_free (remote_file);
1844 return 0;
1847 static int
1848 smbfs_rename (struct vfs_class *me, const char *a, const char *b)
1850 smbfs_connection *sc;
1851 char *ra, *rb;
1852 int retval;
1854 if ((ra = smbfs_get_path (&sc, a)) == 0)
1855 return -1;
1857 if ((rb = smbfs_get_path (&sc, b)) == 0) {
1858 g_free (ra);
1859 return -1;
1862 ra = free_after (smbfs_convert_path (ra, FALSE), ra);
1863 rb = free_after (smbfs_convert_path (rb, FALSE), rb);
1865 retval = cli_rename(sc->cli, ra, rb);
1867 g_free (ra);
1868 g_free (rb);
1870 if (!retval) {
1871 message (1, MSG_ERROR, _(" %s renaming files\n"),
1872 cli_errstr(sc->cli));
1873 return -1;
1875 return 0;
1878 static int
1879 smbfs_fstat (void *data, struct stat *buf)
1881 smbfs_handle *remote_handle = (smbfs_handle *)data;
1883 DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle->fnum));
1885 /* use left over from previous smbfs_get_remote_stat, if available */
1886 if (single_entry)
1887 memcpy(buf, &single_entry->my_stat, sizeof(struct stat));
1888 else { /* single_entry not set up: bug */
1889 my_errno = EFAULT;
1890 return -EFAULT;
1892 return 0;
1895 void
1896 init_smbfs (void)
1898 vfs_smbfs_ops.name = "smbfs";
1899 vfs_smbfs_ops.prefix = "smb:";
1900 vfs_smbfs_ops.flags = VFSF_NOLINKS;
1901 vfs_smbfs_ops.init = smbfs_init;
1902 vfs_smbfs_ops.fill_names = smbfs_fill_names;
1903 vfs_smbfs_ops.open = smbfs_open;
1904 vfs_smbfs_ops.close = smbfs_close;
1905 vfs_smbfs_ops.read = smbfs_read;
1906 vfs_smbfs_ops.write = smbfs_write;
1907 vfs_smbfs_ops.opendir = smbfs_opendir;
1908 vfs_smbfs_ops.readdir = smbfs_readdir;
1909 vfs_smbfs_ops.closedir = smbfs_closedir;
1910 vfs_smbfs_ops.stat = smbfs_stat;
1911 vfs_smbfs_ops.lstat = smbfs_lstat;
1912 vfs_smbfs_ops.fstat = smbfs_fstat;
1913 vfs_smbfs_ops.chmod = smbfs_chmod;
1914 vfs_smbfs_ops.chown = smbfs_chown;
1915 vfs_smbfs_ops.utime = smbfs_utime;
1916 vfs_smbfs_ops.readlink = smbfs_readlink;
1917 vfs_smbfs_ops.symlink = smbfs_symlink;
1918 vfs_smbfs_ops.link = smbfs_link;
1919 vfs_smbfs_ops.unlink = smbfs_unlink;
1920 vfs_smbfs_ops.rename = smbfs_rename;
1921 vfs_smbfs_ops.chdir = smbfs_chdir;
1922 vfs_smbfs_ops.ferrno = smbfs_errno;
1923 vfs_smbfs_ops.lseek = smbfs_lseek;
1924 vfs_smbfs_ops.mknod = smbfs_mknod;
1925 vfs_smbfs_ops.free = smbfs_free;
1926 vfs_smbfs_ops.mkdir = smbfs_mkdir;
1927 vfs_smbfs_ops.rmdir = smbfs_rmdir;
1928 vfs_smbfs_ops.setctl = smbfs_setctl;
1929 vfs_register_class (&vfs_smbfs_ops);