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>
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public License
11 as published by the Free Software Foundation; either version 2 of
12 the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public
20 License along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* Namespace: exports smbfs_vfs_ops */
25 #include <sys/types.h>
29 #include "samba/include/config.h"
30 /* don't load crap in "samba/include/includes.h" we don't use and which
31 conflicts with definitions in other includes */
32 #undef HAVE_LIBREADLINE
36 #include "samba/include/includes.h"
43 #include "../src/dialog.h"
45 #define SMBFS_MAX_CONNECTIONS 16
46 static const char * const IPC
= "IPC$";
47 static const char * const URL_HEADER
= "/#smb:";
53 /* stuff that is same with each connection */
54 extern int DEBUGLEVEL
;
55 static pstring myhostname
;
56 static mode_t myumask
= 0755;
57 extern pstring global_myname
;
58 static int smbfs_open_connections
= 0;
59 static gboolean got_user
= FALSE
;
60 static gboolean got_pass
= FALSE
;
61 static pstring password
;
62 static pstring username
;
64 static struct _smbfs_connection
{
65 struct cli_state
*cli
;
66 struct in_addr dest_ip
;
68 char *host
; /* server name */
69 char *service
; /* share name */
77 } smbfs_connections
[SMBFS_MAX_CONNECTIONS
];
78 /* unique to each connection */
80 typedef struct _smbfs_connection smbfs_connection
;
81 static smbfs_connection
*current_bucket
;
84 struct cli_state
*cli
;
90 static GSList
*auth_list
;
93 authinfo_free (struct smb_authinfo
const *a
)
99 wipe_password (a
->password
);
106 g_slist_foreach (auth_list
, (GFunc
)authinfo_free
, 0);
107 g_slist_free (auth_list
);
113 authinfo_compare_host_and_share (gconstpointer _a
, gconstpointer _b
)
115 struct smb_authinfo
const *a
= (struct smb_authinfo
const *)_a
;
116 struct smb_authinfo
const *b
= (struct smb_authinfo
const *)_b
;
118 if (!a
->host
|| !a
->share
|| !b
->host
|| !b
->share
)
120 if (strcmp (a
->host
, b
->host
) != 0)
122 if (strcmp (a
->share
, b
->share
) != 0)
128 authinfo_compare_host (gconstpointer _a
, gconstpointer _b
)
130 struct smb_authinfo
const *a
= (struct smb_authinfo
const *)_a
;
131 struct smb_authinfo
const *b
= (struct smb_authinfo
const *)_b
;
133 if (!a
->host
|| !b
->host
)
135 if (strcmp (a
->host
, b
->host
) != 0)
137 if (strcmp (a
->share
, IPC
) != 0)
143 authinfo_add (const char *host
, const char *share
, const char *domain
,
144 const char *user
, const char *password
)
146 struct smb_authinfo
*auth
= g_new (struct smb_authinfo
, 1);
151 /* Don't check for NULL, g_strdup already does. */
152 auth
->host
= g_strdup (host
);
153 auth
->share
= g_strdup (share
);
154 auth
->domain
= g_strdup (domain
);
155 auth
->user
= g_strdup (user
);
156 auth
->password
= g_strdup (password
);
157 auth_list
= g_slist_prepend (auth_list
, auth
);
161 authinfo_remove (const char *host
, const char *share
)
163 struct smb_authinfo data
;
164 struct smb_authinfo
*auth
;
167 data
.host
= g_strdup (host
);
168 data
.share
= g_strdup (share
);
169 list
= g_slist_find_custom (auth_list
,
171 authinfo_compare_host_and_share
);
177 auth_list
= g_slist_remove (auth_list
, auth
);
178 authinfo_free (auth
);
181 /* Set authentication information in bucket. Return 1 if successful, else 0 */
182 /* Information in auth_list overrides user if pass is NULL. */
183 /* bucket->host and bucket->service must be valid. */
185 bucket_set_authinfo (smbfs_connection
*bucket
,
186 const char *domain
, const char *user
, const char *pass
,
187 int fallback_to_host
)
189 struct smb_authinfo data
;
190 struct smb_authinfo
*auth
;
193 if (domain
&& user
&& pass
) {
194 g_free (bucket
->domain
);
195 g_free (bucket
->user
);
196 g_free (bucket
->password
);
197 bucket
->domain
= g_strdup (domain
);
198 bucket
->user
= g_strdup (user
);
199 bucket
->password
= g_strdup (pass
);
200 authinfo_remove (bucket
->host
, bucket
->service
);
201 authinfo_add (bucket
->host
, bucket
->service
,
206 data
.host
= bucket
->host
;
207 data
.share
= bucket
->service
;
208 list
= g_slist_find_custom (auth_list
, &data
, authinfo_compare_host_and_share
);
209 if (!list
&& fallback_to_host
)
210 list
= g_slist_find_custom (auth_list
, &data
, authinfo_compare_host
);
213 bucket
->domain
= g_strdup (auth
->domain
);
214 bucket
->user
= g_strdup (auth
->user
);
215 bucket
->password
= g_strdup (auth
->password
);
220 bucket
->domain
= g_strdup (lp_workgroup ());
221 bucket
->user
= g_strdup (got_user
? username
: user
);
222 bucket
->password
= g_strdup (password
);
226 auth
= vfs_smb_get_authinfo (bucket
->host
,
228 (domain
? domain
: lp_workgroup ()),
231 g_free (bucket
->domain
);
232 g_free (bucket
->user
);
233 g_free (bucket
->password
);
234 bucket
->domain
= g_strdup (auth
->domain
);
235 bucket
->user
= g_strdup (auth
->user
);
236 bucket
->password
= g_strdup (auth
->password
);
237 authinfo_remove (bucket
->host
, bucket
->service
);
238 auth_list
= g_slist_prepend (auth_list
, auth
);
245 smbfs_set_debug(int arg
)
250 /********************** The callbacks ******************************/
254 char *servicesf
= "/etc/smb.conf";
256 /* DEBUGLEVEL = 4; */
258 setup_logging("mc", True
);
260 charset_initialise();
262 DEBUG(3, ("smbfs_init(%s)\n", me
->name
));
264 if(!get_myname(myhostname
,NULL
))
265 DEBUG(0,("Failed to get my hostname.\n"));
267 if (!lp_load(servicesf
,True
,False
,False
))
268 DEBUG(0, ("Cannot load %s - run testparm to debug it\n", servicesf
));
270 codepage_initialise(lp_client_code_page());
276 if (getenv("USER")) {
278 pstrcpy(username
, getenv("USER"));
280 DEBUG(3, ("smbfs_init(): $USER:%s\n", username
));
281 if ((p
= strchr(username
, '%'))) {
283 pstrcpy(password
, p
+1);
285 memset(strchr(getenv("USER"), '%')+1, 'X', strlen(password
));
286 DEBUG(3, ("smbfs_init(): $USER%%pass: %s%%%s\n",
287 username
, password
));
291 if (getenv("PASSWD")) {
292 pstrcpy(password
, getenv("PASSWD"));
299 smbfs_fill_names (vfs
*me
, void (*func
)(char *))
303 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
304 if (smbfs_connections
[i
].cli
) {
305 path
= g_strconcat (URL_HEADER
,
306 smbfs_connections
[i
].host
,
307 "/", smbfs_connections
[i
].service
,
315 #define CNV_LANG(s) dos_to_unix(s,False)
316 #define GNAL_VNC(s) unix_to_dos(s,False)
317 /* does same as do_get() in client.c */
318 /* called from vfs.c:1080, count = buffer size */
320 smbfs_read (void *data
, char *buffer
, int count
)
322 smbfs_handle
*info
= (smbfs_handle
*) data
;
325 DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%d)\n",
326 info
->fnum
, (int)info
->nread
, count
));
327 n
= cli_read(info
->cli
, info
->fnum
, buffer
, info
->nread
, count
);
334 smbfs_write (void *data
, char *buf
, int nbyte
)
336 smbfs_handle
*info
= (smbfs_handle
*) data
;
339 DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%d)\n",
340 info
->fnum
, (int)info
->nread
, nbyte
));
341 n
= cli_write(info
->cli
, info
->fnum
, 0, buf
, info
->nread
, nbyte
);
348 smbfs_close (void *data
)
350 smbfs_handle
*info
= (smbfs_handle
*) data
;
351 DEBUG(3, ("smbfs_close(fnum:%d)\n", info
->fnum
));
352 /* if imlementing archive_level: add rname to smbfs_handle
353 if (archive_level >= 2 && (inf->attr & aARCH)) {
354 cli_setatr(info->cli, rname, info->attr & ~(uint16)aARCH, 0);
356 return (cli_close(info
->cli
, info
->fnum
) == True
) ? 0 : -1;
360 smbfs_errno (vfs
*me
)
362 DEBUG(3, ("smbfs_errno: %s\n", g_strerror(my_errno
)));
366 typedef struct dir_entry
{
368 struct dir_entry
*next
;
374 gboolean server_list
;
376 char *path
; /* the dir originally passed to smbfs_opendir */
377 smbfs_connection
*conn
;
386 *current_server_info
;
388 static gboolean first_direntry
;
391 new_dir_entry (const char * name
)
393 dir_entry
*new_entry
;
394 new_entry
= g_new0 (dir_entry
, 1);
395 new_entry
->text
= dos_to_unix (g_strdup (name
), 1);
397 if (first_direntry
) {
398 current_info
->entries
= new_entry
;
399 first_direntry
= FALSE
;
401 current_info
->current
->next
= new_entry
;
403 current_info
->current
= new_entry
;
408 /* browse for shares on server */
410 browsing_helper(const char *name
, uint32 type
, const char *comment
)
414 dir_entry
*new_entry
= new_dir_entry (name
);
419 /* show this as dir */
420 new_entry
->my_stat
.st_mode
= S_IFDIR
|S_IRUSR
|S_IRGRP
|S_IROTH
;
423 typestr
= "Printer"; break;
425 typestr
= "Device"; break;
427 typestr
= "IPC"; break;
429 DEBUG(3, ("\t%-15.15s%-10.10s%s\n", name
, typestr
, comment
));
433 loaddir_helper(file_info
*finfo
, const char *mask
)
435 dir_entry
*new_entry
;
436 time_t t
= finfo
->mtime
; /* the time is assumed to be passed as GMT */
437 #if 0 /* I want to see dot files */
438 if (finfo
->mode
& aHIDDEN
)
439 return; /* don't bother with hidden files, "~$" screws up mc */
441 new_entry
= new_dir_entry (finfo
->name
);
443 new_entry
->my_stat
.st_size
= finfo
->size
;
444 new_entry
->my_stat
.st_mtime
= finfo
->mtime
;
445 new_entry
->my_stat
.st_atime
= finfo
->atime
;
446 new_entry
->my_stat
.st_ctime
= finfo
->ctime
;
447 new_entry
->my_stat
.st_uid
= finfo
->uid
;
448 new_entry
->my_stat
.st_gid
= finfo
->gid
;
451 new_entry
->my_stat
.st_mode
=
452 S_IRUSR
|S_IRGRP
|S_IROTH
|S_IWUSR
|S_IWGRP
|S_IWOTH
;
454 /* if (finfo->mode & aVOLID); nothing similar in real world */
455 if (finfo
->mode
& aDIR
)
456 new_entry
->my_stat
.st_mode
|= S_IFDIR
;
458 new_entry
->my_stat
.st_mode
|= S_IFREG
; /* if not dir, regular file? */
459 /* if (finfo->mode & aARCH); DOS archive */
460 /* if (finfo->mode & aHIDDEN); like a dot file? */
461 /* if (finfo->mode & aSYSTEM); like a kernel? */
462 if (finfo
->mode
& aRONLY
)
463 new_entry
->my_stat
.st_mode
&= ~(S_IRUSR
|S_IRGRP
|S_IROTH
);
465 DEBUG(3, (" %-30s%7.7s%8.0f %s",
466 CNV_LANG(finfo
->name
),
467 attrib_string(finfo
->mode
),
469 asctime(LocalTime(&t
))));
472 /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
474 convert_path(char **remote_file
, gboolean trailing_asterik
)
478 my_remote
= *remote_file
;
479 if (strncmp (my_remote
, URL_HEADER
, HEADER_LEN
) == 0) { /* if passed directly */
481 if (*my_remote
== '/') /* from server browsing */
483 p
= strchr(my_remote
, '/');
485 my_remote
= p
+1; /* advance to end of server name */
488 if (*my_remote
== '/')
489 my_remote
++; /* strip off leading '/' */
490 p
= strchr(my_remote
, '/');
492 my_remote
= p
; /* strip off share/service name */
493 /* create remote filename as understood by smb clientgen */
494 p
= *remote_file
= g_strconcat (my_remote
, trailing_asterik
? "/*" : "", 0);
495 unix_to_dos (*remote_file
, 1);
496 while ((p
= strchr(p
, '/')))
502 server_browsing_helper(const char *name
, uint32 m
, const char *comment
)
504 dir_entry
*new_entry
= new_dir_entry (name
);
506 /* show this as dir */
507 new_entry
->my_stat
.st_mode
= S_IFDIR
|S_IRUSR
|S_IRGRP
|S_IROTH
;
509 DEBUG(3, ("\t%-16.16s %s\n", name
, comment
));
513 reconnect(smbfs_connection
*conn
, int *retries
)
516 DEBUG(3, ("RECONNECT\n"));
518 if (*(conn
->host
) == 0)
519 host
= g_strdup(conn
->cli
->desthost
); /* server browsing */
521 host
= g_strdup(conn
->host
);
523 cli_shutdown(conn
->cli
);
525 if (!(conn
->cli
= do_connect(host
, conn
->service
))) {
526 message_2s (1, MSG_ERROR
,
527 _(" reconnect to %s failed\n "), conn
->host
);
532 if (++(*retries
) == 2)
538 smb_send(struct cli_state
*cli
)
544 len
= smb_len(cli
->outbuf
) + 4;
546 while (nwritten
< len
) {
547 ret
= write_socket(cli
->fd
, cli
->outbuf
+nwritten
, len
- nwritten
);
548 if (ret
<= 0 && errno
== EPIPE
)
556 /****************************************************************************
557 See if server has cut us off by checking for EPIPE when writing.
558 Taken from cli_chkpath()
559 ****************************************************************************/
561 chkpath(struct cli_state
*cli
, char *path
, BOOL send_only
)
567 unix_to_dos (path2
, 1);
568 trim_string(path2
,NULL
,"\\");
569 if (!*path2
) *path2
= '\\';
571 memset(cli
->outbuf
,'\0',smb_size
);
572 set_message(cli
->outbuf
,0,4 + strlen(path2
),True
);
573 SCVAL(cli
->outbuf
,smb_com
,SMBchkpth
);
574 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
578 SSVAL(cli
->outbuf
,smb_pid
,cli
->pid
);
579 SSVAL(cli
->outbuf
,smb_uid
,cli
->vuid
);
580 SSVAL(cli
->outbuf
,smb_mid
,cli
->mid
);
581 if (cli
->protocol
> PROTOCOL_CORE
) {
582 SCVAL(cli
->outbuf
,smb_flg
,0x8);
583 SSVAL(cli
->outbuf
,smb_flg2
,0x1);
586 p
= smb_buf(cli
->outbuf
);
590 if (!smb_send(cli
)) {
591 DEBUG(3, ("chkpath: couldnt send\n"));
595 client_receive_smb(cli
->fd
, cli
->inbuf
, cli
->timeout
);
596 DEBUG(3, ("chkpath: send only OK\n"));
597 return True
; /* just testing for EPIPE */
599 if (!client_receive_smb(cli
->fd
, cli
->inbuf
, cli
->timeout
)) {
600 DEBUG(3, ("chkpath: receive error\n"));
603 if ((my_errno
= cli_error(cli
, NULL
, NULL
, NULL
))) {
604 if (my_errno
== 20 || my_errno
== 13)
605 return True
; /* ignore if 'not a directory' error */
606 DEBUG(3, ("chkpath: cli_error: %s\n", g_strerror(my_errno
)));
615 fs (const char *text
)
617 const char *p
= text
;
620 while ((p
= strchr(p
, '/')) != NULL
) {
631 smbfs_loaddir (opendir_info
*smbfs_info
)
633 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
634 int servlen
= strlen(smbfs_info
->conn
->service
);
635 char *my_dirname
= smbfs_info
->dirname
;
637 DEBUG(3, ("smbfs_loaddir: dirname:%s\n", my_dirname
));
638 first_direntry
= TRUE
;
641 DEBUG(3, ("smbfs_loaddir: new:'%s', cached:'%s'\n", my_dirname
, current_info
->dirname
));
642 /* if new desired dir is longer than cached in current_info */
643 if (fs(my_dirname
) > fs(current_info
->dirname
)) {
644 DEBUG(3, ("saving to previous_info\n"));
645 previous_info
= current_info
;
649 current_info
= smbfs_info
;
651 if (strcmp(my_dirname
, "/") == 0) {
652 if (!strcmp(smbfs_info
->path
, URL_HEADER
)) {
653 DEBUG(6, ("smbfs_loaddir: browsing %s\n", IPC
));
654 /* browse for servers */
655 if (!cli_NetServerEnum(smbfs_info
->conn
->cli
, smbfs_info
->conn
->domain
,
656 SV_TYPE_ALL
, server_browsing_helper
))
659 current_server_info
= smbfs_info
;
660 smbfs_info
->server_list
= TRUE
;
662 /* browse for shares */
663 if (cli_RNetShareEnum(smbfs_info
->conn
->cli
, browsing_helper
) < 1)
666 current_share_info
= smbfs_info
;
671 /* do regular directory listing */
672 if(strncmp(smbfs_info
->conn
->service
, my_dirname
+1, servlen
) == 0) {
673 /* strip share name from dir */
674 char *p
= my_dirname
= g_strdup(my_dirname
+ servlen
);
676 convert_path(&my_dirname
, TRUE
);
679 convert_path(&my_dirname
, TRUE
);
681 DEBUG(6, ("smbfs_loaddir: service: %s\n", smbfs_info
->conn
->service
));
682 DEBUG(6, ("smbfs_loaddir: cli->share: %s\n", smbfs_info
->conn
->cli
->share
));
683 DEBUG(6, ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname
));
684 /* do file listing: cli_list returns number of files */
686 smbfs_info
->conn
->cli
, my_dirname
, attribute
, loaddir_helper
) < 0) {
687 /* cli_list returns -1 if directory empty or cannot read socket */
688 my_errno
= cli_error(smbfs_info
->conn
->cli
, NULL
, &err
, NULL
);
692 if (*(my_dirname
) == 0)
693 smbfs_info
->dirname
= smbfs_info
->conn
->service
;
698 /* current_info->parent = smbfs_info->dirname; */
700 smbfs_info
->current
= smbfs_info
->entries
;
701 return 1; /* 1 = ok */
704 #ifdef SMBFS_FREE_DIR
706 smbfs_free_dir (dir_entry
*de
)
710 smbfs_free_dir (de
->next
);
717 * On some operating systems (Slowaris 2 for example)
718 * the d_name member is just a char long (Nice trick that break everything,
719 * so we need to set up some space for the filename.
723 #ifdef NEED_EXTRA_DIRENT_BUFFER
724 char extra_buffer
[MC_MAXPATHLEN
];
726 } smbfs_readdir_data
;
729 /* The readdir routine loads the complete directory */
730 /* It's too slow to ask the server each time */
731 /* It now also sends the complete lstat information for each file */
733 smbfs_readdir (void *info
)
735 static char * const dirent_dest
= &(smbfs_readdir_data
.dent
.d_name
[0]);
736 opendir_info
*smbfs_info
= (opendir_info
*) info
;
738 DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info
->dirname
));
740 if (!smbfs_info
->entries
)
741 if (!smbfs_loaddir (smbfs_info
))
744 if (smbfs_info
->current
== 0) { /* reached end of dir entries */
745 DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
746 #ifdef SMBFS_FREE_DIR
747 smbfs_free_dir (smbfs_info
->entries
);
748 smbfs_info
->entries
= 0;
752 pstrcpy (dirent_dest
, smbfs_info
->current
->text
);
753 smbfs_info
->current
= smbfs_info
->current
->next
;
755 #ifndef DIRENT_LENGTH_COMPUTED
756 smbfs_readdir_data
.dent
.d_namlen
= strlen (smbfs_readdir_data
.dent
.d_name
);
759 return &smbfs_readdir_data
;
763 smbfs_closedir (void *info
)
765 opendir_info
*smbfs_info
= (opendir_info
*) info
;
766 /* dir_entry *p, *q; */
768 DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info
->dirname
));
771 /* for (p = smbfs_info->entries; p;){
782 smbfs_chmod (vfs
*me
, char *path
, int mode
)
784 DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path
, mode
));
785 /* my_errno = EOPNOTSUPP;
786 return -1; */ /* cant chmod on smb filesystem */
787 return 0; /* make mc happy */
791 smbfs_chown (vfs
*me
, char *path
, int owner
, int group
)
793 DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path
, owner
, group
));
794 my_errno
= EOPNOTSUPP
; /* ready for your labotomy? */
799 smbfs_utime (vfs
*me
, char *path
, struct utimbuf
*times
)
801 DEBUG(3, ("smbfs_utime(path:%s)\n", path
));
802 my_errno
= EOPNOTSUPP
;
807 smbfs_readlink (vfs
*me
, char *path
, char *buf
, int size
)
809 DEBUG(3, ("smbfs_readlink(path:%s, buf:%s, size:%d)\n", path
, buf
, size
));
810 my_errno
= EOPNOTSUPP
;
811 return -1; /* no symlinks on smb filesystem? */
815 smbfs_symlink (vfs
*me
, char *n1
, char *n2
)
817 DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1
, n2
));
818 my_errno
= EOPNOTSUPP
;
819 return -1; /* no symlinks on smb filesystem? */
822 /* Extract the hostname and username from the path */
823 /* path is in the form: hostname:user/remote-dir */
826 smbfs_get_host_and_username
827 (char **path
, char **host
, char **user
, int *port
, char **pass
)
832 ret
= vfs_split_url (*path
, host
, user
, port
, pass
, SMB_PORT
, 0);
835 if ((p
= strchr(*path
, '@'))) /* user:pass@server */
836 *path
= ++p
; /* don't want user:pass@ in path */
837 if ((p
= strchr(ret
, '@'))) /* user:pass@server */
838 ret
= ++p
; /* don't want user:pass@ in path */
844 #define smbfs_get_host_and_username(path, host, user, port, pass) \
845 vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
847 /*****************************************************
848 return a connection to a SMB server
849 current_bucket needs to be set before calling
850 *******************************************************/
852 do_connect (char *server
, char *share
)
855 struct nmb_name called
, calling
;
857 extern struct in_addr ipzero
;
859 DEBUG(3, ("do_connect(%s, %s)\n", server
, share
));
860 if (*share
== '\\') {
862 share
= strchr(server
,'\\');
863 if (!share
) return NULL
;
870 make_nmb_name(&calling
, global_myname
, 0x0, "");
871 make_nmb_name(&called
, server
, current_bucket
->name_type
, "");
876 if (current_bucket
->have_ip
) ip
= current_bucket
->dest_ip
;
878 /* have to open a new connection */
879 if (!(c
= cli_initialise(NULL
))) {
884 pwd_init(&(c
->pwd
)); /* should be moved into cli_initialise()? */
885 pwd_set_cleartext(&(c
->pwd
), current_bucket
->password
);
887 if ((cli_set_port(c
, current_bucket
->port
) == 0) ||
888 !cli_connect(c
, server
, &ip
)) {
889 DEBUG(1, ("Connection to %s failed\n", server
));
893 if (!cli_session_request(c
, &calling
, &called
)) {
894 my_errno
= cli_error(c
, NULL
, &err
, NULL
);
895 DEBUG(1, ("session request to %s failed\n", called
.name
));
897 if (strcmp(called
.name
, "*SMBSERVER")) {
898 make_nmb_name(&called
, "*SMBSERVER", 0x20, "");
904 DEBUG(3, (" session request ok\n"));
906 if (!cli_negprot(c
)) {
907 DEBUG(1, ("protocol negotiation failed\n"));
911 if (!cli_session_setup(c
, current_bucket
->user
,
912 current_bucket
->password
, strlen(current_bucket
->password
),
913 current_bucket
->password
, strlen(current_bucket
->password
),
914 current_bucket
->domain
)) {
915 DEBUG(1,("session setup failed: %s\n", cli_errstr(c
)));
916 authinfo_remove (server
, share
);
920 if (*c
->server_domain
|| *c
->server_os
|| *c
->server_type
)
921 DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
922 c
->server_domain
,c
->server_os
,c
->server_type
));
924 DEBUG(3, (" session setup ok\n"));
926 if (!cli_send_tconX(c
, share
, "?????",
927 current_bucket
->password
, strlen(current_bucket
->password
)+1)) {
928 DEBUG(1,("%s: tree connect failed: %s\n", share
, cli_errstr(c
)));
932 DEBUG(3, (" tconx ok\n"));
938 my_errno
= cli_error(c
, NULL
, &err
, NULL
);
945 get_master_browser(char **host
)
948 struct in_addr
*ip_list
, bcast_addr
, ipzero
;
949 /* does port = 137 for win95 master browser? */
950 int fd
= open_socket_in( SOCK_DGRAM
, 0, 3,
951 interpret_addr(lp_socket_address()), True
);
954 set_socket_options(fd
, "SO_BROADCAST");
955 ip_list
= iface_bcast(ipzero
);
956 bcast_addr
= *ip_list
;
957 if ((ip_list
= name_query(fd
, "\01\02__MSBROWSE__\02", 1, True
,
958 True
, bcast_addr
, &count
, NULL
))) {
961 /* just return first master browser */
962 *host
= g_strdup(inet_ntoa(ip_list
[0]));
969 free_bucket (smbfs_connection
*bucket
)
971 g_free (bucket
->host
);
972 g_free (bucket
->service
);
973 g_free (bucket
->domain
);
974 g_free (bucket
->user
);
975 wipe_password (bucket
->password
);
976 g_free (bucket
->home
);
977 bzero (bucket
, sizeof (smbfs_connection
));
980 static smbfs_connection
*
981 smbfs_get_free_bucket ()
985 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++)
986 if (!smbfs_connections
[i
].cli
) return &smbfs_connections
[i
];
988 { /* search for most dormant connection */
989 int oldest
= 0; /* index */
990 time_t oldest_time
= time(NULL
);
991 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
992 if (smbfs_connections
[i
].last_use
< oldest_time
) {
993 oldest_time
= smbfs_connections
[i
].last_use
;
997 cli_shutdown(smbfs_connections
[oldest
].cli
);
998 free_bucket (&smbfs_connections
[oldest
]);
999 return &smbfs_connections
[oldest
];
1002 /* This can't happend, since we have checked for max connections before */
1003 vfs_die("Internal error: smbfs_get_free_bucket");
1004 return 0; /* shut up, stupid gcc */
1007 /* This routine keeps track of open connections */
1008 /* Returns a connected socket to host */
1009 static smbfs_connection
*
1010 smbfs_open_link(char *host
, char *path
, const char *user
, int *port
, char *this_pass
)
1013 smbfs_connection
*bucket
;
1015 struct in_addr
*dest_ip
= NULL
;
1017 DEBUG(3, ("smbfs_open_link(host:%s, path:%s)\n", host
, path
));
1019 if (strcmp(host
, path
) == 0) /* if host & path are same: */
1020 pstrcpy(service
, IPC
); /* setup for browse */
1021 else { /* get share name from path, path starts with server name */
1023 if ((p
= strchr(path
, '/'))) /* get share aka */
1024 pstrcpy(service
, ++p
); /* service name from path */
1026 pstrcpy(service
, "");
1027 /* now check for trailing directory/filenames */
1028 p
= strchr(service
, '/');
1030 *p
= 0; /* cut off dir/files: sharename only */
1032 DEBUG(6, ("smbfs_open_link: service from path:%s\n", service
));
1036 user
= username
; /* global from getenv */
1038 /* Is the link actually open? */
1039 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
1040 if (!smbfs_connections
[i
].cli
)
1042 if ((strcmp (host
, smbfs_connections
[i
].host
) == 0) &&
1043 (strcmp (user
, smbfs_connections
[i
].user
) == 0) &&
1044 (strcmp (service
, smbfs_connections
[i
].service
) == 0)) {
1046 BOOL inshare
= (*host
!= 0 && *path
!= 0 && strchr(path
, '/'));
1047 /* check if this connection has died */
1048 while (!chkpath(smbfs_connections
[i
].cli
, "\\", !inshare
)) {
1049 if (!reconnect(&smbfs_connections
[i
], &retries
))
1052 DEBUG(6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i
));
1053 current_bucket
= &smbfs_connections
[i
];
1054 smbfs_connections
[i
].last_use
= time(NULL
);
1055 return &smbfs_connections
[i
];
1057 /* connection not found, find if we have ip for new connection */
1058 if (strcmp (host
, smbfs_connections
[i
].host
) == 0)
1059 dest_ip
= &smbfs_connections
[i
].cli
->dest_ip
;
1062 /* make new connection */
1063 bucket
= smbfs_get_free_bucket ();
1064 bucket
->name_type
= 0x20;
1066 bucket
->port
= *port
;
1067 bucket
->have_ip
= False
;
1069 bucket
->have_ip
= True
;
1070 bucket
->dest_ip
= *dest_ip
;
1072 current_bucket
= bucket
;
1074 bucket
->user
= g_strdup(user
);
1075 bucket
->service
= g_strdup (service
);
1077 if (!(*host
)) { /* if blank host name, browse for servers */
1078 if (!get_master_browser(&host
)) /* set host to ip of master browser */
1079 return 0; /* couldnt find master browser? */
1081 bucket
->host
= g_strdup(""); /* blank host means master browser */
1083 bucket
->host
= g_strdup(host
);
1085 if (!bucket_set_authinfo (bucket
,
1086 0, /* domain currently not used */
1092 /* connect to share */
1093 while (!(bucket
->cli
= do_connect(host
, service
))) {
1095 if (my_errno
!= EPERM
)
1097 message_1s (1, MSG_ERROR
,
1098 _(" Authentication failed "));
1100 /* authentication failed, try again */
1101 authinfo_remove (bucket
->host
, bucket
->service
);
1102 if (!bucket_set_authinfo (bucket
,
1111 smbfs_open_connections
++;
1112 DEBUG(3, ("smbfs_open_link:smbfs_open_connections: %d\n",
1113 smbfs_open_connections
));
1118 smbfs_get_path(smbfs_connection
**sc
, char *path
)
1120 char *user
, *host
, *remote_path
, *pass
;
1121 int port
= SMB_PORT
;
1123 DEBUG(3, ("smbfs_get_path(%s)\n", path
));
1124 if (strncmp (path
, URL_HEADER
, HEADER_LEN
))
1128 if (*path
== '/') /* '/' leading server name */
1129 path
++; /* probably came from server browsing */
1131 if ((remote_path
= smbfs_get_host_and_username(
1132 &path
, &host
, &user
, &port
, &pass
)))
1133 if ((*sc
= smbfs_open_link (host
, path
, user
, &port
, pass
)) == NULL
){
1134 g_free (remote_path
);
1139 if (pass
) wipe_password (pass
);
1141 if (!remote_path
) return NULL
;
1143 /* NOTE: tildes are deprecated. See ftpfs.c */
1145 int f
= !strcmp( remote_path
, "/~" );
1146 if (f
|| !strncmp( remote_path
, "/~/", 3 )) {
1148 s
= concat_dir_and_file( (*sc
)->home
, remote_path
+3-f
);
1149 g_free (remote_path
);
1158 is_error (int result
, int errno_num
)
1160 if (!(result
== -1))
1161 return my_errno
= 0;
1163 my_errno
= errno_num
;
1169 smbfs_opendir (vfs
*me
, char *dirname
)
1171 opendir_info
*smbfs_info
;
1172 smbfs_connection
*sc
;
1175 DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname
));
1177 if (!(remote_dir
= smbfs_get_path (&sc
, dirname
)))
1180 /* FIXME: where freed? */
1181 smbfs_info
= g_new (opendir_info
, 1);
1182 smbfs_info
->server_list
= FALSE
;
1183 smbfs_info
->path
= g_strdup(dirname
); /* keep original */
1184 smbfs_info
->dirname
= remote_dir
;
1185 smbfs_info
->conn
= sc
;
1186 smbfs_info
->entries
= 0;
1187 smbfs_info
->current
= 0;
1193 fake_server_stat(char *server_url
, const char *path
, struct stat
*buf
)
1198 if ((p
= strrchr(path
, '/')))
1199 path
= p
+ 1; /* advance until last '/' */
1201 if (!current_info
->entries
) {
1202 if (!smbfs_loaddir(current_info
)); /* browse host */
1205 dentry
= current_info
->entries
;
1206 DEBUG(4, ("fake stat for SERVER \"%s\"\n", path
));
1208 if (strcmp(dentry
->text
, path
) == 0) {
1209 DEBUG(4, ("fake_server_stat: %s:%d\n",
1210 dentry
->text
, dentry
->my_stat
.st_mode
));
1211 memcpy(buf
, &dentry
->my_stat
, sizeof(struct stat
));
1214 dentry
= dentry
->next
;
1221 fake_share_stat(char *server_url
, char *path
, struct stat
*buf
)
1224 if (strlen(path
) < strlen(server_url
))
1226 path
+= strlen(server_url
); /* we only want share name */
1229 if (*path
== '/') /* '/' leading server name */
1230 path
++; /* probably came from server browsing */
1232 if (!current_share_info
->entries
) {
1233 if (!smbfs_loaddir(current_share_info
)); /* browse host */
1236 dentry
= current_share_info
->entries
;
1237 DEBUG(3, ("fake_share_stat: %s on %s\n", path
, server_url
));
1239 if (strcmp(dentry
->text
, path
) == 0) {
1240 DEBUG(6, ("fake_share_stat: %s:%d\n",
1241 dentry
->text
, dentry
->my_stat
.st_mode
));
1242 memcpy(buf
, &dentry
->my_stat
, sizeof(struct stat
));
1245 dentry
= dentry
->next
;
1251 /* stat a single file, get_remote_stat callback */
1252 static dir_entry
*single_entry
;
1254 statfile_helper(file_info
*finfo
, const char *mask
)
1256 time_t t
= finfo
->mtime
; /* the time is assumed to be passed as GMT */
1258 #if 0 /* single_entry is never freed now. And only my_stat is used */
1259 single_entry
= g_new (dir_entry
, 1);
1261 single_entry
->text
= dos_to_unix (g_strdup (finfo
->name
), 1);
1263 single_entry
->next
= 0;
1266 single_entry
= g_new0 (dir_entry
, 1);
1268 single_entry
->my_stat
.st_size
= finfo
->size
;
1269 single_entry
->my_stat
.st_mtime
= finfo
->mtime
;
1270 single_entry
->my_stat
.st_atime
= finfo
->atime
;
1271 single_entry
->my_stat
.st_ctime
= finfo
->ctime
;
1272 single_entry
->my_stat
.st_uid
= finfo
->uid
;
1273 single_entry
->my_stat
.st_gid
= finfo
->gid
;
1275 single_entry
->my_stat
.st_mode
=
1276 S_IRUSR
|S_IRGRP
|S_IROTH
|S_IWUSR
|S_IWGRP
|S_IWOTH
;
1278 /* if (finfo->mode & aVOLID); nothing similar in real world */
1279 if (finfo
->mode
& aDIR
)
1280 single_entry
->my_stat
.st_mode
|= S_IFDIR
;
1282 single_entry
->my_stat
.st_mode
|= S_IFREG
;/* if not dir, regular file? */
1283 /* if (finfo->mode & aARCH); DOS archive */
1284 /* if (finfo->mode & aHIDDEN); like a dot file? */
1285 /* if (finfo->mode & aSYSTEM); like a kernel? */
1286 if (finfo
->mode
& aRONLY
)
1287 single_entry
->my_stat
.st_mode
&= ~(S_IRUSR
|S_IRGRP
|S_IROTH
);
1289 DEBUG(6, (" %-30s%7.7s%8.0f %s",
1290 CNV_LANG(finfo
->name
),
1291 attrib_string(finfo
->mode
),
1292 (double)finfo
->size
,
1293 asctime(LocalTime(&t
))));
1296 /* stat a single file */
1298 get_remote_stat(smbfs_connection
*sc
, char *path
, struct stat
*buf
)
1300 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
1301 char *mypath
= path
;
1303 DEBUG(3, ("get_remote_stat(): mypath:%s\n", mypath
));
1305 convert_path(&mypath
, FALSE
);
1307 if (cli_list(sc
->cli
, mypath
, attribute
, statfile_helper
) < 1) {
1310 return -1; /* cli_list returns number of files */
1313 memcpy(buf
, &single_entry
->my_stat
, sizeof(struct stat
));
1315 /* dont free here, use for smbfs_fstat() */
1316 /* g_free(single_entry->text);
1317 g_free(single_entry); */
1323 search_dir_entry (dir_entry
*dentry
, const char *text
, struct stat
*buf
)
1326 if (strcmp(text
, dentry
->text
) == 0) {
1327 memcpy(buf
, &dentry
->my_stat
, sizeof(struct stat
));
1328 memcpy(&single_entry
->my_stat
, &dentry
->my_stat
,
1329 sizeof(struct stat
));
1332 dentry
= dentry
->next
;
1338 get_stat_info (smbfs_connection
*sc
, char *path
, struct stat
*buf
)
1342 dir_entry
*dentry
= current_info
->entries
;
1344 const char *mypath
= path
;
1346 mypath
++; /* cut off leading '/' */
1347 if ((p
= strrchr(mypath
, '/')))
1348 mypath
= p
+ 1; /* advance until last file/dir name */
1349 DEBUG(3, ("get_stat_info: mypath:%s, current_info->dirname:%s\n",
1350 mypath
, current_info
->dirname
));
1353 DEBUG(1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n", current_info
->dirname
, path
));
1357 if (!single_entry
) /* when found, this will be written too */
1358 single_entry
= g_new (dir_entry
, 1);
1359 if (search_dir_entry(current_info
->entries
, mypath
, buf
) == 0) {
1362 /* now try to identify mypath as PARENT dir */
1366 mdp
= mydir
= g_strdup(current_info
->dirname
);
1367 if ((p
= strrchr(mydir
, '/')))
1368 *p
= 0; /* advance util last '/' */
1369 if ((p
= strrchr(mydir
, '/')))
1370 mydir
= p
+ 1; /* advance util last '/' */
1371 if (strcmp(mydir
, mypath
) == 0) { /* fake a stat for ".." */
1372 bzero(buf
, sizeof(struct stat
));
1373 buf
->st_mode
= S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
;
1374 memcpy(&single_entry
->my_stat
, buf
, sizeof(struct stat
));
1376 DEBUG(1, (" PARENT:found in %s\n", current_info
->dirname
));
1381 /* now try to identify as CURRENT dir? */
1383 char *dnp
= current_info
->dirname
;
1384 DEBUG(6, ("get_stat_info: is %s current dir? this dir is: %s\n",
1385 mypath
, current_info
->dirname
));
1391 if (strcmp(mypath
, dnp
) == 0) {
1392 bzero(buf
, sizeof(struct stat
));
1393 buf
->st_mode
= S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
;
1394 memcpy(&single_entry
->my_stat
, buf
, sizeof(struct stat
));
1395 DEBUG(1, (" CURRENT:found in %s\n", current_info
->dirname
));
1399 DEBUG(3, ("'%s' not found in current_info '%s'\n", path
, current_info
->dirname
));
1400 /* try to find this in the PREVIOUS listing */
1401 if (previous_info
) {
1402 if (search_dir_entry(previous_info
->entries
, mypath
, buf
) == 0)
1404 DEBUG(3, ("'%s' not found in previous_info '%s'\n", path
, previous_info
->dirname
));
1406 /* try to find this in the SHARE listing */
1407 if (current_share_info
&& search_dir_entry(current_share_info
->entries
, mypath
, buf
) == 0)
1409 DEBUG(3, ("'%s' not found in share_info '%s'\n", path
, current_share_info
->dirname
));
1410 /* try to find this in the SERVER listing */
1411 if (current_server_info
&& search_dir_entry(current_server_info
->entries
, mypath
, buf
) == 0)
1413 DEBUG(3, ("'%s' not found in server_info '%s'\n", path
, current_server_info
->dirname
));
1414 /* nothing found. get stat file info from server */
1415 return get_remote_stat(sc
, path
, buf
);
1419 smbfs_chdir (vfs
*me
, char *path
)
1422 smbfs_connection
*sc
;
1424 DEBUG(3, ("smbfs_chdir(path:%s)\n", path
));
1425 if (!(remote_dir
= smbfs_get_path (&sc
, path
)))
1427 g_free (remote_dir
);
1433 loaddir(vfs
*me
, const char *path
)
1437 mypath
= g_strdup(path
);
1441 while (strchr(p
, '/'))
1445 DEBUG(6, ("loaddir(%s)\n", mypath
));
1446 smbfs_chdir(me
, mypath
);
1447 info
= smbfs_opendir (me
, mypath
);
1451 smbfs_readdir(info
);
1452 smbfs_loaddir(info
);
1457 smbfs_stat (vfs
*me
, char *path
, struct stat
*buf
)
1460 smbfs_connection
*sc
;
1465 DEBUG(3, ("smbfs_stat(path:%s)\n", path
));
1468 if (p
= strchr(path
, '@')) /* user:pass@server */
1469 path
= ++p
; /* don't want user:pass@ in path */
1472 if (!current_info
) {
1473 DEBUG(1, ("current_info = NULL: "));
1474 if (loaddir(me
, path
) < 0)
1478 pstrcpy(server_url
, URL_HEADER
);
1479 pstrcat(server_url
, current_bucket
->host
);
1481 /* check if stating server */
1483 if (strncmp(p
, URL_HEADER
, HEADER_LEN
)) {
1484 DEBUG(1, ("'%s' doesnt start with '%s' (length %d)\n",
1485 p
, URL_HEADER
, HEADER_LEN
));
1492 p
= strchr(p
, '/'); /* advance past next '/' */
1494 if (!current_info
->server_list
) {
1495 if (loaddir(me
, path
) < 0)
1498 return fake_server_stat(server_url
, path
, buf
);
1500 if (!strchr(++p
, '/')) {
1501 return fake_share_stat(server_url
, path
, buf
);
1504 /* stating inside share at this point */
1505 if (!(remote_dir
= smbfs_get_path (&sc
, path
))) /* connects if necessary */
1507 g_free (remote_dir
);
1509 char *sp
, *pp
= path
;
1510 int hostlen
= strlen(current_bucket
->host
);
1511 pp
+= strlen(path
)-hostlen
;
1513 sp
+= strlen(server_url
)-hostlen
;
1514 if (strcmp(sp
, pp
) == 0) {
1515 /* make server name appear as directory */
1516 DEBUG(1, ("smbfs_stat: showing server as directory\n"));
1517 bzero(buf
, sizeof(struct stat
));
1518 buf
->st_mode
= S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
;
1522 /* check if current_info is in share requested */
1524 if (strncmp(p
, URL_HEADER
, HEADER_LEN
) == 0)
1528 p
= service
= g_strdup(p
);
1529 pp
= strchr(p
, '/');
1531 p
= ++pp
; /* advance past server name */
1532 pp
= strchr(p
, '/');
1534 *pp
= 0; /* cut off everthing after service name */
1536 p
= IPC
; /* browsing for services */
1537 pp
= current_info
->dirname
;
1540 if (strncmp(p
, pp
, strlen(p
)) != 0) {
1541 DEBUG(6, ("desired '%s' is not loaded, we have '%s'\n", p
, pp
));
1542 if (loaddir(me
, path
) < 0) {
1546 DEBUG(6, ("loaded dir: '%s'\n", current_info
->dirname
));
1549 /* stat dirs & files under shares now */
1550 return get_stat_info(sc
, path
, buf
);
1554 smbfs_lstat (vfs
*me
, char *path
, struct stat
*buf
)
1556 DEBUG(4, ("smbfs_lstat(path:%s)\n", path
));
1557 return smbfs_stat(me
, path
, buf
); /* no symlinks on smb filesystem? */
1561 smbfs_lseek (void *data
, off_t offset
, int whence
)
1563 DEBUG(3, ("smbfs_lseek()\n"));
1564 my_errno
= EOPNOTSUPP
;
1569 smbfs_mknod (vfs
*me
, char *path
, int mode
, int dev
)
1571 DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%d)\n", path
, mode
, dev
));
1572 my_errno
= EOPNOTSUPP
;
1577 smbfs_mkdir (vfs
*me
, char *path
, mode_t mode
)
1579 smbfs_connection
*sc
;
1582 DEBUG(3, ("smbfs_mkdir(path:%s, mode:%d)\n", path
, mode
));
1583 if ((remote_file
= smbfs_get_path (&sc
, path
)) == 0)
1585 g_free (remote_file
);
1586 convert_path(&path
, FALSE
);
1588 if (!cli_mkdir(sc
->cli
, path
)) {
1589 my_errno
= cli_error(sc
->cli
, NULL
, &err
, NULL
);
1590 message_3s (1, MSG_ERROR
, _(" %s mkdir'ing %s "),
1591 cli_errstr(sc
->cli
), CNV_LANG(path
));
1600 smbfs_rmdir (vfs
*me
, char *path
)
1602 smbfs_connection
*sc
;
1605 DEBUG(3, ("smbfs_rmdir(path:%s)\n", path
));
1606 if ((remote_file
= smbfs_get_path (&sc
, path
)) == 0)
1608 g_free (remote_file
);
1609 convert_path(&path
, FALSE
);
1611 if (!cli_rmdir(sc
->cli
, path
)) {
1612 my_errno
= cli_error(sc
->cli
, NULL
, &err
, NULL
);
1613 message_3s (1, MSG_ERROR
, _(" %s rmdir'ing %s "),
1614 cli_errstr(sc
->cli
), CNV_LANG(path
));
1624 smbfs_link (vfs
*me
, char *p1
, char *p2
)
1626 DEBUG(3, ("smbfs_link(p1:%s, p2:%s)\n", p1
, p2
));
1627 my_errno
= EOPNOTSUPP
;
1631 /* We do not free anything right now: we free resources when we run
1635 smbfs_getid (vfs
*me
, char *p
, struct vfs_stamping
**parent
)
1638 DEBUG(3, ("smbfs_getid(p:%s)\n", p
));
1644 smbfs_nothingisopen (vfsid id
)
1646 DEBUG(3, ("smbfs_nothingisopen(%d)\n", (int)id
));
1651 smbfs_free (vfsid id
)
1653 DEBUG(3, ("smbfs_free(%d)\n", (int)id
));
1654 /* FIXME: Should not be empty */
1655 authinfo_free_all ();
1658 /* Gives up on a socket and reopens the connection, the child own the socket
1662 my_forget (char *path
)
1664 char *host
, *user
, *p
;
1667 if (strncmp (path
, URL_HEADER
, HEADER_LEN
))
1670 DEBUG(3, ("my_forget(path:%s)\n", path
));
1673 if (path
[0] == '/' && path
[1] == '/')
1676 if ((p
= smbfs_get_host_and_username (&path
, &host
, &user
, &port
, NULL
))) {
1678 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
1679 if ((strcmp (host
, smbfs_connections
[i
].host
) == 0) &&
1680 (strcmp (user
, smbfs_connections
[i
].user
) == 0) &&
1681 (port
== smbfs_connections
[i
].port
)) {
1683 /* close socket: the child owns it now */
1684 cli_shutdown(smbfs_connections
[i
].cli
);
1686 /* reopen the connection */
1687 smbfs_connections
[i
].cli
=
1688 do_connect(host
, smbfs_connections
[i
].service
);
1697 smbfs_setctl (vfs
*me
, char *path
, int ctlop
, char *arg
)
1699 DEBUG(3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path
, ctlop
));
1701 case MCCTL_FORGET_ABOUT
:
1708 static smbfs_handle
*
1709 open_write (smbfs_handle
*remote_handle
, char *rname
, int flags
, int mode
)
1711 if (flags
& O_TRUNC
) /* if it exists truncate to zero */
1712 DEBUG(3, ("open_write: O_TRUNC\n"));
1714 remote_handle
->fnum
= cli_open(remote_handle
->cli
, rname
, flags
, DENY_NONE
);
1716 if (remote_handle
->fnum
== -1) {
1717 message_3s (1, MSG_ERROR
, _(" %s opening remote file %s "),
1718 cli_errstr(remote_handle
->cli
), CNV_LANG(rname
));
1719 DEBUG(1,("smbfs_open(rname:%s) error:%s\n",
1720 rname
, cli_errstr(remote_handle
->cli
)));
1721 my_errno
= cli_error(remote_handle
->cli
, NULL
, &err
, NULL
);
1725 return remote_handle
;
1728 static smbfs_handle
*
1729 open_read (smbfs_handle
*remote_handle
, char *rname
, int flags
, int mode
)
1733 remote_handle
->fnum
=
1734 cli_open(remote_handle
->cli
, rname
, O_RDONLY
, DENY_NONE
);
1736 if (remote_handle
->fnum
== -1) {
1737 message_3s (1, MSG_ERROR
, _(" %s opening remote file %s "),
1738 cli_errstr(remote_handle
->cli
), CNV_LANG(rname
));
1739 DEBUG(1,("smbfs_open(rname:%s) error:%s\n",
1740 rname
, cli_errstr(remote_handle
->cli
)));
1741 my_errno
= cli_error(remote_handle
->cli
, NULL
, &err
, NULL
);
1745 if (!cli_qfileinfo(remote_handle
->cli
, remote_handle
->fnum
,
1746 &remote_handle
->attr
, &size
, NULL
, NULL
, NULL
, NULL
, NULL
) &&
1747 !cli_getattrE(remote_handle
->cli
, remote_handle
->fnum
,
1748 &remote_handle
->attr
, &size
, NULL
, NULL
, NULL
)) {
1749 message_2s (1, MSG_ERROR
, " getattrib: %s ",
1750 cli_errstr(remote_handle
->cli
));
1751 DEBUG(1,("smbfs_open(rname:%s) getattrib:%s\n",
1752 rname
, cli_errstr(remote_handle
->cli
)));
1753 my_errno
= cli_error(remote_handle
->cli
, NULL
, &err
, NULL
);
1757 return remote_handle
;
1761 smbfs_open (vfs
*me
, char *file
, int flags
, int mode
)
1763 char *remote_file
, *p
;
1765 smbfs_connection
*sc
;
1766 smbfs_handle
*remote_handle
;
1768 DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%d)\n", file
, flags
, mode
));
1770 if (!(remote_file
= smbfs_get_path (&sc
, file
)))
1774 convert_path(&remote_file
, FALSE
);
1777 remote_handle
= g_new (smbfs_handle
, 2);
1778 remote_handle
->cli
= sc
->cli
;
1779 remote_handle
->nread
= 0;
1781 if (flags
& O_CREAT
)
1782 ret
= open_write(remote_handle
, remote_file
, flags
, mode
);
1784 ret
= open_read(remote_handle
, remote_file
, flags
, mode
);
1786 g_free (remote_file
);
1792 smbfs_unlink (vfs
*me
, char *path
)
1794 smbfs_connection
*sc
;
1795 char *remote_file
, *p
;
1797 if ((remote_file
= smbfs_get_path (&sc
, path
)) == 0)
1801 convert_path(&remote_file
, FALSE
);
1804 if (!cli_unlink(sc
->cli
, remote_file
)) {
1805 message_3s (1, MSG_ERROR
, _(" %s removing remote file %s "),
1806 cli_errstr(sc
->cli
), CNV_LANG(remote_file
));
1807 g_free (remote_file
);
1810 g_free (remote_file
);
1815 smbfs_rename (vfs
*me
, char *a
, char *b
)
1817 smbfs_connection
*sc
;
1822 if ((ra
= smbfs_get_path (&sc
, a
)) == 0)
1825 if ((rb
= smbfs_get_path (&sc
, b
)) == 0) {
1831 convert_path(&ra
, FALSE
);
1834 convert_path(&rb
, FALSE
);
1837 retval
= cli_rename(sc
->cli
, ra
, rb
);
1843 message_2s (1, MSG_ERROR
, _(" %s renaming files\n"),
1844 cli_errstr(sc
->cli
));
1851 smbfs_fstat (void *data
, struct stat
*buf
)
1853 smbfs_handle
*remote_handle
= (smbfs_handle
*)data
;
1855 DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle
->fnum
));
1857 /* use left over from previous get_remote_stat, if available */
1859 memcpy(buf
, &single_entry
->my_stat
, sizeof(struct stat
));
1860 else { /* single_entry not set up: bug */
1867 vfs vfs_smbfs_ops
= {
1868 NULL
, /* This is place of next pointer */
1871 "smb:", /* prefix */
1910 smbfs_nothingisopen
,