talloc: release 2.4.2
[Samba.git] / source3 / utils / smbget.c
blob67ea259afb8463250aac34ed615d832c580f5614
1 /*
2 smbget: a wget-like utility with support for recursive downloading of
3 smb:// urls
4 Copyright (C) 2003-2004 Jelmer Vernooij <jelmer@samba.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "includes.h"
20 #include "system/filesys.h"
21 #include "lib/cmdline/cmdline.h"
22 #include "lib/param/param.h"
23 #include "libsmbclient.h"
24 #include "cmdline_contexts.h"
25 #include "auth/credentials/credentials.h"
26 #include "auth/gensec/gensec.h"
28 static int columns = 0;
30 static time_t total_start_time = 0;
31 static off_t total_bytes = 0;
33 #define SMB_MAXPATHLEN MAXPATHLEN
36 * Number of bytes to read when checking whether local and remote file
37 * are really the same file
39 #define RESUME_CHECK_SIZE 512
40 #define RESUME_DOWNLOAD_OFFSET 1024
41 #define RESUME_CHECK_OFFSET (RESUME_DOWNLOAD_OFFSET+RESUME_CHECK_SIZE)
42 /* Number of bytes to read at once */
43 #define SMB_DEFAULT_BLOCKSIZE 64000
45 struct opt {
46 char *outputfile;
47 size_t blocksize;
49 int quiet;
50 int dots;
51 int verbose;
52 int send_stdout;
53 int update;
54 unsigned limit_rate;
56 static struct opt opt = { .blocksize = SMB_DEFAULT_BLOCKSIZE };
58 static bool smb_download_file(const char *base, const char *name,
59 bool recursive, bool resume, bool toplevel,
60 char *outfile);
62 static int get_num_cols(void)
64 #ifdef TIOCGWINSZ
65 struct winsize ws;
66 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
67 return 0;
69 return ws.ws_col;
70 #else
71 #warning No support for TIOCGWINSZ
72 char *cols = getenv("COLUMNS");
73 if (!cols) {
74 return 0;
76 return atoi(cols);
77 #endif
80 static void change_columns(int sig)
82 columns = get_num_cols();
85 static void human_readable(off_t s, char *buffer, int l)
87 if (s > 1024 * 1024 * 1024) {
88 snprintf(buffer, l, "%.2fGB", 1.0 * s / (1024 * 1024 * 1024));
89 } else if (s > 1024 * 1024) {
90 snprintf(buffer, l, "%.2fMB", 1.0 * s / (1024 * 1024));
91 } else if (s > 1024) {
92 snprintf(buffer, l, "%.2fkB", 1.0 * s / 1024);
93 } else {
94 snprintf(buffer, l, "%jdb", (intmax_t)s);
99 * Authentication callback for libsmbclient.
101 * The command line parser will take care asking for a password interactively!
103 static void get_auth_data_with_context_fn(SMBCCTX *ctx,
104 const char *srv,
105 const char *shr,
106 char *dom,
107 int dom_len,
108 char *usr,
109 int usr_len,
110 char *pwd,
111 int pwd_len)
113 struct cli_credentials *creds = samba_cmdline_get_creds();
114 const char *username = NULL;
115 const char *password = NULL;
116 const char *domain = NULL;
117 enum credentials_obtained obtained = CRED_UNINITIALISED;
119 domain = cli_credentials_get_domain_and_obtained(creds, &obtained);
120 if (domain != NULL) {
121 bool overwrite = false;
122 if (dom[0] == '\0') {
123 overwrite = true;
125 if (obtained >= CRED_CALLBACK_RESULT) {
126 overwrite = true;
128 if (overwrite) {
129 strncpy(dom, domain, dom_len - 1);
132 cli_credentials_set_domain(creds, dom, obtained);
134 username = cli_credentials_get_username_and_obtained(creds, &obtained);
135 if (username != NULL) {
136 bool overwrite = false;
137 if (usr[0] == '\0') {
138 overwrite = true;
140 if (obtained >= CRED_CALLBACK_RESULT) {
141 overwrite = true;
143 if (overwrite) {
144 strncpy(usr, username, usr_len - 1);
147 cli_credentials_set_username(creds, usr, obtained);
149 password = cli_credentials_get_password_and_obtained(creds, &obtained);
150 if (password != NULL) {
151 bool overwrite = false;
152 if (pwd[0] == '\0') {
153 overwrite = true;
155 if (obtained >= CRED_CALLBACK_RESULT) {
156 overwrite = true;
158 if (overwrite) {
159 strncpy(pwd, password, pwd_len - 1);
162 cli_credentials_set_password(creds, pwd, obtained);
164 smbc_set_credentials_with_fallback(ctx, dom, usr, pwd);
166 if (!opt.quiet) {
167 if (usr[0] == '\0') {
168 printf("Using guest user\n");
169 } else if (dom[0] == '\0') {
170 printf("Using user: %s\n", usr);
171 } else {
172 printf("Using domain: %s, user: %s\n", dom, usr);
177 static bool smb_download_dir(const char *base, const char *name, int resume)
179 char path[SMB_MAXPATHLEN];
180 int dirhandle;
181 struct smbc_dirent *dirent;
182 const char *relname = name;
183 char *tmpname;
184 bool ok = false;
186 snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
187 (base[0] && name[0] && name[0] != '/' &&
188 base[strlen(base)-1] != '/') ? "/" : "",
189 name);
191 /* List files in directory and call smb_download_file on them */
192 dirhandle = smbc_opendir(path);
193 if (dirhandle < 1) {
194 if (errno == ENOTDIR) {
195 return smb_download_file(base, name, true, resume,
196 false, NULL);
198 fprintf(stderr, "Can't open directory %s: %s\n", path,
199 strerror(errno));
200 return false;
203 while (*relname == '/') {
204 relname++;
207 if (strlen(relname) > 0) {
208 int rc = mkdir(relname, 0755);
209 if (rc == -1 && errno != EEXIST) {
210 fprintf(stderr, "Can't create directory %s: %s\n",
211 relname, strerror(errno));
212 return false;
216 tmpname = SMB_STRDUP(name);
218 while ((dirent = smbc_readdir(dirhandle))) {
219 char *newname;
220 if (!strcmp(dirent->name, ".") || !strcmp(dirent->name, "..")) {
221 ok = true;
222 continue;
224 if (asprintf(&newname, "%s/%s", tmpname, dirent->name) == -1) {
225 free(tmpname);
226 return false;
228 switch (dirent->smbc_type) {
229 case SMBC_DIR:
230 ok = smb_download_dir(base, newname, resume);
231 break;
233 case SMBC_WORKGROUP:
234 ok = smb_download_dir("smb://", dirent->name, resume);
235 break;
237 case SMBC_SERVER:
238 ok = smb_download_dir("smb://", dirent->name, resume);
239 break;
241 case SMBC_FILE:
242 ok = smb_download_file(base, newname, true, resume,
243 false, NULL);
244 break;
246 case SMBC_FILE_SHARE:
247 ok = smb_download_dir(base, newname, resume);
248 break;
250 case SMBC_PRINTER_SHARE:
251 if (!opt.quiet) {
252 printf("Ignoring printer share %s\n",
253 dirent->name);
255 break;
257 case SMBC_COMMS_SHARE:
258 if (!opt.quiet) {
259 printf("Ignoring comms share %s\n",
260 dirent->name);
262 break;
264 case SMBC_IPC_SHARE:
265 if (!opt.quiet) {
266 printf("Ignoring ipc$ share %s\n",
267 dirent->name);
269 break;
271 default:
272 fprintf(stderr, "Ignoring file '%s' of type '%d'\n",
273 newname, dirent->smbc_type);
274 break;
277 if (!ok) {
278 fprintf(stderr, "Failed to download %s: %s\n",
279 newname, strerror(errno));
280 free(newname);
281 free(tmpname);
282 return false;
284 free(newname);
286 free(tmpname);
288 smbc_closedir(dirhandle);
289 return ok;
292 static char *print_time(long t)
294 static char buffer[100];
295 int secs, mins, hours;
296 if (t < -1) {
297 strncpy(buffer, "Unknown", sizeof(buffer));
298 return buffer;
301 secs = (int)t % 60;
302 mins = (int)t / 60 % 60;
303 hours = (int)t / (60 * 60);
304 snprintf(buffer, sizeof(buffer) - 1, "%02d:%02d:%02d", hours, mins,
305 secs);
306 return buffer;
309 static void print_progress(const char *name, time_t start, time_t now,
310 off_t start_pos, off_t pos, off_t total)
312 double avg = 0.0;
313 long eta = -1;
314 double prcnt = 0.0;
315 char hpos[22], htotal[22], havg[22];
316 char *status, *filename;
317 int len;
318 if (now - start) {
319 avg = 1.0 * (pos - start_pos) / (now - start);
321 eta = (total - pos) / avg;
322 if (total) {
323 prcnt = 100.0 * pos / total;
326 human_readable(pos, hpos, sizeof(hpos));
327 human_readable(total, htotal, sizeof(htotal));
328 human_readable(avg, havg, sizeof(havg));
330 len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos,
331 htotal, prcnt, havg, print_time(eta));
332 if (len == -1) {
333 return;
336 if (columns) {
337 int required = strlen(name),
338 available = columns - len - strlen("[] ");
339 if (required > available) {
340 if (asprintf(&filename, "...%s",
341 name + required - available + 3) == -1) {
342 return;
344 } else {
345 filename = SMB_STRNDUP(name, available);
347 } else {
348 filename = SMB_STRDUP(name);
351 fprintf(stderr, "\r[%s] %s", filename, status);
353 free(filename);
354 free(status);
357 /* Return false on error, true on success. */
359 static bool smb_download_file(const char *base, const char *name,
360 bool recursive, bool resume, bool toplevel,
361 char *outfile)
363 int remotehandle, localhandle;
364 time_t start_time = time_mono(NULL);
365 const char *newpath;
366 char path[SMB_MAXPATHLEN];
367 char checkbuf[2][RESUME_CHECK_SIZE];
368 char *readbuf = NULL;
369 off_t offset_download = 0, offset_check = 0, curpos = 0,
370 start_offset = 0;
371 struct stat localstat, remotestat;
372 clock_t start_of_bucket_ticks = 0;
373 size_t bytes_in_bucket = 0;
374 size_t bucket_size = 0;
375 clock_t ticks_to_fill_bucket = 0;
377 snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
378 (*base && *name && name[0] != '/' &&
379 base[strlen(base)-1] != '/') ? "/" : "",
380 name);
382 remotehandle = smbc_open(path, O_RDONLY, 0755);
384 if (remotehandle < 0) {
385 switch (errno) {
386 case EISDIR:
387 if (!recursive) {
388 fprintf(stderr,
389 "%s is a directory. Specify -R "
390 "to download recursively\n",
391 path);
392 return false;
394 return smb_download_dir(base, name, resume);
396 case ENOENT:
397 fprintf(stderr,
398 "%s can't be found on the remote server\n",
399 path);
400 return false;
402 case ENOMEM:
403 fprintf(stderr, "Not enough memory\n");
404 return false;
406 case ENODEV:
407 fprintf(stderr,
408 "The share name used in %s does not exist\n",
409 path);
410 return false;
412 case EACCES:
413 fprintf(stderr, "You don't have enough permissions "
414 "to access %s\n",
415 path);
416 return false;
418 default:
419 perror("smbc_open");
420 return false;
424 if (smbc_fstat(remotehandle, &remotestat) < 0) {
425 fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
426 return false;
429 if (outfile) {
430 newpath = outfile;
431 } else if (!name[0]) {
432 newpath = strrchr(base, '/');
433 if (newpath) {
434 newpath++;
435 } else {
436 newpath = base;
438 } else {
439 newpath = name;
442 if (!toplevel && (newpath[0] == '/')) {
443 newpath++;
446 /* Open local file according to the mode */
447 if (opt.update) {
448 /* if it is up-to-date, skip */
449 if (stat(newpath, &localstat) == 0 &&
450 localstat.st_mtime >= remotestat.st_mtime) {
451 if (opt.verbose) {
452 printf("%s is up-to-date, skipping\n", newpath);
454 smbc_close(remotehandle);
455 return true;
457 /* else open it for writing and truncate if it exists */
458 localhandle = open(
459 newpath, O_CREAT | O_NONBLOCK | O_RDWR | O_TRUNC, 0775);
460 if (localhandle < 0) {
461 fprintf(stderr, "Can't open %s : %s\n", newpath,
462 strerror(errno));
463 smbc_close(remotehandle);
464 return false;
466 /* no offset */
467 } else if (!opt.send_stdout) {
468 localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR |
469 (!resume ? O_EXCL : 0),
470 0755);
471 if (localhandle < 0) {
472 fprintf(stderr, "Can't open %s: %s\n", newpath,
473 strerror(errno));
474 smbc_close(remotehandle);
475 return false;
478 if (fstat(localhandle, &localstat) != 0) {
479 fprintf(stderr, "Can't fstat %s: %s\n", newpath,
480 strerror(errno));
481 smbc_close(remotehandle);
482 close(localhandle);
483 return false;
486 start_offset = localstat.st_size;
488 if (localstat.st_size &&
489 localstat.st_size == remotestat.st_size) {
490 if (opt.verbose) {
491 fprintf(stderr, "%s is already downloaded "
492 "completely.\n",
493 path);
494 } else if (!opt.quiet) {
495 fprintf(stderr, "%s\n", path);
497 smbc_close(remotehandle);
498 close(localhandle);
499 return true;
502 if (localstat.st_size > RESUME_CHECK_OFFSET &&
503 remotestat.st_size > RESUME_CHECK_OFFSET) {
504 offset_download =
505 localstat.st_size - RESUME_DOWNLOAD_OFFSET;
506 offset_check = localstat.st_size - RESUME_CHECK_OFFSET;
507 if (opt.verbose) {
508 printf("Trying to start resume of %s at %jd\n"
509 "At the moment %jd of %jd bytes have "
510 "been retrieved\n",
511 newpath, (intmax_t)offset_check,
512 (intmax_t)localstat.st_size,
513 (intmax_t)remotestat.st_size);
517 if (offset_check) {
518 off_t off1, off2;
519 /* First, check all bytes from offset_check to
520 * offset_download */
521 off1 = lseek(localhandle, offset_check, SEEK_SET);
522 if (off1 < 0) {
523 fprintf(stderr,
524 "Can't seek to %jd in local file %s\n",
525 (intmax_t)offset_check, newpath);
526 smbc_close(remotehandle);
527 close(localhandle);
528 return false;
531 off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET);
532 if (off2 < 0) {
533 fprintf(stderr,
534 "Can't seek to %jd in remote file %s\n",
535 (intmax_t)offset_check, newpath);
536 smbc_close(remotehandle);
537 close(localhandle);
538 return false;
541 if (off1 != off2) {
542 fprintf(stderr, "Offset in local and remote "
543 "files are different "
544 "(local: %jd, remote: %jd)\n",
545 (intmax_t)off1, (intmax_t)off2);
546 smbc_close(remotehandle);
547 close(localhandle);
548 return false;
551 if (smbc_read(remotehandle, checkbuf[0],
552 RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
553 fprintf(stderr, "Can't read %d bytes from "
554 "remote file %s\n",
555 RESUME_CHECK_SIZE, path);
556 smbc_close(remotehandle);
557 close(localhandle);
558 return false;
561 if (read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) !=
562 RESUME_CHECK_SIZE) {
563 fprintf(stderr, "Can't read %d bytes from "
564 "local file %s\n",
565 RESUME_CHECK_SIZE, name);
566 smbc_close(remotehandle);
567 close(localhandle);
568 return false;
571 if (memcmp(checkbuf[0], checkbuf[1],
572 RESUME_CHECK_SIZE) == 0) {
573 if (opt.verbose) {
574 printf("Current local and remote file "
575 "appear to be the same. "
576 "Starting download from "
577 "offset %jd\n",
578 (intmax_t)offset_download);
580 } else {
581 fprintf(stderr, "Local and remote file appear "
582 "to be different, not "
583 "doing resume for %s\n",
584 path);
585 smbc_close(remotehandle);
586 close(localhandle);
587 return false;
590 } else {
591 localhandle = STDOUT_FILENO;
592 start_offset = 0;
593 offset_download = 0;
594 offset_check = 0;
597 /* We implement rate limiting by filling up a bucket with bytes and
598 * checking, once the bucket is filled, if it was filled too fast.
599 * If so, we sleep for some time to get an average transfer rate that
600 * equals to the one set by the user.
602 * The bucket size directly affects the traffic characteristics.
603 * The smaller the bucket the more frequent the pause/resume cycle.
604 * A large bucket can result in burst of high speed traffic and large
605 * pauses. A cycle of 100ms looks like a good value. This value (in
606 * ticks) is held in `ticks_to_fill_bucket`. The `bucket_size` is
607 * calculated as:
608 * `limit_rate * 1024 * / (CLOCKS_PER_SEC / ticks_to_fill_bucket)`
610 * After selecting the bucket size we also need to check the blocksize
611 * of the transfer, since this is the minimum unit of traffic that we
612 * can observe. Achieving a ~10% precision requires a blocksize with a
613 * maximum size of `bucket_size / 10`.
615 if (opt.limit_rate > 0) {
616 unsigned max_block_size;
617 /* This is the time that the bucket should take to fill. */
618 ticks_to_fill_bucket = 100 /*ms*/ * CLOCKS_PER_SEC / 1000;
619 /* This is the size of the bucket in bytes.
620 * If we fill the bucket too quickly we should pause */
621 bucket_size = opt.limit_rate * 1024 / (CLOCKS_PER_SEC / ticks_to_fill_bucket);
622 max_block_size = bucket_size / 10;
623 max_block_size = max_block_size > 0 ? max_block_size : 1;
624 if (opt.blocksize > max_block_size) {
625 if (opt.blocksize != SMB_DEFAULT_BLOCKSIZE) {
626 fprintf(stderr,
627 "Warning: Overriding block size to %d "
628 "due to limit-rate", max_block_size);
630 opt.blocksize = max_block_size;
632 start_of_bucket_ticks = clock();
635 readbuf = (char *)SMB_MALLOC(opt.blocksize);
636 if (!readbuf) {
637 fprintf(stderr, "Failed to allocate %zu bytes for read "
638 "buffer (%s)", opt.blocksize, strerror(errno));
639 if (localhandle != STDOUT_FILENO) {
640 close(localhandle);
642 return false;
645 /* Now, download all bytes from offset_download to the end */
646 for (curpos = offset_download; curpos < remotestat.st_size;
647 curpos += opt.blocksize) {
648 ssize_t bytesread;
649 ssize_t byteswritten;
651 /* Rate limiting. This pauses the transfer to limit traffic. */
652 if (opt.limit_rate > 0) {
653 if (bytes_in_bucket > bucket_size) {
654 clock_t now_ticks = clock();
655 clock_t diff_ticks = now_ticks
656 - start_of_bucket_ticks;
657 /* Check if the bucket filled up too fast. */
658 if (diff_ticks < ticks_to_fill_bucket) {
659 /* Pause until `ticks_to_fill_bucket` */
660 double sleep_us
661 = (ticks_to_fill_bucket - diff_ticks)
662 * 1000000.0 / CLOCKS_PER_SEC;
663 usleep(sleep_us);
665 /* Reset the byte counter and the ticks. */
666 bytes_in_bucket = 0;
667 start_of_bucket_ticks = clock();
671 bytesread = smbc_read(remotehandle, readbuf, opt.blocksize);
672 if (opt.limit_rate > 0) {
673 bytes_in_bucket += bytesread;
675 if(bytesread < 0) {
676 fprintf(stderr,
677 "Can't read %zu bytes at offset %jd, file %s\n",
678 opt.blocksize, (intmax_t)curpos, path);
679 smbc_close(remotehandle);
680 if (localhandle != STDOUT_FILENO) {
681 close(localhandle);
683 free(readbuf);
684 return false;
687 total_bytes += bytesread;
689 byteswritten = write(localhandle, readbuf, bytesread);
690 if (byteswritten != bytesread) {
691 fprintf(stderr,
692 "Can't write %zd bytes to local file %s at "
693 "offset %jd\n", bytesread, path,
694 (intmax_t)curpos);
695 free(readbuf);
696 smbc_close(remotehandle);
697 if (localhandle != STDOUT_FILENO) {
698 close(localhandle);
700 return false;
703 if (opt.dots) {
704 fputc('.', stderr);
705 } else if (!opt.quiet) {
706 print_progress(newpath, start_time, time_mono(NULL),
707 start_offset, curpos,
708 remotestat.st_size);
712 free(readbuf);
714 if (opt.dots) {
715 fputc('\n', stderr);
716 printf("%s downloaded\n", path);
717 } else if (!opt.quiet) {
718 int i;
719 fprintf(stderr, "\r%s", path);
720 if (columns) {
721 for (i = strlen(path); i < columns; i++) {
722 fputc(' ', stderr);
725 fputc('\n', stderr);
728 smbc_close(remotehandle);
729 if (localhandle != STDOUT_FILENO) {
730 close(localhandle);
732 return true;
735 static void clean_exit(void)
737 char bs[100];
738 human_readable(total_bytes, bs, sizeof(bs));
739 if (!opt.quiet) {
740 fprintf(stderr, "Downloaded %s in %lu seconds\n", bs,
741 (unsigned long)(time_mono(NULL) - total_start_time));
743 exit(0);
746 static void signal_quit(int v)
748 clean_exit();
751 int main(int argc, char **argv)
753 int c = 0;
754 const char *file = NULL;
755 int smb_encrypt = false;
756 int resume = 0, recursive = 0;
757 TALLOC_CTX *frame = talloc_stackframe();
758 bool ok = false;
759 const char **argv_const = discard_const_p(const char *, argv);
760 struct poptOption long_options[] = {
761 POPT_AUTOHELP
764 .longName = "guest",
765 .shortName = 'a',
766 .argInfo = POPT_ARG_NONE,
767 .arg = NULL,
768 .val = 'a',
769 .descrip = "Work as user guest"
772 .longName = "encrypt",
773 .shortName = 'e',
774 .argInfo = POPT_ARG_NONE,
775 .arg = &smb_encrypt,
776 .val = 1,
777 .descrip = "Encrypt SMB transport"
780 .longName = "resume",
781 .shortName = 'r',
782 .argInfo = POPT_ARG_NONE,
783 .arg = &resume,
784 .val = 1,
785 .descrip = "Automatically resume aborted files"
788 .longName = "update",
789 .shortName = 'u',
790 .argInfo = POPT_ARG_NONE,
791 .arg = &opt.update,
792 .val = 1,
793 .descrip = "Download only when remote file is "
794 "newer than local file or local file "
795 "is missing"
798 .longName = "recursive",
799 .shortName = 0,
800 .argInfo = POPT_ARG_NONE,
801 .arg = &recursive,
802 .val = true,
803 .descrip = "Recursively download files"
806 .longName = "blocksize",
807 .shortName = 'b',
808 .argInfo = POPT_ARG_INT,
809 .arg = &opt.blocksize,
810 .val = 'b',
811 .descrip = "Change number of bytes in a block"
815 .longName = "outputfile",
816 .shortName = 'o',
817 .argInfo = POPT_ARG_STRING,
818 .arg = &opt.outputfile,
819 .val = 'o',
820 .descrip = "Write downloaded data to specified file"
823 .longName = "stdout",
824 .shortName = 0,
825 .argInfo = POPT_ARG_NONE,
826 .arg = &opt.send_stdout,
827 .val = true,
828 .descrip = "Write data to stdout"
831 .longName = "dots",
832 .shortName = 'D',
833 .argInfo = POPT_ARG_NONE,
834 .arg = &opt.dots,
835 .val = 1,
836 .descrip = "Show dots as progress indication"
839 .longName = "quiet",
840 .shortName = 'q',
841 .argInfo = POPT_ARG_NONE,
842 .arg = &opt.quiet,
843 .val = 1,
844 .descrip = "Be quiet"
847 .longName = "verbose",
848 .shortName = 'v',
849 .argInfo = POPT_ARG_NONE,
850 .arg = &opt.verbose,
851 .val = 1,
852 .descrip = "Be verbose"
855 .longName = "limit-rate",
856 .shortName = 0,
857 .argInfo = POPT_ARG_INT,
858 .arg = &opt.limit_rate,
859 .val = 'l',
860 .descrip = "Limit download speed to this many KB/s"
863 POPT_COMMON_SAMBA
864 POPT_COMMON_CONNECTION
865 POPT_COMMON_CREDENTIALS
866 POPT_LEGACY_S3
867 POPT_COMMON_VERSION
868 POPT_TABLEEND
870 poptContext pc = NULL;
871 struct cli_credentials *creds = NULL;
872 enum smb_encryption_setting encryption_state = SMB_ENCRYPTION_DEFAULT;
873 enum credentials_use_kerberos use_kerberos = CRED_USE_KERBEROS_DESIRED;
874 smbc_smb_encrypt_level encrypt_level = SMBC_ENCRYPTLEVEL_DEFAULT;
875 #if 0
876 enum smb_signing_setting signing_state = SMB_SIGNING_DEFAULT;
877 const char *use_signing = "auto";
878 #endif
879 bool is_nt_hash = false;
880 uint32_t gensec_features;
881 bool use_wbccache = false;
882 SMBCCTX *smb_ctx = NULL;
883 int dbg_lvl = -1;
884 int rc;
886 smb_init_locale();
888 ok = samba_cmdline_init(frame,
889 SAMBA_CMDLINE_CONFIG_CLIENT,
890 false);
891 if (!ok) {
892 goto done;
895 #ifdef SIGWINCH
896 signal(SIGWINCH, change_columns);
897 #endif
898 signal(SIGINT, signal_quit);
899 signal(SIGTERM, signal_quit);
901 pc = samba_popt_get_context(getprogname(),
902 argc,
903 argv_const,
904 long_options,
906 if (pc == NULL) {
907 ok = false;
908 goto done;
911 creds = samba_cmdline_get_creds();
913 while ((c = poptGetNextOpt(pc)) != -1) {
914 switch (c) {
915 case 'a':
916 cli_credentials_set_anonymous(creds);
917 break;
918 case POPT_ERROR_BADOPT:
919 fprintf(stderr, "\nInvalid option %s: %s\n\n",
920 poptBadOption(pc, 0), poptStrerror(c));
921 poptPrintUsage(pc, stderr, 0);
922 ok = false;
923 goto done;
926 if (c < -1) {
927 fprintf(stderr, "%s: %s\n",
928 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
929 poptStrerror(c));
930 ok = false;
931 goto done;
935 if ((opt.send_stdout || resume || opt.outputfile) && opt.update) {
936 fprintf(stderr, "The -o, -R or -O and -U options can not be "
937 "used together.\n");
938 ok = true;
939 goto done;
941 if ((opt.send_stdout || opt.outputfile) && recursive) {
942 fprintf(stderr, "The -o or -O and -R options can not be "
943 "used together.\n");
944 ok = true;
945 goto done;
948 if (opt.outputfile && opt.send_stdout) {
949 fprintf(stderr, "The -o and -O options can not be "
950 "used together.\n");
951 ok = true;
952 goto done;
955 samba_cmdline_burn(argc, argv);
957 /* smbc_new_context() will set the log level to 0 */
958 dbg_lvl = debuglevel_get();
960 smb_ctx = smbc_new_context();
961 if (smb_ctx == NULL) {
962 fprintf(stderr, "Unable to initialize libsmbclient\n");
963 ok = false;
964 goto done;
966 smbc_setDebug(smb_ctx, dbg_lvl);
968 rc = smbc_setConfiguration(smb_ctx, lp_default_path());
969 if (rc < 0) {
970 ok = false;
971 goto done;
974 smbc_setFunctionAuthDataWithContext(smb_ctx,
975 get_auth_data_with_context_fn);
977 ok = smbc_init_context(smb_ctx);
978 if (!ok) {
979 goto done;
981 smbc_set_context(smb_ctx);
983 encryption_state = cli_credentials_get_smb_encryption(creds);
984 switch (encryption_state) {
985 case SMB_ENCRYPTION_REQUIRED:
986 encrypt_level = SMBC_ENCRYPTLEVEL_REQUIRE;
987 break;
988 case SMB_ENCRYPTION_DESIRED:
989 case SMB_ENCRYPTION_IF_REQUIRED:
990 encrypt_level = SMBC_ENCRYPTLEVEL_REQUEST;
991 break;
992 case SMB_ENCRYPTION_OFF:
993 encrypt_level = SMBC_ENCRYPTLEVEL_NONE;
994 break;
995 case SMB_ENCRYPTION_DEFAULT:
996 encrypt_level = SMBC_ENCRYPTLEVEL_DEFAULT;
997 break;
999 if (smb_encrypt) {
1000 encrypt_level = SMBC_ENCRYPTLEVEL_REQUIRE;
1002 smbc_setOptionSmbEncryptionLevel(smb_ctx, encrypt_level);
1004 #if 0
1005 signing_state = cli_credentials_get_smb_signing(creds);
1006 if (encryption_state >= SMB_ENCRYPTION_DESIRED) {
1007 signing_state = SMB_SIGNING_REQUIRED;
1009 switch (signing_state) {
1010 case SMB_SIGNING_REQUIRED:
1011 use_signing = "required";
1012 break;
1013 case SMB_SIGNING_DEFAULT:
1014 case SMB_SIGNING_DESIRED:
1015 case SMB_SIGNING_IF_REQUIRED:
1016 use_signing = "yes";
1017 break;
1018 case SMB_SIGNING_OFF:
1019 use_signing = "off";
1020 break;
1021 default:
1022 use_signing = "auto";
1023 break;
1025 /* FIXME: There is no libsmbclient function to set signing state */
1026 #endif
1028 use_kerberos = cli_credentials_get_kerberos_state(creds);
1029 switch (use_kerberos) {
1030 case CRED_USE_KERBEROS_REQUIRED:
1031 smbc_setOptionUseKerberos(smb_ctx, true);
1032 smbc_setOptionFallbackAfterKerberos(smb_ctx, false);
1033 break;
1034 case CRED_USE_KERBEROS_DESIRED:
1035 smbc_setOptionUseKerberos(smb_ctx, true);
1036 smbc_setOptionFallbackAfterKerberos(smb_ctx, true);
1037 break;
1038 case CRED_USE_KERBEROS_DISABLED:
1039 smbc_setOptionUseKerberos(smb_ctx, false);
1040 break;
1043 /* Check if the password supplied is an NT hash */
1044 is_nt_hash = cli_credentials_is_password_nt_hash(creds);
1045 smbc_setOptionUseNTHash(smb_ctx, is_nt_hash);
1047 /* Check if we should use the winbind ccache */
1048 gensec_features = cli_credentials_get_gensec_features(creds);
1049 use_wbccache = (gensec_features & GENSEC_FEATURE_NTLM_CCACHE);
1050 smbc_setOptionUseCCache(smb_ctx, use_wbccache);
1052 columns = get_num_cols();
1054 total_start_time = time_mono(NULL);
1056 while ((file = poptGetArg(pc))) {
1057 if (!recursive) {
1058 ok = smb_download_file(file, "", recursive, resume,
1059 true, opt.outputfile);
1060 } else {
1061 ok = smb_download_dir(file, "", resume);
1065 done:
1066 gfree_all();
1067 poptFreeContext(pc);
1068 TALLOC_FREE(frame);
1069 if (ok) {
1070 clean_exit();
1072 return ok ? 0 : 1;