forked from MidnightCommander/mc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmbfs.c
1915 lines (1640 loc) · 47.7 KB
/
smbfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Virtual File System: Midnight Commander file system.
Copyright (C) 1995, 1996, 1997 The Free Software Foundation
Written by Wayne Roberts <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License
as published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* Namespace: exports vfs_smbfs_ops, smbfs_set_debug(), smbfs_set_debugf() */
#include <config.h>
#include <stdio.h>
#include <sys/types.h>
#undef USE_NCURSES /* Don't include *curses.h */
#include "utilvfs.h"
#undef PACKAGE_BUGREPORT
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION
#include "samba/include/config.h"
/* don't load crap in "samba/include/includes.h" we don't use and which
conflicts with definitions in other includes */
#undef HAVE_LIBREADLINE
#define NO_CONFIG_H
#define BOOL_DEFINED
#undef VERSION
#include "samba/include/includes.h"
#include <string.h>
#include "vfs.h"
#include "smbfs.h"
#include "../src/dialog.h"
#define SMBFS_MAX_CONNECTIONS 16
static const char * const IPC = "IPC$";
static const char * const URL_HEADER = "/#smb:";
#define HEADER_LEN 6
static int my_errno;
static uint32 err;
/* stuff that is same with each connection */
extern int DEBUGLEVEL;
extern pstring myhostname;
extern pstring global_myname;
static int smbfs_open_connections = 0;
static gboolean got_user = FALSE;
static gboolean got_pass = FALSE;
static pstring password;
static pstring username;
static struct _smbfs_connection {
struct cli_state *cli;
struct in_addr dest_ip;
BOOL have_ip;
char *host; /* server name */
char *service; /* share name */
char *domain;
char *user;
char *home;
char *password;
int port;
int name_type;
time_t last_use;
} smbfs_connections [SMBFS_MAX_CONNECTIONS];
/* unique to each connection */
static struct cli_state * smbfs_do_connect (const char *server, char *share);
typedef struct _smbfs_connection smbfs_connection;
static smbfs_connection *current_bucket;
typedef struct {
struct cli_state *cli;
int fnum;
off_t nread;
uint16 attr;
} smbfs_handle;
static GSList *auth_list;
static void
authinfo_free (struct smb_authinfo const *a)
{
g_free (a->host);
g_free (a->share);
g_free (a->domain);
g_free (a->user);
wipe_password (a->password);
}
static void
authinfo_free_all ()
{
if (auth_list) {
g_slist_foreach (auth_list, (GFunc)authinfo_free, 0);
g_slist_free (auth_list);
auth_list = 0;
}
}
static gint
authinfo_compare_host_and_share (gconstpointer _a, gconstpointer _b)
{
struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
if (!a->host || !a->share || !b->host || !b->share)
return 1;
if (strcmp (a->host, b->host) != 0)
return 1;
if (strcmp (a->share, b->share) != 0)
return 1;
return 0;
}
static gint
authinfo_compare_host (gconstpointer _a, gconstpointer _b)
{
struct smb_authinfo const *a = (struct smb_authinfo const *)_a;
struct smb_authinfo const *b = (struct smb_authinfo const *)_b;
if (!a->host || !b->host)
return 1;
if (strcmp (a->host, b->host) != 0)
return 1;
if (strcmp (a->share, IPC) != 0)
return 1;
return 0;
}
static void
authinfo_add (const char *host, const char *share, const char *domain,
const char *user, const char *password)
{
struct smb_authinfo *auth = g_new (struct smb_authinfo, 1);
if (!auth)
return;
/* Don't check for NULL, g_strdup already does. */
auth->host = g_strdup (host);
auth->share = g_strdup (share);
auth->domain = g_strdup (domain);
auth->user = g_strdup (user);
auth->password = g_strdup (password);
auth_list = g_slist_prepend (auth_list, auth);
}
static void
authinfo_remove (const char *host, const char *share)
{
struct smb_authinfo data;
struct smb_authinfo *auth;
GSList *list;
data.host = g_strdup (host);
data.share = g_strdup (share);
list = g_slist_find_custom (auth_list,
&data,
authinfo_compare_host_and_share);
g_free (data.host);
g_free (data.share);
if (!list)
return;
auth = list->data;
auth_list = g_slist_remove (auth_list, auth);
authinfo_free (auth);
}
/* Set authentication information in bucket. Return 1 if successful, else 0 */
/* Information in auth_list overrides user if pass is NULL. */
/* bucket->host and bucket->service must be valid. */
static int
bucket_set_authinfo (smbfs_connection *bucket,
const char *domain, const char *user, const char *pass,
int fallback_to_host)
{
struct smb_authinfo data;
struct smb_authinfo *auth;
GSList *list;
if (domain && user && pass) {
g_free (bucket->domain);
g_free (bucket->user);
g_free (bucket->password);
bucket->domain = g_strdup (domain);
bucket->user = g_strdup (user);
bucket->password = g_strdup (pass);
authinfo_remove (bucket->host, bucket->service);
authinfo_add (bucket->host, bucket->service,
domain, user, pass);
return 1;
}
data.host = bucket->host;
data.share = bucket->service;
list = g_slist_find_custom (auth_list, &data, authinfo_compare_host_and_share);
if (!list && fallback_to_host)
list = g_slist_find_custom (auth_list, &data, authinfo_compare_host);
if (list) {
auth = list->data;
bucket->domain = g_strdup (auth->domain);
bucket->user = g_strdup (auth->user);
bucket->password = g_strdup (auth->password);
return 1;
}
if (got_pass) {
bucket->domain = g_strdup (lp_workgroup ());
bucket->user = g_strdup (got_user ? username : user);
bucket->password = g_strdup (password);
return 1;
}
auth = vfs_smb_get_authinfo (bucket->host,
bucket->service,
(domain ? domain : lp_workgroup ()),
user);
if (auth) {
g_free (bucket->domain);
g_free (bucket->user);
g_free (bucket->password);
bucket->domain = g_strdup (auth->domain);
bucket->user = g_strdup (auth->user);
bucket->password = g_strdup (auth->password);
authinfo_remove (bucket->host, bucket->service);
auth_list = g_slist_prepend (auth_list, auth);
return 1;
}
return 0;
}
void
smbfs_set_debug (int arg)
{
DEBUGLEVEL = arg;
}
void
smbfs_set_debugf (const char *filename)
{
extern pstring debugf;
extern FILE *dbf;
if (DEBUGLEVEL > 0) {
FILE *outfile = fopen (filename, "w");
if (outfile) {
setup_logging ("", True); /* No needs for timestamp for each message */
dbf = outfile;
setbuf (dbf, NULL);
pstrcpy (debugf, filename);
}
}
}
/********************** The callbacks ******************************/
static int
smbfs_init (vfs * me)
{
char *servicesf = CONFIGDIR PATH_SEP_STR "smb.conf";
/* DEBUGLEVEL = 4; */
TimeInit ();
charset_initialise ();
DEBUG (3, ("smbfs_init(%s)\n", me->name));
if (!get_myname (myhostname, NULL))
DEBUG (0, ("Failed to get my hostname.\n"));
if (!lp_load (servicesf, True, False, False))
DEBUG (0, ("Cannot load %s - run testparm to debug it\n", servicesf));
codepage_initialise (lp_client_code_page ());
load_interfaces ();
if (getenv ("USER")) {
char *p;
pstrcpy (username, getenv ("USER"));
got_user = TRUE;
DEBUG (3, ("smbfs_init(): $USER:%s\n", username));
if ((p = strchr (username, '%'))) {
*p = 0;
pstrcpy (password, p + 1);
got_pass = TRUE;
memset (strchr (getenv ("USER"), '%') + 1, 'X', strlen (password));
DEBUG (3, ("smbfs_init(): $USER%%pass: %s%%%s\n",
username, password));
}
strupper (username);
}
if (getenv ("PASSWD")) {
pstrcpy (password, getenv ("PASSWD"));
got_pass = TRUE;
}
return 1;
}
static void
smbfs_fill_names (vfs *me, void (*func)(char *))
{
int i;
char *path;
for (i = 0; i < SMBFS_MAX_CONNECTIONS; i++) {
if (smbfs_connections [i].cli) {
path = g_strconcat (URL_HEADER,
smbfs_connections[i].host,
"/", smbfs_connections[i].service,
NULL);
(*func)(path);
g_free (path);
}
}
}
#define CNV_LANG(s) dos_to_unix(s,False)
#define GNAL_VNC(s) unix_to_dos(s,False)
/* does same as do_get() in client.c */
/* called from vfs.c:1080, count = buffer size */
static int
smbfs_read (void *data, char *buffer, int count)
{
smbfs_handle *info = (smbfs_handle *) data;
int n;
DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%d)\n",
info->fnum, (int)info->nread, count));
n = cli_read(info->cli, info->fnum, buffer, info->nread, count);
if (n > 0)
info->nread += n;
return n;
}
static int
smbfs_write (void *data, char *buf, int nbyte)
{
smbfs_handle *info = (smbfs_handle *) data;
int n;
DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%d)\n",
info->fnum, (int)info->nread, nbyte));
n = cli_write(info->cli, info->fnum, 0, buf, info->nread, nbyte);
if (n > 0)
info->nread += n;
return n;
}
static int
smbfs_close (void *data)
{
smbfs_handle *info = (smbfs_handle *) data;
DEBUG (3, ("smbfs_close(fnum:%d)\n", info->fnum));
/* FIXME: Why too different cli have the same outbuf
* if file is copied to share
*/
if (info->cli->outbuf == NULL) {
my_errno = EINVAL;
return -1;
}
#if 0
/* if imlementing archive_level: add rname to smbfs_handle */
if (archive_level >= 2 && (inf->attr & aARCH)) {
cli_setatr (info->cli, rname, info->attr & ~(uint16) aARCH, 0);
}
#endif
return (cli_close (info->cli, info->fnum) == True) ? 0 : -1;
}
static int
smbfs_errno (vfs *me)
{
DEBUG(3, ("smbfs_errno: %s\n", g_strerror(my_errno)));
return my_errno;
}
typedef struct dir_entry {
char *text;
struct dir_entry *next;
struct stat my_stat;
int merrno;
} dir_entry;
typedef struct {
gboolean server_list;
char *dirname;
char *path; /* the dir originally passed to smbfs_opendir */
smbfs_connection *conn;
dir_entry *entries;
dir_entry *current;
} opendir_info;
static opendir_info
*previous_info,
*current_info,
*current_share_info,
*current_server_info;
static gboolean first_direntry;
static dir_entry *
new_dir_entry (const char * name)
{
dir_entry *new_entry;
new_entry = g_new0 (dir_entry, 1);
new_entry->text = dos_to_unix (g_strdup (name), 1);
if (first_direntry) {
current_info->entries = new_entry;
first_direntry = FALSE;
} else {
current_info->current->next = new_entry;
}
current_info->current = new_entry;
return new_entry;
}
/* browse for shares on server */
static void
browsing_helper (const char *name, uint32 type, const char *comment, void *state)
{
char *typestr = "";
dir_entry *new_entry = new_dir_entry (name);
switch (type) {
case STYPE_DISKTREE:
typestr = "Disk";
/* show this as dir */
new_entry->my_stat.st_mode =
S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP |
S_IXOTH;
break;
case STYPE_PRINTQ:
typestr = "Printer";
break;
case STYPE_DEVICE:
typestr = "Device";
break;
case STYPE_IPC:
typestr = "IPC";
break;
}
DEBUG (3, ("\t%-15.15s%-10.10s%s\n", name, typestr, comment));
}
static void
loaddir_helper (file_info * finfo, const char *mask, void *entry)
{
dir_entry *new_entry = (dir_entry *) entry;
time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
#if 0 /* I want to see dot files */
if (finfo->mode & aHIDDEN)
return; /* don't bother with hidden files, "~$" screws up mc */
#endif
if (!entry)
new_entry = new_dir_entry (finfo->name);
new_entry->my_stat.st_size = finfo->size;
new_entry->my_stat.st_mtime = finfo->mtime;
new_entry->my_stat.st_atime = finfo->atime;
new_entry->my_stat.st_ctime = finfo->ctime;
new_entry->my_stat.st_uid = finfo->uid;
new_entry->my_stat.st_gid = finfo->gid;
new_entry->my_stat.st_mode = /* rw-rw-rw */
S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH;
/* if (finfo->mode & aVOLID); nothing similar in real world */
if (finfo->mode & aDIR)
new_entry->my_stat.st_mode |= /* drwxrwxrwx */
S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
else
new_entry->my_stat.st_mode |= S_IFREG; /* if not dir, regular file? */
/* if (finfo->mode & aARCH); DOS archive */
/* if (finfo->mode & aHIDDEN); like a dot file? */
/* if (finfo->mode & aSYSTEM); like a kernel? */
if (finfo->mode & aRONLY)
new_entry->my_stat.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
DEBUG (entry ? 3 : 6, (" %-30s%7.7s%8.0f %s",
CNV_LANG (finfo->name),
attrib_string (finfo->mode),
(double) finfo->size,
asctime (LocalTime (&t))));
}
/* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
static int
convert_path(char **remote_file, gboolean trailing_asterik)
{
char *p, *my_remote;
my_remote = *remote_file;
if (strncmp (my_remote, URL_HEADER, HEADER_LEN) == 0) { /* if passed directly */
my_remote += 6;
if (*my_remote == '/') /* from server browsing */
my_remote++;
p = strchr(my_remote, '/');
if (p)
my_remote = p+1; /* advance to end of server name */
}
if (*my_remote == '/')
my_remote++; /* strip off leading '/' */
p = strchr(my_remote, '/');
if (p)
my_remote = p; /* strip off share/service name */
/* create remote filename as understood by smb clientgen */
p = *remote_file = g_strconcat (my_remote, trailing_asterik ? "/*" : "", 0);
unix_to_dos (*remote_file, 1);
while ((p = strchr(p, '/')))
*p = '\\';
return 0;
}
static void
server_browsing_helper (const char *name, uint32 m, const char *comment, void *state)
{
dir_entry *new_entry = new_dir_entry (name);
/* show this as dir */
new_entry->my_stat.st_mode =
S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP | S_IXOTH;
DEBUG (3, ("\t%-16.16s %s\n", name, comment));
}
static BOOL
reconnect(smbfs_connection *conn, int *retries)
{
char *host;
DEBUG(3, ("RECONNECT\n"));
if (*(conn->host) == 0)
host = g_strdup(conn->cli->desthost); /* server browsing */
else
host = g_strdup(conn->host);
cli_shutdown(conn->cli);
if (!(conn->cli = smbfs_do_connect(host, conn->service))) {
message_2s (1, MSG_ERROR,
_(" reconnect to %s failed\n "), conn->host);
g_free(host);
return False;
}
g_free(host);
if (++(*retries) == 2)
return False;
return True;
}
static BOOL
smb_send(struct cli_state *cli)
{
size_t len;
size_t nwritten=0;
ssize_t ret;
len = smb_len(cli->outbuf) + 4;
while (nwritten < len) {
ret = write_socket(cli->fd, cli->outbuf+nwritten, len - nwritten);
if (ret <= 0 && errno == EPIPE)
return False;
nwritten += ret;
}
return True;
}
/****************************************************************************
See if server has cut us off by checking for EPIPE when writing.
Taken from cli_chkpath()
****************************************************************************/
static BOOL
chkpath(struct cli_state *cli, char *path, BOOL send_only)
{
fstring path2;
char *p;
fstrcpy(path2,path);
unix_to_dos (path2, 1);
trim_string(path2,NULL,"\\");
if (!*path2) *path2 = '\\';
memset(cli->outbuf,'\0',smb_size);
set_message(cli->outbuf,0,4 + strlen(path2),True);
SCVAL(cli->outbuf,smb_com,SMBchkpth);
SSVAL(cli->outbuf,smb_tid,cli->cnum);
cli->rap_error = 0;
cli->nt_error = 0;
SSVAL(cli->outbuf,smb_pid,cli->pid);
SSVAL(cli->outbuf,smb_uid,cli->vuid);
SSVAL(cli->outbuf,smb_mid,cli->mid);
if (cli->protocol > PROTOCOL_CORE) {
SCVAL(cli->outbuf,smb_flg,0x8);
SSVAL(cli->outbuf,smb_flg2,0x1);
}
p = smb_buf(cli->outbuf);
*p++ = 4;
fstrcpy(p,path2);
if (!smb_send(cli)) {
DEBUG(3, ("chkpath: couldnt send\n"));
return False;
}
if (send_only) {
client_receive_smb(cli->fd, cli->inbuf, cli->timeout);
DEBUG(3, ("chkpath: send only OK\n"));
return True; /* just testing for EPIPE */
}
if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout)) {
DEBUG(3, ("chkpath: receive error\n"));
return False;
}
if ((my_errno = cli_error(cli, NULL, NULL, NULL))) {
if (my_errno == 20 || my_errno == 13)
return True; /* ignore if 'not a directory' error */
DEBUG(3, ("chkpath: cli_error: %s\n", g_strerror(my_errno)));
return False;
}
return True;
}
#if 1
static int
fs (const char *text)
{
const char *p = text;
int count = 0;
while ((p = strchr(p, '/')) != NULL) {
count++;
p++;
}
if (count == 1)
return strlen(text);
return count;
}
#endif
static int
smbfs_loaddir (opendir_info *smbfs_info)
{
uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
int servlen = strlen(smbfs_info->conn->service);
char *my_dirname = smbfs_info->dirname;
DEBUG(3, ("smbfs_loaddir: dirname:%s\n", my_dirname));
first_direntry = TRUE;
if (current_info) {
DEBUG(3, ("smbfs_loaddir: new:'%s', cached:'%s'\n", my_dirname, current_info->dirname));
/* if new desired dir is longer than cached in current_info */
if (fs(my_dirname) > fs(current_info->dirname)) {
DEBUG(3, ("saving to previous_info\n"));
previous_info = current_info;
}
}
current_info = smbfs_info;
if (strcmp(my_dirname, "/") == 0) {
if (!strcmp(smbfs_info->path, URL_HEADER)) {
DEBUG(6, ("smbfs_loaddir: browsing %s\n", IPC));
/* browse for servers */
if (!cli_NetServerEnum(smbfs_info->conn->cli, smbfs_info->conn->domain,
SV_TYPE_ALL, server_browsing_helper, NULL))
return 0;
else
current_server_info = smbfs_info;
smbfs_info->server_list = TRUE;
} else {
/* browse for shares */
if (cli_RNetShareEnum(smbfs_info->conn->cli, browsing_helper, NULL) < 1)
return 0;
else
current_share_info = smbfs_info;
}
goto done;
}
/* do regular directory listing */
if(strncmp(smbfs_info->conn->service, my_dirname+1, servlen) == 0) {
/* strip share name from dir */
char *p = my_dirname = g_strdup(my_dirname + servlen);
*p = '/';
convert_path(&my_dirname, TRUE);
g_free (p);
} else
convert_path(&my_dirname, TRUE);
DEBUG(6, ("smbfs_loaddir: service: %s\n", smbfs_info->conn->service));
DEBUG(6, ("smbfs_loaddir: cli->share: %s\n", smbfs_info->conn->cli->share));
DEBUG(6, ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname));
/* do file listing: cli_list returns number of files */
if (cli_list(
smbfs_info->conn->cli, my_dirname, attribute, loaddir_helper, NULL) < 0) {
/* cli_list returns -1 if directory empty or cannot read socket */
my_errno = cli_error(smbfs_info->conn->cli, NULL, &err, NULL);
g_free (my_dirname);
return 0;
}
if (*(my_dirname) == 0)
smbfs_info->dirname = smbfs_info->conn->service;
g_free (my_dirname);
/* do_dskattr(); */
done:
/* current_info->parent = smbfs_info->dirname; */
smbfs_info->current = smbfs_info->entries;
return 1; /* 1 = ok */
}
#ifdef SMBFS_FREE_DIR
static void
smbfs_free_dir (dir_entry *de)
{
if (!de) return;
smbfs_free_dir (de->next);
g_free (de->text);
g_free (de);
}
#endif
/* The readdir routine loads the complete directory */
/* It's too slow to ask the server each time */
/* It now also sends the complete lstat information for each file */
static void *
smbfs_readdir(void *info)
{
static union vfs_dirent smbfs_readdir_data;
static char *const dirent_dest = smbfs_readdir_data.dent.d_name;
opendir_info *smbfs_info = (opendir_info *) info;
DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info->dirname));
if (!smbfs_info->entries)
if (!smbfs_loaddir(smbfs_info))
return NULL;
if (smbfs_info->current == 0) { /* reached end of dir entries */
DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
#ifdef SMBFS_FREE_DIR
smbfs_free_dir(smbfs_info->entries);
smbfs_info->entries = 0;
#endif
return NULL;
}
strncpy(dirent_dest, smbfs_info->current->text, MC_MAXPATHLEN);
dirent_dest[MC_MAXPATHLEN] = 0;
smbfs_info->current = smbfs_info->current->next;
compute_namelen(&smbfs_readdir_data.dent);
return &smbfs_readdir_data;
}
static int
smbfs_closedir (void *info)
{
opendir_info *smbfs_info = (opendir_info *) info;
/* dir_entry *p, *q; */
DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info->dirname));
/* CLOSE HERE */
/* for (p = smbfs_info->entries; p;){
q = p;
p = p->next;
g_free (q->text);
g_free (q);
}
g_free (info); */
return 0;
}
static int
smbfs_chmod (vfs *me, char *path, int mode)
{
DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path, mode));
/* my_errno = EOPNOTSUPP;
return -1; */ /* cant chmod on smb filesystem */
return 0; /* make mc happy */
}
static int
smbfs_chown (vfs *me, char *path, int owner, int group)
{
DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path, owner, group));
my_errno = EOPNOTSUPP; /* ready for your labotomy? */
return -1;
}
static int
smbfs_utime (vfs *me, char *path, struct utimbuf *times)
{
DEBUG(3, ("smbfs_utime(path:%s)\n", path));
my_errno = EOPNOTSUPP;
return -1;
}
static int
smbfs_readlink (vfs *me, char *path, char *buf, int size)
{
DEBUG(3, ("smbfs_readlink(path:%s, buf:%s, size:%d)\n", path, buf, size));
my_errno = EOPNOTSUPP;
return -1; /* no symlinks on smb filesystem? */
}
static int
smbfs_symlink (vfs *me, char *n1, char *n2)
{
DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1, n2));
my_errno = EOPNOTSUPP;
return -1; /* no symlinks on smb filesystem? */
}
/* Extract the hostname and username from the path */
/* path is in the form: hostname:user/remote-dir */
#if 0
static char *
smbfs_get_host_and_username
(char **path, char **host, char **user, int *port, char **pass)
{
/* char *p, *ret; */
char *ret;
ret = vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0);
#if 0
if ((p = strchr(*path, '@'))) /* user:pass@server */
*path = ++p; /* don't want user:pass@ in path */
if ((p = strchr(ret, '@'))) /* user:pass@server */
ret = ++p; /* don't want user:pass@ in path */
#endif
return ret;
}
#else
#define smbfs_get_host_and_username(path, host, user, port, pass) \
vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
#endif
/*****************************************************
return a connection to a SMB server
current_bucket needs to be set before calling
*******************************************************/
static struct cli_state *
smbfs_do_connect (const char *server, char *share)
{
struct cli_state *c;
struct nmb_name called, calling;
struct in_addr ip;
extern struct in_addr ipzero;
DEBUG(3, ("smbfs_do_connect(%s, %s)\n", server, share));
if (*share == '\\') {
server = share+2;
share = strchr(server,'\\');
if (!share) return NULL;
*share = 0;
share++;
}
make_nmb_name(&calling, global_myname, 0x0);
make_nmb_name(&called , server, current_bucket->name_type);
for (;;) {
ip = (current_bucket->have_ip) ? current_bucket->dest_ip : ipzero;
/* have to open a new connection */
if (!(c = cli_initialise(NULL))) {
my_errno = ENOMEM;
return NULL;
}
pwd_init(&(c->pwd)); /* should be moved into cli_initialise()? */
pwd_set_cleartext(&(c->pwd), current_bucket->password);
if ((cli_set_port(c, current_bucket->port) == 0) ||
!cli_connect(c, server, &ip)) {
DEBUG(1, ("Connection to %s failed\n", server));
break;
}
if (!cli_session_request(c, &calling, &called)) {
my_errno = cli_error(c, NULL, &err, NULL);
DEBUG(1, ("session request to %s failed\n", called.name));
cli_shutdown(c);
if (strcmp(called.name, "*SMBSERVER")) {
make_nmb_name(&called , "*SMBSERVER", 0x20);
continue;
}
return NULL;
}
DEBUG(3, (" session request ok\n"));
if (!cli_negprot(c)) {
DEBUG(1, ("protocol negotiation failed\n"));
break;
}
if (!cli_session_setup(c, current_bucket->user,
current_bucket->password, strlen(current_bucket->password),
current_bucket->password, strlen(current_bucket->password),
current_bucket->domain)) {
DEBUG(1,("session setup failed: %s\n", cli_errstr(c)));
authinfo_remove (server, share);
break;
}
if (*c->server_domain || *c->server_os || *c->server_type)
DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
c->server_domain,c->server_os,c->server_type));
DEBUG(3, (" session setup ok\n"));
if (!cli_send_tconX(c, share, "?????",
current_bucket->password, strlen(current_bucket->password)+1)) {
DEBUG(1,("%s: tree connect failed: %s\n", share, cli_errstr(c)));
break;
}
DEBUG(3, (" tconx ok\n"));
my_errno = 0;
return c;
}
my_errno = cli_error(c, NULL, &err, NULL);
cli_shutdown(c);
return NULL;
}
static int
get_master_browser(char **host)
{
int count;
struct in_addr *ip_list, bcast_addr;
extern struct in_addr ipzero;
/* does port = 137 for win95 master browser? */
int fd= open_socket_in( SOCK_DGRAM, 0, 3,
interpret_addr(lp_socket_address()), True );
if (fd == -1)
return 0;
set_socket_options(fd, "SO_BROADCAST");
ip_list = iface_bcast(ipzero);
bcast_addr = *ip_list;
if ((ip_list = name_query(fd, "\01\02__MSBROWSE__\02", 1, True,
True, bcast_addr, &count, NULL))) {
if (!count)
return 0;
/* just return first master browser */
*host = g_strdup(inet_ntoa(ip_list[0]));
return 1;
}
return 0;
}
static void
free_bucket (smbfs_connection *bucket)
{
g_free (bucket->host);
g_free (bucket->service);
g_free (bucket->domain);
g_free (bucket->user);
wipe_password (bucket->password);
if (bucket->home) g_free (bucket->home);