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(), smbfs_set_debugf() */
26 #include <sys/types.h>
27 #undef USE_NCURSES /* Don't include *curses.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 extern pstring global_myname
;
64 static int smbfs_open_connections
= 0;
65 static gboolean got_user
= FALSE
;
66 static gboolean got_pass
= FALSE
;
67 static pstring password
;
68 static pstring username
;
70 static struct _smbfs_connection
{
71 struct cli_state
*cli
;
72 struct in_addr dest_ip
;
74 char *host
; /* server name */
75 char *service
; /* share name */
83 } smbfs_connections
[SMBFS_MAX_CONNECTIONS
];
84 /* unique to each connection */
86 static struct cli_state
* smbfs_do_connect (const char *server
, char *share
);
88 typedef struct _smbfs_connection smbfs_connection
;
89 static smbfs_connection
*current_bucket
;
92 struct cli_state
*cli
;
98 static GSList
*auth_list
;
101 authinfo_free (struct smb_authinfo
const *a
)
107 wipe_password (a
->password
);
114 g_slist_foreach (auth_list
, (GFunc
)authinfo_free
, 0);
115 g_slist_free (auth_list
);
121 authinfo_compare_host_and_share (gconstpointer _a
, gconstpointer _b
)
123 struct smb_authinfo
const *a
= (struct smb_authinfo
const *)_a
;
124 struct smb_authinfo
const *b
= (struct smb_authinfo
const *)_b
;
126 if (!a
->host
|| !a
->share
|| !b
->host
|| !b
->share
)
128 if (strcmp (a
->host
, b
->host
) != 0)
130 if (strcmp (a
->share
, b
->share
) != 0)
136 authinfo_compare_host (gconstpointer _a
, gconstpointer _b
)
138 struct smb_authinfo
const *a
= (struct smb_authinfo
const *)_a
;
139 struct smb_authinfo
const *b
= (struct smb_authinfo
const *)_b
;
141 if (!a
->host
|| !b
->host
)
143 if (strcmp (a
->host
, b
->host
) != 0)
145 if (strcmp (a
->share
, IPC
) != 0)
151 authinfo_add (const char *host
, const char *share
, const char *domain
,
152 const char *user
, const char *password
)
154 struct smb_authinfo
*auth
= g_new (struct smb_authinfo
, 1);
159 /* Don't check for NULL, g_strdup already does. */
160 auth
->host
= g_strdup (host
);
161 auth
->share
= g_strdup (share
);
162 auth
->domain
= g_strdup (domain
);
163 auth
->user
= g_strdup (user
);
164 auth
->password
= g_strdup (password
);
165 auth_list
= g_slist_prepend (auth_list
, auth
);
169 authinfo_remove (const char *host
, const char *share
)
171 struct smb_authinfo data
;
172 struct smb_authinfo
*auth
;
175 data
.host
= g_strdup (host
);
176 data
.share
= g_strdup (share
);
177 list
= g_slist_find_custom (auth_list
,
179 authinfo_compare_host_and_share
);
185 auth_list
= g_slist_remove (auth_list
, auth
);
186 authinfo_free (auth
);
189 /* Set authentication information in bucket. Return 1 if successful, else 0 */
190 /* Information in auth_list overrides user if pass is NULL. */
191 /* bucket->host and bucket->service must be valid. */
193 bucket_set_authinfo (smbfs_connection
*bucket
,
194 const char *domain
, const char *user
, const char *pass
,
195 int fallback_to_host
)
197 struct smb_authinfo data
;
198 struct smb_authinfo
*auth
;
201 if (domain
&& user
&& pass
) {
202 g_free (bucket
->domain
);
203 g_free (bucket
->user
);
204 g_free (bucket
->password
);
205 bucket
->domain
= g_strdup (domain
);
206 bucket
->user
= g_strdup (user
);
207 bucket
->password
= g_strdup (pass
);
208 authinfo_remove (bucket
->host
, bucket
->service
);
209 authinfo_add (bucket
->host
, bucket
->service
,
214 data
.host
= bucket
->host
;
215 data
.share
= bucket
->service
;
216 list
= g_slist_find_custom (auth_list
, &data
, authinfo_compare_host_and_share
);
217 if (!list
&& fallback_to_host
)
218 list
= g_slist_find_custom (auth_list
, &data
, authinfo_compare_host
);
221 bucket
->domain
= g_strdup (auth
->domain
);
222 bucket
->user
= g_strdup (auth
->user
);
223 bucket
->password
= g_strdup (auth
->password
);
228 bucket
->domain
= g_strdup (lp_workgroup ());
229 bucket
->user
= g_strdup (got_user
? username
: user
);
230 bucket
->password
= g_strdup (password
);
234 auth
= vfs_smb_get_authinfo (bucket
->host
,
236 (domain
? domain
: lp_workgroup ()),
239 g_free (bucket
->domain
);
240 g_free (bucket
->user
);
241 g_free (bucket
->password
);
242 bucket
->domain
= g_strdup (auth
->domain
);
243 bucket
->user
= g_strdup (auth
->user
);
244 bucket
->password
= g_strdup (auth
->password
);
245 authinfo_remove (bucket
->host
, bucket
->service
);
246 auth_list
= g_slist_prepend (auth_list
, auth
);
253 smbfs_set_debug (int arg
)
259 smbfs_set_debugf (const char *filename
)
261 extern pstring debugf
;
263 if (DEBUGLEVEL
> 0) {
264 FILE *outfile
= fopen (filename
, "w");
266 setup_logging ("", True
); /* No needs for timestamp for each message */
269 pstrcpy (debugf
, filename
);
274 /********************** The callbacks ******************************/
276 smbfs_init (vfs
* me
)
278 char *servicesf
= CONFIGDIR PATH_SEP_STR
"smb.conf";
280 /* DEBUGLEVEL = 4; */
283 charset_initialise ();
285 DEBUG (3, ("smbfs_init(%s)\n", me
->name
));
287 if (!get_myname (myhostname
, NULL
))
288 DEBUG (0, ("Failed to get my hostname.\n"));
290 if (!lp_load (servicesf
, True
, False
, False
))
291 DEBUG (0, ("Cannot load %s - run testparm to debug it\n", servicesf
));
293 codepage_initialise (lp_client_code_page ());
297 if (getenv ("USER")) {
300 pstrcpy (username
, getenv ("USER"));
302 DEBUG (3, ("smbfs_init(): $USER:%s\n", username
));
303 if ((p
= strchr (username
, '%'))) {
305 pstrcpy (password
, p
+ 1);
307 memset (strchr (getenv ("USER"), '%') + 1, 'X', strlen (password
));
308 DEBUG (3, ("smbfs_init(): $USER%%pass: %s%%%s\n",
309 username
, password
));
313 if (getenv ("PASSWD")) {
314 pstrcpy (password
, getenv ("PASSWD"));
321 smbfs_fill_names (vfs
*me
, void (*func
)(char *))
325 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
326 if (smbfs_connections
[i
].cli
) {
327 path
= g_strconcat (URL_HEADER
,
328 smbfs_connections
[i
].host
,
329 "/", smbfs_connections
[i
].service
,
337 #define CNV_LANG(s) dos_to_unix(s,False)
338 #define GNAL_VNC(s) unix_to_dos(s,False)
339 /* does same as do_get() in client.c */
340 /* called from vfs.c:1080, count = buffer size */
342 smbfs_read (void *data
, char *buffer
, int count
)
344 smbfs_handle
*info
= (smbfs_handle
*) data
;
347 DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%d)\n",
348 info
->fnum
, (int)info
->nread
, count
));
349 n
= cli_read(info
->cli
, info
->fnum
, buffer
, info
->nread
, count
);
356 smbfs_write (void *data
, char *buf
, int nbyte
)
358 smbfs_handle
*info
= (smbfs_handle
*) data
;
361 DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%d)\n",
362 info
->fnum
, (int)info
->nread
, nbyte
));
363 n
= cli_write(info
->cli
, info
->fnum
, 0, buf
, info
->nread
, nbyte
);
370 smbfs_close (void *data
)
372 smbfs_handle
*info
= (smbfs_handle
*) data
;
373 DEBUG (3, ("smbfs_close(fnum:%d)\n", info
->fnum
));
375 /* FIXME: Why too different cli have the same outbuf
376 * if file is copied to share
378 if (info
->cli
->outbuf
== NULL
) {
383 /* if imlementing archive_level: add rname to smbfs_handle */
384 if (archive_level
>= 2 && (inf
->attr
& aARCH
)) {
385 cli_setatr (info
->cli
, rname
, info
->attr
& ~(uint16
) aARCH
, 0);
388 return (cli_close (info
->cli
, info
->fnum
) == True
) ? 0 : -1;
392 smbfs_errno (vfs
*me
)
394 DEBUG(3, ("smbfs_errno: %s\n", g_strerror(my_errno
)));
398 typedef struct dir_entry
{
400 struct dir_entry
*next
;
406 gboolean server_list
;
408 char *path
; /* the dir originally passed to smbfs_opendir */
409 smbfs_connection
*conn
;
418 *current_server_info
;
420 static gboolean first_direntry
;
423 new_dir_entry (const char * name
)
425 dir_entry
*new_entry
;
426 new_entry
= g_new0 (dir_entry
, 1);
427 new_entry
->text
= dos_to_unix (g_strdup (name
), 1);
429 if (first_direntry
) {
430 current_info
->entries
= new_entry
;
431 first_direntry
= FALSE
;
433 current_info
->current
->next
= new_entry
;
435 current_info
->current
= new_entry
;
440 /* browse for shares on server */
442 browsing_helper (const char *name
, uint32 type
, const char *comment
, void *state
)
446 dir_entry
*new_entry
= new_dir_entry (name
);
451 /* show this as dir */
452 new_entry
->my_stat
.st_mode
=
453 S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
| S_IXUSR
| S_IXGRP
|
466 DEBUG (3, ("\t%-15.15s%-10.10s%s\n", name
, typestr
, comment
));
470 loaddir_helper (file_info
* finfo
, const char *mask
, void *entry
)
472 dir_entry
*new_entry
= (dir_entry
*) entry
;
473 time_t t
= finfo
->mtime
; /* the time is assumed to be passed as GMT */
474 #if 0 /* I want to see dot files */
475 if (finfo
->mode
& aHIDDEN
)
476 return; /* don't bother with hidden files, "~$" screws up mc */
479 new_entry
= new_dir_entry (finfo
->name
);
481 new_entry
->my_stat
.st_size
= finfo
->size
;
482 new_entry
->my_stat
.st_mtime
= finfo
->mtime
;
483 new_entry
->my_stat
.st_atime
= finfo
->atime
;
484 new_entry
->my_stat
.st_ctime
= finfo
->ctime
;
485 new_entry
->my_stat
.st_uid
= finfo
->uid
;
486 new_entry
->my_stat
.st_gid
= finfo
->gid
;
488 new_entry
->my_stat
.st_mode
= /* rw-rw-rw */
489 S_IRUSR
| S_IRGRP
| S_IROTH
| S_IWUSR
| S_IWGRP
| S_IWOTH
;
491 /* if (finfo->mode & aVOLID); nothing similar in real world */
492 if (finfo
->mode
& aDIR
)
493 new_entry
->my_stat
.st_mode
|= /* drwxrwxrwx */
494 S_IFDIR
| S_IXUSR
| S_IXGRP
| S_IXOTH
;
496 new_entry
->my_stat
.st_mode
|= S_IFREG
; /* if not dir, regular file? */
497 /* if (finfo->mode & aARCH); DOS archive */
498 /* if (finfo->mode & aHIDDEN); like a dot file? */
499 /* if (finfo->mode & aSYSTEM); like a kernel? */
500 if (finfo
->mode
& aRONLY
)
501 new_entry
->my_stat
.st_mode
&= ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
503 DEBUG (entry
? 3 : 6, (" %-30s%7.7s%8.0f %s",
504 CNV_LANG (finfo
->name
),
505 attrib_string (finfo
->mode
),
506 (double) finfo
->size
,
507 asctime (LocalTime (&t
))));
510 /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
512 convert_path(char **remote_file
, gboolean trailing_asterik
)
516 my_remote
= *remote_file
;
517 if (strncmp (my_remote
, URL_HEADER
, HEADER_LEN
) == 0) { /* if passed directly */
519 if (*my_remote
== '/') /* from server browsing */
521 p
= strchr(my_remote
, '/');
523 my_remote
= p
+1; /* advance to end of server name */
526 if (*my_remote
== '/')
527 my_remote
++; /* strip off leading '/' */
528 p
= strchr(my_remote
, '/');
530 my_remote
= p
; /* strip off share/service name */
531 /* create remote filename as understood by smb clientgen */
532 p
= *remote_file
= g_strconcat (my_remote
, trailing_asterik
? "/*" : "", 0);
533 unix_to_dos (*remote_file
, 1);
534 while ((p
= strchr(p
, '/')))
540 server_browsing_helper (const char *name
, uint32 m
, const char *comment
, void *state
)
542 dir_entry
*new_entry
= new_dir_entry (name
);
544 /* show this as dir */
545 new_entry
->my_stat
.st_mode
=
546 S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
| S_IXUSR
| S_IXGRP
| S_IXOTH
;
548 DEBUG (3, ("\t%-16.16s %s\n", name
, comment
));
552 reconnect(smbfs_connection
*conn
, int *retries
)
555 DEBUG(3, ("RECONNECT\n"));
557 if (*(conn
->host
) == 0)
558 host
= g_strdup(conn
->cli
->desthost
); /* server browsing */
560 host
= g_strdup(conn
->host
);
562 cli_shutdown(conn
->cli
);
564 if (!(conn
->cli
= smbfs_do_connect(host
, conn
->service
))) {
565 message_2s (1, MSG_ERROR
,
566 _(" reconnect to %s failed\n "), conn
->host
);
571 if (++(*retries
) == 2)
577 smb_send(struct cli_state
*cli
)
583 len
= smb_len(cli
->outbuf
) + 4;
585 while (nwritten
< len
) {
586 ret
= write_socket(cli
->fd
, cli
->outbuf
+nwritten
, len
- nwritten
);
587 if (ret
<= 0 && errno
== EPIPE
)
595 /****************************************************************************
596 See if server has cut us off by checking for EPIPE when writing.
597 Taken from cli_chkpath()
598 ****************************************************************************/
600 chkpath(struct cli_state
*cli
, char *path
, BOOL send_only
)
606 unix_to_dos (path2
, 1);
607 trim_string(path2
,NULL
,"\\");
608 if (!*path2
) *path2
= '\\';
610 memset(cli
->outbuf
,'\0',smb_size
);
611 set_message(cli
->outbuf
,0,4 + strlen(path2
),True
);
612 SCVAL(cli
->outbuf
,smb_com
,SMBchkpth
);
613 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
617 SSVAL(cli
->outbuf
,smb_pid
,cli
->pid
);
618 SSVAL(cli
->outbuf
,smb_uid
,cli
->vuid
);
619 SSVAL(cli
->outbuf
,smb_mid
,cli
->mid
);
620 if (cli
->protocol
> PROTOCOL_CORE
) {
621 SCVAL(cli
->outbuf
,smb_flg
,0x8);
622 SSVAL(cli
->outbuf
,smb_flg2
,0x1);
625 p
= smb_buf(cli
->outbuf
);
629 if (!smb_send(cli
)) {
630 DEBUG(3, ("chkpath: couldnt send\n"));
634 client_receive_smb(cli
->fd
, cli
->inbuf
, cli
->timeout
);
635 DEBUG(3, ("chkpath: send only OK\n"));
636 return True
; /* just testing for EPIPE */
638 if (!client_receive_smb(cli
->fd
, cli
->inbuf
, cli
->timeout
)) {
639 DEBUG(3, ("chkpath: receive error\n"));
642 if ((my_errno
= cli_error(cli
, NULL
, NULL
, NULL
))) {
643 if (my_errno
== 20 || my_errno
== 13)
644 return True
; /* ignore if 'not a directory' error */
645 DEBUG(3, ("chkpath: cli_error: %s\n", g_strerror(my_errno
)));
654 fs (const char *text
)
656 const char *p
= text
;
659 while ((p
= strchr(p
, '/')) != NULL
) {
670 smbfs_loaddir (opendir_info
*smbfs_info
)
672 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
673 int servlen
= strlen(smbfs_info
->conn
->service
);
674 char *my_dirname
= smbfs_info
->dirname
;
676 DEBUG(3, ("smbfs_loaddir: dirname:%s\n", my_dirname
));
677 first_direntry
= TRUE
;
680 DEBUG(3, ("smbfs_loaddir: new:'%s', cached:'%s'\n", my_dirname
, current_info
->dirname
));
681 /* if new desired dir is longer than cached in current_info */
682 if (fs(my_dirname
) > fs(current_info
->dirname
)) {
683 DEBUG(3, ("saving to previous_info\n"));
684 previous_info
= current_info
;
688 current_info
= smbfs_info
;
690 if (strcmp(my_dirname
, "/") == 0) {
691 if (!strcmp(smbfs_info
->path
, URL_HEADER
)) {
692 DEBUG(6, ("smbfs_loaddir: browsing %s\n", IPC
));
693 /* browse for servers */
694 if (!cli_NetServerEnum(smbfs_info
->conn
->cli
, smbfs_info
->conn
->domain
,
695 SV_TYPE_ALL
, server_browsing_helper
, NULL
))
698 current_server_info
= smbfs_info
;
699 smbfs_info
->server_list
= TRUE
;
701 /* browse for shares */
702 if (cli_RNetShareEnum(smbfs_info
->conn
->cli
, browsing_helper
, NULL
) < 1)
705 current_share_info
= smbfs_info
;
710 /* do regular directory listing */
711 if(strncmp(smbfs_info
->conn
->service
, my_dirname
+1, servlen
) == 0) {
712 /* strip share name from dir */
713 char *p
= my_dirname
= g_strdup(my_dirname
+ servlen
);
715 convert_path(&my_dirname
, TRUE
);
718 convert_path(&my_dirname
, TRUE
);
720 DEBUG(6, ("smbfs_loaddir: service: %s\n", smbfs_info
->conn
->service
));
721 DEBUG(6, ("smbfs_loaddir: cli->share: %s\n", smbfs_info
->conn
->cli
->share
));
722 DEBUG(6, ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname
));
723 /* do file listing: cli_list returns number of files */
725 smbfs_info
->conn
->cli
, my_dirname
, attribute
, loaddir_helper
, NULL
) < 0) {
726 /* cli_list returns -1 if directory empty or cannot read socket */
727 my_errno
= cli_error(smbfs_info
->conn
->cli
, NULL
, &err
, NULL
);
731 if (*(my_dirname
) == 0)
732 smbfs_info
->dirname
= smbfs_info
->conn
->service
;
737 /* current_info->parent = smbfs_info->dirname; */
739 smbfs_info
->current
= smbfs_info
->entries
;
740 return 1; /* 1 = ok */
743 #ifdef SMBFS_FREE_DIR
745 smbfs_free_dir (dir_entry
*de
)
749 smbfs_free_dir (de
->next
);
756 /* The readdir routine loads the complete directory */
757 /* It's too slow to ask the server each time */
758 /* It now also sends the complete lstat information for each file */
760 smbfs_readdir(void *info
)
762 static union vfs_dirent smbfs_readdir_data
;
763 static char *const dirent_dest
= smbfs_readdir_data
.dent
.d_name
;
764 opendir_info
*smbfs_info
= (opendir_info
*) info
;
766 DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info
->dirname
));
768 if (!smbfs_info
->entries
)
769 if (!smbfs_loaddir(smbfs_info
))
772 if (smbfs_info
->current
== 0) { /* reached end of dir entries */
773 DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
774 #ifdef SMBFS_FREE_DIR
775 smbfs_free_dir(smbfs_info
->entries
);
776 smbfs_info
->entries
= 0;
780 strncpy(dirent_dest
, smbfs_info
->current
->text
, MC_MAXPATHLEN
);
781 dirent_dest
[MC_MAXPATHLEN
] = 0;
782 smbfs_info
->current
= smbfs_info
->current
->next
;
784 compute_namelen(&smbfs_readdir_data
.dent
);
786 return &smbfs_readdir_data
;
790 smbfs_closedir (void *info
)
792 opendir_info
*smbfs_info
= (opendir_info
*) info
;
793 /* dir_entry *p, *q; */
795 DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info
->dirname
));
798 /* for (p = smbfs_info->entries; p;){
809 smbfs_chmod (vfs
*me
, char *path
, int mode
)
811 DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path
, mode
));
812 /* my_errno = EOPNOTSUPP;
813 return -1; */ /* cant chmod on smb filesystem */
814 return 0; /* make mc happy */
818 smbfs_chown (vfs
*me
, char *path
, int owner
, int group
)
820 DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path
, owner
, group
));
821 my_errno
= EOPNOTSUPP
; /* ready for your labotomy? */
826 smbfs_utime (vfs
*me
, char *path
, struct utimbuf
*times
)
828 DEBUG(3, ("smbfs_utime(path:%s)\n", path
));
829 my_errno
= EOPNOTSUPP
;
834 smbfs_readlink (vfs
*me
, char *path
, char *buf
, int size
)
836 DEBUG(3, ("smbfs_readlink(path:%s, buf:%s, size:%d)\n", path
, buf
, size
));
837 my_errno
= EOPNOTSUPP
;
838 return -1; /* no symlinks on smb filesystem? */
842 smbfs_symlink (vfs
*me
, char *n1
, char *n2
)
844 DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1
, n2
));
845 my_errno
= EOPNOTSUPP
;
846 return -1; /* no symlinks on smb filesystem? */
849 /* Extract the hostname and username from the path */
850 /* path is in the form: hostname:user/remote-dir */
853 smbfs_get_host_and_username
854 (char **path
, char **host
, char **user
, int *port
, char **pass
)
859 ret
= vfs_split_url (*path
, host
, user
, port
, pass
, SMB_PORT
, 0);
862 if ((p
= strchr(*path
, '@'))) /* user:pass@server */
863 *path
= ++p
; /* don't want user:pass@ in path */
864 if ((p
= strchr(ret
, '@'))) /* user:pass@server */
865 ret
= ++p
; /* don't want user:pass@ in path */
871 #define smbfs_get_host_and_username(path, host, user, port, pass) \
872 vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
874 /*****************************************************
875 return a connection to a SMB server
876 current_bucket needs to be set before calling
877 *******************************************************/
878 static struct cli_state
*
879 smbfs_do_connect (const char *server
, char *share
)
882 struct nmb_name called
, calling
;
884 extern struct in_addr ipzero
;
886 DEBUG(3, ("smbfs_do_connect(%s, %s)\n", server
, share
));
887 if (*share
== '\\') {
889 share
= strchr(server
,'\\');
890 if (!share
) return NULL
;
895 make_nmb_name(&calling
, global_myname
, 0x0);
896 make_nmb_name(&called
, server
, current_bucket
->name_type
);
900 ip
= (current_bucket
->have_ip
) ? current_bucket
->dest_ip
: ipzero
;
902 /* have to open a new connection */
903 if (!(c
= cli_initialise(NULL
))) {
908 pwd_init(&(c
->pwd
)); /* should be moved into cli_initialise()? */
909 pwd_set_cleartext(&(c
->pwd
), current_bucket
->password
);
911 if ((cli_set_port(c
, current_bucket
->port
) == 0) ||
912 !cli_connect(c
, server
, &ip
)) {
913 DEBUG(1, ("Connection to %s failed\n", server
));
917 if (!cli_session_request(c
, &calling
, &called
)) {
918 my_errno
= cli_error(c
, NULL
, &err
, NULL
);
919 DEBUG(1, ("session request to %s failed\n", called
.name
));
921 if (strcmp(called
.name
, "*SMBSERVER")) {
922 make_nmb_name(&called
, "*SMBSERVER", 0x20);
928 DEBUG(3, (" session request ok\n"));
930 if (!cli_negprot(c
)) {
931 DEBUG(1, ("protocol negotiation failed\n"));
935 if (!cli_session_setup(c
, current_bucket
->user
,
936 current_bucket
->password
, strlen(current_bucket
->password
),
937 current_bucket
->password
, strlen(current_bucket
->password
),
938 current_bucket
->domain
)) {
939 DEBUG(1,("session setup failed: %s\n", cli_errstr(c
)));
940 authinfo_remove (server
, share
);
944 if (*c
->server_domain
|| *c
->server_os
|| *c
->server_type
)
945 DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
946 c
->server_domain
,c
->server_os
,c
->server_type
));
948 DEBUG(3, (" session setup ok\n"));
950 if (!cli_send_tconX(c
, share
, "?????",
951 current_bucket
->password
, strlen(current_bucket
->password
)+1)) {
952 DEBUG(1,("%s: tree connect failed: %s\n", share
, cli_errstr(c
)));
956 DEBUG(3, (" tconx ok\n"));
962 my_errno
= cli_error(c
, NULL
, &err
, NULL
);
969 get_master_browser(char **host
)
972 struct in_addr
*ip_list
, bcast_addr
;
973 extern struct in_addr ipzero
;
975 /* does port = 137 for win95 master browser? */
976 int fd
= open_socket_in( SOCK_DGRAM
, 0, 3,
977 interpret_addr(lp_socket_address()), True
);
980 set_socket_options(fd
, "SO_BROADCAST");
981 ip_list
= iface_bcast(ipzero
);
982 bcast_addr
= *ip_list
;
983 if ((ip_list
= name_query(fd
, "\01\02__MSBROWSE__\02", 1, True
,
984 True
, bcast_addr
, &count
, NULL
))) {
987 /* just return first master browser */
988 *host
= g_strdup(inet_ntoa(ip_list
[0]));
995 free_bucket (smbfs_connection
*bucket
)
997 g_free (bucket
->host
);
998 g_free (bucket
->service
);
999 g_free (bucket
->domain
);
1000 g_free (bucket
->user
);
1001 wipe_password (bucket
->password
);
1002 if (bucket
->home
) g_free (bucket
->home
);
1003 memset (bucket
, 0, sizeof (smbfs_connection
));
1006 static smbfs_connection
*
1007 smbfs_get_free_bucket ()
1011 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++)
1012 if (!smbfs_connections
[i
].cli
) return &smbfs_connections
[i
];
1014 { /* search for most dormant connection */
1015 int oldest
= 0; /* index */
1016 time_t oldest_time
= smbfs_connections
[0].last_use
;
1017 for (i
= 1; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
1018 if (smbfs_connections
[i
].last_use
< oldest_time
) {
1019 oldest_time
= smbfs_connections
[i
].last_use
;
1023 cli_shutdown(smbfs_connections
[oldest
].cli
);
1024 free_bucket (&smbfs_connections
[oldest
]);
1025 return &smbfs_connections
[oldest
];
1028 /* This can't happend, since we have checked for max connections before */
1029 vfs_die("Internal error: smbfs_get_free_bucket");
1030 return 0; /* shut up, stupid gcc */
1033 /* This routine keeps track of open connections */
1034 /* Returns a connected socket to host */
1035 static smbfs_connection
*
1036 smbfs_open_link(char *host
, char *path
, const char *user
, int *port
, char *this_pass
)
1039 smbfs_connection
*bucket
;
1041 struct in_addr
*dest_ip
= NULL
;
1043 DEBUG(3, ("smbfs_open_link(host:%s, path:%s)\n", host
, path
));
1045 if (strcmp(host
, path
) == 0) /* if host & path are same: */
1046 pstrcpy(service
, IPC
); /* setup for browse */
1047 else { /* get share name from path, path starts with server name */
1049 if ((p
= strchr(path
, '/'))) /* get share aka */
1050 pstrcpy(service
, ++p
); /* service name from path */
1052 pstrcpy(service
, "");
1053 /* now check for trailing directory/filenames */
1054 p
= strchr(service
, '/');
1056 *p
= 0; /* cut off dir/files: sharename only */
1058 DEBUG(6, ("smbfs_open_link: service from path:%s\n", service
));
1062 user
= username
; /* global from getenv */
1064 /* Is the link actually open? */
1065 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
1066 if (!smbfs_connections
[i
].cli
)
1068 if ((strcmp (host
, smbfs_connections
[i
].host
) == 0) &&
1069 (strcmp (user
, smbfs_connections
[i
].user
) == 0) &&
1070 (strcmp (service
, smbfs_connections
[i
].service
) == 0)) {
1072 BOOL inshare
= (*host
!= 0 && *path
!= 0 && strchr(path
, '/'));
1073 /* check if this connection has died */
1074 while (!chkpath(smbfs_connections
[i
].cli
, "\\", !inshare
)) {
1075 if (!reconnect(&smbfs_connections
[i
], &retries
))
1078 DEBUG(6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i
));
1079 current_bucket
= &smbfs_connections
[i
];
1080 smbfs_connections
[i
].last_use
= time(NULL
);
1081 return &smbfs_connections
[i
];
1083 /* connection not found, find if we have ip for new connection */
1084 if (strcmp (host
, smbfs_connections
[i
].host
) == 0)
1085 dest_ip
= &smbfs_connections
[i
].cli
->dest_ip
;
1088 /* make new connection */
1089 bucket
= smbfs_get_free_bucket ();
1090 bucket
->name_type
= 0x20;
1092 bucket
->port
= *port
;
1093 bucket
->have_ip
= False
;
1095 bucket
->have_ip
= True
;
1096 bucket
->dest_ip
= *dest_ip
;
1098 current_bucket
= bucket
;
1100 bucket
->user
= g_strdup(user
);
1101 bucket
->service
= g_strdup (service
);
1103 if (!(*host
)) { /* if blank host name, browse for servers */
1104 if (!get_master_browser(&host
)) /* set host to ip of master browser */
1105 return 0; /* couldnt find master browser? */
1107 bucket
->host
= g_strdup(""); /* blank host means master browser */
1109 bucket
->host
= g_strdup(host
);
1111 if (!bucket_set_authinfo (bucket
,
1112 0, /* domain currently not used */
1118 /* connect to share */
1119 while (!(bucket
->cli
= smbfs_do_connect(host
, service
))) {
1121 if (my_errno
!= EPERM
)
1123 message_1s (1, MSG_ERROR
,
1124 _(" Authentication failed "));
1126 /* authentication failed, try again */
1127 authinfo_remove (bucket
->host
, bucket
->service
);
1128 if (!bucket_set_authinfo (bucket
,
1137 smbfs_open_connections
++;
1138 DEBUG(3, ("smbfs_open_link:smbfs_open_connections: %d\n",
1139 smbfs_open_connections
));
1144 smbfs_get_path(smbfs_connection
**sc
, char *path
)
1146 char *user
, *host
, *remote_path
, *pass
;
1147 int port
= SMB_PORT
;
1149 DEBUG(3, ("smbfs_get_path(%s)\n", path
));
1150 if (strncmp (path
, URL_HEADER
, HEADER_LEN
))
1154 if (*path
== '/') /* '/' leading server name */
1155 path
++; /* probably came from server browsing */
1157 if ((remote_path
= smbfs_get_host_and_username(
1158 &path
, &host
, &user
, &port
, &pass
)))
1159 if ((*sc
= smbfs_open_link (host
, path
, user
, &port
, pass
)) == NULL
){
1160 g_free (remote_path
);
1165 if (pass
) wipe_password (pass
);
1167 if (!remote_path
) return NULL
;
1169 /* NOTE: tildes are deprecated. See ftpfs.c */
1171 int f
= !strcmp( remote_path
, "/~" );
1172 if (f
|| !strncmp( remote_path
, "/~/", 3 )) {
1174 s
= concat_dir_and_file( (*sc
)->home
, remote_path
+3-f
);
1175 g_free (remote_path
);
1184 is_error (int result
, int errno_num
)
1186 if (!(result
== -1))
1187 return my_errno
= 0;
1189 my_errno
= errno_num
;
1195 smbfs_opendir (vfs
*me
, char *dirname
)
1197 opendir_info
*smbfs_info
;
1198 smbfs_connection
*sc
;
1201 DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname
));
1203 if (!(remote_dir
= smbfs_get_path (&sc
, dirname
)))
1206 /* FIXME: where freed? */
1207 smbfs_info
= g_new (opendir_info
, 1);
1208 smbfs_info
->server_list
= FALSE
;
1209 smbfs_info
->path
= g_strdup(dirname
); /* keep original */
1210 smbfs_info
->dirname
= remote_dir
;
1211 smbfs_info
->conn
= sc
;
1212 smbfs_info
->entries
= 0;
1213 smbfs_info
->current
= 0;
1219 fake_server_stat(const char *server_url
, const char *path
, struct stat
*buf
)
1224 if ((p
= strrchr(path
, '/')))
1225 path
= p
+ 1; /* advance until last '/' */
1227 if (!current_info
->entries
) {
1228 if (!smbfs_loaddir(current_info
)); /* browse host */
1232 if (current_info
->server_list
== True
) {
1233 dentry
= current_info
->entries
;
1234 DEBUG(4, ("fake stat for SERVER \"%s\"\n", path
));
1236 if (strcmp(dentry
->text
, path
) == 0) {
1237 DEBUG(4, ("fake_server_stat: %s:%4o\n",
1238 dentry
->text
, dentry
->my_stat
.st_mode
));
1239 memcpy(buf
, &dentry
->my_stat
, sizeof(struct stat
));
1242 dentry
= dentry
->next
;
1250 fake_share_stat(const char *server_url
, const char *path
, struct stat
*buf
)
1253 if (strlen(path
) < strlen(server_url
))
1255 path
+= strlen(server_url
); /* we only want share name */
1258 if (*path
== '/') /* '/' leading server name */
1259 path
++; /* probably came from server browsing */
1261 if (!current_share_info
->entries
) {
1262 if (!smbfs_loaddir(current_share_info
)); /* browse host */
1265 dentry
= current_share_info
->entries
;
1266 DEBUG(3, ("fake_share_stat: %s on %s\n", path
, server_url
));
1268 if (strcmp(dentry
->text
, path
) == 0) {
1269 DEBUG(6, ("fake_share_stat: %s:%4o\n",
1270 dentry
->text
, dentry
->my_stat
.st_mode
));
1271 memcpy(buf
, &dentry
->my_stat
, sizeof(struct stat
));
1274 dentry
= dentry
->next
;
1280 /* stat a single file, get_remote_stat callback */
1281 static dir_entry
*single_entry
;
1283 /* stat a single file */
1285 get_remote_stat (smbfs_connection
* sc
, char *path
, struct stat
*buf
)
1287 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
1288 char *mypath
= path
;
1290 DEBUG (3, ("get_remote_stat(): mypath:%s\n", mypath
));
1292 convert_path (&mypath
, FALSE
);
1294 #if 0 /* single_entry is never free()d now. And only my_stat is used */
1295 single_entry
= g_new (dir_entry
, 1);
1297 single_entry
->text
= dos_to_unix (g_strdup (finfo
->name
), 1);
1299 single_entry
->next
= 0;
1302 single_entry
= g_new0 (dir_entry
, 1);
1305 (sc
->cli
, mypath
, attribute
, loaddir_helper
, single_entry
) < 1) {
1308 return -1; /* cli_list returns number of files */
1311 memcpy (buf
, &single_entry
->my_stat
, sizeof (struct stat
));
1313 /* don't free here, use for smbfs_fstat() */
1314 /* g_free(single_entry->text);
1315 g_free(single_entry); */
1321 search_dir_entry (dir_entry
*dentry
, const char *text
, struct stat
*buf
)
1324 if (strcmp(text
, dentry
->text
) == 0) {
1325 memcpy(buf
, &dentry
->my_stat
, sizeof(struct stat
));
1326 memcpy(&single_entry
->my_stat
, &dentry
->my_stat
,
1327 sizeof(struct stat
));
1330 dentry
= dentry
->next
;
1336 get_stat_info (smbfs_connection
* sc
, char *path
, struct stat
*buf
)
1340 dir_entry
*dentry
= current_info
->entries
;
1342 const char *mypath
= path
;
1344 mypath
++; /* cut off leading '/' */
1345 if ((p
= strrchr (mypath
, '/')))
1346 mypath
= p
+ 1; /* advance until last file/dir name */
1347 DEBUG (3, ("get_stat_info: mypath:%s, current_info->dirname:%s\n",
1348 mypath
, current_info
->dirname
));
1351 DEBUG (1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n",
1352 current_info
->dirname
, path
));
1356 if (!single_entry
) /* when found, this will be written too */
1357 single_entry
= g_new (dir_entry
, 1);
1358 if (search_dir_entry (current_info
->entries
, mypath
, buf
) == 0) {
1361 /* now try to identify mypath as PARENT dir */
1365 mdp
= mydir
= g_strdup (current_info
->dirname
);
1366 if ((p
= strrchr (mydir
, '/')))
1367 *p
= 0; /* advance util last '/' */
1368 if ((p
= strrchr (mydir
, '/')))
1369 mydir
= p
+ 1; /* advance util last '/' */
1370 if (strcmp (mydir
, mypath
) == 0) { /* fake a stat for ".." */
1371 memset (buf
, 0, sizeof (struct stat
));
1372 buf
->st_mode
= S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
;
1373 memcpy (&single_entry
->my_stat
, buf
, sizeof (struct stat
));
1375 DEBUG (1, (" PARENT:found in %s\n", current_info
->dirname
));
1380 /* now try to identify as CURRENT dir? */
1382 char *dnp
= current_info
->dirname
;
1383 DEBUG (6, ("get_stat_info: is %s current dir? this dir is: %s\n",
1384 mypath
, current_info
->dirname
));
1390 if (strcmp (mypath
, dnp
) == 0) {
1391 memset (buf
, 0, sizeof (struct stat
));
1392 buf
->st_mode
= S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
;
1393 memcpy (&single_entry
->my_stat
, buf
, sizeof (struct stat
));
1394 DEBUG (1, (" CURRENT:found in %s\n", current_info
->dirname
));
1398 DEBUG (3, ("'%s' not found in current_info '%s'\n", path
,
1399 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
,
1405 previous_info
->dirname
));
1407 /* try to find this in the SHARE listing */
1408 if (current_share_info
) {
1409 if (search_dir_entry (current_share_info
->entries
, mypath
, buf
) == 0)
1411 DEBUG (3, ("'%s' not found in share_info '%s'\n", path
,
1412 current_share_info
->dirname
));
1414 /* try to find this in the SERVER listing */
1415 if (current_server_info
) {
1416 if (search_dir_entry (current_server_info
->entries
, mypath
, buf
) == 0)
1418 DEBUG (3, ("'%s' not found in server_info '%s'\n", path
,
1419 current_server_info
->dirname
));
1421 /* nothing found. get stat file info from server */
1422 return get_remote_stat (sc
, path
, buf
);
1426 smbfs_chdir (vfs
*me
, char *path
)
1429 smbfs_connection
*sc
;
1431 DEBUG(3, ("smbfs_chdir(path:%s)\n", path
));
1432 if (!(remote_dir
= smbfs_get_path (&sc
, path
)))
1434 g_free (remote_dir
);
1440 loaddir(vfs
*me
, const char *path
)
1445 mypath
= g_strdup(path
);
1446 p
= strrchr(mypath
, '/');
1450 DEBUG(6, ("loaddir(%s)\n", mypath
));
1451 smbfs_chdir(me
, mypath
);
1452 info
= smbfs_opendir (me
, mypath
);
1456 smbfs_readdir(info
);
1457 smbfs_loaddir(info
);
1462 smbfs_stat (vfs
*me
, char *path
, struct stat
*buf
)
1465 smbfs_connection
*sc
;
1470 DEBUG(3, ("smbfs_stat(path:%s)\n", path
));
1473 if (p
= strchr(path
, '@')) /* user:pass@server */
1474 path
= ++p
; /* don't want user:pass@ in path */
1477 if (!current_info
) {
1478 DEBUG(1, ("current_info = NULL: "));
1479 if (loaddir(me
, path
) < 0)
1483 pstrcpy(server_url
, URL_HEADER
);
1484 pstrcat(server_url
, current_bucket
->host
);
1486 /* check if stating server */
1488 if (strncmp(p
, URL_HEADER
, HEADER_LEN
)) {
1489 DEBUG(1, ("'%s' doesnt start with '%s' (length %d)\n",
1490 p
, URL_HEADER
, HEADER_LEN
));
1497 pp
= strchr(p
, '/'); /* advance past next '/' */
1499 if (!current_info
->server_list
) {
1500 if (loaddir(me
, path
) < 0)
1503 return fake_server_stat(server_url
, path
, buf
);
1505 if (!strchr(++pp
, '/')) {
1506 return fake_share_stat(server_url
, path
, buf
);
1509 /* stating inside share at this point */
1510 if (!(remote_dir
= smbfs_get_path (&sc
, path
))) /* connects if necessary */
1512 g_free (remote_dir
);
1514 int hostlen
= strlen(current_bucket
->host
);
1515 char *pp
= path
+ strlen(path
)-hostlen
;
1516 char *sp
= server_url
+ strlen(server_url
)-hostlen
;
1518 if (strcmp(sp
, pp
) == 0) {
1519 /* make server name appear as directory */
1520 DEBUG(1, ("smbfs_stat: showing server as directory\n"));
1521 memset(buf
, 0, sizeof(struct stat
));
1522 buf
->st_mode
= S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
;
1526 /* check if current_info is in share requested */
1527 p
= service
= g_strdup(p
);
1528 pp
= strchr(p
, '/');
1530 p
= ++pp
; /* advance past server name */
1531 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
);
1553 #define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */
1556 smbfs_lseek (void *data
, off_t offset
, int whence
)
1558 DEBUG(3, ("smbfs_lseek()\n"));
1559 my_errno
= EOPNOTSUPP
;
1564 smbfs_mknod (vfs
*me
, char *path
, int mode
, int dev
)
1566 DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%d)\n", path
, mode
, dev
));
1567 my_errno
= EOPNOTSUPP
;
1572 smbfs_mkdir (vfs
*me
, char *path
, mode_t mode
)
1574 smbfs_connection
*sc
;
1577 DEBUG(3, ("smbfs_mkdir(path:%s, mode:%d)\n", path
, mode
));
1578 if ((remote_file
= smbfs_get_path (&sc
, path
)) == 0)
1580 g_free (remote_file
);
1581 convert_path(&path
, FALSE
);
1583 if (!cli_mkdir(sc
->cli
, path
)) {
1584 my_errno
= cli_error(sc
->cli
, NULL
, &err
, NULL
);
1585 message_3s (1, MSG_ERROR
, _(" Error %s creating directory %s "),
1586 cli_errstr(sc
->cli
), CNV_LANG(path
));
1595 smbfs_rmdir (vfs
*me
, char *path
)
1597 smbfs_connection
*sc
;
1600 DEBUG(3, ("smbfs_rmdir(path:%s)\n", path
));
1601 if ((remote_file
= smbfs_get_path (&sc
, path
)) == 0)
1603 g_free (remote_file
);
1604 convert_path(&path
, FALSE
);
1606 if (!cli_rmdir(sc
->cli
, path
)) {
1607 my_errno
= cli_error(sc
->cli
, NULL
, &err
, NULL
);
1608 message_3s (1, MSG_ERROR
, _(" Error %s removing directory %s "),
1609 cli_errstr(sc
->cli
), CNV_LANG(path
));
1619 smbfs_link (vfs
*me
, char *p1
, char *p2
)
1621 DEBUG(3, ("smbfs_link(p1:%s, p2:%s)\n", p1
, p2
));
1622 my_errno
= EOPNOTSUPP
;
1626 /* We do not free anything right now: we free resources when we run
1630 smbfs_getid (vfs
*me
, char *p
, struct vfs_stamping
**parent
)
1633 DEBUG(3, ("smbfs_getid(p:%s)\n", p
));
1639 smbfs_nothingisopen (vfsid id
)
1641 DEBUG(3, ("smbfs_nothingisopen(%d)\n", (int)id
));
1646 smbfs_free (vfsid id
)
1648 DEBUG(3, ("smbfs_free(%d)\n", (int)id
));
1649 /* FIXME: Should not be empty */
1650 authinfo_free_all ();
1653 /* Gives up on a socket and reopens the connection, the child own the socket
1657 my_forget (char *path
)
1659 char *host
, *user
, *p
;
1662 if (strncmp (path
, URL_HEADER
, HEADER_LEN
))
1665 DEBUG(3, ("my_forget(path:%s)\n", path
));
1668 if (path
[0] == '/' && path
[1] == '/')
1671 if ((p
= smbfs_get_host_and_username (&path
, &host
, &user
, &port
, NULL
))) {
1673 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
1674 if ((strcmp (host
, smbfs_connections
[i
].host
) == 0) &&
1675 (strcmp (user
, smbfs_connections
[i
].user
) == 0) &&
1676 (port
== smbfs_connections
[i
].port
)) {
1678 /* close socket: the child owns it now */
1679 cli_shutdown(smbfs_connections
[i
].cli
);
1681 /* reopen the connection */
1682 smbfs_connections
[i
].cli
=
1683 smbfs_do_connect(host
, smbfs_connections
[i
].service
);
1692 smbfs_setctl (vfs
*me
, char *path
, int ctlop
, char *arg
)
1694 DEBUG(3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path
, ctlop
));
1696 case MCCTL_FORGET_ABOUT
:
1703 static smbfs_handle
*
1704 open_write (smbfs_handle
*remote_handle
, char *rname
, int flags
, int mode
)
1706 if (flags
& O_TRUNC
) /* if it exists truncate to zero */
1707 DEBUG(3, ("open_write: O_TRUNC\n"));
1709 remote_handle
->fnum
= cli_open(remote_handle
->cli
, rname
, flags
, DENY_NONE
);
1711 if (remote_handle
->fnum
== -1) {
1712 message_3s (1, MSG_ERROR
, _(" %s opening remote file %s "),
1713 cli_errstr(remote_handle
->cli
), CNV_LANG(rname
));
1714 DEBUG(1,("smbfs_open(rname:%s) error:%s\n",
1715 rname
, cli_errstr(remote_handle
->cli
)));
1716 my_errno
= cli_error(remote_handle
->cli
, NULL
, &err
, NULL
);
1720 return remote_handle
;
1723 static smbfs_handle
*
1724 open_read (smbfs_handle
*remote_handle
, char *rname
, int flags
, int mode
)
1728 remote_handle
->fnum
=
1729 cli_open(remote_handle
->cli
, rname
, O_RDONLY
, DENY_NONE
);
1731 if (remote_handle
->fnum
== -1) {
1732 message_3s (1, MSG_ERROR
, _(" %s opening remote file %s "),
1733 cli_errstr(remote_handle
->cli
), CNV_LANG(rname
));
1734 DEBUG(1,("smbfs_open(rname:%s) error:%s\n",
1735 rname
, cli_errstr(remote_handle
->cli
)));
1736 my_errno
= cli_error(remote_handle
->cli
, NULL
, &err
, NULL
);
1740 if (!cli_qfileinfo(remote_handle
->cli
, remote_handle
->fnum
,
1741 &remote_handle
->attr
, &size
, NULL
, NULL
, NULL
, NULL
, NULL
) &&
1742 !cli_getattrE(remote_handle
->cli
, remote_handle
->fnum
,
1743 &remote_handle
->attr
, &size
, NULL
, NULL
, NULL
)) {
1744 message_2s (1, MSG_ERROR
, " getattrib: %s ",
1745 cli_errstr(remote_handle
->cli
));
1746 DEBUG(1,("smbfs_open(rname:%s) getattrib:%s\n",
1747 rname
, cli_errstr(remote_handle
->cli
)));
1748 my_errno
= cli_error(remote_handle
->cli
, NULL
, &err
, NULL
);
1752 return remote_handle
;
1756 smbfs_open (vfs
*me
, char *file
, int flags
, int mode
)
1758 char *remote_file
, *p
;
1760 smbfs_connection
*sc
;
1761 smbfs_handle
*remote_handle
;
1763 DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%d)\n", file
, flags
, mode
));
1765 if (!(remote_file
= smbfs_get_path (&sc
, file
)))
1769 convert_path(&remote_file
, FALSE
);
1772 remote_handle
= g_new (smbfs_handle
, 2);
1773 remote_handle
->cli
= sc
->cli
;
1774 remote_handle
->nread
= 0;
1776 if (flags
& O_CREAT
)
1777 ret
= open_write(remote_handle
, remote_file
, flags
, mode
);
1779 ret
= open_read(remote_handle
, remote_file
, flags
, mode
);
1781 g_free (remote_file
);
1787 smbfs_unlink (vfs
*me
, char *path
)
1789 smbfs_connection
*sc
;
1790 char *remote_file
, *p
;
1792 if ((remote_file
= smbfs_get_path (&sc
, path
)) == 0)
1796 convert_path(&remote_file
, FALSE
);
1799 if (!cli_unlink(sc
->cli
, remote_file
)) {
1800 message_3s (1, MSG_ERROR
, _(" %s removing remote file %s "),
1801 cli_errstr(sc
->cli
), CNV_LANG(remote_file
));
1802 g_free (remote_file
);
1805 g_free (remote_file
);
1810 smbfs_rename (vfs
*me
, char *a
, char *b
)
1812 smbfs_connection
*sc
;
1817 if ((ra
= smbfs_get_path (&sc
, a
)) == 0)
1820 if ((rb
= smbfs_get_path (&sc
, b
)) == 0) {
1826 convert_path(&ra
, FALSE
);
1829 convert_path(&rb
, FALSE
);
1832 retval
= cli_rename(sc
->cli
, ra
, rb
);
1838 message_2s (1, MSG_ERROR
, _(" %s renaming files\n"),
1839 cli_errstr(sc
->cli
));
1846 smbfs_fstat (void *data
, struct stat
*buf
)
1848 smbfs_handle
*remote_handle
= (smbfs_handle
*)data
;
1850 DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle
->fnum
));
1852 /* use left over from previous get_remote_stat, if available */
1854 memcpy(buf
, &single_entry
->my_stat
, sizeof(struct stat
));
1855 else { /* single_entry not set up: bug */
1862 vfs vfs_smbfs_ops
= {
1863 NULL
, /* This is place of next pointer */
1866 "smb:", /* prefix */
1905 smbfs_nothingisopen
,