2 smbget: a wget-like utility with support for recursive downloading of
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/>. */
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
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,
62 static int get_num_cols(void)
66 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
71 #warning No support for TIOCGWINSZ
72 char *cols = getenv("COLUMNS");
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);
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,
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') {
125 if (obtained >= CRED_CALLBACK_RESULT) {
129 strncpy(dom, domain, dom_len - 1);
133 username = cli_credentials_get_username_and_obtained(creds, &obtained);
134 if (username != NULL) {
135 bool overwrite = false;
136 if (usr[0] == '\0') {
139 if (obtained >= CRED_CALLBACK_RESULT) {
143 strncpy(usr, username, usr_len - 1);
147 password = cli_credentials_get_password_and_obtained(creds, &obtained);
148 if (password != NULL) {
149 bool overwrite = false;
150 if (pwd[0] == '\0') {
153 if (obtained >= CRED_CALLBACK_RESULT) {
157 strncpy(pwd, password, pwd_len - 1);
161 smbc_set_credentials_with_fallback(ctx, domain, username, password);
163 if (!opt.quiet && username != NULL) {
164 if (username[0] == '\0') {
165 printf("Using guest user\n");
167 printf("Using domain: %s, user: %s\n",
174 static bool smb_download_dir(const char *base, const char *name, int resume)
176 char path[SMB_MAXPATHLEN];
178 struct smbc_dirent *dirent;
179 const char *relname = name;
183 snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
184 (base[0] && name[0] && name[0] != '/' &&
185 base[strlen(base)-1] != '/') ? "/" : "",
188 /* List files in directory and call smb_download_file on them */
189 dirhandle = smbc_opendir(path);
191 if (errno == ENOTDIR) {
192 return smb_download_file(base, name, true, resume,
195 fprintf(stderr, "Can't open directory %s: %s\n", path,
200 while (*relname == '/') {
204 if (strlen(relname) > 0) {
205 int rc = mkdir(relname, 0755);
206 if (rc == -1 && errno != EEXIST) {
207 fprintf(stderr, "Can't create directory %s: %s\n",
208 relname, strerror(errno));
213 tmpname = SMB_STRDUP(name);
215 while ((dirent = smbc_readdir(dirhandle))) {
217 if (!strcmp(dirent->name, ".") || !strcmp(dirent->name, "..")) {
221 if (asprintf(&newname, "%s/%s", tmpname, dirent->name) == -1) {
225 switch (dirent->smbc_type) {
227 ok = smb_download_dir(base, newname, resume);
231 ok = smb_download_dir("smb://", dirent->name, resume);
235 ok = smb_download_dir("smb://", dirent->name, resume);
239 ok = smb_download_file(base, newname, true, resume,
243 case SMBC_FILE_SHARE:
244 ok = smb_download_dir(base, newname, resume);
247 case SMBC_PRINTER_SHARE:
249 printf("Ignoring printer share %s\n",
254 case SMBC_COMMS_SHARE:
256 printf("Ignoring comms share %s\n",
263 printf("Ignoring ipc$ share %s\n",
269 fprintf(stderr, "Ignoring file '%s' of type '%d'\n",
270 newname, dirent->smbc_type);
275 fprintf(stderr, "Failed to download %s: %s\n",
276 newname, strerror(errno));
285 smbc_closedir(dirhandle);
289 static char *print_time(long t)
291 static char buffer[100];
292 int secs, mins, hours;
294 strncpy(buffer, "Unknown", sizeof(buffer));
299 mins = (int)t / 60 % 60;
300 hours = (int)t / (60 * 60);
301 snprintf(buffer, sizeof(buffer) - 1, "%02d:%02d:%02d", hours, mins,
306 static void print_progress(const char *name, time_t start, time_t now,
307 off_t start_pos, off_t pos, off_t total)
312 char hpos[22], htotal[22], havg[22];
313 char *status, *filename;
316 avg = 1.0 * (pos - start_pos) / (now - start);
318 eta = (total - pos) / avg;
320 prcnt = 100.0 * pos / total;
323 human_readable(pos, hpos, sizeof(hpos));
324 human_readable(total, htotal, sizeof(htotal));
325 human_readable(avg, havg, sizeof(havg));
327 len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos,
328 htotal, prcnt, havg, print_time(eta));
334 int required = strlen(name),
335 available = columns - len - strlen("[] ");
336 if (required > available) {
337 if (asprintf(&filename, "...%s",
338 name + required - available + 3) == -1) {
342 filename = SMB_STRNDUP(name, available);
345 filename = SMB_STRDUP(name);
348 fprintf(stderr, "\r[%s] %s", filename, status);
354 /* Return false on error, true on success. */
356 static bool smb_download_file(const char *base, const char *name,
357 bool recursive, bool resume, bool toplevel,
360 int remotehandle, localhandle;
361 time_t start_time = time_mono(NULL);
363 char path[SMB_MAXPATHLEN];
364 char checkbuf[2][RESUME_CHECK_SIZE];
365 char *readbuf = NULL;
366 off_t offset_download = 0, offset_check = 0, curpos = 0,
368 struct stat localstat, remotestat;
369 clock_t start_of_bucket_ticks = 0;
370 size_t bytes_in_bucket = 0;
371 size_t bucket_size = 0;
372 clock_t ticks_to_fill_bucket = 0;
374 snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
375 (*base && *name && name[0] != '/' &&
376 base[strlen(base)-1] != '/') ? "/" : "",
379 remotehandle = smbc_open(path, O_RDONLY, 0755);
381 if (remotehandle < 0) {
386 "%s is a directory. Specify -R "
387 "to download recursively\n",
391 return smb_download_dir(base, name, resume);
395 "%s can't be found on the remote server\n",
400 fprintf(stderr, "Not enough memory\n");
405 "The share name used in %s does not exist\n",
410 fprintf(stderr, "You don't have enough permissions "
421 if (smbc_fstat(remotehandle, &remotestat) < 0) {
422 fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
428 } else if (!name[0]) {
429 newpath = strrchr(base, '/');
439 if (!toplevel && (newpath[0] == '/')) {
443 /* Open local file according to the mode */
445 /* if it is up-to-date, skip */
446 if (stat(newpath, &localstat) == 0 &&
447 localstat.st_mtime >= remotestat.st_mtime) {
449 printf("%s is up-to-date, skipping\n", newpath);
451 smbc_close(remotehandle);
454 /* else open it for writing and truncate if it exists */
456 newpath, O_CREAT | O_NONBLOCK | O_RDWR | O_TRUNC, 0775);
457 if (localhandle < 0) {
458 fprintf(stderr, "Can't open %s : %s\n", newpath,
460 smbc_close(remotehandle);
464 } else if (!opt.send_stdout) {
465 localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR |
466 (!resume ? O_EXCL : 0),
468 if (localhandle < 0) {
469 fprintf(stderr, "Can't open %s: %s\n", newpath,
471 smbc_close(remotehandle);
475 if (fstat(localhandle, &localstat) != 0) {
476 fprintf(stderr, "Can't fstat %s: %s\n", newpath,
478 smbc_close(remotehandle);
483 start_offset = localstat.st_size;
485 if (localstat.st_size &&
486 localstat.st_size == remotestat.st_size) {
488 fprintf(stderr, "%s is already downloaded "
491 } else if (!opt.quiet) {
492 fprintf(stderr, "%s\n", path);
494 smbc_close(remotehandle);
499 if (localstat.st_size > RESUME_CHECK_OFFSET &&
500 remotestat.st_size > RESUME_CHECK_OFFSET) {
502 localstat.st_size - RESUME_DOWNLOAD_OFFSET;
503 offset_check = localstat.st_size - RESUME_CHECK_OFFSET;
505 printf("Trying to start resume of %s at %jd\n"
506 "At the moment %jd of %jd bytes have "
508 newpath, (intmax_t)offset_check,
509 (intmax_t)localstat.st_size,
510 (intmax_t)remotestat.st_size);
516 /* First, check all bytes from offset_check to
518 off1 = lseek(localhandle, offset_check, SEEK_SET);
521 "Can't seek to %jd in local file %s\n",
522 (intmax_t)offset_check, newpath);
523 smbc_close(remotehandle);
528 off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET);
531 "Can't seek to %jd in remote file %s\n",
532 (intmax_t)offset_check, newpath);
533 smbc_close(remotehandle);
539 fprintf(stderr, "Offset in local and remote "
540 "files are different "
541 "(local: %jd, remote: %jd)\n",
542 (intmax_t)off1, (intmax_t)off2);
543 smbc_close(remotehandle);
548 if (smbc_read(remotehandle, checkbuf[0],
549 RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
550 fprintf(stderr, "Can't read %d bytes from "
552 RESUME_CHECK_SIZE, path);
553 smbc_close(remotehandle);
558 if (read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) !=
560 fprintf(stderr, "Can't read %d bytes from "
562 RESUME_CHECK_SIZE, name);
563 smbc_close(remotehandle);
568 if (memcmp(checkbuf[0], checkbuf[1],
569 RESUME_CHECK_SIZE) == 0) {
571 printf("Current local and remote file "
572 "appear to be the same. "
573 "Starting download from "
575 (intmax_t)offset_download);
578 fprintf(stderr, "Local and remote file appear "
579 "to be different, not "
580 "doing resume for %s\n",
582 smbc_close(remotehandle);
588 localhandle = STDOUT_FILENO;
594 /* We implement rate limiting by filling up a bucket with bytes and
595 * checking, once the bucket is filled, if it was filled too fast.
596 * If so, we sleep for some time to get an average transfer rate that
597 * equals to the one set by the user.
599 * The bucket size directly affects the traffic characteristics.
600 * The smaller the bucket the more frequent the pause/resume cycle.
601 * A large bucket can result in burst of high speed traffic and large
602 * pauses. A cycle of 100ms looks like a good value. This value (in
603 * ticks) is held in `ticks_to_fill_bucket`. The `bucket_size` is
605 * `limit_rate * 1024 * / (CLOCKS_PER_SEC / ticks_to_fill_bucket)`
607 * After selecting the bucket size we also need to check the blocksize
608 * of the transfer, since this is the minimum unit of traffic that we
609 * can observe. Achieving a ~10% precision requires a blocksize with a
610 * maximum size of `bucket_size / 10`.
612 if (opt.limit_rate > 0) {
613 unsigned max_block_size;
614 /* This is the time that the bucket should take to fill. */
615 ticks_to_fill_bucket = 100 /*ms*/ * CLOCKS_PER_SEC / 1000;
616 /* This is the size of the bucket in bytes.
617 * If we fill the bucket too quickly we should pause */
618 bucket_size = opt.limit_rate * 1024 / (CLOCKS_PER_SEC / ticks_to_fill_bucket);
619 max_block_size = bucket_size / 10;
620 max_block_size = max_block_size > 0 ? max_block_size : 1;
621 if (opt.blocksize > max_block_size) {
622 if (opt.blocksize != SMB_DEFAULT_BLOCKSIZE) {
624 "Warning: Overriding block size to %d "
625 "due to limit-rate", max_block_size);
627 opt.blocksize = max_block_size;
629 start_of_bucket_ticks = clock();
632 readbuf = (char *)SMB_MALLOC(opt.blocksize);
634 fprintf(stderr, "Failed to allocate %zu bytes for read "
635 "buffer (%s)", opt.blocksize, strerror(errno));
636 if (localhandle != STDOUT_FILENO) {
642 /* Now, download all bytes from offset_download to the end */
643 for (curpos = offset_download; curpos < remotestat.st_size;
644 curpos += opt.blocksize) {
646 ssize_t byteswritten;
648 /* Rate limiting. This pauses the transfer to limit traffic. */
649 if (opt.limit_rate > 0) {
650 if (bytes_in_bucket > bucket_size) {
651 clock_t now_ticks = clock();
652 clock_t diff_ticks = now_ticks
653 - start_of_bucket_ticks;
654 /* Check if the bucket filled up too fast. */
655 if (diff_ticks < ticks_to_fill_bucket) {
656 /* Pause until `ticks_to_fill_bucket` */
658 = (ticks_to_fill_bucket - diff_ticks)
659 * 1000000.0 / CLOCKS_PER_SEC;
662 /* Reset the byte counter and the ticks. */
664 start_of_bucket_ticks = clock();
668 bytesread = smbc_read(remotehandle, readbuf, opt.blocksize);
669 if (opt.limit_rate > 0) {
670 bytes_in_bucket += bytesread;
674 "Can't read %zu bytes at offset %jd, file %s\n",
675 opt.blocksize, (intmax_t)curpos, path);
676 smbc_close(remotehandle);
677 if (localhandle != STDOUT_FILENO) {
684 total_bytes += bytesread;
686 byteswritten = write(localhandle, readbuf, bytesread);
687 if (byteswritten != bytesread) {
689 "Can't write %zd bytes to local file %s at "
690 "offset %jd\n", bytesread, path,
693 smbc_close(remotehandle);
694 if (localhandle != STDOUT_FILENO) {
702 } else if (!opt.quiet) {
703 print_progress(newpath, start_time, time_mono(NULL),
704 start_offset, curpos,
713 printf("%s downloaded\n", path);
714 } else if (!opt.quiet) {
716 fprintf(stderr, "\r%s", path);
718 for (i = strlen(path); i < columns; i++) {
725 smbc_close(remotehandle);
726 if (localhandle != STDOUT_FILENO) {
732 static void clean_exit(void)
735 human_readable(total_bytes, bs, sizeof(bs));
737 fprintf(stderr, "Downloaded %s in %lu seconds\n", bs,
738 (unsigned long)(time_mono(NULL) - total_start_time));
743 static void signal_quit(int v)
748 int main(int argc, char **argv)
751 const char *file = NULL;
752 int smb_encrypt = false;
753 int resume = 0, recursive = 0;
754 TALLOC_CTX *frame = talloc_stackframe();
756 const char **argv_const = discard_const_p(const char *, argv);
757 struct poptOption long_options[] = {
763 .argInfo = POPT_ARG_NONE,
766 .descrip = "Work as user guest"
769 .longName = "encrypt",
771 .argInfo = POPT_ARG_NONE,
774 .descrip = "Encrypt SMB transport"
777 .longName = "resume",
779 .argInfo = POPT_ARG_NONE,
782 .descrip = "Automatically resume aborted files"
785 .longName = "update",
787 .argInfo = POPT_ARG_NONE,
790 .descrip = "Download only when remote file is "
791 "newer than local file or local file "
795 .longName = "recursive",
797 .argInfo = POPT_ARG_NONE,
800 .descrip = "Recursively download files"
803 .longName = "blocksize",
805 .argInfo = POPT_ARG_INT,
806 .arg = &opt.blocksize,
808 .descrip = "Change number of bytes in a block"
812 .longName = "outputfile",
814 .argInfo = POPT_ARG_STRING,
815 .arg = &opt.outputfile,
817 .descrip = "Write downloaded data to specified file"
820 .longName = "stdout",
822 .argInfo = POPT_ARG_NONE,
823 .arg = &opt.send_stdout,
825 .descrip = "Write data to stdout"
830 .argInfo = POPT_ARG_NONE,
833 .descrip = "Show dots as progress indication"
838 .argInfo = POPT_ARG_NONE,
841 .descrip = "Be quiet"
844 .longName = "verbose",
846 .argInfo = POPT_ARG_NONE,
849 .descrip = "Be verbose"
852 .longName = "limit-rate",
854 .argInfo = POPT_ARG_INT,
855 .arg = &opt.limit_rate,
857 .descrip = "Limit download speed to this many KB/s"
861 POPT_COMMON_CONNECTION
862 POPT_COMMON_CREDENTIALS
867 poptContext pc = NULL;
868 struct cli_credentials *creds = NULL;
869 enum smb_encryption_setting encryption_state = SMB_ENCRYPTION_DEFAULT;
870 enum credentials_use_kerberos use_kerberos = CRED_USE_KERBEROS_DESIRED;
871 smbc_smb_encrypt_level encrypt_level = SMBC_ENCRYPTLEVEL_DEFAULT;
873 enum smb_signing_setting signing_state = SMB_SIGNING_DEFAULT;
874 const char *use_signing = "auto";
876 bool is_nt_hash = false;
877 uint32_t gensec_features;
878 bool use_wbccache = false;
879 SMBCCTX *smb_ctx = NULL;
885 ok = samba_cmdline_init(frame,
886 SAMBA_CMDLINE_CONFIG_CLIENT,
893 signal(SIGWINCH, change_columns);
895 signal(SIGINT, signal_quit);
896 signal(SIGTERM, signal_quit);
898 pc = samba_popt_get_context(getprogname(),
908 creds = samba_cmdline_get_creds();
910 while ((c = poptGetNextOpt(pc)) != -1) {
913 cli_credentials_set_anonymous(creds);
915 case POPT_ERROR_BADOPT:
916 fprintf(stderr, "\nInvalid option %s: %s\n\n",
917 poptBadOption(pc, 0), poptStrerror(c));
918 poptPrintUsage(pc, stderr, 0);
924 fprintf(stderr, "%s: %s\n",
925 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
932 if ((opt.send_stdout || resume || opt.outputfile) && opt.update) {
933 fprintf(stderr, "The -o, -R or -O and -U options can not be "
938 if ((opt.send_stdout || opt.outputfile) && recursive) {
939 fprintf(stderr, "The -o or -O and -R options can not be "
945 if (opt.outputfile && opt.send_stdout) {
946 fprintf(stderr, "The -o and -O options can not be "
952 samba_cmdline_burn(argc, argv);
954 /* smbc_new_context() will set the log level to 0 */
955 dbg_lvl = debuglevel_get();
957 smb_ctx = smbc_new_context();
958 if (smb_ctx == NULL) {
959 fprintf(stderr, "Unable to initialize libsmbclient\n");
963 smbc_setDebug(smb_ctx, dbg_lvl);
965 rc = smbc_setConfiguration(smb_ctx, lp_default_path());
971 smbc_setFunctionAuthDataWithContext(smb_ctx,
972 get_auth_data_with_context_fn);
974 ok = smbc_init_context(smb_ctx);
978 smbc_set_context(smb_ctx);
980 encryption_state = cli_credentials_get_smb_encryption(creds);
981 switch (encryption_state) {
982 case SMB_ENCRYPTION_REQUIRED:
983 encrypt_level = SMBC_ENCRYPTLEVEL_REQUIRE;
985 case SMB_ENCRYPTION_DESIRED:
986 case SMB_ENCRYPTION_IF_REQUIRED:
987 encrypt_level = SMBC_ENCRYPTLEVEL_REQUEST;
989 case SMB_ENCRYPTION_OFF:
990 encrypt_level = SMBC_ENCRYPTLEVEL_NONE;
992 case SMB_ENCRYPTION_DEFAULT:
993 encrypt_level = SMBC_ENCRYPTLEVEL_DEFAULT;
997 encrypt_level = SMBC_ENCRYPTLEVEL_REQUIRE;
999 smbc_setOptionSmbEncryptionLevel(smb_ctx, encrypt_level);
1002 signing_state = cli_credentials_get_smb_signing(creds);
1003 if (encryption_state >= SMB_ENCRYPTION_DESIRED) {
1004 signing_state = SMB_SIGNING_REQUIRED;
1006 switch (signing_state) {
1007 case SMB_SIGNING_REQUIRED:
1008 use_signing = "required";
1010 case SMB_SIGNING_DEFAULT:
1011 case SMB_SIGNING_DESIRED:
1012 case SMB_SIGNING_IF_REQUIRED:
1013 use_signing = "yes";
1015 case SMB_SIGNING_OFF:
1016 use_signing = "off";
1019 use_signing = "auto";
1022 /* FIXME: There is no libsmbclient function to set signing state */
1025 use_kerberos = cli_credentials_get_kerberos_state(creds);
1026 switch (use_kerberos) {
1027 case CRED_USE_KERBEROS_REQUIRED:
1028 smbc_setOptionUseKerberos(smb_ctx, true);
1029 smbc_setOptionFallbackAfterKerberos(smb_ctx, false);
1031 case CRED_USE_KERBEROS_DESIRED:
1032 smbc_setOptionUseKerberos(smb_ctx, true);
1033 smbc_setOptionFallbackAfterKerberos(smb_ctx, true);
1035 case CRED_USE_KERBEROS_DISABLED:
1036 smbc_setOptionUseKerberos(smb_ctx, false);
1040 /* Check if the password supplied is an NT hash */
1041 is_nt_hash = cli_credentials_is_password_nt_hash(creds);
1042 smbc_setOptionUseNTHash(smb_ctx, is_nt_hash);
1044 /* Check if we should use the winbind ccache */
1045 gensec_features = cli_credentials_get_gensec_features(creds);
1046 use_wbccache = (gensec_features & GENSEC_FEATURE_NTLM_CCACHE);
1047 smbc_setOptionUseCCache(smb_ctx, use_wbccache);
1049 columns = get_num_cols();
1051 total_start_time = time_mono(NULL);
1053 while ((file = poptGetArg(pc))) {
1055 ok = smb_download_file(file, "", recursive, resume,
1056 true, opt.outputfile);
1058 ok = smb_download_dir(file, "", resume);
1064 poptFreeContext(pc);