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;
118 username = cli_credentials_get_username(creds);
119 if (username != NULL) {
120 strncpy(usr, username, usr_len - 1);
123 password = cli_credentials_get_password(creds);
124 if (password != NULL) {
125 strncpy(pwd, password, pwd_len - 1);
128 domain = cli_credentials_get_domain(creds);
129 if (domain != NULL) {
130 strncpy(dom, domain, dom_len - 1);
133 smbc_set_credentials_with_fallback(ctx, domain, username, password);
135 if (!opt.quiet && username != NULL) {
136 if (username[0] == '\0') {
137 printf("Using guest user\n");
139 printf("Using domain: %s, user: %s\n",
146 static bool smb_download_dir(const char *base, const char *name, int resume)
148 char path[SMB_MAXPATHLEN];
150 struct smbc_dirent *dirent;
151 const char *relname = name;
155 snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
156 (base[0] && name[0] && name[0] != '/' &&
157 base[strlen(base)-1] != '/') ? "/" : "",
160 /* List files in directory and call smb_download_file on them */
161 dirhandle = smbc_opendir(path);
163 if (errno == ENOTDIR) {
164 return smb_download_file(base, name, true, resume,
167 fprintf(stderr, "Can't open directory %s: %s\n", path,
172 while (*relname == '/') {
176 if (strlen(relname) > 0) {
177 int rc = mkdir(relname, 0755);
178 if (rc == -1 && errno != EEXIST) {
179 fprintf(stderr, "Can't create directory %s: %s\n",
180 relname, strerror(errno));
185 tmpname = SMB_STRDUP(name);
187 while ((dirent = smbc_readdir(dirhandle))) {
189 if (!strcmp(dirent->name, ".") || !strcmp(dirent->name, "..")) {
193 if (asprintf(&newname, "%s/%s", tmpname, dirent->name) == -1) {
197 switch (dirent->smbc_type) {
199 ok = smb_download_dir(base, newname, resume);
203 ok = smb_download_dir("smb://", dirent->name, resume);
207 ok = smb_download_dir("smb://", dirent->name, resume);
211 ok = smb_download_file(base, newname, true, resume,
215 case SMBC_FILE_SHARE:
216 ok = smb_download_dir(base, newname, resume);
219 case SMBC_PRINTER_SHARE:
221 printf("Ignoring printer share %s\n",
226 case SMBC_COMMS_SHARE:
228 printf("Ignoring comms share %s\n",
235 printf("Ignoring ipc$ share %s\n",
241 fprintf(stderr, "Ignoring file '%s' of type '%d'\n",
242 newname, dirent->smbc_type);
247 fprintf(stderr, "Failed to download %s: %s\n",
248 newname, strerror(errno));
257 smbc_closedir(dirhandle);
261 static char *print_time(long t)
263 static char buffer[100];
264 int secs, mins, hours;
266 strncpy(buffer, "Unknown", sizeof(buffer));
271 mins = (int)t / 60 % 60;
272 hours = (int)t / (60 * 60);
273 snprintf(buffer, sizeof(buffer) - 1, "%02d:%02d:%02d", hours, mins,
278 static void print_progress(const char *name, time_t start, time_t now,
279 off_t start_pos, off_t pos, off_t total)
284 char hpos[22], htotal[22], havg[22];
285 char *status, *filename;
288 avg = 1.0 * (pos - start_pos) / (now - start);
290 eta = (total - pos) / avg;
292 prcnt = 100.0 * pos / total;
295 human_readable(pos, hpos, sizeof(hpos));
296 human_readable(total, htotal, sizeof(htotal));
297 human_readable(avg, havg, sizeof(havg));
299 len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos,
300 htotal, prcnt, havg, print_time(eta));
306 int required = strlen(name),
307 available = columns - len - strlen("[] ");
308 if (required > available) {
309 if (asprintf(&filename, "...%s",
310 name + required - available + 3) == -1) {
314 filename = SMB_STRNDUP(name, available);
317 filename = SMB_STRDUP(name);
320 fprintf(stderr, "\r[%s] %s", filename, status);
326 /* Return false on error, true on success. */
328 static bool smb_download_file(const char *base, const char *name,
329 bool recursive, bool resume, bool toplevel,
332 int remotehandle, localhandle;
333 time_t start_time = time_mono(NULL);
335 char path[SMB_MAXPATHLEN];
336 char checkbuf[2][RESUME_CHECK_SIZE];
337 char *readbuf = NULL;
338 off_t offset_download = 0, offset_check = 0, curpos = 0,
340 struct stat localstat, remotestat;
341 clock_t start_of_bucket_ticks = 0;
342 size_t bytes_in_bucket = 0;
343 size_t bucket_size = 0;
344 clock_t ticks_to_fill_bucket = 0;
346 snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
347 (*base && *name && name[0] != '/' &&
348 base[strlen(base)-1] != '/') ? "/" : "",
351 remotehandle = smbc_open(path, O_RDONLY, 0755);
353 if (remotehandle < 0) {
358 "%s is a directory. Specify -R "
359 "to download recursively\n",
363 return smb_download_dir(base, name, resume);
367 "%s can't be found on the remote server\n",
372 fprintf(stderr, "Not enough memory\n");
377 "The share name used in %s does not exist\n",
382 fprintf(stderr, "You don't have enough permissions "
393 if (smbc_fstat(remotehandle, &remotestat) < 0) {
394 fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
400 } else if (!name[0]) {
401 newpath = strrchr(base, '/');
411 if (!toplevel && (newpath[0] == '/')) {
415 /* Open local file according to the mode */
417 /* if it is up-to-date, skip */
418 if (stat(newpath, &localstat) == 0 &&
419 localstat.st_mtime >= remotestat.st_mtime) {
421 printf("%s is up-to-date, skipping\n", newpath);
423 smbc_close(remotehandle);
426 /* else open it for writing and truncate if it exists */
428 newpath, O_CREAT | O_NONBLOCK | O_RDWR | O_TRUNC, 0775);
429 if (localhandle < 0) {
430 fprintf(stderr, "Can't open %s : %s\n", newpath,
432 smbc_close(remotehandle);
436 } else if (!opt.send_stdout) {
437 localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR |
438 (!resume ? O_EXCL : 0),
440 if (localhandle < 0) {
441 fprintf(stderr, "Can't open %s: %s\n", newpath,
443 smbc_close(remotehandle);
447 if (fstat(localhandle, &localstat) != 0) {
448 fprintf(stderr, "Can't fstat %s: %s\n", newpath,
450 smbc_close(remotehandle);
455 start_offset = localstat.st_size;
457 if (localstat.st_size &&
458 localstat.st_size == remotestat.st_size) {
460 fprintf(stderr, "%s is already downloaded "
463 } else if (!opt.quiet) {
464 fprintf(stderr, "%s\n", path);
466 smbc_close(remotehandle);
471 if (localstat.st_size > RESUME_CHECK_OFFSET &&
472 remotestat.st_size > RESUME_CHECK_OFFSET) {
474 localstat.st_size - RESUME_DOWNLOAD_OFFSET;
475 offset_check = localstat.st_size - RESUME_CHECK_OFFSET;
477 printf("Trying to start resume of %s at %jd\n"
478 "At the moment %jd of %jd bytes have "
480 newpath, (intmax_t)offset_check,
481 (intmax_t)localstat.st_size,
482 (intmax_t)remotestat.st_size);
488 /* First, check all bytes from offset_check to
490 off1 = lseek(localhandle, offset_check, SEEK_SET);
493 "Can't seek to %jd in local file %s\n",
494 (intmax_t)offset_check, newpath);
495 smbc_close(remotehandle);
500 off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET);
503 "Can't seek to %jd in remote file %s\n",
504 (intmax_t)offset_check, newpath);
505 smbc_close(remotehandle);
511 fprintf(stderr, "Offset in local and remote "
512 "files are different "
513 "(local: %jd, remote: %jd)\n",
514 (intmax_t)off1, (intmax_t)off2);
515 smbc_close(remotehandle);
520 if (smbc_read(remotehandle, checkbuf[0],
521 RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
522 fprintf(stderr, "Can't read %d bytes from "
524 RESUME_CHECK_SIZE, path);
525 smbc_close(remotehandle);
530 if (read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) !=
532 fprintf(stderr, "Can't read %d bytes from "
534 RESUME_CHECK_SIZE, name);
535 smbc_close(remotehandle);
540 if (memcmp(checkbuf[0], checkbuf[1],
541 RESUME_CHECK_SIZE) == 0) {
543 printf("Current local and remote file "
544 "appear to be the same. "
545 "Starting download from "
547 (intmax_t)offset_download);
550 fprintf(stderr, "Local and remote file appear "
551 "to be different, not "
552 "doing resume for %s\n",
554 smbc_close(remotehandle);
560 localhandle = STDOUT_FILENO;
566 /* We implement rate limiting by filling up a bucket with bytes and
567 * checking, once the bucket is filled, if it was filled too fast.
568 * If so, we sleep for some time to get an average transfer rate that
569 * equals to the one set by the user.
571 * The bucket size directly affects the traffic characteristics.
572 * The smaller the bucket the more frequent the pause/resume cycle.
573 * A large bucket can result in burst of high speed traffic and large
574 * pauses. A cycle of 100ms looks like a good value. This value (in
575 * ticks) is held in `ticks_to_fill_bucket`. The `bucket_size` is
577 * `limit_rate * 1024 * / (CLOCKS_PER_SEC / ticks_to_fill_bucket)`
579 * After selecting the bucket size we also need to check the blocksize
580 * of the transfer, since this is the minimum unit of traffic that we
581 * can observe. Achieving a ~10% precision requires a blocksize with a
582 * maximum size of `bucket_size / 10`.
584 if (opt.limit_rate > 0) {
585 unsigned max_block_size;
586 /* This is the time that the bucket should take to fill. */
587 ticks_to_fill_bucket = 100 /*ms*/ * CLOCKS_PER_SEC / 1000;
588 /* This is the size of the bucket in bytes.
589 * If we fill the bucket too quickly we should pause */
590 bucket_size = opt.limit_rate * 1024 / (CLOCKS_PER_SEC / ticks_to_fill_bucket);
591 max_block_size = bucket_size / 10;
592 max_block_size = max_block_size > 0 ? max_block_size : 1;
593 if (opt.blocksize > max_block_size) {
594 if (opt.blocksize != SMB_DEFAULT_BLOCKSIZE) {
596 "Warning: Overriding block size to %d \
597 due to limit-rate", max_block_size);
599 opt.blocksize = max_block_size;
601 start_of_bucket_ticks = clock();
604 readbuf = (char *)SMB_MALLOC(opt.blocksize);
606 fprintf(stderr, "Failed to allocate %zu bytes for read "
607 "buffer (%s)", opt.blocksize, strerror(errno));
608 if (localhandle != STDOUT_FILENO) {
614 /* Now, download all bytes from offset_download to the end */
615 for (curpos = offset_download; curpos < remotestat.st_size;
616 curpos += opt.blocksize) {
618 ssize_t byteswritten;
620 /* Rate limiting. This pauses the transfer to limit traffic. */
621 if (opt.limit_rate > 0) {
622 if (bytes_in_bucket > bucket_size) {
623 clock_t now_ticks = clock();
624 clock_t diff_ticks = now_ticks
625 - start_of_bucket_ticks;
626 /* Check if the bucket filled up too fast. */
627 if (diff_ticks < ticks_to_fill_bucket) {
628 /* Pause until `ticks_to_fill_bucket` */
630 = (ticks_to_fill_bucket - diff_ticks)
631 * 1000000.0 / CLOCKS_PER_SEC;
634 /* Reset the byte counter and the ticks. */
636 start_of_bucket_ticks = clock();
640 bytesread = smbc_read(remotehandle, readbuf, opt.blocksize);
641 if (opt.limit_rate > 0) {
642 bytes_in_bucket += bytesread;
646 "Can't read %zu bytes at offset %jd, file %s\n",
647 opt.blocksize, (intmax_t)curpos, path);
648 smbc_close(remotehandle);
649 if (localhandle != STDOUT_FILENO) {
656 total_bytes += bytesread;
658 byteswritten = write(localhandle, readbuf, bytesread);
659 if (byteswritten != bytesread) {
661 "Can't write %zd bytes to local file %s at "
662 "offset %jd\n", bytesread, path,
665 smbc_close(remotehandle);
666 if (localhandle != STDOUT_FILENO) {
674 } else if (!opt.quiet) {
675 print_progress(newpath, start_time, time_mono(NULL),
676 start_offset, curpos,
685 printf("%s downloaded\n", path);
686 } else if (!opt.quiet) {
688 fprintf(stderr, "\r%s", path);
690 for (i = strlen(path); i < columns; i++) {
697 smbc_close(remotehandle);
698 if (localhandle != STDOUT_FILENO) {
704 static void clean_exit(void)
707 human_readable(total_bytes, bs, sizeof(bs));
709 fprintf(stderr, "Downloaded %s in %lu seconds\n", bs,
710 (unsigned long)(time_mono(NULL) - total_start_time));
715 static void signal_quit(int v)
720 int main(int argc, char **argv)
723 const char *file = NULL;
724 int smb_encrypt = false;
725 int resume = 0, recursive = 0;
726 TALLOC_CTX *frame = talloc_stackframe();
728 const char **argv_const = discard_const_p(const char *, argv);
729 struct poptOption long_options[] = {
735 .argInfo = POPT_ARG_NONE,
738 .descrip = "Work as user guest"
741 .longName = "encrypt",
743 .argInfo = POPT_ARG_NONE,
746 .descrip = "Encrypt SMB transport"
749 .longName = "resume",
751 .argInfo = POPT_ARG_NONE,
754 .descrip = "Automatically resume aborted files"
757 .longName = "update",
759 .argInfo = POPT_ARG_NONE,
762 .descrip = "Download only when remote file is "
763 "newer than local file or local file "
767 .longName = "recursive",
769 .argInfo = POPT_ARG_NONE,
772 .descrip = "Recursively download files"
775 .longName = "blocksize",
777 .argInfo = POPT_ARG_INT,
778 .arg = &opt.blocksize,
780 .descrip = "Change number of bytes in a block"
784 .longName = "outputfile",
786 .argInfo = POPT_ARG_STRING,
787 .arg = &opt.outputfile,
789 .descrip = "Write downloaded data to specified file"
792 .longName = "stdout",
794 .argInfo = POPT_ARG_NONE,
795 .arg = &opt.send_stdout,
797 .descrip = "Write data to stdout"
802 .argInfo = POPT_ARG_NONE,
805 .descrip = "Show dots as progress indication"
810 .argInfo = POPT_ARG_NONE,
813 .descrip = "Be quiet"
816 .longName = "verbose",
818 .argInfo = POPT_ARG_NONE,
821 .descrip = "Be verbose"
824 .longName = "limit-rate",
826 .argInfo = POPT_ARG_INT,
827 .arg = &opt.limit_rate,
829 .descrip = "Limit download speed to this many KB/s"
833 POPT_COMMON_CONNECTION
834 POPT_COMMON_CREDENTIALS
839 poptContext pc = NULL;
840 struct cli_credentials *creds = NULL;
841 enum smb_encryption_setting encryption_state = SMB_ENCRYPTION_DEFAULT;
842 enum credentials_use_kerberos use_kerberos = CRED_USE_KERBEROS_DESIRED;
843 smbc_smb_encrypt_level encrypt_level = SMBC_ENCRYPTLEVEL_DEFAULT;
845 enum smb_signing_setting signing_state = SMB_SIGNING_DEFAULT;
846 const char *use_signing = "auto";
848 bool is_nt_hash = false;
849 uint32_t gensec_features;
850 bool use_wbccache = false;
851 SMBCCTX *smb_ctx = NULL;
857 ok = samba_cmdline_init(frame,
858 SAMBA_CMDLINE_CONFIG_CLIENT,
865 signal(SIGWINCH, change_columns);
867 signal(SIGINT, signal_quit);
868 signal(SIGTERM, signal_quit);
870 pc = samba_popt_get_context(getprogname(),
880 creds = samba_cmdline_get_creds();
882 while ((c = poptGetNextOpt(pc)) != -1) {
885 cli_credentials_set_anonymous(creds);
887 case POPT_ERROR_BADOPT:
888 fprintf(stderr, "\nInvalid option %s: %s\n\n",
889 poptBadOption(pc, 0), poptStrerror(c));
890 poptPrintUsage(pc, stderr, 0);
896 fprintf(stderr, "%s: %s\n",
897 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
904 if ((opt.send_stdout || resume || opt.outputfile) && opt.update) {
905 fprintf(stderr, "The -o, -R or -O and -U options can not be "
910 if ((opt.send_stdout || opt.outputfile) && recursive) {
911 fprintf(stderr, "The -o or -O and -R options can not be "
917 if (opt.outputfile && opt.send_stdout) {
918 fprintf(stderr, "The -o and -O options can not be "
924 samba_cmdline_burn(argc, argv);
926 /* smbc_new_context() will set the log level to 0 */
927 dbg_lvl = debuglevel_get();
929 smb_ctx = smbc_new_context();
930 if (smb_ctx == NULL) {
931 fprintf(stderr, "Unable to initialize libsmbclient\n");
935 smbc_setDebug(smb_ctx, dbg_lvl);
937 rc = smbc_setConfiguration(smb_ctx, lp_default_path());
943 smbc_setFunctionAuthDataWithContext(smb_ctx,
944 get_auth_data_with_context_fn);
946 ok = smbc_init_context(smb_ctx);
950 smbc_set_context(smb_ctx);
952 encryption_state = cli_credentials_get_smb_encryption(creds);
953 switch (encryption_state) {
954 case SMB_ENCRYPTION_REQUIRED:
955 encrypt_level = SMBC_ENCRYPTLEVEL_REQUIRE;
957 case SMB_ENCRYPTION_DESIRED:
958 case SMB_ENCRYPTION_IF_REQUIRED:
959 encrypt_level = SMBC_ENCRYPTLEVEL_REQUEST;
961 case SMB_ENCRYPTION_OFF:
962 encrypt_level = SMBC_ENCRYPTLEVEL_NONE;
964 case SMB_ENCRYPTION_DEFAULT:
965 encrypt_level = SMBC_ENCRYPTLEVEL_DEFAULT;
969 encrypt_level = SMBC_ENCRYPTLEVEL_REQUIRE;
971 smbc_setOptionSmbEncryptionLevel(smb_ctx, encrypt_level);
974 signing_state = cli_credentials_get_smb_signing(creds);
975 if (encryption_state >= SMB_ENCRYPTION_DESIRED) {
976 signing_state = SMB_SIGNING_REQUIRED;
978 switch (signing_state) {
979 case SMB_SIGNING_REQUIRED:
980 use_signing = "required";
982 case SMB_SIGNING_DEFAULT:
983 case SMB_SIGNING_DESIRED:
984 case SMB_SIGNING_IF_REQUIRED:
987 case SMB_SIGNING_OFF:
991 use_signing = "auto";
994 /* FIXME: There is no libsmbclient function to set signing state */
997 use_kerberos = cli_credentials_get_kerberos_state(creds);
998 switch (use_kerberos) {
999 case CRED_USE_KERBEROS_REQUIRED:
1000 smbc_setOptionUseKerberos(smb_ctx, true);
1001 smbc_setOptionFallbackAfterKerberos(smb_ctx, false);
1003 case CRED_USE_KERBEROS_DESIRED:
1004 smbc_setOptionUseKerberos(smb_ctx, true);
1005 smbc_setOptionFallbackAfterKerberos(smb_ctx, true);
1007 case CRED_USE_KERBEROS_DISABLED:
1008 smbc_setOptionUseKerberos(smb_ctx, false);
1012 /* Check if the password supplied is an NT hash */
1013 is_nt_hash = cli_credentials_is_password_nt_hash(creds);
1014 smbc_setOptionUseNTHash(smb_ctx, is_nt_hash);
1016 /* Check if we should use the winbind ccache */
1017 gensec_features = cli_credentials_get_gensec_features(creds);
1018 use_wbccache = (gensec_features & GENSEC_FEATURE_NTLM_CCACHE);
1019 smbc_setOptionUseCCache(smb_ctx, use_wbccache);
1021 columns = get_num_cols();
1023 total_start_time = time_mono(NULL);
1025 while ((file = poptGetArg(pc))) {
1027 ok = smb_download_file(file, "", recursive, resume,
1028 true, opt.outputfile);
1030 ok = smb_download_dir(file, "", resume);
1035 poptFreeContext(pc);