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 "popt_common_cmdline.h"
22 #include "libsmbclient.h"
23 #include "cmdline_contexts.h"
25 static int columns = 0;
27 static time_t total_start_time = 0;
28 static off_t total_bytes = 0;
30 #define SMB_MAXPATHLEN MAXPATHLEN
33 * Number of bytes to read when checking whether local and remote file
34 * are really the same file
36 #define RESUME_CHECK_SIZE 512
37 #define RESUME_DOWNLOAD_OFFSET 1024
38 #define RESUME_CHECK_OFFSET (RESUME_DOWNLOAD_OFFSET+RESUME_CHECK_SIZE)
39 /* Number of bytes to read at once */
40 #define SMB_DEFAULT_BLOCKSIZE 64000
44 bool username_specified;
46 bool password_specified;
60 static struct opt opt = { .blocksize = SMB_DEFAULT_BLOCKSIZE };
62 static bool smb_download_file(const char *base, const char *name,
63 bool recursive, bool resume, bool toplevel,
66 static int get_num_cols(void)
70 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
75 #warning No support for TIOCGWINSZ
76 char *cols = getenv("COLUMNS");
84 static void change_columns(int sig)
86 columns = get_num_cols();
89 static void human_readable(off_t s, char *buffer, int l)
91 if (s > 1024 * 1024 * 1024) {
92 snprintf(buffer, l, "%.2fGB", 1.0 * s / (1024 * 1024 * 1024));
93 } else if (s > 1024 * 1024) {
94 snprintf(buffer, l, "%.2fMB", 1.0 * s / (1024 * 1024));
95 } else if (s > 1024) {
96 snprintf(buffer, l, "%.2fkB", 1.0 * s / 1024);
98 snprintf(buffer, l, "%jdb", (intmax_t)s);
102 static void get_auth_data(const char *srv, const char *shr, char *wg, int wglen,
103 char *un, int unlen, char *pw, int pwlen)
105 static bool hasasked = false;
106 static char *savedwg;
107 static char *savedun;
108 static char *savedpw;
111 strncpy(wg, savedwg, wglen - 1);
112 strncpy(un, savedun, unlen - 1);
113 strncpy(pw, savedpw, pwlen - 1);
119 * If no user has been specified un is initialized with the current
120 * username of the user who started smbget.
122 if (opt.username_specified) {
123 strncpy(un, opt.username, unlen - 1);
126 if (!opt.nonprompt && !opt.password_specified && pw[0] == '\0') {
130 rc = asprintf(&prompt,
131 "Password for [%s] connecting to //%s/%s: ",
136 (void)samba_getpass(prompt, pw, pwlen, false, false);
138 } else if (opt.password != NULL) {
139 strncpy(pw, opt.password, pwlen-1);
142 if (opt.workgroup != NULL) {
143 strncpy(wg, opt.workgroup, wglen-1);
146 /* save the values found for later */
147 savedwg = SMB_STRDUP(wg);
148 savedun = SMB_STRDUP(un);
149 savedpw = SMB_STRDUP(pw);
152 char *wgtmp, *usertmp;
153 wgtmp = SMB_STRNDUP(wg, wglen);
154 usertmp = SMB_STRNDUP(un, unlen);
155 printf("Using workgroup %s, %s%s\n",
157 *usertmp ? "user " : "guest user",
164 static bool smb_download_dir(const char *base, const char *name, int resume)
166 char path[SMB_MAXPATHLEN];
168 struct smbc_dirent *dirent;
169 const char *relname = name;
173 snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
174 (base[0] && name[0] && name[0] != '/' &&
175 base[strlen(base)-1] != '/') ? "/" : "",
178 /* List files in directory and call smb_download_file on them */
179 dirhandle = smbc_opendir(path);
181 if (errno == ENOTDIR) {
182 return smb_download_file(base, name, true, resume,
185 fprintf(stderr, "Can't open directory %s: %s\n", path,
190 while (*relname == '/') {
194 if (strlen(relname) > 0) {
195 int rc = mkdir(relname, 0755);
196 if (rc == -1 && errno != EEXIST) {
197 fprintf(stderr, "Can't create directory %s: %s\n",
198 relname, strerror(errno));
203 tmpname = SMB_STRDUP(name);
205 while ((dirent = smbc_readdir(dirhandle))) {
207 if (!strcmp(dirent->name, ".") || !strcmp(dirent->name, "..")) {
211 if (asprintf(&newname, "%s/%s", tmpname, dirent->name) == -1) {
215 switch (dirent->smbc_type) {
217 ok = smb_download_dir(base, newname, resume);
221 ok = smb_download_dir("smb://", dirent->name, resume);
225 ok = smb_download_dir("smb://", dirent->name, resume);
229 ok = smb_download_file(base, newname, true, resume,
233 case SMBC_FILE_SHARE:
234 ok = smb_download_dir(base, newname, resume);
237 case SMBC_PRINTER_SHARE:
239 printf("Ignoring printer share %s\n",
244 case SMBC_COMMS_SHARE:
246 printf("Ignoring comms share %s\n",
253 printf("Ignoring ipc$ share %s\n",
259 fprintf(stderr, "Ignoring file '%s' of type '%d'\n",
260 newname, dirent->smbc_type);
265 fprintf(stderr, "Failed to download %s: %s\n",
266 newname, strerror(errno));
274 smbc_closedir(dirhandle);
278 static char *print_time(long t)
280 static char buffer[100];
281 int secs, mins, hours;
283 strncpy(buffer, "Unknown", sizeof(buffer));
288 mins = (int)t / 60 % 60;
289 hours = (int)t / (60 * 60);
290 snprintf(buffer, sizeof(buffer) - 1, "%02d:%02d:%02d", hours, mins,
295 static void print_progress(const char *name, time_t start, time_t now,
296 off_t start_pos, off_t pos, off_t total)
301 char hpos[22], htotal[22], havg[22];
302 char *status, *filename;
305 avg = 1.0 * (pos - start_pos) / (now - start);
307 eta = (total - pos) / avg;
309 prcnt = 100.0 * pos / total;
312 human_readable(pos, hpos, sizeof(hpos));
313 human_readable(total, htotal, sizeof(htotal));
314 human_readable(avg, havg, sizeof(havg));
316 len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos,
317 htotal, prcnt, havg, print_time(eta));
323 int required = strlen(name),
324 available = columns - len - strlen("[] ");
325 if (required > available) {
326 if (asprintf(&filename, "...%s",
327 name + required - available + 3) == -1) {
331 filename = SMB_STRNDUP(name, available);
334 filename = SMB_STRDUP(name);
337 fprintf(stderr, "\r[%s] %s", filename, status);
343 /* Return false on error, true on success. */
345 static bool smb_download_file(const char *base, const char *name,
346 bool recursive, bool resume, bool toplevel,
349 int remotehandle, localhandle;
350 time_t start_time = time_mono(NULL);
352 char path[SMB_MAXPATHLEN];
353 char checkbuf[2][RESUME_CHECK_SIZE];
354 char *readbuf = NULL;
355 off_t offset_download = 0, offset_check = 0, curpos = 0,
357 struct stat localstat, remotestat;
359 snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
360 (*base && *name && name[0] != '/' &&
361 base[strlen(base)-1] != '/') ? "/" : "",
364 remotehandle = smbc_open(path, O_RDONLY, 0755);
366 if (remotehandle < 0) {
371 "%s is a directory. Specify -R "
372 "to download recursively\n",
376 return smb_download_dir(base, name, resume);
380 "%s can't be found on the remote server\n",
385 fprintf(stderr, "Not enough memory\n");
390 "The share name used in %s does not exist\n",
395 fprintf(stderr, "You don't have enough permissions "
406 if (smbc_fstat(remotehandle, &remotestat) < 0) {
407 fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
413 } else if (!name[0]) {
414 newpath = strrchr(base, '/');
424 if (!toplevel && (newpath[0] == '/')) {
428 /* Open local file according to the mode */
430 /* if it is up-to-date, skip */
431 if (stat(newpath, &localstat) == 0 &&
432 localstat.st_mtime >= remotestat.st_mtime) {
434 printf("%s is up-to-date, skipping\n", newpath);
436 smbc_close(remotehandle);
439 /* else open it for writing and truncate if it exists */
441 newpath, O_CREAT | O_NONBLOCK | O_RDWR | O_TRUNC, 0775);
442 if (localhandle < 0) {
443 fprintf(stderr, "Can't open %s : %s\n", newpath,
445 smbc_close(remotehandle);
449 } else if (!opt.send_stdout) {
450 localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR |
451 (!resume ? O_EXCL : 0),
453 if (localhandle < 0) {
454 fprintf(stderr, "Can't open %s: %s\n", newpath,
456 smbc_close(remotehandle);
460 if (fstat(localhandle, &localstat) != 0) {
461 fprintf(stderr, "Can't fstat %s: %s\n", newpath,
463 smbc_close(remotehandle);
468 start_offset = localstat.st_size;
470 if (localstat.st_size &&
471 localstat.st_size == remotestat.st_size) {
473 fprintf(stderr, "%s is already downloaded "
476 } else if (!opt.quiet) {
477 fprintf(stderr, "%s\n", path);
479 smbc_close(remotehandle);
484 if (localstat.st_size > RESUME_CHECK_OFFSET &&
485 remotestat.st_size > RESUME_CHECK_OFFSET) {
487 localstat.st_size - RESUME_DOWNLOAD_OFFSET;
488 offset_check = localstat.st_size - RESUME_CHECK_OFFSET;
490 printf("Trying to start resume of %s at %jd\n"
491 "At the moment %jd of %jd bytes have "
493 newpath, (intmax_t)offset_check,
494 (intmax_t)localstat.st_size,
495 (intmax_t)remotestat.st_size);
501 /* First, check all bytes from offset_check to
503 off1 = lseek(localhandle, offset_check, SEEK_SET);
506 "Can't seek to %jd in local file %s\n",
507 (intmax_t)offset_check, newpath);
508 smbc_close(remotehandle);
513 off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET);
516 "Can't seek to %jd in remote file %s\n",
517 (intmax_t)offset_check, newpath);
518 smbc_close(remotehandle);
524 fprintf(stderr, "Offset in local and remote "
525 "files are different "
526 "(local: %jd, remote: %jd)\n",
527 (intmax_t)off1, (intmax_t)off2);
528 smbc_close(remotehandle);
533 if (smbc_read(remotehandle, checkbuf[0],
534 RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
535 fprintf(stderr, "Can't read %d bytes from "
537 RESUME_CHECK_SIZE, path);
538 smbc_close(remotehandle);
543 if (read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) !=
545 fprintf(stderr, "Can't read %d bytes from "
547 RESUME_CHECK_SIZE, name);
548 smbc_close(remotehandle);
553 if (memcmp(checkbuf[0], checkbuf[1],
554 RESUME_CHECK_SIZE) == 0) {
556 printf("Current local and remote file "
557 "appear to be the same. "
558 "Starting download from "
560 (intmax_t)offset_download);
563 fprintf(stderr, "Local and remote file appear "
564 "to be different, not "
565 "doing resume for %s\n",
567 smbc_close(remotehandle);
573 localhandle = STDOUT_FILENO;
579 readbuf = (char *)SMB_MALLOC(opt.blocksize);
581 fprintf(stderr, "Failed to allocate %zu bytes for read "
582 "buffer (%s)", opt.blocksize, strerror(errno));
583 if (localhandle != STDOUT_FILENO) {
589 /* Now, download all bytes from offset_download to the end */
590 for (curpos = offset_download; curpos < remotestat.st_size;
591 curpos += opt.blocksize) {
593 ssize_t byteswritten;
595 bytesread = smbc_read(remotehandle, readbuf, opt.blocksize);
598 "Can't read %zu bytes at offset %jd, file %s\n",
599 opt.blocksize, (intmax_t)curpos, path);
600 smbc_close(remotehandle);
601 if (localhandle != STDOUT_FILENO) {
608 total_bytes += bytesread;
610 byteswritten = write(localhandle, readbuf, bytesread);
611 if (byteswritten != bytesread) {
613 "Can't write %zd bytes to local file %s at "
614 "offset %jd\n", bytesread, path,
617 smbc_close(remotehandle);
618 if (localhandle != STDOUT_FILENO) {
626 } else if (!opt.quiet) {
627 print_progress(newpath, start_time, time_mono(NULL),
628 start_offset, curpos,
637 printf("%s downloaded\n", path);
638 } else if (!opt.quiet) {
640 fprintf(stderr, "\r%s", path);
642 for (i = strlen(path); i < columns; i++) {
649 smbc_close(remotehandle);
650 if (localhandle != STDOUT_FILENO) {
656 static void clean_exit(void)
659 human_readable(total_bytes, bs, sizeof(bs));
661 fprintf(stderr, "Downloaded %s in %lu seconds\n", bs,
662 (unsigned long)(time_mono(NULL) - total_start_time));
667 static void signal_quit(int v)
672 static int readrcfile(const char *name, const struct poptOption long_options[])
674 FILE *fd = fopen(name, "r");
676 char var[101], val[101];
681 fprintf(stderr, "Can't open RC file %s\n", name);
687 if (fscanf(fd, "%100s %100s\n", var, val) < 2) {
689 "Can't parse line %d of %s, ignoring.\n",
696 for (i = 0; long_options[i].argInfo; i++) {
697 if (!long_options[i].longName) {
700 if (strcmp(long_options[i].longName, var)) {
703 if (!long_options[i].arg) {
707 switch (long_options[i].argInfo) {
709 intdata = (int *)long_options[i].arg;
710 if (!strcmp(val, "on")) {
712 } else if (!strcmp(val, "off")) {
715 fprintf(stderr, "Illegal value %s for "
716 "%s at line %d in %s\n",
717 val, var, lineno, name);
721 intdata = (int *)long_options[i].arg;
722 *intdata = atoi(val);
724 case POPT_ARG_STRING:
725 stringdata = (char **)long_options[i].arg;
726 *stringdata = SMB_STRDUP(val);
727 if (long_options[i].shortName == 'U') {
729 opt.username_specified = true;
730 p = strchr(*stringdata, '%');
733 opt.password = p + 1;
734 opt.password_specified = true;
739 fprintf(stderr, "Invalid variable %s at "
749 "Invalid variable %s at line %d in %s\n", var,
758 int main(int argc, char **argv)
761 const char *file = NULL;
763 bool smb_encrypt = false;
764 int resume = 0, recursive = 0;
765 TALLOC_CTX *frame = talloc_stackframe();
768 const char **argv_const = discard_const_p(const char *, argv);
769 struct poptOption long_options[] = {
773 .longName = "workgroup",
775 .argInfo = POPT_ARG_STRING,
776 .arg = &opt.workgroup,
778 .descrip = "Workgroup to use (optional)"
783 .argInfo = POPT_ARG_STRING,
784 .arg = &opt.username,
786 .descrip = "Username to use"
791 .argInfo = POPT_ARG_NONE,
794 .descrip = "Work as user guest"
798 .longName = "nonprompt",
800 .argInfo = POPT_ARG_NONE,
803 .descrip = "Don't ask anything (non-interactive)"
806 .longName = "debuglevel",
808 .argInfo = POPT_ARG_INT,
809 .arg = &opt.debuglevel,
811 .descrip = "Debuglevel to use"
815 .longName = "encrypt",
817 .argInfo = POPT_ARG_NONE,
820 .descrip = "Encrypt SMB transport"
823 .longName = "resume",
825 .argInfo = POPT_ARG_NONE,
828 .descrip = "Automatically resume aborted files"
831 .longName = "update",
833 .argInfo = POPT_ARG_NONE,
836 .descrip = "Download only when remote file is "
837 "newer than local file or local file "
841 .longName = "recursive",
843 .argInfo = POPT_ARG_NONE,
846 .descrip = "Recursively download files"
849 .longName = "blocksize",
851 .argInfo = POPT_ARG_INT,
852 .arg = &opt.blocksize,
854 .descrip = "Change number of bytes in a block"
858 .longName = "outputfile",
860 .argInfo = POPT_ARG_STRING,
861 .arg = &opt.outputfile,
863 .descrip = "Write downloaded data to specified file"
866 .longName = "stdout",
868 .argInfo = POPT_ARG_NONE,
871 .descrip = "Write data to stdout"
876 .argInfo = POPT_ARG_NONE,
879 .descrip = "Show dots as progress indication"
884 .argInfo = POPT_ARG_NONE,
887 .descrip = "Be quiet"
890 .longName = "verbose",
892 .argInfo = POPT_ARG_NONE,
895 .descrip = "Be verbose"
898 .longName = "rcfile",
900 .argInfo = POPT_ARG_STRING,
903 .descrip = "Use specified rc file"
912 /* only read rcfile if it exists */
913 if (asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME")) == -1) {
916 if (access(rcfile, F_OK) == 0) {
917 readrcfile(rcfile, long_options);
922 signal(SIGWINCH, change_columns);
924 signal(SIGINT, signal_quit);
925 signal(SIGTERM, signal_quit);
927 pc = poptGetContext(argv[0], argc, argv_const, long_options, 0);
929 while ((c = poptGetNextOpt(pc)) > 0) {
932 readrcfile(poptGetOptArg(pc), long_options);
935 opt.username_specified = true;
936 opt.username = talloc_strdup(frame, "");
937 opt.password_specified = true;
938 opt.password = talloc_strdup(frame, "");
944 opt.username_specified = true;
945 opt.username = talloc_strdup(frame, opt.username);
946 p = strchr(opt.username,'%');
949 opt.password = p + 1;
950 opt.password_specified = true;
954 opt.nonprompt = true;
966 opt.send_stdout = true;
981 fprintf(stderr, "%s: %s\n",
982 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
987 if ((opt.send_stdout || resume || opt.outputfile) && opt.update) {
988 fprintf(stderr, "The -o, -R or -O and -U options can not be "
992 if ((opt.send_stdout || opt.outputfile) && recursive) {
993 fprintf(stderr, "The -o or -O and -R options can not be "
998 if (opt.outputfile && opt.send_stdout) {
999 fprintf(stderr, "The -o and -O options can not be "
1000 "used together.\n");
1004 popt_burn_cmdline_password(argc, argv);
1006 cmdline_messaging_context(get_dyn_CONFIGFILE());
1008 if (smbc_init(get_auth_data, opt.debuglevel) < 0) {
1009 fprintf(stderr, "Unable to initialize libsmbclient\n");
1014 SMBCCTX *smb_ctx = smbc_set_context(NULL);
1015 smbc_option_set(smb_ctx,
1016 discard_const_p(char, "smb_encrypt_level"),
1020 columns = get_num_cols();
1022 total_start_time = time_mono(NULL);
1024 while ((file = poptGetArg(pc))) {
1026 ret = smb_download_file(file, "", recursive, resume,
1027 true, opt.outputfile);
1029 ret = smb_download_dir(file, "", resume);