s3-util: Fix asking for username and password in smbget.
[Samba.git] / source3 / utils / smbget.c
blobd2d5e00a8edfb527905cea8e32a11482bac2b8f9
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 "popt_common.h"
22 #include "libsmbclient.h"
24 static int columns = 0;
26 static time_t total_start_time = 0;
27 static off_t total_bytes = 0;
29 #define SMB_MAXPATHLEN MAXPATHLEN
32 * Number of bytes to read when checking whether local and remote file
33 * are really the same file
35 #define RESUME_CHECK_SIZE 512
36 #define RESUME_DOWNLOAD_OFFSET 1024
37 #define RESUME_CHECK_OFFSET (RESUME_DOWNLOAD_OFFSET+RESUME_CHECK_SIZE)
38 /* Number of bytes to read at once */
39 #define SMB_DEFAULT_BLOCKSIZE 64000
41 struct opt {
42 char *workgroup;
43 bool username_specified;
44 char *username;
45 bool password_specified;
46 char *password;
48 char *outputfile;
49 size_t blocksize;
51 bool nonprompt;
52 bool quiet;
53 bool dots;
54 bool verbose;
55 bool send_stdout;
56 bool update;
57 int debuglevel;
59 static struct opt opt = { .blocksize = SMB_DEFAULT_BLOCKSIZE };
61 static bool smb_download_file(const char *base, const char *name,
62 bool recursive, bool resume, bool toplevel,
63 char *outfile);
65 static int get_num_cols(void)
67 #ifdef TIOCGWINSZ
68 struct winsize ws;
69 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
70 return 0;
72 return ws.ws_col;
73 #else
74 #warning No support for TIOCGWINSZ
75 char *cols = getenv("COLUMNS");
76 if (!cols) {
77 return 0;
79 return atoi(cols);
80 #endif
83 static void change_columns(int sig)
85 columns = get_num_cols();
88 static void human_readable(off_t s, char *buffer, int l)
90 if (s > 1024 * 1024 * 1024) {
91 snprintf(buffer, l, "%.2fGB", 1.0 * s / (1024 * 1024 * 1024));
92 } else if (s > 1024 * 1024) {
93 snprintf(buffer, l, "%.2fMB", 1.0 * s / (1024 * 1024));
94 } else if (s > 1024) {
95 snprintf(buffer, l, "%.2fkB", 1.0 * s / 1024);
96 } else {
97 snprintf(buffer, l, "%jdb", (intmax_t)s);
101 static void get_auth_data(const char *srv, const char *shr, char *wg, int wglen,
102 char *un, int unlen, char *pw, int pwlen)
104 static bool hasasked = false;
105 static char *savedwg;
106 static char *savedun;
107 static char *savedpw;
109 if (hasasked) {
110 strncpy(wg, savedwg, wglen - 1);
111 strncpy(un, savedun, unlen - 1);
112 strncpy(pw, savedpw, pwlen - 1);
113 return;
115 hasasked = true;
118 * If no user has been specified un is initialized with the current
119 * username of the user who started smbget.
121 if (opt.username_specified) {
122 strncpy(un, opt.username, unlen - 1);
125 if (!opt.nonprompt && !opt.password_specified && pw[0] == '\0') {
126 char *prompt;
127 int rc;
129 rc = asprintf(&prompt,
130 "Password for [%s] connecting to //%s/%s: ",
131 un, shr, srv);
132 if (rc == -1) {
133 return;
135 (void)samba_getpass(prompt, pw, pwlen, false, false);
136 free(prompt);
137 } else if (opt.password != NULL) {
138 strncpy(pw, opt.password, pwlen-1);
141 if (opt.workgroup != NULL) {
142 strncpy(wg, opt.workgroup, wglen-1);
145 /* save the values found for later */
146 savedwg = SMB_STRDUP(wg);
147 savedun = SMB_STRDUP(un);
148 savedpw = SMB_STRDUP(pw);
150 if (!opt.quiet) {
151 char *wgtmp, *usertmp;
152 wgtmp = SMB_STRNDUP(wg, wglen);
153 usertmp = SMB_STRNDUP(un, unlen);
154 printf("Using workgroup %s, %s%s\n",
155 wgtmp,
156 *usertmp ? "user " : "guest user",
157 usertmp);
158 free(wgtmp);
159 free(usertmp);
163 static bool smb_download_dir(const char *base, const char *name, int resume)
165 char path[SMB_MAXPATHLEN];
166 int dirhandle;
167 struct smbc_dirent *dirent;
168 const char *relname = name;
169 char *tmpname;
170 bool ok = false;
172 snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
173 (base[0] && name[0] && name[0] != '/' &&
174 base[strlen(base)-1] != '/') ? "/" : "",
175 name);
177 /* List files in directory and call smb_download_file on them */
178 dirhandle = smbc_opendir(path);
179 if (dirhandle < 1) {
180 if (errno == ENOTDIR) {
181 return smb_download_file(base, name, true, resume,
182 false, NULL);
184 fprintf(stderr, "Can't open directory %s: %s\n", path,
185 strerror(errno));
186 return false;
189 while (*relname == '/') {
190 relname++;
192 mkdir(relname, 0755);
194 tmpname = SMB_STRDUP(name);
196 while ((dirent = smbc_readdir(dirhandle))) {
197 char *newname;
198 if (!strcmp(dirent->name, ".") || !strcmp(dirent->name, "..")) {
199 continue;
201 if (asprintf(&newname, "%s/%s", tmpname, dirent->name) == -1) {
202 free(tmpname);
203 return false;
205 switch (dirent->smbc_type) {
206 case SMBC_DIR:
207 ok = smb_download_dir(base, newname, resume);
208 break;
210 case SMBC_WORKGROUP:
211 ok = smb_download_dir("smb://", dirent->name, resume);
212 break;
214 case SMBC_SERVER:
215 ok = smb_download_dir("smb://", dirent->name, resume);
216 break;
218 case SMBC_FILE:
219 ok = smb_download_file(base, newname, true, resume,
220 false, NULL);
221 break;
223 case SMBC_FILE_SHARE:
224 ok = smb_download_dir(base, newname, resume);
225 break;
227 case SMBC_PRINTER_SHARE:
228 if (!opt.quiet) {
229 printf("Ignoring printer share %s\n",
230 dirent->name);
232 break;
234 case SMBC_COMMS_SHARE:
235 if (!opt.quiet) {
236 printf("Ignoring comms share %s\n",
237 dirent->name);
239 break;
241 case SMBC_IPC_SHARE:
242 if (!opt.quiet) {
243 printf("Ignoring ipc$ share %s\n",
244 dirent->name);
246 break;
248 default:
249 fprintf(stderr, "Ignoring file '%s' of type '%d'\n",
250 newname, dirent->smbc_type);
251 break;
254 if (!ok) {
255 fprintf(stderr, "Failed to download %s: %s\n",
256 newname, strerror(errno));
257 free(tmpname);
258 return false;
260 free(newname);
262 free(tmpname);
264 smbc_closedir(dirhandle);
265 return ok;
268 static char *print_time(long t)
270 static char buffer[100];
271 int secs, mins, hours;
272 if (t < -1) {
273 strncpy(buffer, "Unknown", sizeof(buffer));
274 return buffer;
277 secs = (int)t % 60;
278 mins = (int)t / 60 % 60;
279 hours = (int)t / (60 * 60);
280 snprintf(buffer, sizeof(buffer) - 1, "%02d:%02d:%02d", hours, mins,
281 secs);
282 return buffer;
285 static void print_progress(const char *name, time_t start, time_t now,
286 off_t start_pos, off_t pos, off_t total)
288 double avg = 0.0;
289 long eta = -1;
290 double prcnt = 0.0;
291 char hpos[20], htotal[20], havg[20];
292 char *status, *filename;
293 int len;
294 if (now - start) {
295 avg = 1.0 * (pos - start_pos) / (now - start);
297 eta = (total - pos) / avg;
298 if (total) {
299 prcnt = 100.0 * pos / total;
302 human_readable(pos, hpos, sizeof(hpos));
303 human_readable(total, htotal, sizeof(htotal));
304 human_readable(avg, havg, sizeof(havg));
306 len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos,
307 htotal, prcnt, havg, print_time(eta));
308 if (len == -1) {
309 return;
312 if (columns) {
313 int required = strlen(name),
314 available = columns - len - strlen("[] ");
315 if (required > available) {
316 if (asprintf(&filename, "...%s",
317 name + required - available + 3) == -1) {
318 return;
320 } else {
321 filename = SMB_STRNDUP(name, available);
323 } else {
324 filename = SMB_STRDUP(name);
327 fprintf(stderr, "\r[%s] %s", filename, status);
329 free(filename);
330 free(status);
333 /* Return false on error, true on success. */
335 static bool smb_download_file(const char *base, const char *name,
336 bool recursive, bool resume, bool toplevel,
337 char *outfile)
339 int remotehandle, localhandle;
340 time_t start_time = time_mono(NULL);
341 const char *newpath;
342 char path[SMB_MAXPATHLEN];
343 char checkbuf[2][RESUME_CHECK_SIZE];
344 char *readbuf = NULL;
345 off_t offset_download = 0, offset_check = 0, curpos = 0,
346 start_offset = 0;
347 struct stat localstat, remotestat;
349 snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
350 (*base && *name && name[0] != '/' &&
351 base[strlen(base)-1] != '/') ? "/" : "",
352 name);
354 remotehandle = smbc_open(path, O_RDONLY, 0755);
356 if (remotehandle < 0) {
357 switch (errno) {
358 case EISDIR:
359 if (!recursive) {
360 fprintf(stderr,
361 "%s is a directory. Specify -R "
362 "to download recursively\n",
363 path);
364 return false;
366 return smb_download_dir(base, name, resume);
368 case ENOENT:
369 fprintf(stderr,
370 "%s can't be found on the remote server\n",
371 path);
372 return false;
374 case ENOMEM:
375 fprintf(stderr, "Not enough memory\n");
376 return false;
378 case ENODEV:
379 fprintf(stderr,
380 "The share name used in %s does not exist\n",
381 path);
382 return false;
384 case EACCES:
385 fprintf(stderr, "You don't have enough permissions "
386 "to access %s\n",
387 path);
388 return false;
390 default:
391 perror("smbc_open");
392 return false;
396 if (smbc_fstat(remotehandle, &remotestat) < 0) {
397 fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
398 return false;
401 if (outfile) {
402 newpath = outfile;
403 } else if (!name[0]) {
404 newpath = strrchr(base, '/');
405 if (newpath) {
406 newpath++;
407 } else {
408 newpath = base;
410 } else {
411 newpath = name;
414 if (!toplevel && (newpath[0] == '/')) {
415 newpath++;
418 /* Open local file according to the mode */
419 if (opt.update) {
420 /* if it is up-to-date, skip */
421 if (stat(newpath, &localstat) == 0 &&
422 localstat.st_mtime >= remotestat.st_mtime) {
423 if (opt.verbose) {
424 printf("%s is up-to-date, skipping\n", newpath);
426 smbc_close(remotehandle);
427 return true;
429 /* else open it for writing and truncate if it exists */
430 localhandle = open(
431 newpath, O_CREAT | O_NONBLOCK | O_RDWR | O_TRUNC, 0775);
432 if (localhandle < 0) {
433 fprintf(stderr, "Can't open %s : %s\n", newpath,
434 strerror(errno));
435 smbc_close(remotehandle);
436 return false;
438 /* no offset */
439 } else if (!opt.send_stdout) {
440 localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR |
441 (!resume ? O_EXCL : 0),
442 0755);
443 if (localhandle < 0) {
444 fprintf(stderr, "Can't open %s: %s\n", newpath,
445 strerror(errno));
446 smbc_close(remotehandle);
447 return false;
450 if (fstat(localhandle, &localstat) != 0) {
451 fprintf(stderr, "Can't fstat %s: %s\n", newpath,
452 strerror(errno));
453 smbc_close(remotehandle);
454 close(localhandle);
455 return false;
458 start_offset = localstat.st_size;
460 if (localstat.st_size &&
461 localstat.st_size == remotestat.st_size) {
462 if (opt.verbose) {
463 fprintf(stderr, "%s is already downloaded "
464 "completely.\n",
465 path);
466 } else if (!opt.quiet) {
467 fprintf(stderr, "%s\n", path);
469 smbc_close(remotehandle);
470 close(localhandle);
471 return true;
474 if (localstat.st_size > RESUME_CHECK_OFFSET &&
475 remotestat.st_size > RESUME_CHECK_OFFSET) {
476 offset_download =
477 localstat.st_size - RESUME_DOWNLOAD_OFFSET;
478 offset_check = localstat.st_size - RESUME_CHECK_OFFSET;
479 if (opt.verbose) {
480 printf("Trying to start resume of %s at %jd\n"
481 "At the moment %jd of %jd bytes have "
482 "been retrieved\n",
483 newpath, (intmax_t)offset_check,
484 (intmax_t)localstat.st_size,
485 (intmax_t)remotestat.st_size);
489 if (offset_check) {
490 off_t off1, off2;
491 /* First, check all bytes from offset_check to
492 * offset_download */
493 off1 = lseek(localhandle, offset_check, SEEK_SET);
494 if (off1 < 0) {
495 fprintf(stderr,
496 "Can't seek to %jd in local file %s\n",
497 (intmax_t)offset_check, newpath);
498 smbc_close(remotehandle);
499 close(localhandle);
500 return false;
503 off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET);
504 if (off2 < 0) {
505 fprintf(stderr,
506 "Can't seek to %jd in remote file %s\n",
507 (intmax_t)offset_check, newpath);
508 smbc_close(remotehandle);
509 close(localhandle);
510 return false;
513 if (off1 != off2) {
514 fprintf(stderr, "Offset in local and remote "
515 "files are different "
516 "(local: %jd, remote: %jd)\n",
517 (intmax_t)off1, (intmax_t)off2);
518 smbc_close(remotehandle);
519 close(localhandle);
520 return false;
523 if (smbc_read(remotehandle, checkbuf[0],
524 RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
525 fprintf(stderr, "Can't read %d bytes from "
526 "remote file %s\n",
527 RESUME_CHECK_SIZE, path);
528 smbc_close(remotehandle);
529 close(localhandle);
530 return false;
533 if (read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) !=
534 RESUME_CHECK_SIZE) {
535 fprintf(stderr, "Can't read %d bytes from "
536 "local file %s\n",
537 RESUME_CHECK_SIZE, name);
538 smbc_close(remotehandle);
539 close(localhandle);
540 return false;
543 if (memcmp(checkbuf[0], checkbuf[1],
544 RESUME_CHECK_SIZE) == 0) {
545 if (opt.verbose) {
546 printf("Current local and remote file "
547 "appear to be the same. "
548 "Starting download from "
549 "offset %jd\n",
550 (intmax_t)offset_download);
552 } else {
553 fprintf(stderr, "Local and remote file appear "
554 "to be different, not "
555 "doing resume for %s\n",
556 path);
557 smbc_close(remotehandle);
558 close(localhandle);
559 return false;
562 } else {
563 localhandle = STDOUT_FILENO;
564 start_offset = 0;
565 offset_download = 0;
566 offset_check = 0;
569 readbuf = (char *)SMB_MALLOC(opt.blocksize);
570 if (!readbuf) {
571 fprintf(stderr, "Failed to allocate %zu bytes for read "
572 "buffer (%s)", opt.blocksize, strerror(errno));
573 if (localhandle != STDOUT_FILENO) {
574 close(localhandle);
576 return false;
579 /* Now, download all bytes from offset_download to the end */
580 for (curpos = offset_download; curpos < remotestat.st_size;
581 curpos += opt.blocksize) {
582 ssize_t bytesread;
583 ssize_t byteswritten;
585 bytesread = smbc_read(remotehandle, readbuf, opt.blocksize);
586 if(bytesread < 0) {
587 fprintf(stderr,
588 "Can't read %zu bytes at offset %jd, file %s\n",
589 opt.blocksize, (intmax_t)curpos, path);
590 smbc_close(remotehandle);
591 if (localhandle != STDOUT_FILENO) {
592 close(localhandle);
594 free(readbuf);
595 return false;
598 total_bytes += bytesread;
600 byteswritten = write(localhandle, readbuf, bytesread);
601 if (byteswritten != bytesread) {
602 fprintf(stderr,
603 "Can't write %zd bytes to local file %s at "
604 "offset %jd\n", bytesread, path,
605 (intmax_t)curpos);
606 free(readbuf);
607 smbc_close(remotehandle);
608 if (localhandle != STDOUT_FILENO) {
609 close(localhandle);
611 return false;
614 if (opt.dots) {
615 fputc('.', stderr);
616 } else if (!opt.quiet) {
617 print_progress(newpath, start_time, time_mono(NULL),
618 start_offset, curpos,
619 remotestat.st_size);
623 free(readbuf);
625 if (opt.dots) {
626 fputc('\n', stderr);
627 printf("%s downloaded\n", path);
628 } else if (!opt.quiet) {
629 int i;
630 fprintf(stderr, "\r%s", path);
631 if (columns) {
632 for (i = strlen(path); i < columns; i++) {
633 fputc(' ', stderr);
636 fputc('\n', stderr);
639 smbc_close(remotehandle);
640 if (localhandle != STDOUT_FILENO) {
641 close(localhandle);
643 return true;
646 static void clean_exit(void)
648 char bs[100];
649 human_readable(total_bytes, bs, sizeof(bs));
650 if (!opt.quiet) {
651 fprintf(stderr, "Downloaded %s in %lu seconds\n", bs,
652 (unsigned long)(time_mono(NULL) - total_start_time));
654 exit(0);
657 static void signal_quit(int v)
659 clean_exit();
662 static int readrcfile(const char *name, const struct poptOption long_options[])
664 FILE *fd = fopen(name, "r");
665 int lineno = 0, i;
666 char var[101], val[101];
667 bool found;
668 int *intdata;
669 char **stringdata;
670 if (!fd) {
671 fprintf(stderr, "Can't open RC file %s\n", name);
672 return 1;
675 while (!feof(fd)) {
676 lineno++;
677 if (fscanf(fd, "%100s %100s\n", var, val) < 2) {
678 fprintf(stderr,
679 "Can't parse line %d of %s, ignoring.\n",
680 lineno, name);
681 continue;
684 found = false;
686 for (i = 0; long_options[i].argInfo; i++) {
687 if (!long_options[i].longName) {
688 continue;
690 if (strcmp(long_options[i].longName, var)) {
691 continue;
693 if (!long_options[i].arg) {
694 continue;
697 switch (long_options[i].argInfo) {
698 case POPT_ARG_NONE:
699 intdata = (int *)long_options[i].arg;
700 if (!strcmp(val, "on")) {
701 *intdata = 1;
702 } else if (!strcmp(val, "off")) {
703 *intdata = 0;
704 } else {
705 fprintf(stderr, "Illegal value %s for "
706 "%s at line %d in %s\n",
707 val, var, lineno, name);
709 break;
710 case POPT_ARG_INT:
711 intdata = (int *)long_options[i].arg;
712 *intdata = atoi(val);
713 break;
714 case POPT_ARG_STRING:
715 stringdata = (char **)long_options[i].arg;
716 *stringdata = SMB_STRDUP(val);
717 if (long_options[i].shortName == 'U') {
718 char *p;
719 opt.username_specified = true;
720 p = strchr(*stringdata, '%');
721 if (p != NULL) {
722 *p = '\0';
723 opt.password = p + 1;
724 opt.password_specified = true;
727 break;
728 default:
729 fprintf(stderr, "Invalid variable %s at "
730 "line %d in %s\n",
731 var, lineno, name);
732 break;
735 found = true;
737 if (!found) {
738 fprintf(stderr,
739 "Invalid variable %s at line %d in %s\n", var,
740 lineno, name);
744 fclose(fd);
745 return 0;
748 int main(int argc, char **argv)
750 int c = 0;
751 const char *file = NULL;
752 char *rcfile = NULL;
753 bool smb_encrypt = false;
754 int resume = 0, recursive = 0;
755 TALLOC_CTX *frame = talloc_stackframe();
756 bool ret = true;
757 char *p;
758 const char **argv_const = discard_const_p(const char *, argv);
759 struct poptOption long_options[] = {
760 POPT_AUTOHELP
762 {"workgroup", 'w', POPT_ARG_STRING, &opt.workgroup, 'w', "Workgroup to use (optional)" },
763 {"user", 'U', POPT_ARG_STRING, &opt.username, 'U', "Username to use" },
764 {"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" },
766 {"nonprompt", 'n', POPT_ARG_NONE, NULL, 'n', "Don't ask anything (non-interactive)" },
767 {"debuglevel", 'd', POPT_ARG_INT, &opt.debuglevel, 'd', "Debuglevel to use" },
769 {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport" },
770 {"resume", 'r', POPT_ARG_NONE, NULL, 'r', "Automatically resume aborted files" },
771 {"update", 'u', POPT_ARG_NONE, NULL, 'u', "Download only when remote file is newer than local file or local file is missing"},
772 {"recursive", 'R', POPT_ARG_NONE, NULL, 'R', "Recursively download files" },
773 {"blocksize", 'b', POPT_ARG_INT, &opt.blocksize, 'b', "Change number of bytes in a block"},
775 {"outputfile", 'o', POPT_ARG_STRING, &opt.outputfile, 'o', "Write downloaded data to specified file" },
776 {"stdout", 'O', POPT_ARG_NONE, NULL, 'O', "Write data to stdout" },
777 {"dots", 'D', POPT_ARG_NONE, NULL, 'D', "Show dots as progress indication" },
778 {"quiet", 'q', POPT_ARG_NONE, NULL, 'q', "Be quiet" },
779 {"verbose", 'v', POPT_ARG_NONE, NULL, 'v', "Be verbose" },
780 {"rcfile", 'f', POPT_ARG_STRING, NULL, 'f', "Use specified rc file"},
782 POPT_TABLEEND
784 poptContext pc;
786 smb_init_locale();
788 /* only read rcfile if it exists */
789 if (asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME")) == -1) {
790 return 1;
792 if (access(rcfile, F_OK) == 0) {
793 readrcfile(rcfile, long_options);
795 free(rcfile);
797 #ifdef SIGWINCH
798 signal(SIGWINCH, change_columns);
799 #endif
800 signal(SIGINT, signal_quit);
801 signal(SIGTERM, signal_quit);
803 pc = poptGetContext(argv[0], argc, argv_const, long_options, 0);
805 while ((c = poptGetNextOpt(pc)) > 0) {
806 switch (c) {
807 case 'f':
808 readrcfile(poptGetOptArg(pc), long_options);
809 break;
810 case 'a':
811 opt.username_specified = true;
812 opt.username = talloc_strdup(frame, "");
813 opt.password_specified = true;
814 opt.password = talloc_strdup(frame, "");
815 break;
816 case 'e':
817 smb_encrypt = true;
818 break;
819 case 'U':
820 opt.username_specified = true;
821 opt.username = talloc_strdup(frame, opt.username);
822 p = strchr(opt.username,'%');
823 if (p != NULL) {
824 *p = '\0';
825 opt.password = p + 1;
826 opt.password_specified = true;
828 break;
829 case 'n':
830 opt.nonprompt = true;
831 break;
832 case 'r':
833 resume = true;
834 break;
835 case 'u':
836 opt.update = true;
837 break;
838 case 'R':
839 recursive = true;
840 break;
841 case 'O':
842 opt.send_stdout = true;
843 break;
844 case 'D':
845 opt.dots = true;
846 break;
847 case 'q':
848 opt.quiet = true;
849 break;
850 case 'v':
851 opt.verbose = true;
852 break;
856 if (c < -1) {
857 fprintf(stderr, "%s: %s\n",
858 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
859 poptStrerror(c));
860 return 1;
863 if ((opt.send_stdout || resume || opt.outputfile) && opt.update) {
864 fprintf(stderr, "The -o, -R or -O and -U options can not be "
865 "used together.\n");
866 return 1;
868 if ((opt.send_stdout || opt.outputfile) && recursive) {
869 fprintf(stderr, "The -o or -O and -R options can not be "
870 "used together.\n");
871 return 1;
874 if (opt.outputfile && opt.send_stdout) {
875 fprintf(stderr, "The -o and -O options can not be "
876 "used together.\n");
877 return 1;
880 popt_burn_cmdline_password(argc, argv);
882 if (smbc_init(get_auth_data, opt.debuglevel) < 0) {
883 fprintf(stderr, "Unable to initialize libsmbclient\n");
884 return 1;
887 if (smb_encrypt) {
888 SMBCCTX *smb_ctx = smbc_set_context(NULL);
889 smbc_option_set(smb_ctx,
890 discard_const_p(char, "smb_encrypt_level"),
891 "require");
894 columns = get_num_cols();
896 total_start_time = time_mono(NULL);
898 while ((file = poptGetArg(pc))) {
899 if (!recursive) {
900 ret = smb_download_file(file, "", recursive, resume,
901 true, opt.outputfile);
902 } else {
903 ret = smb_download_dir(file, "", resume);
907 TALLOC_FREE(frame);
908 if (ret) {
909 clean_exit();
911 return ret?0:1;