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 vfs_smbfs_ops, smbfs_set_debug */
26 #include <sys/types.h>
30 #undef PACKAGE_BUGREPORT
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
44 #include "samba/include/includes.h"
50 #include "../src/dialog.h"
52 #define SMBFS_MAX_CONNECTIONS 16
53 static const char * const IPC
= "IPC$";
54 static const char * const URL_HEADER
= "/#smb:";
60 /* stuff that is same with each connection */
61 extern int DEBUGLEVEL
;
62 extern pstring myhostname
;
63 static mode_t myumask
= 0755;
64 extern pstring global_myname
;
65 static int smbfs_open_connections
= 0;
66 static gboolean got_user
= FALSE
;
67 static gboolean got_pass
= FALSE
;
68 static pstring password
;
69 static pstring username
;
71 static struct _smbfs_connection
{
72 struct cli_state
*cli
;
73 struct in_addr dest_ip
;
75 char *host
; /* server name */
76 char *service
; /* share name */
84 } smbfs_connections
[SMBFS_MAX_CONNECTIONS
];
85 /* unique to each connection */
87 static struct cli_state
* smbfs_do_connect (const char *server
, char *share
);
89 typedef struct _smbfs_connection smbfs_connection
;
90 static smbfs_connection
*current_bucket
;
93 struct cli_state
*cli
;
99 static GSList
*auth_list
;
102 authinfo_free (struct smb_authinfo
const *a
)
108 wipe_password (a
->password
);
115 g_slist_foreach (auth_list
, (GFunc
)authinfo_free
, 0);
116 g_slist_free (auth_list
);
122 authinfo_compare_host_and_share (gconstpointer _a
, gconstpointer _b
)
124 struct smb_authinfo
const *a
= (struct smb_authinfo
const *)_a
;
125 struct smb_authinfo
const *b
= (struct smb_authinfo
const *)_b
;
127 if (!a
->host
|| !a
->share
|| !b
->host
|| !b
->share
)
129 if (strcmp (a
->host
, b
->host
) != 0)
131 if (strcmp (a
->share
, b
->share
) != 0)
137 authinfo_compare_host (gconstpointer _a
, gconstpointer _b
)
139 struct smb_authinfo
const *a
= (struct smb_authinfo
const *)_a
;
140 struct smb_authinfo
const *b
= (struct smb_authinfo
const *)_b
;
142 if (!a
->host
|| !b
->host
)
144 if (strcmp (a
->host
, b
->host
) != 0)
146 if (strcmp (a
->share
, IPC
) != 0)
152 authinfo_add (const char *host
, const char *share
, const char *domain
,
153 const char *user
, const char *password
)
155 struct smb_authinfo
*auth
= g_new (struct smb_authinfo
, 1);
160 /* Don't check for NULL, g_strdup already does. */
161 auth
->host
= g_strdup (host
);
162 auth
->share
= g_strdup (share
);
163 auth
->domain
= g_strdup (domain
);
164 auth
->user
= g_strdup (user
);
165 auth
->password
= g_strdup (password
);
166 auth_list
= g_slist_prepend (auth_list
, auth
);
170 authinfo_remove (const char *host
, const char *share
)
172 struct smb_authinfo data
;
173 struct smb_authinfo
*auth
;
176 data
.host
= g_strdup (host
);
177 data
.share
= g_strdup (share
);
178 list
= g_slist_find_custom (auth_list
,
180 authinfo_compare_host_and_share
);
186 auth_list
= g_slist_remove (auth_list
, auth
);
187 authinfo_free (auth
);
190 /* Set authentication information in bucket. Return 1 if successful, else 0 */
191 /* Information in auth_list overrides user if pass is NULL. */
192 /* bucket->host and bucket->service must be valid. */
194 bucket_set_authinfo (smbfs_connection
*bucket
,
195 const char *domain
, const char *user
, const char *pass
,
196 int fallback_to_host
)
198 struct smb_authinfo data
;
199 struct smb_authinfo
*auth
;
202 if (domain
&& user
&& pass
) {
203 g_free (bucket
->domain
);
204 g_free (bucket
->user
);
205 g_free (bucket
->password
);
206 bucket
->domain
= g_strdup (domain
);
207 bucket
->user
= g_strdup (user
);
208 bucket
->password
= g_strdup (pass
);
209 authinfo_remove (bucket
->host
, bucket
->service
);
210 authinfo_add (bucket
->host
, bucket
->service
,
215 data
.host
= bucket
->host
;
216 data
.share
= bucket
->service
;
217 list
= g_slist_find_custom (auth_list
, &data
, authinfo_compare_host_and_share
);
218 if (!list
&& fallback_to_host
)
219 list
= g_slist_find_custom (auth_list
, &data
, authinfo_compare_host
);
222 bucket
->domain
= g_strdup (auth
->domain
);
223 bucket
->user
= g_strdup (auth
->user
);
224 bucket
->password
= g_strdup (auth
->password
);
229 bucket
->domain
= g_strdup (lp_workgroup ());
230 bucket
->user
= g_strdup (got_user
? username
: user
);
231 bucket
->password
= g_strdup (password
);
235 auth
= vfs_smb_get_authinfo (bucket
->host
,
237 (domain
? domain
: lp_workgroup ()),
240 g_free (bucket
->domain
);
241 g_free (bucket
->user
);
242 g_free (bucket
->password
);
243 bucket
->domain
= g_strdup (auth
->domain
);
244 bucket
->user
= g_strdup (auth
->user
);
245 bucket
->password
= g_strdup (auth
->password
);
246 authinfo_remove (bucket
->host
, bucket
->service
);
247 auth_list
= g_slist_prepend (auth_list
, auth
);
254 smbfs_set_debug(int arg
)
259 /********************** The callbacks ******************************/
263 char *servicesf
= CONFIGDIR
"/smb.conf";
267 setup_logging("mc", True
);
269 charset_initialise();
271 DEBUG(3, ("smbfs_init(%s)\n", me
->name
));
273 if(!get_myname(myhostname
,NULL
))
274 DEBUG(0,("Failed to get my hostname.\n"));
276 if (!lp_load(servicesf
,True
,False
,False
))
277 DEBUG(0, ("Cannot load %s - run testparm to debug it\n", servicesf
));
279 codepage_initialise(lp_client_code_page());
285 if (getenv("USER")) {
287 pstrcpy(username
, getenv("USER"));
289 DEBUG(3, ("smbfs_init(): $USER:%s\n", username
));
290 if ((p
= strchr(username
, '%'))) {
292 pstrcpy(password
, p
+1);
294 memset(strchr(getenv("USER"), '%')+1, 'X', strlen(password
));
295 DEBUG(3, ("smbfs_init(): $USER%%pass: %s%%%s\n",
296 username
, password
));
300 if (getenv("PASSWD")) {
301 pstrcpy(password
, getenv("PASSWD"));
308 smbfs_fill_names (vfs
*me
, void (*func
)(char *))
312 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
313 if (smbfs_connections
[i
].cli
) {
314 path
= g_strconcat (URL_HEADER
,
315 smbfs_connections
[i
].host
,
316 "/", smbfs_connections
[i
].service
,
324 #define CNV_LANG(s) dos_to_unix(s,False)
325 #define GNAL_VNC(s) unix_to_dos(s,False)
326 /* does same as do_get() in client.c */
327 /* called from vfs.c:1080, count = buffer size */
329 smbfs_read (void *data
, char *buffer
, int count
)
331 smbfs_handle
*info
= (smbfs_handle
*) data
;
334 DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%d)\n",
335 info
->fnum
, (int)info
->nread
, count
));
336 n
= cli_read(info
->cli
, info
->fnum
, buffer
, info
->nread
, count
);
343 smbfs_write (void *data
, char *buf
, int nbyte
)
345 smbfs_handle
*info
= (smbfs_handle
*) data
;
348 DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%d)\n",
349 info
->fnum
, (int)info
->nread
, nbyte
));
350 n
= cli_write(info
->cli
, info
->fnum
, 0, buf
, info
->nread
, nbyte
);
357 smbfs_close (void *data
)
359 smbfs_handle
*info
= (smbfs_handle
*) data
;
360 DEBUG(3, ("smbfs_close(fnum:%d)\n", info
->fnum
));
361 /* if imlementing archive_level: add rname to smbfs_handle
362 if (archive_level >= 2 && (inf->attr & aARCH)) {
363 cli_setatr(info->cli, rname, info->attr & ~(uint16)aARCH, 0);
365 return (cli_close(info
->cli
, info
->fnum
) == True
) ? 0 : -1;
369 smbfs_errno (vfs
*me
)
371 DEBUG(3, ("smbfs_errno: %s\n", g_strerror(my_errno
)));
375 typedef struct dir_entry
{
377 struct dir_entry
*next
;
383 gboolean server_list
;
385 char *path
; /* the dir originally passed to smbfs_opendir */
386 smbfs_connection
*conn
;
395 *current_server_info
;
397 static gboolean first_direntry
;
400 new_dir_entry (const char * name
)
402 dir_entry
*new_entry
;
403 new_entry
= g_new0 (dir_entry
, 1);
404 new_entry
->text
= dos_to_unix (g_strdup (name
), 1);
406 if (first_direntry
) {
407 current_info
->entries
= new_entry
;
408 first_direntry
= FALSE
;
410 current_info
->current
->next
= new_entry
;
412 current_info
->current
= new_entry
;
417 /* browse for shares on server */
419 browsing_helper(const char *name
, uint32 type
, const char *comment
)
423 dir_entry
*new_entry
= new_dir_entry (name
);
428 /* show this as dir */
429 new_entry
->my_stat
.st_mode
= S_IFDIR
|S_IRUSR
|S_IRGRP
|S_IROTH
;
432 typestr
= "Printer"; break;
434 typestr
= "Device"; break;
436 typestr
= "IPC"; break;
438 DEBUG(3, ("\t%-15.15s%-10.10s%s\n", name
, typestr
, comment
));
442 loaddir_helper(file_info
*finfo
, const char *mask
)
444 dir_entry
*new_entry
;
445 time_t t
= finfo
->mtime
; /* the time is assumed to be passed as GMT */
446 #if 0 /* I want to see dot files */
447 if (finfo
->mode
& aHIDDEN
)
448 return; /* don't bother with hidden files, "~$" screws up mc */
450 new_entry
= new_dir_entry (finfo
->name
);
452 new_entry
->my_stat
.st_size
= finfo
->size
;
453 new_entry
->my_stat
.st_mtime
= finfo
->mtime
;
454 new_entry
->my_stat
.st_atime
= finfo
->atime
;
455 new_entry
->my_stat
.st_ctime
= finfo
->ctime
;
456 new_entry
->my_stat
.st_uid
= finfo
->uid
;
457 new_entry
->my_stat
.st_gid
= finfo
->gid
;
460 new_entry
->my_stat
.st_mode
=
461 S_IRUSR
|S_IRGRP
|S_IROTH
|S_IWUSR
|S_IWGRP
|S_IWOTH
;
463 /* if (finfo->mode & aVOLID); nothing similar in real world */
464 if (finfo
->mode
& aDIR
)
465 new_entry
->my_stat
.st_mode
|= S_IFDIR
;
467 new_entry
->my_stat
.st_mode
|= S_IFREG
; /* if not dir, regular file? */
468 /* if (finfo->mode & aARCH); DOS archive */
469 /* if (finfo->mode & aHIDDEN); like a dot file? */
470 /* if (finfo->mode & aSYSTEM); like a kernel? */
471 if (finfo
->mode
& aRONLY
)
472 new_entry
->my_stat
.st_mode
&= ~(S_IRUSR
|S_IRGRP
|S_IROTH
);
474 DEBUG(3, (" %-30s%7.7s%8.0f %s",
475 CNV_LANG(finfo
->name
),
476 attrib_string(finfo
->mode
),
478 asctime(LocalTime(&t
))));
481 /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
483 convert_path(char **remote_file
, gboolean trailing_asterik
)
487 my_remote
= *remote_file
;
488 if (strncmp (my_remote
, URL_HEADER
, HEADER_LEN
) == 0) { /* if passed directly */
490 if (*my_remote
== '/') /* from server browsing */
492 p
= strchr(my_remote
, '/');
494 my_remote
= p
+1; /* advance to end of server name */
497 if (*my_remote
== '/')
498 my_remote
++; /* strip off leading '/' */
499 p
= strchr(my_remote
, '/');
501 my_remote
= p
; /* strip off share/service name */
502 /* create remote filename as understood by smb clientgen */
503 p
= *remote_file
= g_strconcat (my_remote
, trailing_asterik
? "/*" : "", 0);
504 unix_to_dos (*remote_file
, 1);
505 while ((p
= strchr(p
, '/')))
511 server_browsing_helper(const char *name
, uint32 m
, const char *comment
)
513 dir_entry
*new_entry
= new_dir_entry (name
);
515 /* show this as dir */
516 new_entry
->my_stat
.st_mode
= S_IFDIR
|S_IRUSR
|S_IRGRP
|S_IROTH
;
518 DEBUG(3, ("\t%-16.16s %s\n", name
, comment
));
522 reconnect(smbfs_connection
*conn
, int *retries
)
525 DEBUG(3, ("RECONNECT\n"));
527 if (*(conn
->host
) == 0)
528 host
= g_strdup(conn
->cli
->desthost
); /* server browsing */
530 host
= g_strdup(conn
->host
);
532 cli_shutdown(conn
->cli
);
534 if (!(conn
->cli
= smbfs_do_connect(host
, conn
->service
))) {
535 message_2s (1, MSG_ERROR
,
536 _(" reconnect to %s failed\n "), conn
->host
);
541 if (++(*retries
) == 2)
547 smb_send(struct cli_state
*cli
)
553 len
= smb_len(cli
->outbuf
) + 4;
555 while (nwritten
< len
) {
556 ret
= write_socket(cli
->fd
, cli
->outbuf
+nwritten
, len
- nwritten
);
557 if (ret
<= 0 && errno
== EPIPE
)
565 /****************************************************************************
566 See if server has cut us off by checking for EPIPE when writing.
567 Taken from cli_chkpath()
568 ****************************************************************************/
570 chkpath(struct cli_state
*cli
, char *path
, BOOL send_only
)
576 unix_to_dos (path2
, 1);
577 trim_string(path2
,NULL
,"\\");
578 if (!*path2
) *path2
= '\\';
580 memset(cli
->outbuf
,'\0',smb_size
);
581 set_message(cli
->outbuf
,0,4 + strlen(path2
),True
);
582 SCVAL(cli
->outbuf
,smb_com
,SMBchkpth
);
583 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
587 SSVAL(cli
->outbuf
,smb_pid
,cli
->pid
);
588 SSVAL(cli
->outbuf
,smb_uid
,cli
->vuid
);
589 SSVAL(cli
->outbuf
,smb_mid
,cli
->mid
);
590 if (cli
->protocol
> PROTOCOL_CORE
) {
591 SCVAL(cli
->outbuf
,smb_flg
,0x8);
592 SSVAL(cli
->outbuf
,smb_flg2
,0x1);
595 p
= smb_buf(cli
->outbuf
);
599 if (!smb_send(cli
)) {
600 DEBUG(3, ("chkpath: couldnt send\n"));
604 client_receive_smb(cli
->fd
, cli
->inbuf
, cli
->timeout
);
605 DEBUG(3, ("chkpath: send only OK\n"));
606 return True
; /* just testing for EPIPE */
608 if (!client_receive_smb(cli
->fd
, cli
->inbuf
, cli
->timeout
)) {
609 DEBUG(3, ("chkpath: receive error\n"));
612 if ((my_errno
= cli_error(cli
, NULL
, NULL
, NULL
))) {
613 if (my_errno
== 20 || my_errno
== 13)
614 return True
; /* ignore if 'not a directory' error */
615 DEBUG(3, ("chkpath: cli_error: %s\n", g_strerror(my_errno
)));
624 fs (const char *text
)
626 const char *p
= text
;
629 while ((p
= strchr(p
, '/')) != NULL
) {
640 smbfs_loaddir (opendir_info
*smbfs_info
)
642 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
643 int servlen
= strlen(smbfs_info
->conn
->service
);
644 char *my_dirname
= smbfs_info
->dirname
;
646 DEBUG(3, ("smbfs_loaddir: dirname:%s\n", my_dirname
));
647 first_direntry
= TRUE
;
650 DEBUG(3, ("smbfs_loaddir: new:'%s', cached:'%s'\n", my_dirname
, current_info
->dirname
));
651 /* if new desired dir is longer than cached in current_info */
652 if (fs(my_dirname
) > fs(current_info
->dirname
)) {
653 DEBUG(3, ("saving to previous_info\n"));
654 previous_info
= current_info
;
658 current_info
= smbfs_info
;
660 if (strcmp(my_dirname
, "/") == 0) {
661 if (!strcmp(smbfs_info
->path
, URL_HEADER
)) {
662 DEBUG(6, ("smbfs_loaddir: browsing %s\n", IPC
));
663 /* browse for servers */
664 if (!cli_NetServerEnum(smbfs_info
->conn
->cli
, smbfs_info
->conn
->domain
,
665 SV_TYPE_ALL
, server_browsing_helper
))
668 current_server_info
= smbfs_info
;
669 smbfs_info
->server_list
= TRUE
;
671 /* browse for shares */
672 if (cli_RNetShareEnum(smbfs_info
->conn
->cli
, browsing_helper
) < 1)
675 current_share_info
= smbfs_info
;
680 /* do regular directory listing */
681 if(strncmp(smbfs_info
->conn
->service
, my_dirname
+1, servlen
) == 0) {
682 /* strip share name from dir */
683 char *p
= my_dirname
= g_strdup(my_dirname
+ servlen
);
685 convert_path(&my_dirname
, TRUE
);
688 convert_path(&my_dirname
, TRUE
);
690 DEBUG(6, ("smbfs_loaddir: service: %s\n", smbfs_info
->conn
->service
));
691 DEBUG(6, ("smbfs_loaddir: cli->share: %s\n", smbfs_info
->conn
->cli
->share
));
692 DEBUG(6, ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname
));
693 /* do file listing: cli_list returns number of files */
695 smbfs_info
->conn
->cli
, my_dirname
, attribute
, loaddir_helper
) < 0) {
696 /* cli_list returns -1 if directory empty or cannot read socket */
697 my_errno
= cli_error(smbfs_info
->conn
->cli
, NULL
, &err
, NULL
);
701 if (*(my_dirname
) == 0)
702 smbfs_info
->dirname
= smbfs_info
->conn
->service
;
707 /* current_info->parent = smbfs_info->dirname; */
709 smbfs_info
->current
= smbfs_info
->entries
;
710 return 1; /* 1 = ok */
713 #ifdef SMBFS_FREE_DIR
715 smbfs_free_dir (dir_entry
*de
)
719 smbfs_free_dir (de
->next
);
726 * On some operating systems (Slowaris 2 for example)
727 * the d_name member is just a char long (Nice trick that break everything,
728 * so we need to set up some space for the filename.
732 #ifdef NEED_EXTRA_DIRENT_BUFFER
733 char extra_buffer
[MC_MAXPATHLEN
];
735 } smbfs_readdir_data
;
738 /* The readdir routine loads the complete directory */
739 /* It's too slow to ask the server each time */
740 /* It now also sends the complete lstat information for each file */
742 smbfs_readdir (void *info
)
744 static char * const dirent_dest
= &(smbfs_readdir_data
.dent
.d_name
[0]);
745 opendir_info
*smbfs_info
= (opendir_info
*) info
;
747 DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info
->dirname
));
749 if (!smbfs_info
->entries
)
750 if (!smbfs_loaddir (smbfs_info
))
753 if (smbfs_info
->current
== 0) { /* reached end of dir entries */
754 DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
755 #ifdef SMBFS_FREE_DIR
756 smbfs_free_dir (smbfs_info
->entries
);
757 smbfs_info
->entries
= 0;
761 pstrcpy (dirent_dest
, smbfs_info
->current
->text
);
762 smbfs_info
->current
= smbfs_info
->current
->next
;
764 #ifndef DIRENT_LENGTH_COMPUTED
765 smbfs_readdir_data
.dent
.d_namlen
= strlen (smbfs_readdir_data
.dent
.d_name
);
768 return &smbfs_readdir_data
;
772 smbfs_closedir (void *info
)
774 opendir_info
*smbfs_info
= (opendir_info
*) info
;
775 /* dir_entry *p, *q; */
777 DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info
->dirname
));
780 /* for (p = smbfs_info->entries; p;){
791 smbfs_chmod (vfs
*me
, char *path
, int mode
)
793 DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path
, mode
));
794 /* my_errno = EOPNOTSUPP;
795 return -1; */ /* cant chmod on smb filesystem */
796 return 0; /* make mc happy */
800 smbfs_chown (vfs
*me
, char *path
, int owner
, int group
)
802 DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path
, owner
, group
));
803 my_errno
= EOPNOTSUPP
; /* ready for your labotomy? */
808 smbfs_utime (vfs
*me
, char *path
, struct utimbuf
*times
)
810 DEBUG(3, ("smbfs_utime(path:%s)\n", path
));
811 my_errno
= EOPNOTSUPP
;
816 smbfs_readlink (vfs
*me
, char *path
, char *buf
, int size
)
818 DEBUG(3, ("smbfs_readlink(path:%s, buf:%s, size:%d)\n", path
, buf
, size
));
819 my_errno
= EOPNOTSUPP
;
820 return -1; /* no symlinks on smb filesystem? */
824 smbfs_symlink (vfs
*me
, char *n1
, char *n2
)
826 DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1
, n2
));
827 my_errno
= EOPNOTSUPP
;
828 return -1; /* no symlinks on smb filesystem? */
831 /* Extract the hostname and username from the path */
832 /* path is in the form: hostname:user/remote-dir */
835 smbfs_get_host_and_username
836 (char **path
, char **host
, char **user
, int *port
, char **pass
)
841 ret
= vfs_split_url (*path
, host
, user
, port
, pass
, SMB_PORT
, 0);
844 if ((p
= strchr(*path
, '@'))) /* user:pass@server */
845 *path
= ++p
; /* don't want user:pass@ in path */
846 if ((p
= strchr(ret
, '@'))) /* user:pass@server */
847 ret
= ++p
; /* don't want user:pass@ in path */
853 #define smbfs_get_host_and_username(path, host, user, port, pass) \
854 vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
856 /*****************************************************
857 return a connection to a SMB server
858 current_bucket needs to be set before calling
859 *******************************************************/
860 static struct cli_state
*
861 smbfs_do_connect (const char *server
, char *share
)
864 struct nmb_name called
, calling
;
866 extern struct in_addr ipzero
;
868 DEBUG(3, ("smbfs_do_connect(%s, %s)\n", server
, share
));
869 if (*share
== '\\') {
871 share
= strchr(server
,'\\');
872 if (!share
) return NULL
;
877 make_nmb_name(&calling
, global_myname
, 0x0, "");
878 make_nmb_name(&called
, server
, current_bucket
->name_type
, "");
882 ip
= (current_bucket
->have_ip
) ? current_bucket
->dest_ip
: ipzero
;
884 /* have to open a new connection */
885 if (!(c
= cli_initialise(NULL
))) {
890 pwd_init(&(c
->pwd
)); /* should be moved into cli_initialise()? */
891 pwd_set_cleartext(&(c
->pwd
), current_bucket
->password
);
893 if ((cli_set_port(c
, current_bucket
->port
) == 0) ||
894 !cli_connect(c
, server
, &ip
)) {
895 DEBUG(1, ("Connection to %s failed\n", server
));
899 if (!cli_session_request(c
, &calling
, &called
)) {
900 my_errno
= cli_error(c
, NULL
, &err
, NULL
);
901 DEBUG(1, ("session request to %s failed\n", called
.name
));
903 if (strcmp(called
.name
, "*SMBSERVER")) {
904 make_nmb_name(&called
, "*SMBSERVER", 0x20, "");
910 DEBUG(3, (" session request ok\n"));
912 if (!cli_negprot(c
)) {
913 DEBUG(1, ("protocol negotiation failed\n"));
917 if (!cli_session_setup(c
, current_bucket
->user
,
918 current_bucket
->password
, strlen(current_bucket
->password
),
919 current_bucket
->password
, strlen(current_bucket
->password
),
920 current_bucket
->domain
)) {
921 DEBUG(1,("session setup failed: %s\n", cli_errstr(c
)));
922 authinfo_remove (server
, share
);
926 if (*c
->server_domain
|| *c
->server_os
|| *c
->server_type
)
927 DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
928 c
->server_domain
,c
->server_os
,c
->server_type
));
930 DEBUG(3, (" session setup ok\n"));
932 if (!cli_send_tconX(c
, share
, "?????",
933 current_bucket
->password
, strlen(current_bucket
->password
)+1)) {
934 DEBUG(1,("%s: tree connect failed: %s\n", share
, cli_errstr(c
)));
938 DEBUG(3, (" tconx ok\n"));
944 my_errno
= cli_error(c
, NULL
, &err
, NULL
);
951 get_master_browser(char **host
)
954 struct in_addr
*ip_list
, bcast_addr
;
955 extern struct in_addr ipzero
;
957 /* does port = 137 for win95 master browser? */
958 int fd
= open_socket_in( SOCK_DGRAM
, 0, 3,
959 interpret_addr(lp_socket_address()), True
);
962 set_socket_options(fd
, "SO_BROADCAST");
963 ip_list
= iface_bcast(ipzero
);
964 bcast_addr
= *ip_list
;
965 if ((ip_list
= name_query(fd
, "\01\02__MSBROWSE__\02", 1, True
,
966 True
, bcast_addr
, &count
, NULL
))) {
969 /* just return first master browser */
970 *host
= g_strdup(inet_ntoa(ip_list
[0]));
977 free_bucket (smbfs_connection
*bucket
)
979 g_free (bucket
->host
);
980 g_free (bucket
->service
);
981 g_free (bucket
->domain
);
982 g_free (bucket
->user
);
983 wipe_password (bucket
->password
);
984 if (bucket
->home
) g_free (bucket
->home
);
985 memset (bucket
, 0, sizeof (smbfs_connection
));
988 static smbfs_connection
*
989 smbfs_get_free_bucket ()
993 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++)
994 if (!smbfs_connections
[i
].cli
) return &smbfs_connections
[i
];
996 { /* search for most dormant connection */
997 int oldest
= 0; /* index */
998 time_t oldest_time
= smbfs_connections
[0].last_use
;
999 for (i
= 1; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
1000 if (smbfs_connections
[i
].last_use
< oldest_time
) {
1001 oldest_time
= smbfs_connections
[i
].last_use
;
1005 cli_shutdown(smbfs_connections
[oldest
].cli
);
1006 free_bucket (&smbfs_connections
[oldest
]);
1007 return &smbfs_connections
[oldest
];
1010 /* This can't happend, since we have checked for max connections before */
1011 vfs_die("Internal error: smbfs_get_free_bucket");
1012 return 0; /* shut up, stupid gcc */
1015 /* This routine keeps track of open connections */
1016 /* Returns a connected socket to host */
1017 static smbfs_connection
*
1018 smbfs_open_link(char *host
, char *path
, const char *user
, int *port
, char *this_pass
)
1021 smbfs_connection
*bucket
;
1023 struct in_addr
*dest_ip
= NULL
;
1025 DEBUG(3, ("smbfs_open_link(host:%s, path:%s)\n", host
, path
));
1027 if (strcmp(host
, path
) == 0) /* if host & path are same: */
1028 pstrcpy(service
, IPC
); /* setup for browse */
1029 else { /* get share name from path, path starts with server name */
1031 if ((p
= strchr(path
, '/'))) /* get share aka */
1032 pstrcpy(service
, ++p
); /* service name from path */
1034 pstrcpy(service
, "");
1035 /* now check for trailing directory/filenames */
1036 p
= strchr(service
, '/');
1038 *p
= 0; /* cut off dir/files: sharename only */
1040 DEBUG(6, ("smbfs_open_link: service from path:%s\n", service
));
1044 user
= username
; /* global from getenv */
1046 /* Is the link actually open? */
1047 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
1048 if (!smbfs_connections
[i
].cli
)
1050 if ((strcmp (host
, smbfs_connections
[i
].host
) == 0) &&
1051 (strcmp (user
, smbfs_connections
[i
].user
) == 0) &&
1052 (strcmp (service
, smbfs_connections
[i
].service
) == 0)) {
1054 BOOL inshare
= (*host
!= 0 && *path
!= 0 && strchr(path
, '/'));
1055 /* check if this connection has died */
1056 while (!chkpath(smbfs_connections
[i
].cli
, "\\", !inshare
)) {
1057 if (!reconnect(&smbfs_connections
[i
], &retries
))
1060 DEBUG(6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i
));
1061 current_bucket
= &smbfs_connections
[i
];
1062 smbfs_connections
[i
].last_use
= time(NULL
);
1063 return &smbfs_connections
[i
];
1065 /* connection not found, find if we have ip for new connection */
1066 if (strcmp (host
, smbfs_connections
[i
].host
) == 0)
1067 dest_ip
= &smbfs_connections
[i
].cli
->dest_ip
;
1070 /* make new connection */
1071 bucket
= smbfs_get_free_bucket ();
1072 bucket
->name_type
= 0x20;
1074 bucket
->port
= *port
;
1075 bucket
->have_ip
= False
;
1077 bucket
->have_ip
= True
;
1078 bucket
->dest_ip
= *dest_ip
;
1080 current_bucket
= bucket
;
1082 bucket
->user
= g_strdup(user
);
1083 bucket
->service
= g_strdup (service
);
1085 if (!(*host
)) { /* if blank host name, browse for servers */
1086 if (!get_master_browser(&host
)) /* set host to ip of master browser */
1087 return 0; /* couldnt find master browser? */
1089 bucket
->host
= g_strdup(""); /* blank host means master browser */
1091 bucket
->host
= g_strdup(host
);
1093 if (!bucket_set_authinfo (bucket
,
1094 0, /* domain currently not used */
1100 /* connect to share */
1101 while (!(bucket
->cli
= smbfs_do_connect(host
, service
))) {
1103 if (my_errno
!= EPERM
)
1105 message_1s (1, MSG_ERROR
,
1106 _(" Authentication failed "));
1108 /* authentication failed, try again */
1109 authinfo_remove (bucket
->host
, bucket
->service
);
1110 if (!bucket_set_authinfo (bucket
,
1119 smbfs_open_connections
++;
1120 DEBUG(3, ("smbfs_open_link:smbfs_open_connections: %d\n",
1121 smbfs_open_connections
));
1126 smbfs_get_path(smbfs_connection
**sc
, char *path
)
1128 char *user
, *host
, *remote_path
, *pass
;
1129 int port
= SMB_PORT
;
1131 DEBUG(3, ("smbfs_get_path(%s)\n", path
));
1132 if (strncmp (path
, URL_HEADER
, HEADER_LEN
))
1136 if (*path
== '/') /* '/' leading server name */
1137 path
++; /* probably came from server browsing */
1139 if ((remote_path
= smbfs_get_host_and_username(
1140 &path
, &host
, &user
, &port
, &pass
)))
1141 if ((*sc
= smbfs_open_link (host
, path
, user
, &port
, pass
)) == NULL
){
1142 g_free (remote_path
);
1147 if (pass
) wipe_password (pass
);
1149 if (!remote_path
) return NULL
;
1151 /* NOTE: tildes are deprecated. See ftpfs.c */
1153 int f
= !strcmp( remote_path
, "/~" );
1154 if (f
|| !strncmp( remote_path
, "/~/", 3 )) {
1156 s
= concat_dir_and_file( (*sc
)->home
, remote_path
+3-f
);
1157 g_free (remote_path
);
1166 is_error (int result
, int errno_num
)
1168 if (!(result
== -1))
1169 return my_errno
= 0;
1171 my_errno
= errno_num
;
1177 smbfs_opendir (vfs
*me
, char *dirname
)
1179 opendir_info
*smbfs_info
;
1180 smbfs_connection
*sc
;
1183 DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname
));
1185 if (!(remote_dir
= smbfs_get_path (&sc
, dirname
)))
1188 /* FIXME: where freed? */
1189 smbfs_info
= g_new (opendir_info
, 1);
1190 smbfs_info
->server_list
= FALSE
;
1191 smbfs_info
->path
= g_strdup(dirname
); /* keep original */
1192 smbfs_info
->dirname
= remote_dir
;
1193 smbfs_info
->conn
= sc
;
1194 smbfs_info
->entries
= 0;
1195 smbfs_info
->current
= 0;
1201 fake_server_stat(const char *server_url
, const char *path
, struct stat
*buf
)
1206 if ((p
= strrchr(path
, '/')))
1207 path
= p
+ 1; /* advance until last '/' */
1209 if (!current_info
->entries
) {
1210 if (!smbfs_loaddir(current_info
)); /* browse host */
1214 if (current_info
->server_list
== True
) {
1215 dentry
= current_info
->entries
;
1216 DEBUG(4, ("fake stat for SERVER \"%s\"\n", path
));
1218 if (strcmp(dentry
->text
, path
) == 0) {
1219 DEBUG(4, ("fake_server_stat: %s:%4o\n",
1220 dentry
->text
, dentry
->my_stat
.st_mode
));
1221 memcpy(buf
, &dentry
->my_stat
, sizeof(struct stat
));
1224 dentry
= dentry
->next
;
1232 fake_share_stat(const char *server_url
, const char *path
, struct stat
*buf
)
1235 if (strlen(path
) < strlen(server_url
))
1237 path
+= strlen(server_url
); /* we only want share name */
1240 if (*path
== '/') /* '/' leading server name */
1241 path
++; /* probably came from server browsing */
1243 if (!current_share_info
->entries
) {
1244 if (!smbfs_loaddir(current_share_info
)); /* browse host */
1247 dentry
= current_share_info
->entries
;
1248 DEBUG(3, ("fake_share_stat: %s on %s\n", path
, server_url
));
1250 if (strcmp(dentry
->text
, path
) == 0) {
1251 DEBUG(6, ("fake_share_stat: %s:%4o\n",
1252 dentry
->text
, dentry
->my_stat
.st_mode
));
1253 memcpy(buf
, &dentry
->my_stat
, sizeof(struct stat
));
1256 dentry
= dentry
->next
;
1262 /* stat a single file, get_remote_stat callback */
1263 static dir_entry
*single_entry
;
1265 statfile_helper(file_info
*finfo
, const char *mask
)
1267 time_t t
= finfo
->mtime
; /* the time is assumed to be passed as GMT */
1269 #if 0 /* single_entry is never freed now. And only my_stat is used */
1270 single_entry
= g_new (dir_entry
, 1);
1272 single_entry
->text
= dos_to_unix (g_strdup (finfo
->name
), 1);
1274 single_entry
->next
= 0;
1277 single_entry
= g_new0 (dir_entry
, 1);
1279 single_entry
->my_stat
.st_size
= finfo
->size
;
1280 single_entry
->my_stat
.st_mtime
= finfo
->mtime
;
1281 single_entry
->my_stat
.st_atime
= finfo
->atime
;
1282 single_entry
->my_stat
.st_ctime
= finfo
->ctime
;
1283 single_entry
->my_stat
.st_uid
= finfo
->uid
;
1284 single_entry
->my_stat
.st_gid
= finfo
->gid
;
1286 single_entry
->my_stat
.st_mode
=
1287 S_IRUSR
|S_IRGRP
|S_IROTH
|S_IWUSR
|S_IWGRP
|S_IWOTH
;
1289 /* if (finfo->mode & aVOLID); nothing similar in real world */
1290 if (finfo
->mode
& aDIR
)
1291 single_entry
->my_stat
.st_mode
|= S_IFDIR
;
1293 single_entry
->my_stat
.st_mode
|= S_IFREG
;/* if not dir, regular file? */
1294 /* if (finfo->mode & aARCH); DOS archive */
1295 /* if (finfo->mode & aHIDDEN); like a dot file? */
1296 /* if (finfo->mode & aSYSTEM); like a kernel? */
1297 if (finfo
->mode
& aRONLY
)
1298 single_entry
->my_stat
.st_mode
&= ~(S_IRUSR
|S_IRGRP
|S_IROTH
);
1300 DEBUG(6, (" %-30s%7.7s%8.0f %s",
1301 CNV_LANG(finfo
->name
),
1302 attrib_string(finfo
->mode
),
1303 (double)finfo
->size
,
1304 asctime(LocalTime(&t
))));
1307 /* stat a single file */
1309 get_remote_stat(smbfs_connection
*sc
, char *path
, struct stat
*buf
)
1311 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
1312 char *mypath
= path
;
1314 DEBUG(3, ("get_remote_stat(): mypath:%s\n", mypath
));
1316 convert_path(&mypath
, FALSE
);
1318 if (cli_list(sc
->cli
, mypath
, attribute
, statfile_helper
) < 1) {
1321 return -1; /* cli_list returns number of files */
1324 memcpy(buf
, &single_entry
->my_stat
, sizeof(struct stat
));
1326 /* dont free here, use for smbfs_fstat() */
1327 /* g_free(single_entry->text);
1328 g_free(single_entry); */
1334 search_dir_entry (dir_entry
*dentry
, const char *text
, struct stat
*buf
)
1337 if (strcmp(text
, dentry
->text
) == 0) {
1338 memcpy(buf
, &dentry
->my_stat
, sizeof(struct stat
));
1339 memcpy(&single_entry
->my_stat
, &dentry
->my_stat
,
1340 sizeof(struct stat
));
1343 dentry
= dentry
->next
;
1349 get_stat_info (smbfs_connection
*sc
, char *path
, struct stat
*buf
)
1353 dir_entry
*dentry
= current_info
->entries
;
1355 const char *mypath
= path
;
1357 mypath
++; /* cut off leading '/' */
1358 if ((p
= strrchr(mypath
, '/')))
1359 mypath
= p
+ 1; /* advance until last file/dir name */
1360 DEBUG(3, ("get_stat_info: mypath:%s, current_info->dirname:%s\n",
1361 mypath
, current_info
->dirname
));
1364 DEBUG(1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n", current_info
->dirname
, path
));
1368 if (!single_entry
) /* when found, this will be written too */
1369 single_entry
= g_new (dir_entry
, 1);
1370 if (search_dir_entry(current_info
->entries
, mypath
, buf
) == 0) {
1373 /* now try to identify mypath as PARENT dir */
1377 mdp
= mydir
= g_strdup(current_info
->dirname
);
1378 if ((p
= strrchr(mydir
, '/')))
1379 *p
= 0; /* advance util last '/' */
1380 if ((p
= strrchr(mydir
, '/')))
1381 mydir
= p
+ 1; /* advance util last '/' */
1382 if (strcmp(mydir
, mypath
) == 0) { /* fake a stat for ".." */
1383 memset(buf
, 0, sizeof(struct stat
));
1384 buf
->st_mode
= S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
;
1385 memcpy(&single_entry
->my_stat
, buf
, sizeof(struct stat
));
1387 DEBUG(1, (" PARENT:found in %s\n", current_info
->dirname
));
1392 /* now try to identify as CURRENT dir? */
1394 char *dnp
= current_info
->dirname
;
1395 DEBUG(6, ("get_stat_info: is %s current dir? this dir is: %s\n",
1396 mypath
, current_info
->dirname
));
1402 if (strcmp(mypath
, dnp
) == 0) {
1403 memset(buf
, 0, sizeof(struct stat
));
1404 buf
->st_mode
= S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
;
1405 memcpy(&single_entry
->my_stat
, buf
, sizeof(struct stat
));
1406 DEBUG(1, (" CURRENT:found in %s\n", current_info
->dirname
));
1410 DEBUG(3, ("'%s' not found in current_info '%s'\n", path
, current_info
->dirname
));
1411 /* try to find this in the PREVIOUS listing */
1412 if (previous_info
) {
1413 if (search_dir_entry(previous_info
->entries
, mypath
, buf
) == 0)
1415 DEBUG(3, ("'%s' not found in previous_info '%s'\n", path
, previous_info
->dirname
));
1417 /* try to find this in the SHARE listing */
1418 if (current_share_info
&& search_dir_entry(current_share_info
->entries
, mypath
, buf
) == 0)
1420 DEBUG(3, ("'%s' not found in share_info '%s'\n", path
, current_share_info
->dirname
));
1421 /* try to find this in the SERVER listing */
1422 if (current_server_info
&& search_dir_entry(current_server_info
->entries
, mypath
, buf
) == 0)
1424 DEBUG(3, ("'%s' not found in server_info '%s'\n", path
, current_server_info
->dirname
));
1425 /* nothing found. get stat file info from server */
1426 return get_remote_stat(sc
, path
, buf
);
1430 smbfs_chdir (vfs
*me
, char *path
)
1433 smbfs_connection
*sc
;
1435 DEBUG(3, ("smbfs_chdir(path:%s)\n", path
));
1436 if (!(remote_dir
= smbfs_get_path (&sc
, path
)))
1438 g_free (remote_dir
);
1444 loaddir(vfs
*me
, const char *path
)
1449 mypath
= g_strdup(path
);
1450 p
= strrchr(mypath
, '/');
1454 DEBUG(6, ("loaddir(%s)\n", mypath
));
1455 smbfs_chdir(me
, mypath
);
1456 info
= smbfs_opendir (me
, mypath
);
1460 smbfs_readdir(info
);
1461 smbfs_loaddir(info
);
1466 smbfs_stat (vfs
*me
, char *path
, struct stat
*buf
)
1469 smbfs_connection
*sc
;
1474 DEBUG(3, ("smbfs_stat(path:%s)\n", path
));
1477 if (p
= strchr(path
, '@')) /* user:pass@server */
1478 path
= ++p
; /* don't want user:pass@ in path */
1481 if (!current_info
) {
1482 DEBUG(1, ("current_info = NULL: "));
1483 if (loaddir(me
, path
) < 0)
1487 pstrcpy(server_url
, URL_HEADER
);
1488 pstrcat(server_url
, current_bucket
->host
);
1490 /* check if stating server */
1492 if (strncmp(p
, URL_HEADER
, HEADER_LEN
)) {
1493 DEBUG(1, ("'%s' doesnt start with '%s' (length %d)\n",
1494 p
, URL_HEADER
, HEADER_LEN
));
1501 pp
= strchr(p
, '/'); /* advance past next '/' */
1503 if (!current_info
->server_list
) {
1504 if (loaddir(me
, path
) < 0)
1507 return fake_server_stat(server_url
, path
, buf
);
1509 if (!strchr(++pp
, '/')) {
1510 return fake_share_stat(server_url
, path
, buf
);
1513 /* stating inside share at this point */
1514 if (!(remote_dir
= smbfs_get_path (&sc
, path
))) /* connects if necessary */
1516 g_free (remote_dir
);
1518 int hostlen
= strlen(current_bucket
->host
);
1519 char *pp
= path
+ strlen(path
)-hostlen
;
1520 char *sp
= server_url
+ strlen(server_url
)-hostlen
;
1522 if (strcmp(sp
, pp
) == 0) {
1523 /* make server name appear as directory */
1524 DEBUG(1, ("smbfs_stat: showing server as directory\n"));
1525 memset(buf
, 0, sizeof(struct stat
));
1526 buf
->st_mode
= S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
;
1530 /* check if current_info is in share requested */
1531 p
= service
= g_strdup(p
);
1532 pp
= strchr(p
, '/');
1534 p
= ++pp
; /* advance past server name */
1535 pp
= strchr(p
, '/');
1538 *pp
= 0; /* cut off everthing after service name */
1540 p
= IPC
; /* browsing for services */
1541 pp
= current_info
->dirname
;
1544 if (strncmp(p
, pp
, strlen(p
)) != 0) {
1545 DEBUG(6, ("desired '%s' is not loaded, we have '%s'\n", p
, pp
));
1546 if (loaddir(me
, path
) < 0) {
1550 DEBUG(6, ("loaded dir: '%s'\n", current_info
->dirname
));
1553 /* stat dirs & files under shares now */
1554 return get_stat_info(sc
, path
, buf
);
1557 #define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */
1560 smbfs_lseek (void *data
, off_t offset
, int whence
)
1562 DEBUG(3, ("smbfs_lseek()\n"));
1563 my_errno
= EOPNOTSUPP
;
1568 smbfs_mknod (vfs
*me
, char *path
, int mode
, int dev
)
1570 DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%d)\n", path
, mode
, dev
));
1571 my_errno
= EOPNOTSUPP
;
1576 smbfs_mkdir (vfs
*me
, char *path
, mode_t mode
)
1578 smbfs_connection
*sc
;
1581 DEBUG(3, ("smbfs_mkdir(path:%s, mode:%d)\n", path
, mode
));
1582 if ((remote_file
= smbfs_get_path (&sc
, path
)) == 0)
1584 g_free (remote_file
);
1585 convert_path(&path
, FALSE
);
1587 if (!cli_mkdir(sc
->cli
, path
)) {
1588 my_errno
= cli_error(sc
->cli
, NULL
, &err
, NULL
);
1589 message_3s (1, MSG_ERROR
, _(" %s mkdir'ing %s "),
1590 cli_errstr(sc
->cli
), CNV_LANG(path
));
1599 smbfs_rmdir (vfs
*me
, char *path
)
1601 smbfs_connection
*sc
;
1604 DEBUG(3, ("smbfs_rmdir(path:%s)\n", path
));
1605 if ((remote_file
= smbfs_get_path (&sc
, path
)) == 0)
1607 g_free (remote_file
);
1608 convert_path(&path
, FALSE
);
1610 if (!cli_rmdir(sc
->cli
, path
)) {
1611 my_errno
= cli_error(sc
->cli
, NULL
, &err
, NULL
);
1612 message_3s (1, MSG_ERROR
, _(" %s rmdir'ing %s "),
1613 cli_errstr(sc
->cli
), CNV_LANG(path
));
1623 smbfs_link (vfs
*me
, char *p1
, char *p2
)
1625 DEBUG(3, ("smbfs_link(p1:%s, p2:%s)\n", p1
, p2
));
1626 my_errno
= EOPNOTSUPP
;
1630 /* We do not free anything right now: we free resources when we run
1634 smbfs_getid (vfs
*me
, char *p
, struct vfs_stamping
**parent
)
1637 DEBUG(3, ("smbfs_getid(p:%s)\n", p
));
1643 smbfs_nothingisopen (vfsid id
)
1645 DEBUG(3, ("smbfs_nothingisopen(%d)\n", (int)id
));
1650 smbfs_free (vfsid id
)
1652 DEBUG(3, ("smbfs_free(%d)\n", (int)id
));
1653 /* FIXME: Should not be empty */
1654 authinfo_free_all ();
1657 /* Gives up on a socket and reopens the connection, the child own the socket
1661 my_forget (char *path
)
1663 char *host
, *user
, *p
;
1666 if (strncmp (path
, URL_HEADER
, HEADER_LEN
))
1669 DEBUG(3, ("my_forget(path:%s)\n", path
));
1672 if (path
[0] == '/' && path
[1] == '/')
1675 if ((p
= smbfs_get_host_and_username (&path
, &host
, &user
, &port
, NULL
))) {
1677 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
1678 if ((strcmp (host
, smbfs_connections
[i
].host
) == 0) &&
1679 (strcmp (user
, smbfs_connections
[i
].user
) == 0) &&
1680 (port
== smbfs_connections
[i
].port
)) {
1682 /* close socket: the child owns it now */
1683 cli_shutdown(smbfs_connections
[i
].cli
);
1685 /* reopen the connection */
1686 smbfs_connections
[i
].cli
=
1687 smbfs_do_connect(host
, smbfs_connections
[i
].service
);
1696 smbfs_setctl (vfs
*me
, char *path
, int ctlop
, char *arg
)
1698 DEBUG(3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path
, ctlop
));
1700 case MCCTL_FORGET_ABOUT
:
1707 static smbfs_handle
*
1708 open_write (smbfs_handle
*remote_handle
, char *rname
, int flags
, int mode
)
1710 if (flags
& O_TRUNC
) /* if it exists truncate to zero */
1711 DEBUG(3, ("open_write: O_TRUNC\n"));
1713 remote_handle
->fnum
= cli_open(remote_handle
->cli
, rname
, flags
, DENY_NONE
);
1715 if (remote_handle
->fnum
== -1) {
1716 message_3s (1, MSG_ERROR
, _(" %s opening remote file %s "),
1717 cli_errstr(remote_handle
->cli
), CNV_LANG(rname
));
1718 DEBUG(1,("smbfs_open(rname:%s) error:%s\n",
1719 rname
, cli_errstr(remote_handle
->cli
)));
1720 my_errno
= cli_error(remote_handle
->cli
, NULL
, &err
, NULL
);
1724 return remote_handle
;
1727 static smbfs_handle
*
1728 open_read (smbfs_handle
*remote_handle
, char *rname
, int flags
, int mode
)
1732 remote_handle
->fnum
=
1733 cli_open(remote_handle
->cli
, rname
, O_RDONLY
, DENY_NONE
);
1735 if (remote_handle
->fnum
== -1) {
1736 message_3s (1, MSG_ERROR
, _(" %s opening remote file %s "),
1737 cli_errstr(remote_handle
->cli
), CNV_LANG(rname
));
1738 DEBUG(1,("smbfs_open(rname:%s) error:%s\n",
1739 rname
, cli_errstr(remote_handle
->cli
)));
1740 my_errno
= cli_error(remote_handle
->cli
, NULL
, &err
, NULL
);
1744 if (!cli_qfileinfo(remote_handle
->cli
, remote_handle
->fnum
,
1745 &remote_handle
->attr
, &size
, NULL
, NULL
, NULL
, NULL
, NULL
) &&
1746 !cli_getattrE(remote_handle
->cli
, remote_handle
->fnum
,
1747 &remote_handle
->attr
, &size
, NULL
, NULL
, NULL
)) {
1748 message_2s (1, MSG_ERROR
, " getattrib: %s ",
1749 cli_errstr(remote_handle
->cli
));
1750 DEBUG(1,("smbfs_open(rname:%s) getattrib:%s\n",
1751 rname
, cli_errstr(remote_handle
->cli
)));
1752 my_errno
= cli_error(remote_handle
->cli
, NULL
, &err
, NULL
);
1756 return remote_handle
;
1760 smbfs_open (vfs
*me
, char *file
, int flags
, int mode
)
1762 char *remote_file
, *p
;
1764 smbfs_connection
*sc
;
1765 smbfs_handle
*remote_handle
;
1767 DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%d)\n", file
, flags
, mode
));
1769 if (!(remote_file
= smbfs_get_path (&sc
, file
)))
1773 convert_path(&remote_file
, FALSE
);
1776 remote_handle
= g_new (smbfs_handle
, 2);
1777 remote_handle
->cli
= sc
->cli
;
1778 remote_handle
->nread
= 0;
1780 if (flags
& O_CREAT
)
1781 ret
= open_write(remote_handle
, remote_file
, flags
, mode
);
1783 ret
= open_read(remote_handle
, remote_file
, flags
, mode
);
1785 g_free (remote_file
);
1791 smbfs_unlink (vfs
*me
, char *path
)
1793 smbfs_connection
*sc
;
1794 char *remote_file
, *p
;
1796 if ((remote_file
= smbfs_get_path (&sc
, path
)) == 0)
1800 convert_path(&remote_file
, FALSE
);
1803 if (!cli_unlink(sc
->cli
, remote_file
)) {
1804 message_3s (1, MSG_ERROR
, _(" %s removing remote file %s "),
1805 cli_errstr(sc
->cli
), CNV_LANG(remote_file
));
1806 g_free (remote_file
);
1809 g_free (remote_file
);
1814 smbfs_rename (vfs
*me
, char *a
, char *b
)
1816 smbfs_connection
*sc
;
1821 if ((ra
= smbfs_get_path (&sc
, a
)) == 0)
1824 if ((rb
= smbfs_get_path (&sc
, b
)) == 0) {
1830 convert_path(&ra
, FALSE
);
1833 convert_path(&rb
, FALSE
);
1836 retval
= cli_rename(sc
->cli
, ra
, rb
);
1842 message_2s (1, MSG_ERROR
, _(" %s renaming files\n"),
1843 cli_errstr(sc
->cli
));
1850 smbfs_fstat (void *data
, struct stat
*buf
)
1852 smbfs_handle
*remote_handle
= (smbfs_handle
*)data
;
1854 DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle
->fnum
));
1856 /* use left over from previous get_remote_stat, if available */
1858 memcpy(buf
, &single_entry
->my_stat
, sizeof(struct stat
));
1859 else { /* single_entry not set up: bug */
1866 vfs vfs_smbfs_ops
= {
1867 NULL
, /* This is place of next pointer */
1870 "smb:", /* prefix */
1909 smbfs_nothingisopen
,