-
Notifications
You must be signed in to change notification settings - Fork 428
/
client.c
3552 lines (2971 loc) · 95.8 KB
/
client.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
/*
Unix SMB/CIFS implementation.
SMB client
Copyright (C) Andrew Tridgell 1994-1998
Copyright (C) Simo Sorce 2001-2002
Copyright (C) Jelmer Vernooij 2003-2004
Copyright (C) James J Myers 2003 <myersjj@samba.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* TODO: remove this ... and don't use talloc_append_string()
*
* NOTE: I'm not changing the code yet, because I assume there're
* some bugs in the existing code and I'm not sure how to fix
* them correctly.
*/
#define TALLOC_DEPRECATED 1
#include "includes.h"
#include "version.h"
#include "libcli/libcli.h"
#include "lib/events/events.h"
#include "lib/cmdline/cmdline.h"
#include "librpc/gen_ndr/ndr_srvsvc_c.h"
#include "librpc/gen_ndr/ndr_lsa.h"
#include "librpc/gen_ndr/ndr_security.h"
#include "libcli/util/clilsa.h"
#include "system/dir.h"
#include "system/filesys.h"
#include "../lib/util/dlinklist.h"
#include "system/readline.h"
#include "auth/credentials/credentials.h"
#include "auth/gensec/gensec.h"
#include "system/time.h" /* needed by some systems for asctime() */
#include "libcli/resolve/resolve.h"
#include "libcli/security/security.h"
#include "../libcli/smbreadline/smbreadline.h"
#include "librpc/gen_ndr/ndr_nbt.h"
#include "param/param.h"
#include "libcli/raw/raw_proto.h"
/* the default pager to use for the client "more" command. Users can
* override this with the PAGER environment variable */
#ifndef DEFAULT_PAGER
#define DEFAULT_PAGER "more"
#endif
#undef strncasecmp
struct smbclient_context {
char *remote_cur_dir;
struct smbcli_state *cli;
char *fileselection;
time_t newer_than;
bool prompt;
bool recurse;
int archive_level;
bool lowercase;
int printmode;
bool translation;
int io_bufsize;
};
/* timing globals */
static uint64_t get_total_size = 0;
static unsigned int get_total_time_ms = 0;
static uint64_t put_total_size = 0;
static unsigned int put_total_time_ms = 0;
/* Unfortunately, there is no way to pass a context to the completion function as an argument */
static struct smbclient_context *rl_ctx;
/* totals globals */
static double dir_total;
/*******************************************************************
Reduce a file name, removing .. elements.
********************************************************************/
static void dos_clean_name(char *s)
{
char *p=NULL,*r;
DEBUG(3,("dos_clean_name [%s]\n",s));
/* remove any double slashes */
all_string_sub(s, "\\\\", "\\", 0);
while ((p = strstr(s,"\\..\\")) != NULL) {
*p = '\0';
if ((r = strrchr(s,'\\')) != NULL)
memmove(r,p+3,strlen(p+3)+1);
}
trim_string(s,NULL,"\\..");
all_string_sub(s, "\\.\\", "\\", 0);
}
/****************************************************************************
write to a local file with CR/LF->LF translation if appropriate. return the
number taken from the buffer. This may not equal the number written.
****************************************************************************/
static int writefile(int f, const void *_b, int n, bool translation)
{
const uint8_t *b = (const uint8_t *)_b;
int i;
if (!translation) {
return write(f,b,n);
}
i = 0;
while (i < n) {
if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
b++;i++;
}
if (write(f, b, 1) != 1) {
break;
}
b++;
i++;
}
return(i);
}
/****************************************************************************
read from a file with LF->CR/LF translation if appropriate. return the
number read. read approx n bytes.
****************************************************************************/
static int readfile(void *_b, int n, FILE *f, bool translation)
{
uint8_t *b = (uint8_t *)_b;
int i;
int c;
if (!translation)
return fread(b,1,n,f);
i = 0;
while (i < (n - 1)) {
if ((c = getc(f)) == EOF) {
break;
}
if (c == '\n') { /* change all LFs to CR/LF */
b[i++] = '\r';
}
b[i++] = c;
}
return(i);
}
/****************************************************************************
send a message
****************************************************************************/
static void send_message(struct smbcli_state *cli, const char *desthost)
{
char msg[1600];
int total_len = 0;
int grp_id;
struct cli_credentials *creds = samba_cmdline_get_creds();
if (!smbcli_message_start(cli->tree,
desthost,
cli_credentials_get_username(creds),
&grp_id)) {
d_printf("message start: %s\n", smbcli_errstr(cli->tree));
return;
}
d_printf("Connected. Type your message, ending it with a Control-D\n");
while (!feof(stdin) && total_len < 1600) {
int maxlen = MIN(1600 - total_len,127);
int l=0;
int c;
for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
if (c == '\n')
msg[l++] = '\r';
msg[l] = c;
}
if (!smbcli_message_text(cli->tree, msg, l, grp_id)) {
d_printf("SMBsendtxt failed (%s)\n",smbcli_errstr(cli->tree));
return;
}
total_len += l;
}
if (total_len >= 1600)
d_printf("the message was truncated to 1600 bytes\n");
else
d_printf("sent %d bytes\n",total_len);
if (!smbcli_message_end(cli->tree, grp_id)) {
d_printf("SMBsendend failed (%s)\n",smbcli_errstr(cli->tree));
return;
}
}
/****************************************************************************
check the space on a device
****************************************************************************/
static int do_dskattr(struct smbclient_context *ctx)
{
uint32_t bsize;
uint64_t total, avail;
if (NT_STATUS_IS_ERR(smbcli_dskattr(ctx->cli->tree, &bsize, &total, &avail))) {
d_printf("Error in dskattr: %s\n",smbcli_errstr(ctx->cli->tree));
return 1;
}
d_printf("\n\t\t%llu blocks of size %u. %llu blocks available\n",
(unsigned long long)total,
(unsigned)bsize,
(unsigned long long)avail);
return 0;
}
/****************************************************************************
show cd/pwd
****************************************************************************/
static int cmd_pwd(struct smbclient_context *ctx, const char **args)
{
d_printf("Current directory is %s\n", ctx->remote_cur_dir);
return 0;
}
/*
convert a string to dos format
*/
static void dos_format(char *s)
{
string_replace(s, '/', '\\');
}
/****************************************************************************
change directory - inner section
****************************************************************************/
static int do_cd(struct smbclient_context *ctx, const char *newdir)
{
char *dname;
/* Save the current directory in case the
new directory is invalid */
if (newdir[0] == '\\')
dname = talloc_strdup(ctx, newdir);
else
dname = talloc_asprintf(ctx, "%s\\%s", ctx->remote_cur_dir, newdir);
dos_format(dname);
if (*(dname+strlen(dname)-1) != '\\') {
dname = talloc_append_string(NULL, dname, "\\");
}
dos_clean_name(dname);
if (NT_STATUS_IS_ERR(smbcli_chkpath(ctx->cli->tree, dname))) {
d_printf("cd %s: %s\n", dname, smbcli_errstr(ctx->cli->tree));
talloc_free(dname);
} else {
ctx->remote_cur_dir = dname;
}
return 0;
}
/****************************************************************************
change directory
****************************************************************************/
static int cmd_cd(struct smbclient_context *ctx, const char **args)
{
int rc = 0;
if (args[1])
rc = do_cd(ctx, args[1]);
else
d_printf("Current directory is %s\n",ctx->remote_cur_dir);
return rc;
}
static bool mask_match(struct smbcli_state *c, const char *string,
const char *pattern, bool is_case_sensitive)
{
int ret;
if (ISDOTDOT(string))
string = ".";
if (ISDOT(pattern))
return false;
ret = ms_fnmatch_protocol(pattern, string,
c->transport->negotiate.protocol,
is_case_sensitive);
return (ret == 0);
}
/*******************************************************************
decide if a file should be operated on
********************************************************************/
static bool do_this_one(struct smbclient_context *ctx, struct clilist_file_info *finfo)
{
if (finfo->attrib & FILE_ATTRIBUTE_DIRECTORY) return(true);
if (ctx->fileselection &&
!mask_match(ctx->cli, finfo->name,ctx->fileselection,false)) {
DEBUG(3,("mask_match %s failed\n", finfo->name));
return false;
}
if (ctx->newer_than && finfo->mtime < ctx->newer_than) {
DEBUG(3,("newer_than %s failed\n", finfo->name));
return(false);
}
if ((ctx->archive_level==1 || ctx->archive_level==2) && !(finfo->attrib & FILE_ATTRIBUTE_ARCHIVE)) {
DEBUG(3,("archive %s failed\n", finfo->name));
return(false);
}
return(true);
}
/****************************************************************************
display info about a file
****************************************************************************/
static void display_finfo(struct smbclient_context *ctx, struct clilist_file_info *finfo)
{
if (do_this_one(ctx, finfo)) {
time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
char *astr = attrib_string(NULL, finfo->attrib);
d_printf(" %-30s%7.7s %8.0f %s",
finfo->name,
astr,
(double)finfo->size,
asctime(localtime(&t)));
dir_total += finfo->size;
talloc_free(astr);
}
}
/****************************************************************************
accumulate size of a file
****************************************************************************/
static void do_du(struct smbclient_context *ctx, struct clilist_file_info *finfo)
{
if (do_this_one(ctx, finfo)) {
dir_total += finfo->size;
}
}
static bool do_list_recurse;
static bool do_list_dirs;
static char *do_list_queue = 0;
static long do_list_queue_size = 0;
static long do_list_queue_start = 0;
static long do_list_queue_end = 0;
static void (*do_list_fn)(struct smbclient_context *, struct clilist_file_info *);
/****************************************************************************
functions for do_list_queue
****************************************************************************/
/*
* The do_list_queue is a NUL-separated list of strings stored in a
* char*. Since this is a FIFO, we keep track of the beginning and
* ending locations of the data in the queue. When we overflow, we
* double the size of the char*. When the start of the data passes
* the midpoint, we move everything back. This is logically more
* complex than a linked list, but easier from a memory management
* angle. In any memory error condition, do_list_queue is reset.
* Functions check to ensure that do_list_queue is non-NULL before
* accessing it.
*/
static void reset_do_list_queue(void)
{
SAFE_FREE(do_list_queue);
do_list_queue_size = 0;
do_list_queue_start = 0;
do_list_queue_end = 0;
}
static void init_do_list_queue(void)
{
reset_do_list_queue();
do_list_queue_size = 1024;
do_list_queue = malloc_array_p(char, do_list_queue_size);
if (do_list_queue == 0) {
d_printf("malloc fail for size %d\n",
(int)do_list_queue_size);
reset_do_list_queue();
} else {
memset(do_list_queue, 0, do_list_queue_size);
}
}
static void adjust_do_list_queue(void)
{
if (do_list_queue == NULL) return;
/*
* If the starting point of the queue is more than half way through,
* move everything toward the beginning.
*/
if (do_list_queue_start == do_list_queue_end)
{
DEBUG(4,("do_list_queue is empty\n"));
do_list_queue_start = do_list_queue_end = 0;
*do_list_queue = '\0';
}
else if (do_list_queue_start > (do_list_queue_size / 2))
{
DEBUG(4,("sliding do_list_queue backward\n"));
memmove(do_list_queue,
do_list_queue + do_list_queue_start,
do_list_queue_end - do_list_queue_start);
do_list_queue_end -= do_list_queue_start;
do_list_queue_start = 0;
}
}
static void add_to_do_list_queue(const char* entry)
{
char *dlq;
long new_end;
if (entry == NULL) {
entry = "";
}
new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
while (new_end > do_list_queue_size)
{
do_list_queue_size *= 2;
DEBUG(4,("enlarging do_list_queue to %d\n",
(int)do_list_queue_size));
dlq = realloc_p(do_list_queue, char, do_list_queue_size);
if (! dlq) {
d_printf("failure enlarging do_list_queue to %d bytes\n",
(int)do_list_queue_size);
reset_do_list_queue();
}
else
{
do_list_queue = dlq;
memset(do_list_queue + do_list_queue_size / 2,
0, do_list_queue_size / 2);
}
}
if (do_list_queue)
{
strlcpy(do_list_queue + do_list_queue_end, entry,
do_list_queue_size - do_list_queue_end);
do_list_queue_end = new_end;
DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
entry, (int)do_list_queue_start, (int)do_list_queue_end));
}
}
static char *do_list_queue_head(void)
{
return do_list_queue + do_list_queue_start;
}
static void remove_do_list_queue_head(void)
{
if (do_list_queue_end > do_list_queue_start)
{
do_list_queue_start += strlen(do_list_queue_head()) + 1;
adjust_do_list_queue();
DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
(int)do_list_queue_start, (int)do_list_queue_end));
}
}
static int do_list_queue_empty(void)
{
return (! (do_list_queue && *do_list_queue));
}
/****************************************************************************
a helper for do_list
****************************************************************************/
static void do_list_helper(struct clilist_file_info *f, const char *mask, void *state)
{
struct smbclient_context *ctx = (struct smbclient_context *)state;
if (f->attrib & FILE_ATTRIBUTE_DIRECTORY) {
if (do_list_dirs && do_this_one(ctx, f)) {
do_list_fn(ctx, f);
}
if (do_list_recurse &&
!ISDOT(f->name) &&
!ISDOTDOT(f->name)) {
char *mask2;
char *p;
mask2 = talloc_strdup(NULL, mask);
p = strrchr_m(mask2,'\\');
if (!p) return;
p[1] = 0;
mask2 = talloc_asprintf_append_buffer(mask2, "%s\\*", f->name);
add_to_do_list_queue(mask2);
}
return;
}
if (do_this_one(ctx, f)) {
do_list_fn(ctx, f);
}
}
/****************************************************************************
a wrapper around smbcli_list that adds recursion
****************************************************************************/
static void do_list(struct smbclient_context *ctx, const char *mask,uint16_t attribute,
void (*fn)(struct smbclient_context *, struct clilist_file_info *),bool rec, bool dirs)
{
static int in_do_list = 0;
if (in_do_list && rec)
{
fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
exit(1);
}
in_do_list = 1;
do_list_recurse = rec;
do_list_dirs = dirs;
do_list_fn = fn;
if (rec)
{
init_do_list_queue();
add_to_do_list_queue(mask);
while (! do_list_queue_empty())
{
/*
* Need to copy head so that it doesn't become
* invalid inside the call to smbcli_list. This
* would happen if the list were expanded
* during the call.
* Fix from E. Jay Berkenbilt (ejb@ql.org)
*/
char *head;
head = do_list_queue_head();
smbcli_list(ctx->cli->tree, head, attribute, do_list_helper, ctx);
remove_do_list_queue_head();
if ((! do_list_queue_empty()) && (fn == display_finfo))
{
char* next_file = do_list_queue_head();
char* save_ch = 0;
if ((strlen(next_file) >= 2) &&
(next_file[strlen(next_file) - 1] == '*') &&
(next_file[strlen(next_file) - 2] == '\\'))
{
save_ch = next_file +
strlen(next_file) - 2;
*save_ch = '\0';
}
d_printf("\n%s\n",next_file);
if (save_ch)
{
*save_ch = '\\';
}
}
}
}
else
{
if (smbcli_list(ctx->cli->tree, mask, attribute, do_list_helper, ctx) == -1)
{
d_printf("%s listing %s\n", smbcli_errstr(ctx->cli->tree), mask);
}
}
in_do_list = 0;
reset_do_list_queue();
}
/****************************************************************************
get a directory listing
****************************************************************************/
static int cmd_dir(struct smbclient_context *ctx, const char **args)
{
uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
char *mask;
int rc;
dir_total = 0;
mask = talloc_strdup(ctx, ctx->remote_cur_dir);
if(mask[strlen(mask)-1]!='\\')
mask = talloc_append_string(ctx, mask,"\\");
if (args[1]) {
mask = talloc_strdup(ctx, args[1]);
if (mask[0] != '\\')
mask = talloc_append_string(ctx, mask, "\\");
dos_format(mask);
}
else {
if (ctx->cli->tree->session->transport->negotiate.protocol <=
PROTOCOL_LANMAN1) {
mask = talloc_append_string(ctx, mask, "*.*");
} else {
mask = talloc_append_string(ctx, mask, "*");
}
}
do_list(ctx, mask, attribute, display_finfo, ctx->recurse, true);
rc = do_dskattr(ctx);
DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
return rc;
}
/****************************************************************************
get a directory listing
****************************************************************************/
static int cmd_du(struct smbclient_context *ctx, const char **args)
{
uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
int rc;
char *mask;
dir_total = 0;
if (args[1]) {
if (args[1][0] == '\\')
mask = talloc_strdup(ctx, args[1]);
else
mask = talloc_asprintf(ctx, "%s\\%s", ctx->remote_cur_dir, args[1]);
dos_format(mask);
} else {
mask = talloc_asprintf(ctx, "%s\\*", ctx->remote_cur_dir);
}
do_list(ctx, mask, attribute, do_du, ctx->recurse, true);
talloc_free(mask);
rc = do_dskattr(ctx);
d_printf("Total number of bytes: %.0f\n", dir_total);
return rc;
}
/****************************************************************************
get a file from rname to lname
****************************************************************************/
static int do_get(struct smbclient_context *ctx, char *rname, const char *p_lname, bool reget)
{
int handle = 0, fnum;
bool newhandle = false;
uint8_t *data;
struct timeval tp_start;
int read_size = ctx->io_bufsize;
uint16_t attr;
size_t size;
off_t start = 0;
off_t nread = 0;
int rc = 0;
char *lname;
GetTimeOfDay(&tp_start);
if (ctx->lowercase) {
lname = strlower_talloc(ctx, p_lname);
} else {
lname = talloc_strdup(ctx, p_lname);
}
fnum = smbcli_open(ctx->cli->tree, rname, O_RDONLY, DENY_NONE);
if (fnum == -1) {
d_printf("%s opening remote file %s\n",smbcli_errstr(ctx->cli->tree),rname);
return 1;
}
if(!strcmp(lname,"-")) {
handle = fileno(stdout);
} else {
if (reget) {
handle = open(lname, O_WRONLY|O_CREAT, 0644);
if (handle >= 0) {
start = lseek(handle, 0, SEEK_END);
if (start == -1) {
d_printf("Error seeking local file\n");
close(handle);
return 1;
}
}
} else {
handle = open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
}
newhandle = true;
}
if (handle < 0) {
d_printf("Error opening local file %s\n",lname);
return 1;
}
if (NT_STATUS_IS_ERR(smbcli_qfileinfo(ctx->cli->tree, fnum,
&attr, &size, NULL, NULL, NULL, NULL, NULL)) &&
NT_STATUS_IS_ERR(smbcli_getattrE(ctx->cli->tree, fnum,
&attr, &size, NULL, NULL, NULL))) {
d_printf("getattrib: %s\n",smbcli_errstr(ctx->cli->tree));
if (newhandle) {
close(handle);
}
return 1;
}
DEBUG(2,("getting file %s of size %.0f as %s ",
rname, (double)size, lname));
if(!(data = (uint8_t *)malloc(read_size))) {
d_printf("malloc fail for size %d\n", read_size);
smbcli_close(ctx->cli->tree, fnum);
if (newhandle) {
close(handle);
}
return 1;
}
while (1) {
int n = smbcli_read(ctx->cli->tree, fnum, data, nread + start, read_size);
if (n <= 0) break;
if (writefile(handle,data, n, ctx->translation) != n) {
d_printf("Error writing local file\n");
rc = 1;
break;
}
nread += n;
}
if (nread + start < size) {
DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
rname, (long)nread));
rc = 1;
}
SAFE_FREE(data);
if (NT_STATUS_IS_ERR(smbcli_close(ctx->cli->tree, fnum))) {
d_printf("Error %s closing remote file\n",smbcli_errstr(ctx->cli->tree));
rc = 1;
}
if (newhandle) {
close(handle);
}
if (ctx->archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
smbcli_setatr(ctx->cli->tree, rname, attr & ~(uint16_t)FILE_ATTRIBUTE_ARCHIVE, 0);
}
{
struct timeval tp_end;
int this_time;
GetTimeOfDay(&tp_end);
this_time =
(tp_end.tv_sec - tp_start.tv_sec)*1000 +
(tp_end.tv_usec - tp_start.tv_usec)/1000;
get_total_time_ms += this_time;
get_total_size += nread;
DEBUG(2,("(%3.1f kb/s) (average %3.1f kb/s)\n",
nread / (1.024*this_time + 1.0e-4),
get_total_size / (1.024*get_total_time_ms)));
}
return rc;
}
/****************************************************************************
get a file
****************************************************************************/
static int cmd_get(struct smbclient_context *ctx, const char **args)
{
const char *lname;
char *rname;
if (!args[1]) {
d_printf("get <filename>\n");
return 1;
}
rname = talloc_asprintf(ctx, "%s\\%s", ctx->remote_cur_dir, args[1]);
if (args[2])
lname = args[2];
else
lname = args[1];
dos_clean_name(rname);
return do_get(ctx, rname, lname, false);
}
/****************************************************************************
Put up a yes/no prompt.
****************************************************************************/
static bool yesno(char *p)
{
char ans[4];
printf("%s",p);
if (!fgets(ans,sizeof(ans)-1,stdin))
return(false);
if (*ans == 'y' || *ans == 'Y')
return(true);
return(false);
}
/****************************************************************************
do a mget operation on one file
****************************************************************************/
static void do_mget(struct smbclient_context *ctx, struct clilist_file_info *finfo)
{
char *rname;
char *quest;
char *mget_mask;
char *saved_curdir;
char *l_fname;
int ret;
if (ISDOT(finfo->name) || ISDOTDOT(finfo->name))
return;
if (finfo->attrib & FILE_ATTRIBUTE_DIRECTORY)
quest = talloc_asprintf(ctx, "Get directory %s? ",finfo->name);
else
quest = talloc_asprintf(ctx, "Get file %s? ",finfo->name);
if (ctx->prompt && !yesno(quest)) return;
talloc_free(quest);
if (!(finfo->attrib & FILE_ATTRIBUTE_DIRECTORY)) {
rname = talloc_asprintf(ctx, "%s%s",ctx->remote_cur_dir,
finfo->name);
do_get(ctx, rname, finfo->name, false);
talloc_free(rname);
return;
}
/* handle directories */
saved_curdir = talloc_strdup(ctx, ctx->remote_cur_dir);
ctx->remote_cur_dir = talloc_asprintf_append_buffer(NULL, "%s\\", finfo->name);
if (ctx->lowercase) {
l_fname = strlower_talloc(ctx, finfo->name);
} else {
l_fname = talloc_strdup(ctx, finfo->name);
}
string_replace(l_fname, '\\', '/');
if (!directory_exist(l_fname) &&
mkdir(l_fname, 0777) != 0) {
d_printf("failed to create directory %s\n", l_fname);
return;
}
if (chdir(l_fname) != 0) {
d_printf("failed to chdir to directory %s\n", l_fname);
return;
}
mget_mask = talloc_asprintf(ctx, "%s*", ctx->remote_cur_dir);
do_list(ctx, mget_mask, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,do_mget,false, true);
ret = chdir("..");
if (ret == -1) {
d_printf("failed to chdir to '..': %s\n", strerror(errno));
return;
}
talloc_free(ctx->remote_cur_dir);
ctx->remote_cur_dir = saved_curdir;
}
/****************************************************************************
view the file using the pager
****************************************************************************/
static int cmd_more(struct smbclient_context *ctx, const char **args)
{
char *rname;
char *pager_cmd;
char *lname;
char *pager;
int fd;
int rc = 0;
mode_t mask;
lname = talloc_asprintf(ctx, "%s/smbmore.XXXXXX",tmpdir());
mask = umask(S_IRWXO | S_IRWXG);
fd = mkstemp(lname);
umask(mask);
if (fd == -1) {
d_printf("failed to create temporary file for more\n");
return 1;
}
close(fd);
if (!args[1]) {
d_printf("more <filename>\n");
unlink(lname);
return 1;
}
rname = talloc_asprintf(ctx, "%s%s", ctx->remote_cur_dir, args[1]);
dos_clean_name(rname);
rc = do_get(ctx, rname, lname, false);
pager=getenv("PAGER");
pager_cmd = talloc_asprintf(ctx, "%s %s",(pager? pager:DEFAULT_PAGER), lname);
rc = system(pager_cmd);
if (rc == -1) {
d_printf("failed to call pager command\n");
return 1;
}
unlink(lname);
return rc;
}
/****************************************************************************
do a mget command
****************************************************************************/
static int cmd_mget(struct smbclient_context *ctx, const char **args)
{
uint16_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
char *mget_mask = NULL;
int i;
if (ctx->recurse)
attribute |= FILE_ATTRIBUTE_DIRECTORY;
for (i = 1; args[i]; i++) {
mget_mask = talloc_strdup(ctx, ctx->remote_cur_dir);
if(mget_mask[strlen(mget_mask)-1]!='\\')
mget_mask = talloc_append_string(ctx, mget_mask, "\\");
mget_mask = talloc_strdup(ctx, args[i]);