1 /* Virtual File System: Midnight Commander file system.
3 Copyright (C) 1995, 1996, 1997 The Free Software Foundation
5 Written by Wayne Roberts <wroberts1@home.com>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License
9 as published by the Free Software Foundation; either version 2 of
10 the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* Namespace: exports vfs_smbfs_ops, smbfs_set_debug(), smbfs_set_debugf() */
24 #include <sys/types.h>
26 #undef USE_NCURSES /* Don't include *curses.h */
29 #undef PACKAGE_BUGREPORT
32 #undef PACKAGE_TARNAME
33 #undef PACKAGE_VERSION
35 #include "samba/include/config.h"
36 /* don't load crap in "samba/include/includes.h" we don't use and which
37 conflicts with definitions in other includes */
38 #undef HAVE_LIBREADLINE
42 #include "samba/include/includes.h"
48 #include "../src/dialog.h"
50 #define SMBFS_MAX_CONNECTIONS 16
51 static const char * const IPC
= "IPC$";
52 static const char * const URL_HEADER
= "/#smb:";
58 /* stuff that is same with each connection */
59 extern int DEBUGLEVEL
;
60 extern pstring myhostname
;
61 extern pstring global_myname
;
62 static int smbfs_open_connections
= 0;
63 static gboolean got_user
= FALSE
;
64 static gboolean got_pass
= FALSE
;
65 static pstring password
;
66 static pstring username
;
68 static struct _smbfs_connection
{
69 struct cli_state
*cli
;
70 struct in_addr dest_ip
;
72 char *host
; /* server name */
73 char *service
; /* share name */
81 } smbfs_connections
[SMBFS_MAX_CONNECTIONS
];
82 /* unique to each connection */
84 static struct cli_state
* smbfs_do_connect (const char *server
, char *share
);
86 typedef struct _smbfs_connection smbfs_connection
;
87 static smbfs_connection
*current_bucket
;
90 struct cli_state
*cli
;
96 static GSList
*auth_list
;
99 authinfo_free (struct smb_authinfo
const *a
)
105 wipe_password (a
->password
);
112 g_slist_foreach (auth_list
, (GFunc
)authinfo_free
, 0);
113 g_slist_free (auth_list
);
119 authinfo_compare_host_and_share (gconstpointer _a
, gconstpointer _b
)
121 struct smb_authinfo
const *a
= (struct smb_authinfo
const *)_a
;
122 struct smb_authinfo
const *b
= (struct smb_authinfo
const *)_b
;
124 if (!a
->host
|| !a
->share
|| !b
->host
|| !b
->share
)
126 if (strcmp (a
->host
, b
->host
) != 0)
128 if (strcmp (a
->share
, b
->share
) != 0)
134 authinfo_compare_host (gconstpointer _a
, gconstpointer _b
)
136 struct smb_authinfo
const *a
= (struct smb_authinfo
const *)_a
;
137 struct smb_authinfo
const *b
= (struct smb_authinfo
const *)_b
;
139 if (!a
->host
|| !b
->host
)
141 if (strcmp (a
->host
, b
->host
) != 0)
143 if (strcmp (a
->share
, IPC
) != 0)
149 authinfo_add (const char *host
, const char *share
, const char *domain
,
150 const char *user
, const char *password
)
152 struct smb_authinfo
*auth
= g_new (struct smb_authinfo
, 1);
157 /* Don't check for NULL, g_strdup already does. */
158 auth
->host
= g_strdup (host
);
159 auth
->share
= g_strdup (share
);
160 auth
->domain
= g_strdup (domain
);
161 auth
->user
= g_strdup (user
);
162 auth
->password
= g_strdup (password
);
163 auth_list
= g_slist_prepend (auth_list
, auth
);
167 authinfo_remove (const char *host
, const char *share
)
169 struct smb_authinfo data
;
170 struct smb_authinfo
*auth
;
173 data
.host
= g_strdup (host
);
174 data
.share
= g_strdup (share
);
175 list
= g_slist_find_custom (auth_list
,
177 authinfo_compare_host_and_share
);
183 auth_list
= g_slist_remove (auth_list
, auth
);
184 authinfo_free (auth
);
187 /* Set authentication information in bucket. Return 1 if successful, else 0 */
188 /* Information in auth_list overrides user if pass is NULL. */
189 /* bucket->host and bucket->service must be valid. */
191 bucket_set_authinfo (smbfs_connection
*bucket
,
192 const char *domain
, const char *user
, const char *pass
,
193 int fallback_to_host
)
195 struct smb_authinfo data
;
196 struct smb_authinfo
*auth
;
199 if (domain
&& user
&& pass
) {
200 g_free (bucket
->domain
);
201 g_free (bucket
->user
);
202 g_free (bucket
->password
);
203 bucket
->domain
= g_strdup (domain
);
204 bucket
->user
= g_strdup (user
);
205 bucket
->password
= g_strdup (pass
);
206 authinfo_remove (bucket
->host
, bucket
->service
);
207 authinfo_add (bucket
->host
, bucket
->service
,
212 data
.host
= bucket
->host
;
213 data
.share
= bucket
->service
;
214 list
= g_slist_find_custom (auth_list
, &data
, authinfo_compare_host_and_share
);
215 if (!list
&& fallback_to_host
)
216 list
= g_slist_find_custom (auth_list
, &data
, authinfo_compare_host
);
219 bucket
->domain
= g_strdup (auth
->domain
);
220 bucket
->user
= g_strdup (auth
->user
);
221 bucket
->password
= g_strdup (auth
->password
);
226 bucket
->domain
= g_strdup (lp_workgroup ());
227 bucket
->user
= g_strdup (got_user
? username
: user
);
228 bucket
->password
= g_strdup (password
);
232 auth
= vfs_smb_get_authinfo (bucket
->host
,
234 (domain
? domain
: lp_workgroup ()),
237 g_free (bucket
->domain
);
238 g_free (bucket
->user
);
239 g_free (bucket
->password
);
240 bucket
->domain
= g_strdup (auth
->domain
);
241 bucket
->user
= g_strdup (auth
->user
);
242 bucket
->password
= g_strdup (auth
->password
);
243 authinfo_remove (bucket
->host
, bucket
->service
);
244 auth_list
= g_slist_prepend (auth_list
, auth
);
251 smbfs_set_debug (int arg
)
257 smbfs_set_debugf (const char *filename
)
259 extern pstring debugf
;
261 if (DEBUGLEVEL
> 0) {
262 FILE *outfile
= fopen (filename
, "w");
264 setup_logging ("", True
); /* No needs for timestamp for each message */
267 pstrcpy (debugf
, filename
);
272 /********************** The callbacks ******************************/
274 smbfs_init (vfs
* me
)
276 char *servicesf
= CONFIGDIR PATH_SEP_STR
"smb.conf";
278 /* DEBUGLEVEL = 4; */
281 charset_initialise ();
283 DEBUG (3, ("smbfs_init(%s)\n", me
->name
));
285 if (!get_myname (myhostname
, NULL
))
286 DEBUG (0, ("Failed to get my hostname.\n"));
288 if (!lp_load (servicesf
, True
, False
, False
))
289 DEBUG (0, ("Cannot load %s - run testparm to debug it\n", servicesf
));
291 codepage_initialise (lp_client_code_page ());
295 if (getenv ("USER")) {
298 pstrcpy (username
, getenv ("USER"));
300 DEBUG (3, ("smbfs_init(): $USER:%s\n", username
));
301 if ((p
= strchr (username
, '%'))) {
303 pstrcpy (password
, p
+ 1);
305 memset (strchr (getenv ("USER"), '%') + 1, 'X', strlen (password
));
306 DEBUG (3, ("smbfs_init(): $USER%%pass: %s%%%s\n",
307 username
, password
));
311 if (getenv ("PASSWD")) {
312 pstrcpy (password
, getenv ("PASSWD"));
319 smbfs_fill_names (vfs
*me
, void (*func
)(char *))
323 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
324 if (smbfs_connections
[i
].cli
) {
325 path
= g_strconcat (URL_HEADER
,
326 smbfs_connections
[i
].host
,
327 "/", smbfs_connections
[i
].service
,
335 #define CNV_LANG(s) dos_to_unix(s,False)
336 #define GNAL_VNC(s) unix_to_dos(s,False)
337 /* does same as do_get() in client.c */
338 /* called from vfs.c:1080, count = buffer size */
340 smbfs_read (void *data
, char *buffer
, int count
)
342 smbfs_handle
*info
= (smbfs_handle
*) data
;
345 DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%d)\n",
346 info
->fnum
, (int)info
->nread
, count
));
347 n
= cli_read(info
->cli
, info
->fnum
, buffer
, info
->nread
, count
);
354 smbfs_write (void *data
, char *buf
, int nbyte
)
356 smbfs_handle
*info
= (smbfs_handle
*) data
;
359 DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%d)\n",
360 info
->fnum
, (int)info
->nread
, nbyte
));
361 n
= cli_write(info
->cli
, info
->fnum
, 0, buf
, info
->nread
, nbyte
);
368 smbfs_close (void *data
)
370 smbfs_handle
*info
= (smbfs_handle
*) data
;
371 DEBUG (3, ("smbfs_close(fnum:%d)\n", info
->fnum
));
373 /* FIXME: Why too different cli have the same outbuf
374 * if file is copied to share
376 if (info
->cli
->outbuf
== NULL
) {
381 /* if imlementing archive_level: add rname to smbfs_handle */
382 if (archive_level
>= 2 && (inf
->attr
& aARCH
)) {
383 cli_setatr (info
->cli
, rname
, info
->attr
& ~(uint16
) aARCH
, 0);
386 return (cli_close (info
->cli
, info
->fnum
) == True
) ? 0 : -1;
390 smbfs_errno (vfs
*me
)
392 DEBUG(3, ("smbfs_errno: %s\n", g_strerror(my_errno
)));
396 typedef struct dir_entry
{
398 struct dir_entry
*next
;
404 gboolean server_list
;
406 char *path
; /* the dir originally passed to smbfs_opendir */
407 smbfs_connection
*conn
;
416 *current_server_info
;
418 static gboolean first_direntry
;
421 new_dir_entry (const char * name
)
423 dir_entry
*new_entry
;
424 new_entry
= g_new0 (dir_entry
, 1);
425 new_entry
->text
= dos_to_unix (g_strdup (name
), 1);
427 if (first_direntry
) {
428 current_info
->entries
= new_entry
;
429 first_direntry
= FALSE
;
431 current_info
->current
->next
= new_entry
;
433 current_info
->current
= new_entry
;
438 /* browse for shares on server */
440 browsing_helper (const char *name
, uint32 type
, const char *comment
, void *state
)
444 dir_entry
*new_entry
= new_dir_entry (name
);
449 /* show this as dir */
450 new_entry
->my_stat
.st_mode
=
451 S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
| S_IXUSR
| S_IXGRP
|
464 DEBUG (3, ("\t%-15.15s%-10.10s%s\n", name
, typestr
, comment
));
468 loaddir_helper (file_info
* finfo
, const char *mask
, void *entry
)
470 dir_entry
*new_entry
= (dir_entry
*) entry
;
471 time_t t
= finfo
->mtime
; /* the time is assumed to be passed as GMT */
472 #if 0 /* I want to see dot files */
473 if (finfo
->mode
& aHIDDEN
)
474 return; /* don't bother with hidden files, "~$" screws up mc */
477 new_entry
= new_dir_entry (finfo
->name
);
479 new_entry
->my_stat
.st_size
= finfo
->size
;
480 new_entry
->my_stat
.st_mtime
= finfo
->mtime
;
481 new_entry
->my_stat
.st_atime
= finfo
->atime
;
482 new_entry
->my_stat
.st_ctime
= finfo
->ctime
;
483 new_entry
->my_stat
.st_uid
= finfo
->uid
;
484 new_entry
->my_stat
.st_gid
= finfo
->gid
;
486 new_entry
->my_stat
.st_mode
= /* rw-rw-rw */
487 S_IRUSR
| S_IRGRP
| S_IROTH
| S_IWUSR
| S_IWGRP
| S_IWOTH
;
489 /* if (finfo->mode & aVOLID); nothing similar in real world */
490 if (finfo
->mode
& aDIR
)
491 new_entry
->my_stat
.st_mode
|= /* drwxrwxrwx */
492 S_IFDIR
| S_IXUSR
| S_IXGRP
| S_IXOTH
;
494 new_entry
->my_stat
.st_mode
|= S_IFREG
; /* if not dir, regular file? */
495 /* if (finfo->mode & aARCH); DOS archive */
496 /* if (finfo->mode & aHIDDEN); like a dot file? */
497 /* if (finfo->mode & aSYSTEM); like a kernel? */
498 if (finfo
->mode
& aRONLY
)
499 new_entry
->my_stat
.st_mode
&= ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
501 DEBUG (entry
? 3 : 6, (" %-30s%7.7s%8.0f %s",
502 CNV_LANG (finfo
->name
),
503 attrib_string (finfo
->mode
),
504 (double) finfo
->size
,
505 asctime (LocalTime (&t
))));
508 /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
510 convert_path(char **remote_file
, gboolean trailing_asterik
)
514 my_remote
= *remote_file
;
515 if (strncmp (my_remote
, URL_HEADER
, HEADER_LEN
) == 0) { /* if passed directly */
517 if (*my_remote
== '/') /* from server browsing */
519 p
= strchr(my_remote
, '/');
521 my_remote
= p
+1; /* advance to end of server name */
524 if (*my_remote
== '/')
525 my_remote
++; /* strip off leading '/' */
526 p
= strchr(my_remote
, '/');
528 my_remote
= p
; /* strip off share/service name */
529 /* create remote filename as understood by smb clientgen */
530 p
= *remote_file
= g_strconcat (my_remote
, trailing_asterik
? "/*" : "", 0);
531 unix_to_dos (*remote_file
, 1);
532 while ((p
= strchr(p
, '/')))
538 server_browsing_helper (const char *name
, uint32 m
, const char *comment
, void *state
)
540 dir_entry
*new_entry
= new_dir_entry (name
);
542 /* show this as dir */
543 new_entry
->my_stat
.st_mode
=
544 S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
| S_IXUSR
| S_IXGRP
| S_IXOTH
;
546 DEBUG (3, ("\t%-16.16s %s\n", name
, comment
));
550 reconnect(smbfs_connection
*conn
, int *retries
)
553 DEBUG(3, ("RECONNECT\n"));
555 if (*(conn
->host
) == 0)
556 host
= g_strdup(conn
->cli
->desthost
); /* server browsing */
558 host
= g_strdup(conn
->host
);
560 cli_shutdown(conn
->cli
);
562 if (!(conn
->cli
= smbfs_do_connect(host
, conn
->service
))) {
563 message_2s (1, MSG_ERROR
,
564 _(" reconnect to %s failed\n "), conn
->host
);
569 if (++(*retries
) == 2)
575 smb_send(struct cli_state
*cli
)
581 len
= smb_len(cli
->outbuf
) + 4;
583 while (nwritten
< len
) {
584 ret
= write_socket(cli
->fd
, cli
->outbuf
+nwritten
, len
- nwritten
);
585 if (ret
<= 0 && errno
== EPIPE
)
593 /****************************************************************************
594 See if server has cut us off by checking for EPIPE when writing.
595 Taken from cli_chkpath()
596 ****************************************************************************/
598 chkpath(struct cli_state
*cli
, char *path
, BOOL send_only
)
604 unix_to_dos (path2
, 1);
605 trim_string(path2
,NULL
,"\\");
606 if (!*path2
) *path2
= '\\';
608 memset(cli
->outbuf
,'\0',smb_size
);
609 set_message(cli
->outbuf
,0,4 + strlen(path2
),True
);
610 SCVAL(cli
->outbuf
,smb_com
,SMBchkpth
);
611 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
615 SSVAL(cli
->outbuf
,smb_pid
,cli
->pid
);
616 SSVAL(cli
->outbuf
,smb_uid
,cli
->vuid
);
617 SSVAL(cli
->outbuf
,smb_mid
,cli
->mid
);
618 if (cli
->protocol
> PROTOCOL_CORE
) {
619 SCVAL(cli
->outbuf
,smb_flg
,0x8);
620 SSVAL(cli
->outbuf
,smb_flg2
,0x1);
623 p
= smb_buf(cli
->outbuf
);
627 if (!smb_send(cli
)) {
628 DEBUG(3, ("chkpath: couldnt send\n"));
632 client_receive_smb(cli
->fd
, cli
->inbuf
, cli
->timeout
);
633 DEBUG(3, ("chkpath: send only OK\n"));
634 return True
; /* just testing for EPIPE */
636 if (!client_receive_smb(cli
->fd
, cli
->inbuf
, cli
->timeout
)) {
637 DEBUG(3, ("chkpath: receive error\n"));
640 if ((my_errno
= cli_error(cli
, NULL
, NULL
, NULL
))) {
641 if (my_errno
== 20 || my_errno
== 13)
642 return True
; /* ignore if 'not a directory' error */
643 DEBUG(3, ("chkpath: cli_error: %s\n", g_strerror(my_errno
)));
652 fs (const char *text
)
654 const char *p
= text
;
657 while ((p
= strchr(p
, '/')) != NULL
) {
668 smbfs_loaddir (opendir_info
*smbfs_info
)
670 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
671 int servlen
= strlen(smbfs_info
->conn
->service
);
672 char *my_dirname
= smbfs_info
->dirname
;
674 DEBUG(3, ("smbfs_loaddir: dirname:%s\n", my_dirname
));
675 first_direntry
= TRUE
;
678 DEBUG(3, ("smbfs_loaddir: new:'%s', cached:'%s'\n", my_dirname
, current_info
->dirname
));
679 /* if new desired dir is longer than cached in current_info */
680 if (fs(my_dirname
) > fs(current_info
->dirname
)) {
681 DEBUG(3, ("saving to previous_info\n"));
682 previous_info
= current_info
;
686 current_info
= smbfs_info
;
688 if (strcmp(my_dirname
, "/") == 0) {
689 if (!strcmp(smbfs_info
->path
, URL_HEADER
)) {
690 DEBUG(6, ("smbfs_loaddir: browsing %s\n", IPC
));
691 /* browse for servers */
692 if (!cli_NetServerEnum(smbfs_info
->conn
->cli
, smbfs_info
->conn
->domain
,
693 SV_TYPE_ALL
, server_browsing_helper
, NULL
))
696 current_server_info
= smbfs_info
;
697 smbfs_info
->server_list
= TRUE
;
699 /* browse for shares */
700 if (cli_RNetShareEnum(smbfs_info
->conn
->cli
, browsing_helper
, NULL
) < 1)
703 current_share_info
= smbfs_info
;
708 /* do regular directory listing */
709 if(strncmp(smbfs_info
->conn
->service
, my_dirname
+1, servlen
) == 0) {
710 /* strip share name from dir */
711 char *p
= my_dirname
= g_strdup(my_dirname
+ servlen
);
713 convert_path(&my_dirname
, TRUE
);
716 convert_path(&my_dirname
, TRUE
);
718 DEBUG(6, ("smbfs_loaddir: service: %s\n", smbfs_info
->conn
->service
));
719 DEBUG(6, ("smbfs_loaddir: cli->share: %s\n", smbfs_info
->conn
->cli
->share
));
720 DEBUG(6, ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname
));
721 /* do file listing: cli_list returns number of files */
723 smbfs_info
->conn
->cli
, my_dirname
, attribute
, loaddir_helper
, NULL
) < 0) {
724 /* cli_list returns -1 if directory empty or cannot read socket */
725 my_errno
= cli_error(smbfs_info
->conn
->cli
, NULL
, &err
, NULL
);
729 if (*(my_dirname
) == 0)
730 smbfs_info
->dirname
= smbfs_info
->conn
->service
;
735 /* current_info->parent = smbfs_info->dirname; */
737 smbfs_info
->current
= smbfs_info
->entries
;
738 return 1; /* 1 = ok */
741 #ifdef SMBFS_FREE_DIR
743 smbfs_free_dir (dir_entry
*de
)
747 smbfs_free_dir (de
->next
);
754 /* The readdir routine loads the complete directory */
755 /* It's too slow to ask the server each time */
756 /* It now also sends the complete lstat information for each file */
758 smbfs_readdir(void *info
)
760 static union vfs_dirent smbfs_readdir_data
;
761 static char *const dirent_dest
= smbfs_readdir_data
.dent
.d_name
;
762 opendir_info
*smbfs_info
= (opendir_info
*) info
;
764 DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info
->dirname
));
766 if (!smbfs_info
->entries
)
767 if (!smbfs_loaddir(smbfs_info
))
770 if (smbfs_info
->current
== 0) { /* reached end of dir entries */
771 DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
772 #ifdef SMBFS_FREE_DIR
773 smbfs_free_dir(smbfs_info
->entries
);
774 smbfs_info
->entries
= 0;
778 strncpy(dirent_dest
, smbfs_info
->current
->text
, MC_MAXPATHLEN
);
779 dirent_dest
[MC_MAXPATHLEN
] = 0;
780 smbfs_info
->current
= smbfs_info
->current
->next
;
782 compute_namelen(&smbfs_readdir_data
.dent
);
784 return &smbfs_readdir_data
;
788 smbfs_closedir (void *info
)
790 opendir_info
*smbfs_info
= (opendir_info
*) info
;
791 /* dir_entry *p, *q; */
793 DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info
->dirname
));
796 /* for (p = smbfs_info->entries; p;){
807 smbfs_chmod (vfs
*me
, char *path
, int mode
)
809 DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path
, mode
));
810 /* my_errno = EOPNOTSUPP;
811 return -1; */ /* cant chmod on smb filesystem */
812 return 0; /* make mc happy */
816 smbfs_chown (vfs
*me
, char *path
, int owner
, int group
)
818 DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path
, owner
, group
));
819 my_errno
= EOPNOTSUPP
; /* ready for your labotomy? */
824 smbfs_utime (vfs
*me
, char *path
, struct utimbuf
*times
)
826 DEBUG(3, ("smbfs_utime(path:%s)\n", path
));
827 my_errno
= EOPNOTSUPP
;
832 smbfs_readlink (vfs
*me
, char *path
, char *buf
, int size
)
834 DEBUG(3, ("smbfs_readlink(path:%s, buf:%s, size:%d)\n", path
, buf
, size
));
835 my_errno
= EOPNOTSUPP
;
836 return -1; /* no symlinks on smb filesystem? */
840 smbfs_symlink (vfs
*me
, char *n1
, char *n2
)
842 DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1
, n2
));
843 my_errno
= EOPNOTSUPP
;
844 return -1; /* no symlinks on smb filesystem? */
847 /* Extract the hostname and username from the path */
848 /* path is in the form: hostname:user/remote-dir */
851 smbfs_get_host_and_username
852 (char **path
, char **host
, char **user
, int *port
, char **pass
)
857 ret
= vfs_split_url (*path
, host
, user
, port
, pass
, SMB_PORT
, 0);
860 if ((p
= strchr(*path
, '@'))) /* user:pass@server */
861 *path
= ++p
; /* don't want user:pass@ in path */
862 if ((p
= strchr(ret
, '@'))) /* user:pass@server */
863 ret
= ++p
; /* don't want user:pass@ in path */
869 #define smbfs_get_host_and_username(path, host, user, port, pass) \
870 vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
872 /*****************************************************
873 return a connection to a SMB server
874 current_bucket needs to be set before calling
875 *******************************************************/
876 static struct cli_state
*
877 smbfs_do_connect (const char *server
, char *share
)
880 struct nmb_name called
, calling
;
882 extern struct in_addr ipzero
;
884 DEBUG(3, ("smbfs_do_connect(%s, %s)\n", server
, share
));
885 if (*share
== '\\') {
887 share
= strchr(server
,'\\');
888 if (!share
) return NULL
;
893 make_nmb_name(&calling
, global_myname
, 0x0);
894 make_nmb_name(&called
, server
, current_bucket
->name_type
);
898 ip
= (current_bucket
->have_ip
) ? current_bucket
->dest_ip
: ipzero
;
900 /* have to open a new connection */
901 if (!(c
= cli_initialise(NULL
))) {
906 pwd_init(&(c
->pwd
)); /* should be moved into cli_initialise()? */
907 pwd_set_cleartext(&(c
->pwd
), current_bucket
->password
);
909 if ((cli_set_port(c
, current_bucket
->port
) == 0) ||
910 !cli_connect(c
, server
, &ip
)) {
911 DEBUG(1, ("Connection to %s failed\n", server
));
915 if (!cli_session_request(c
, &calling
, &called
)) {
916 my_errno
= cli_error(c
, NULL
, &err
, NULL
);
917 DEBUG(1, ("session request to %s failed\n", called
.name
));
919 if (strcmp(called
.name
, "*SMBSERVER")) {
920 make_nmb_name(&called
, "*SMBSERVER", 0x20);
926 DEBUG(3, (" session request ok\n"));
928 if (!cli_negprot(c
)) {
929 DEBUG(1, ("protocol negotiation failed\n"));
933 if (!cli_session_setup(c
, current_bucket
->user
,
934 current_bucket
->password
, strlen(current_bucket
->password
),
935 current_bucket
->password
, strlen(current_bucket
->password
),
936 current_bucket
->domain
)) {
937 DEBUG(1,("session setup failed: %s\n", cli_errstr(c
)));
938 authinfo_remove (server
, share
);
942 if (*c
->server_domain
|| *c
->server_os
|| *c
->server_type
)
943 DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
944 c
->server_domain
,c
->server_os
,c
->server_type
));
946 DEBUG(3, (" session setup ok\n"));
948 if (!cli_send_tconX(c
, share
, "?????",
949 current_bucket
->password
, strlen(current_bucket
->password
)+1)) {
950 DEBUG(1,("%s: tree connect failed: %s\n", share
, cli_errstr(c
)));
954 DEBUG(3, (" tconx ok\n"));
960 my_errno
= cli_error(c
, NULL
, &err
, NULL
);
967 get_master_browser(char **host
)
970 struct in_addr
*ip_list
, bcast_addr
;
971 extern struct in_addr ipzero
;
973 /* does port = 137 for win95 master browser? */
974 int fd
= open_socket_in( SOCK_DGRAM
, 0, 3,
975 interpret_addr(lp_socket_address()), True
);
978 set_socket_options(fd
, "SO_BROADCAST");
979 ip_list
= iface_bcast(ipzero
);
980 bcast_addr
= *ip_list
;
981 if ((ip_list
= name_query(fd
, "\01\02__MSBROWSE__\02", 1, True
,
982 True
, bcast_addr
, &count
, NULL
))) {
985 /* just return first master browser */
986 *host
= g_strdup(inet_ntoa(ip_list
[0]));
993 free_bucket (smbfs_connection
*bucket
)
995 g_free (bucket
->host
);
996 g_free (bucket
->service
);
997 g_free (bucket
->domain
);
998 g_free (bucket
->user
);
999 wipe_password (bucket
->password
);
1000 if (bucket
->home
) g_free (bucket
->home
);
1001 memset (bucket
, 0, sizeof (smbfs_connection
));
1004 static smbfs_connection
*
1005 smbfs_get_free_bucket ()
1009 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++)
1010 if (!smbfs_connections
[i
].cli
) return &smbfs_connections
[i
];
1012 { /* search for most dormant connection */
1013 int oldest
= 0; /* index */
1014 time_t oldest_time
= smbfs_connections
[0].last_use
;
1015 for (i
= 1; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
1016 if (smbfs_connections
[i
].last_use
< oldest_time
) {
1017 oldest_time
= smbfs_connections
[i
].last_use
;
1021 cli_shutdown(smbfs_connections
[oldest
].cli
);
1022 free_bucket (&smbfs_connections
[oldest
]);
1023 return &smbfs_connections
[oldest
];
1026 /* This can't happend, since we have checked for max connections before */
1027 vfs_die("Internal error: smbfs_get_free_bucket");
1028 return 0; /* shut up, stupid gcc */
1031 /* This routine keeps track of open connections */
1032 /* Returns a connected socket to host */
1033 static smbfs_connection
*
1034 smbfs_open_link(char *host
, char *path
, const char *user
, int *port
, char *this_pass
)
1037 smbfs_connection
*bucket
;
1039 struct in_addr
*dest_ip
= NULL
;
1041 DEBUG(3, ("smbfs_open_link(host:%s, path:%s)\n", host
, path
));
1043 if (strcmp(host
, path
) == 0) /* if host & path are same: */
1044 pstrcpy(service
, IPC
); /* setup for browse */
1045 else { /* get share name from path, path starts with server name */
1047 if ((p
= strchr(path
, '/'))) /* get share aka */
1048 pstrcpy(service
, ++p
); /* service name from path */
1050 pstrcpy(service
, "");
1051 /* now check for trailing directory/filenames */
1052 p
= strchr(service
, '/');
1054 *p
= 0; /* cut off dir/files: sharename only */
1056 DEBUG(6, ("smbfs_open_link: service from path:%s\n", service
));
1060 user
= username
; /* global from getenv */
1062 /* Is the link actually open? */
1063 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
1064 if (!smbfs_connections
[i
].cli
)
1066 if ((strcmp (host
, smbfs_connections
[i
].host
) == 0) &&
1067 (strcmp (user
, smbfs_connections
[i
].user
) == 0) &&
1068 (strcmp (service
, smbfs_connections
[i
].service
) == 0)) {
1070 BOOL inshare
= (*host
!= 0 && *path
!= 0 && strchr(path
, '/'));
1071 /* check if this connection has died */
1072 while (!chkpath(smbfs_connections
[i
].cli
, "\\", !inshare
)) {
1073 if (!reconnect(&smbfs_connections
[i
], &retries
))
1076 DEBUG(6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i
));
1077 current_bucket
= &smbfs_connections
[i
];
1078 smbfs_connections
[i
].last_use
= time(NULL
);
1079 return &smbfs_connections
[i
];
1081 /* connection not found, find if we have ip for new connection */
1082 if (strcmp (host
, smbfs_connections
[i
].host
) == 0)
1083 dest_ip
= &smbfs_connections
[i
].cli
->dest_ip
;
1086 /* make new connection */
1087 bucket
= smbfs_get_free_bucket ();
1088 bucket
->name_type
= 0x20;
1090 bucket
->port
= *port
;
1091 bucket
->have_ip
= False
;
1093 bucket
->have_ip
= True
;
1094 bucket
->dest_ip
= *dest_ip
;
1096 current_bucket
= bucket
;
1098 bucket
->user
= g_strdup(user
);
1099 bucket
->service
= g_strdup (service
);
1101 if (!(*host
)) { /* if blank host name, browse for servers */
1102 if (!get_master_browser(&host
)) /* set host to ip of master browser */
1103 return 0; /* couldnt find master browser? */
1105 bucket
->host
= g_strdup(""); /* blank host means master browser */
1107 bucket
->host
= g_strdup(host
);
1109 if (!bucket_set_authinfo (bucket
,
1110 0, /* domain currently not used */
1116 /* connect to share */
1117 while (!(bucket
->cli
= smbfs_do_connect(host
, service
))) {
1119 if (my_errno
!= EPERM
)
1121 message_1s (1, MSG_ERROR
,
1122 _(" Authentication failed "));
1124 /* authentication failed, try again */
1125 authinfo_remove (bucket
->host
, bucket
->service
);
1126 if (!bucket_set_authinfo (bucket
,
1135 smbfs_open_connections
++;
1136 DEBUG(3, ("smbfs_open_link:smbfs_open_connections: %d\n",
1137 smbfs_open_connections
));
1142 smbfs_get_path(smbfs_connection
**sc
, char *path
)
1144 char *user
, *host
, *remote_path
, *pass
;
1145 int port
= SMB_PORT
;
1147 DEBUG(3, ("smbfs_get_path(%s)\n", path
));
1148 if (strncmp (path
, URL_HEADER
, HEADER_LEN
))
1152 if (*path
== '/') /* '/' leading server name */
1153 path
++; /* probably came from server browsing */
1155 if ((remote_path
= smbfs_get_host_and_username(
1156 &path
, &host
, &user
, &port
, &pass
)))
1157 if ((*sc
= smbfs_open_link (host
, path
, user
, &port
, pass
)) == NULL
){
1158 g_free (remote_path
);
1163 if (pass
) wipe_password (pass
);
1165 if (!remote_path
) return NULL
;
1167 /* NOTE: tildes are deprecated. See ftpfs.c */
1169 int f
= !strcmp( remote_path
, "/~" );
1170 if (f
|| !strncmp( remote_path
, "/~/", 3 )) {
1172 s
= concat_dir_and_file( (*sc
)->home
, remote_path
+3-f
);
1173 g_free (remote_path
);
1182 is_error (int result
, int errno_num
)
1184 if (!(result
== -1))
1185 return my_errno
= 0;
1187 my_errno
= errno_num
;
1193 smbfs_opendir (vfs
*me
, char *dirname
)
1195 opendir_info
*smbfs_info
;
1196 smbfs_connection
*sc
;
1199 DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname
));
1201 if (!(remote_dir
= smbfs_get_path (&sc
, dirname
)))
1204 /* FIXME: where freed? */
1205 smbfs_info
= g_new (opendir_info
, 1);
1206 smbfs_info
->server_list
= FALSE
;
1207 smbfs_info
->path
= g_strdup(dirname
); /* keep original */
1208 smbfs_info
->dirname
= remote_dir
;
1209 smbfs_info
->conn
= sc
;
1210 smbfs_info
->entries
= 0;
1211 smbfs_info
->current
= 0;
1217 fake_server_stat(const char *server_url
, const char *path
, struct stat
*buf
)
1222 if ((p
= strrchr(path
, '/')))
1223 path
= p
+ 1; /* advance until last '/' */
1225 if (!current_info
->entries
) {
1226 if (!smbfs_loaddir(current_info
)); /* browse host */
1230 if (current_info
->server_list
== True
) {
1231 dentry
= current_info
->entries
;
1232 DEBUG(4, ("fake stat for SERVER \"%s\"\n", path
));
1234 if (strcmp(dentry
->text
, path
) == 0) {
1235 DEBUG(4, ("fake_server_stat: %s:%4o\n",
1236 dentry
->text
, dentry
->my_stat
.st_mode
));
1237 memcpy(buf
, &dentry
->my_stat
, sizeof(struct stat
));
1240 dentry
= dentry
->next
;
1248 fake_share_stat(const char *server_url
, const char *path
, struct stat
*buf
)
1251 if (strlen(path
) < strlen(server_url
))
1253 path
+= strlen(server_url
); /* we only want share name */
1256 if (*path
== '/') /* '/' leading server name */
1257 path
++; /* probably came from server browsing */
1259 if (!current_share_info
->entries
) {
1260 if (!smbfs_loaddir(current_share_info
)); /* browse host */
1263 dentry
= current_share_info
->entries
;
1264 DEBUG(3, ("fake_share_stat: %s on %s\n", path
, server_url
));
1266 if (strcmp(dentry
->text
, path
) == 0) {
1267 DEBUG(6, ("fake_share_stat: %s:%4o\n",
1268 dentry
->text
, dentry
->my_stat
.st_mode
));
1269 memcpy(buf
, &dentry
->my_stat
, sizeof(struct stat
));
1272 dentry
= dentry
->next
;
1278 /* stat a single file, get_remote_stat callback */
1279 static dir_entry
*single_entry
;
1281 /* stat a single file */
1283 get_remote_stat (smbfs_connection
* sc
, char *path
, struct stat
*buf
)
1285 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
1286 char *mypath
= path
;
1288 DEBUG (3, ("get_remote_stat(): mypath:%s\n", mypath
));
1290 convert_path (&mypath
, FALSE
);
1292 #if 0 /* single_entry is never free()d now. And only my_stat is used */
1293 single_entry
= g_new (dir_entry
, 1);
1295 single_entry
->text
= dos_to_unix (g_strdup (finfo
->name
), 1);
1297 single_entry
->next
= 0;
1300 single_entry
= g_new0 (dir_entry
, 1);
1303 (sc
->cli
, mypath
, attribute
, loaddir_helper
, single_entry
) < 1) {
1306 return -1; /* cli_list returns number of files */
1309 memcpy (buf
, &single_entry
->my_stat
, sizeof (struct stat
));
1311 /* don't free here, use for smbfs_fstat() */
1312 /* g_free(single_entry->text);
1313 g_free(single_entry); */
1319 search_dir_entry (dir_entry
*dentry
, const char *text
, struct stat
*buf
)
1322 if (strcmp(text
, dentry
->text
) == 0) {
1323 memcpy(buf
, &dentry
->my_stat
, sizeof(struct stat
));
1324 memcpy(&single_entry
->my_stat
, &dentry
->my_stat
,
1325 sizeof(struct stat
));
1328 dentry
= dentry
->next
;
1334 get_stat_info (smbfs_connection
* sc
, char *path
, struct stat
*buf
)
1338 dir_entry
*dentry
= current_info
->entries
;
1340 const char *mypath
= path
;
1342 mypath
++; /* cut off leading '/' */
1343 if ((p
= strrchr (mypath
, '/')))
1344 mypath
= p
+ 1; /* advance until last file/dir name */
1345 DEBUG (3, ("get_stat_info: mypath:%s, current_info->dirname:%s\n",
1346 mypath
, current_info
->dirname
));
1349 DEBUG (1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n",
1350 current_info
->dirname
, path
));
1354 if (!single_entry
) /* when found, this will be written too */
1355 single_entry
= g_new (dir_entry
, 1);
1356 if (search_dir_entry (current_info
->entries
, mypath
, buf
) == 0) {
1359 /* now try to identify mypath as PARENT dir */
1363 mdp
= mydir
= g_strdup (current_info
->dirname
);
1364 if ((p
= strrchr (mydir
, '/')))
1365 *p
= 0; /* advance util last '/' */
1366 if ((p
= strrchr (mydir
, '/')))
1367 mydir
= p
+ 1; /* advance util last '/' */
1368 if (strcmp (mydir
, mypath
) == 0) { /* fake a stat for ".." */
1369 memset (buf
, 0, sizeof (struct stat
));
1370 buf
->st_mode
= S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
;
1371 memcpy (&single_entry
->my_stat
, buf
, sizeof (struct stat
));
1373 DEBUG (1, (" PARENT:found in %s\n", current_info
->dirname
));
1378 /* now try to identify as CURRENT dir? */
1380 char *dnp
= current_info
->dirname
;
1381 DEBUG (6, ("get_stat_info: is %s current dir? this dir is: %s\n",
1382 mypath
, current_info
->dirname
));
1388 if (strcmp (mypath
, dnp
) == 0) {
1389 memset (buf
, 0, sizeof (struct stat
));
1390 buf
->st_mode
= S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
;
1391 memcpy (&single_entry
->my_stat
, buf
, sizeof (struct stat
));
1392 DEBUG (1, (" CURRENT:found in %s\n", current_info
->dirname
));
1396 DEBUG (3, ("'%s' not found in current_info '%s'\n", path
,
1397 current_info
->dirname
));
1398 /* try to find this in the PREVIOUS listing */
1399 if (previous_info
) {
1400 if (search_dir_entry (previous_info
->entries
, mypath
, buf
) == 0)
1402 DEBUG (3, ("'%s' not found in previous_info '%s'\n", path
,
1403 previous_info
->dirname
));
1405 /* try to find this in the SHARE listing */
1406 if (current_share_info
) {
1407 if (search_dir_entry (current_share_info
->entries
, mypath
, buf
) == 0)
1409 DEBUG (3, ("'%s' not found in share_info '%s'\n", path
,
1410 current_share_info
->dirname
));
1412 /* try to find this in the SERVER listing */
1413 if (current_server_info
) {
1414 if (search_dir_entry (current_server_info
->entries
, mypath
, buf
) == 0)
1416 DEBUG (3, ("'%s' not found in server_info '%s'\n", path
,
1417 current_server_info
->dirname
));
1419 /* nothing found. get stat file info from server */
1420 return get_remote_stat (sc
, path
, buf
);
1424 smbfs_chdir (vfs
*me
, char *path
)
1427 smbfs_connection
*sc
;
1429 DEBUG(3, ("smbfs_chdir(path:%s)\n", path
));
1430 if (!(remote_dir
= smbfs_get_path (&sc
, path
)))
1432 g_free (remote_dir
);
1438 loaddir(vfs
*me
, const char *path
)
1443 mypath
= g_strdup(path
);
1444 p
= strrchr(mypath
, '/');
1448 DEBUG(6, ("loaddir(%s)\n", mypath
));
1449 smbfs_chdir(me
, mypath
);
1450 info
= smbfs_opendir (me
, mypath
);
1454 smbfs_readdir(info
);
1455 smbfs_loaddir(info
);
1460 smbfs_stat (vfs
*me
, char *path
, struct stat
*buf
)
1463 smbfs_connection
*sc
;
1468 DEBUG(3, ("smbfs_stat(path:%s)\n", path
));
1471 if (p
= strchr(path
, '@')) /* user:pass@server */
1472 path
= ++p
; /* don't want user:pass@ in path */
1475 if (!current_info
) {
1476 DEBUG(1, ("current_info = NULL: "));
1477 if (loaddir(me
, path
) < 0)
1481 pstrcpy(server_url
, URL_HEADER
);
1482 pstrcat(server_url
, current_bucket
->host
);
1484 /* check if stating server */
1486 if (strncmp(p
, URL_HEADER
, HEADER_LEN
)) {
1487 DEBUG(1, ("'%s' doesnt start with '%s' (length %d)\n",
1488 p
, URL_HEADER
, HEADER_LEN
));
1495 pp
= strchr(p
, '/'); /* advance past next '/' */
1497 if (!current_info
->server_list
) {
1498 if (loaddir(me
, path
) < 0)
1501 return fake_server_stat(server_url
, path
, buf
);
1503 if (!strchr(++pp
, '/')) {
1504 return fake_share_stat(server_url
, path
, buf
);
1507 /* stating inside share at this point */
1508 if (!(remote_dir
= smbfs_get_path (&sc
, path
))) /* connects if necessary */
1510 g_free (remote_dir
);
1512 int hostlen
= strlen(current_bucket
->host
);
1513 char *pp
= path
+ strlen(path
)-hostlen
;
1514 char *sp
= server_url
+ strlen(server_url
)-hostlen
;
1516 if (strcmp(sp
, pp
) == 0) {
1517 /* make server name appear as directory */
1518 DEBUG(1, ("smbfs_stat: showing server as directory\n"));
1519 memset(buf
, 0, sizeof(struct stat
));
1520 buf
->st_mode
= S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
;
1524 /* check if current_info is in share requested */
1525 p
= service
= g_strdup(p
);
1526 pp
= strchr(p
, '/');
1528 p
= ++pp
; /* advance past server name */
1529 pp
= strchr(p
, '/');
1532 *pp
= 0; /* cut off everthing after service name */
1534 p
= IPC
; /* browsing for services */
1535 pp
= current_info
->dirname
;
1538 if (strncmp(p
, pp
, strlen(p
)) != 0) {
1539 DEBUG(6, ("desired '%s' is not loaded, we have '%s'\n", p
, pp
));
1540 if (loaddir(me
, path
) < 0) {
1544 DEBUG(6, ("loaded dir: '%s'\n", current_info
->dirname
));
1547 /* stat dirs & files under shares now */
1548 return get_stat_info(sc
, path
, buf
);
1551 #define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */
1554 smbfs_lseek (void *data
, off_t offset
, int whence
)
1556 DEBUG(3, ("smbfs_lseek()\n"));
1557 my_errno
= EOPNOTSUPP
;
1562 smbfs_mknod (vfs
*me
, char *path
, int mode
, int dev
)
1564 DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%d)\n", path
, mode
, dev
));
1565 my_errno
= EOPNOTSUPP
;
1570 smbfs_mkdir (vfs
*me
, char *path
, mode_t mode
)
1572 smbfs_connection
*sc
;
1575 DEBUG(3, ("smbfs_mkdir(path:%s, mode:%d)\n", path
, mode
));
1576 if ((remote_file
= smbfs_get_path (&sc
, path
)) == 0)
1578 g_free (remote_file
);
1579 convert_path(&path
, FALSE
);
1581 if (!cli_mkdir(sc
->cli
, path
)) {
1582 my_errno
= cli_error(sc
->cli
, NULL
, &err
, NULL
);
1583 message_3s (1, MSG_ERROR
, _(" Error %s creating directory %s "),
1584 cli_errstr(sc
->cli
), CNV_LANG(path
));
1593 smbfs_rmdir (vfs
*me
, char *path
)
1595 smbfs_connection
*sc
;
1598 DEBUG(3, ("smbfs_rmdir(path:%s)\n", path
));
1599 if ((remote_file
= smbfs_get_path (&sc
, path
)) == 0)
1601 g_free (remote_file
);
1602 convert_path(&path
, FALSE
);
1604 if (!cli_rmdir(sc
->cli
, path
)) {
1605 my_errno
= cli_error(sc
->cli
, NULL
, &err
, NULL
);
1606 message_3s (1, MSG_ERROR
, _(" Error %s removing directory %s "),
1607 cli_errstr(sc
->cli
), CNV_LANG(path
));
1617 smbfs_link (vfs
*me
, char *p1
, char *p2
)
1619 DEBUG(3, ("smbfs_link(p1:%s, p2:%s)\n", p1
, p2
));
1620 my_errno
= EOPNOTSUPP
;
1624 /* We do not free anything right now: we free resources when we run
1628 smbfs_getid (vfs
*me
, char *p
, struct vfs_stamping
**parent
)
1631 DEBUG(3, ("smbfs_getid(p:%s)\n", p
));
1637 smbfs_nothingisopen (vfsid id
)
1639 DEBUG(3, ("smbfs_nothingisopen(%d)\n", (int)id
));
1644 smbfs_free (vfsid id
)
1646 DEBUG(3, ("smbfs_free(%d)\n", (int)id
));
1647 /* FIXME: Should not be empty */
1648 authinfo_free_all ();
1651 /* Gives up on a socket and reopens the connection, the child own the socket
1655 my_forget (char *path
)
1657 char *host
, *user
, *p
;
1660 if (strncmp (path
, URL_HEADER
, HEADER_LEN
))
1663 DEBUG(3, ("my_forget(path:%s)\n", path
));
1666 if (path
[0] == '/' && path
[1] == '/')
1669 if ((p
= smbfs_get_host_and_username (&path
, &host
, &user
, &port
, NULL
))) {
1671 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
1672 if ((strcmp (host
, smbfs_connections
[i
].host
) == 0) &&
1673 (strcmp (user
, smbfs_connections
[i
].user
) == 0) &&
1674 (port
== smbfs_connections
[i
].port
)) {
1676 /* close socket: the child owns it now */
1677 cli_shutdown(smbfs_connections
[i
].cli
);
1679 /* reopen the connection */
1680 smbfs_connections
[i
].cli
=
1681 smbfs_do_connect(host
, smbfs_connections
[i
].service
);
1690 smbfs_setctl (vfs
*me
, char *path
, int ctlop
, char *arg
)
1692 DEBUG(3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path
, ctlop
));
1694 case MCCTL_FORGET_ABOUT
:
1701 static smbfs_handle
*
1702 open_write (smbfs_handle
*remote_handle
, char *rname
, int flags
, int mode
)
1704 if (flags
& O_TRUNC
) /* if it exists truncate to zero */
1705 DEBUG(3, ("open_write: O_TRUNC\n"));
1707 remote_handle
->fnum
= cli_open(remote_handle
->cli
, rname
, flags
, DENY_NONE
);
1709 if (remote_handle
->fnum
== -1) {
1710 message_3s (1, MSG_ERROR
, _(" %s opening remote file %s "),
1711 cli_errstr(remote_handle
->cli
), CNV_LANG(rname
));
1712 DEBUG(1,("smbfs_open(rname:%s) error:%s\n",
1713 rname
, cli_errstr(remote_handle
->cli
)));
1714 my_errno
= cli_error(remote_handle
->cli
, NULL
, &err
, NULL
);
1718 return remote_handle
;
1721 static smbfs_handle
*
1722 open_read (smbfs_handle
*remote_handle
, char *rname
, int flags
, int mode
)
1726 remote_handle
->fnum
=
1727 cli_open(remote_handle
->cli
, rname
, O_RDONLY
, DENY_NONE
);
1729 if (remote_handle
->fnum
== -1) {
1730 message_3s (1, MSG_ERROR
, _(" %s opening remote file %s "),
1731 cli_errstr(remote_handle
->cli
), CNV_LANG(rname
));
1732 DEBUG(1,("smbfs_open(rname:%s) error:%s\n",
1733 rname
, cli_errstr(remote_handle
->cli
)));
1734 my_errno
= cli_error(remote_handle
->cli
, NULL
, &err
, NULL
);
1738 if (!cli_qfileinfo(remote_handle
->cli
, remote_handle
->fnum
,
1739 &remote_handle
->attr
, &size
, NULL
, NULL
, NULL
, NULL
, NULL
) &&
1740 !cli_getattrE(remote_handle
->cli
, remote_handle
->fnum
,
1741 &remote_handle
->attr
, &size
, NULL
, NULL
, NULL
)) {
1742 message_2s (1, MSG_ERROR
, " getattrib: %s ",
1743 cli_errstr(remote_handle
->cli
));
1744 DEBUG(1,("smbfs_open(rname:%s) getattrib:%s\n",
1745 rname
, cli_errstr(remote_handle
->cli
)));
1746 my_errno
= cli_error(remote_handle
->cli
, NULL
, &err
, NULL
);
1750 return remote_handle
;
1754 smbfs_open (vfs
*me
, char *file
, int flags
, int mode
)
1756 char *remote_file
, *p
;
1758 smbfs_connection
*sc
;
1759 smbfs_handle
*remote_handle
;
1761 DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%d)\n", file
, flags
, mode
));
1763 if (!(remote_file
= smbfs_get_path (&sc
, file
)))
1767 convert_path(&remote_file
, FALSE
);
1770 remote_handle
= g_new (smbfs_handle
, 2);
1771 remote_handle
->cli
= sc
->cli
;
1772 remote_handle
->nread
= 0;
1774 if (flags
& O_CREAT
)
1775 ret
= open_write(remote_handle
, remote_file
, flags
, mode
);
1777 ret
= open_read(remote_handle
, remote_file
, flags
, mode
);
1779 g_free (remote_file
);
1785 smbfs_unlink (vfs
*me
, char *path
)
1787 smbfs_connection
*sc
;
1788 char *remote_file
, *p
;
1790 if ((remote_file
= smbfs_get_path (&sc
, path
)) == 0)
1794 convert_path(&remote_file
, FALSE
);
1797 if (!cli_unlink(sc
->cli
, remote_file
)) {
1798 message_3s (1, MSG_ERROR
, _(" %s removing remote file %s "),
1799 cli_errstr(sc
->cli
), CNV_LANG(remote_file
));
1800 g_free (remote_file
);
1803 g_free (remote_file
);
1808 smbfs_rename (vfs
*me
, char *a
, char *b
)
1810 smbfs_connection
*sc
;
1815 if ((ra
= smbfs_get_path (&sc
, a
)) == 0)
1818 if ((rb
= smbfs_get_path (&sc
, b
)) == 0) {
1824 convert_path(&ra
, FALSE
);
1827 convert_path(&rb
, FALSE
);
1830 retval
= cli_rename(sc
->cli
, ra
, rb
);
1836 message_2s (1, MSG_ERROR
, _(" %s renaming files\n"),
1837 cli_errstr(sc
->cli
));
1844 smbfs_fstat (void *data
, struct stat
*buf
)
1846 smbfs_handle
*remote_handle
= (smbfs_handle
*)data
;
1848 DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle
->fnum
));
1850 /* use left over from previous get_remote_stat, if available */
1852 memcpy(buf
, &single_entry
->my_stat
, sizeof(struct stat
));
1853 else { /* single_entry not set up: bug */
1860 vfs vfs_smbfs_ops
= {
1861 NULL
, /* This is place of next pointer */
1864 "smb:", /* prefix */
1903 smbfs_nothingisopen
,