libcli/smb: don't overwrite status code
[Samba.git] / source3 / utils / smbget.c
blob49cca4efa2a9752b71b2a669d44a6f0b9ea026c7
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_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
42 struct opt {
43 char *workgroup;
44 bool username_specified;
45 char *username;
46 bool password_specified;
47 char *password;
49 char *outputfile;
50 size_t blocksize;
52 bool nonprompt;
53 bool quiet;
54 bool dots;
55 bool verbose;
56 bool send_stdout;
57 bool update;
58 int debuglevel;
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,
64 char *outfile);
66 static int get_num_cols(void)
68 #ifdef TIOCGWINSZ
69 struct winsize ws;
70 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
71 return 0;
73 return ws.ws_col;
74 #else
75 #warning No support for TIOCGWINSZ
76 char *cols = getenv("COLUMNS");
77 if (!cols) {
78 return 0;
80 return atoi(cols);
81 #endif
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);
97 } else {
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;
110 if (hasasked) {
111 strncpy(wg, savedwg, wglen - 1);
112 strncpy(un, savedun, unlen - 1);
113 strncpy(pw, savedpw, pwlen - 1);
114 return;
116 hasasked = true;
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') {
127 char *prompt;
128 int rc;
130 rc = asprintf(&prompt,
131 "Password for [%s] connecting to //%s/%s: ",
132 un, shr, srv);
133 if (rc == -1) {
134 return;
136 (void)samba_getpass(prompt, pw, pwlen, false, false);
137 free(prompt);
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);
151 if (!opt.quiet) {
152 char *wgtmp, *usertmp;
153 wgtmp = SMB_STRNDUP(wg, wglen);
154 usertmp = SMB_STRNDUP(un, unlen);
155 printf("Using workgroup %s, %s%s\n",
156 wgtmp,
157 *usertmp ? "user " : "guest user",
158 usertmp);
159 free(wgtmp);
160 free(usertmp);
164 static bool smb_download_dir(const char *base, const char *name, int resume)
166 char path[SMB_MAXPATHLEN];
167 int dirhandle;
168 struct smbc_dirent *dirent;
169 const char *relname = name;
170 char *tmpname;
171 bool ok = false;
173 snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
174 (base[0] && name[0] && name[0] != '/' &&
175 base[strlen(base)-1] != '/') ? "/" : "",
176 name);
178 /* List files in directory and call smb_download_file on them */
179 dirhandle = smbc_opendir(path);
180 if (dirhandle < 1) {
181 if (errno == ENOTDIR) {
182 return smb_download_file(base, name, true, resume,
183 false, NULL);
185 fprintf(stderr, "Can't open directory %s: %s\n", path,
186 strerror(errno));
187 return false;
190 while (*relname == '/') {
191 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));
199 return false;
203 tmpname = SMB_STRDUP(name);
205 while ((dirent = smbc_readdir(dirhandle))) {
206 char *newname;
207 if (!strcmp(dirent->name, ".") || !strcmp(dirent->name, "..")) {
208 ok = true;
209 continue;
211 if (asprintf(&newname, "%s/%s", tmpname, dirent->name) == -1) {
212 free(tmpname);
213 return false;
215 switch (dirent->smbc_type) {
216 case SMBC_DIR:
217 ok = smb_download_dir(base, newname, resume);
218 break;
220 case SMBC_WORKGROUP:
221 ok = smb_download_dir("smb://", dirent->name, resume);
222 break;
224 case SMBC_SERVER:
225 ok = smb_download_dir("smb://", dirent->name, resume);
226 break;
228 case SMBC_FILE:
229 ok = smb_download_file(base, newname, true, resume,
230 false, NULL);
231 break;
233 case SMBC_FILE_SHARE:
234 ok = smb_download_dir(base, newname, resume);
235 break;
237 case SMBC_PRINTER_SHARE:
238 if (!opt.quiet) {
239 printf("Ignoring printer share %s\n",
240 dirent->name);
242 break;
244 case SMBC_COMMS_SHARE:
245 if (!opt.quiet) {
246 printf("Ignoring comms share %s\n",
247 dirent->name);
249 break;
251 case SMBC_IPC_SHARE:
252 if (!opt.quiet) {
253 printf("Ignoring ipc$ share %s\n",
254 dirent->name);
256 break;
258 default:
259 fprintf(stderr, "Ignoring file '%s' of type '%d'\n",
260 newname, dirent->smbc_type);
261 break;
264 if (!ok) {
265 fprintf(stderr, "Failed to download %s: %s\n",
266 newname, strerror(errno));
267 free(tmpname);
268 return false;
270 free(newname);
272 free(tmpname);
274 smbc_closedir(dirhandle);
275 return ok;
278 static char *print_time(long t)
280 static char buffer[100];
281 int secs, mins, hours;
282 if (t < -1) {
283 strncpy(buffer, "Unknown", sizeof(buffer));
284 return buffer;
287 secs = (int)t % 60;
288 mins = (int)t / 60 % 60;
289 hours = (int)t / (60 * 60);
290 snprintf(buffer, sizeof(buffer) - 1, "%02d:%02d:%02d", hours, mins,
291 secs);
292 return buffer;
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)
298 double avg = 0.0;
299 long eta = -1;
300 double prcnt = 0.0;
301 char hpos[22], htotal[22], havg[22];
302 char *status, *filename;
303 int len;
304 if (now - start) {
305 avg = 1.0 * (pos - start_pos) / (now - start);
307 eta = (total - pos) / avg;
308 if (total) {
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));
318 if (len == -1) {
319 return;
322 if (columns) {
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) {
328 return;
330 } else {
331 filename = SMB_STRNDUP(name, available);
333 } else {
334 filename = SMB_STRDUP(name);
337 fprintf(stderr, "\r[%s] %s", filename, status);
339 free(filename);
340 free(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,
347 char *outfile)
349 int remotehandle, localhandle;
350 time_t start_time = time_mono(NULL);
351 const char *newpath;
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,
356 start_offset = 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] != '/') ? "/" : "",
362 name);
364 remotehandle = smbc_open(path, O_RDONLY, 0755);
366 if (remotehandle < 0) {
367 switch (errno) {
368 case EISDIR:
369 if (!recursive) {
370 fprintf(stderr,
371 "%s is a directory. Specify -R "
372 "to download recursively\n",
373 path);
374 return false;
376 return smb_download_dir(base, name, resume);
378 case ENOENT:
379 fprintf(stderr,
380 "%s can't be found on the remote server\n",
381 path);
382 return false;
384 case ENOMEM:
385 fprintf(stderr, "Not enough memory\n");
386 return false;
388 case ENODEV:
389 fprintf(stderr,
390 "The share name used in %s does not exist\n",
391 path);
392 return false;
394 case EACCES:
395 fprintf(stderr, "You don't have enough permissions "
396 "to access %s\n",
397 path);
398 return false;
400 default:
401 perror("smbc_open");
402 return false;
406 if (smbc_fstat(remotehandle, &remotestat) < 0) {
407 fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
408 return false;
411 if (outfile) {
412 newpath = outfile;
413 } else if (!name[0]) {
414 newpath = strrchr(base, '/');
415 if (newpath) {
416 newpath++;
417 } else {
418 newpath = base;
420 } else {
421 newpath = name;
424 if (!toplevel && (newpath[0] == '/')) {
425 newpath++;
428 /* Open local file according to the mode */
429 if (opt.update) {
430 /* if it is up-to-date, skip */
431 if (stat(newpath, &localstat) == 0 &&
432 localstat.st_mtime >= remotestat.st_mtime) {
433 if (opt.verbose) {
434 printf("%s is up-to-date, skipping\n", newpath);
436 smbc_close(remotehandle);
437 return true;
439 /* else open it for writing and truncate if it exists */
440 localhandle = open(
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,
444 strerror(errno));
445 smbc_close(remotehandle);
446 return false;
448 /* no offset */
449 } else if (!opt.send_stdout) {
450 localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR |
451 (!resume ? O_EXCL : 0),
452 0755);
453 if (localhandle < 0) {
454 fprintf(stderr, "Can't open %s: %s\n", newpath,
455 strerror(errno));
456 smbc_close(remotehandle);
457 return false;
460 if (fstat(localhandle, &localstat) != 0) {
461 fprintf(stderr, "Can't fstat %s: %s\n", newpath,
462 strerror(errno));
463 smbc_close(remotehandle);
464 close(localhandle);
465 return false;
468 start_offset = localstat.st_size;
470 if (localstat.st_size &&
471 localstat.st_size == remotestat.st_size) {
472 if (opt.verbose) {
473 fprintf(stderr, "%s is already downloaded "
474 "completely.\n",
475 path);
476 } else if (!opt.quiet) {
477 fprintf(stderr, "%s\n", path);
479 smbc_close(remotehandle);
480 close(localhandle);
481 return true;
484 if (localstat.st_size > RESUME_CHECK_OFFSET &&
485 remotestat.st_size > RESUME_CHECK_OFFSET) {
486 offset_download =
487 localstat.st_size - RESUME_DOWNLOAD_OFFSET;
488 offset_check = localstat.st_size - RESUME_CHECK_OFFSET;
489 if (opt.verbose) {
490 printf("Trying to start resume of %s at %jd\n"
491 "At the moment %jd of %jd bytes have "
492 "been retrieved\n",
493 newpath, (intmax_t)offset_check,
494 (intmax_t)localstat.st_size,
495 (intmax_t)remotestat.st_size);
499 if (offset_check) {
500 off_t off1, off2;
501 /* First, check all bytes from offset_check to
502 * offset_download */
503 off1 = lseek(localhandle, offset_check, SEEK_SET);
504 if (off1 < 0) {
505 fprintf(stderr,
506 "Can't seek to %jd in local file %s\n",
507 (intmax_t)offset_check, newpath);
508 smbc_close(remotehandle);
509 close(localhandle);
510 return false;
513 off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET);
514 if (off2 < 0) {
515 fprintf(stderr,
516 "Can't seek to %jd in remote file %s\n",
517 (intmax_t)offset_check, newpath);
518 smbc_close(remotehandle);
519 close(localhandle);
520 return false;
523 if (off1 != off2) {
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);
529 close(localhandle);
530 return false;
533 if (smbc_read(remotehandle, checkbuf[0],
534 RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
535 fprintf(stderr, "Can't read %d bytes from "
536 "remote file %s\n",
537 RESUME_CHECK_SIZE, path);
538 smbc_close(remotehandle);
539 close(localhandle);
540 return false;
543 if (read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) !=
544 RESUME_CHECK_SIZE) {
545 fprintf(stderr, "Can't read %d bytes from "
546 "local file %s\n",
547 RESUME_CHECK_SIZE, name);
548 smbc_close(remotehandle);
549 close(localhandle);
550 return false;
553 if (memcmp(checkbuf[0], checkbuf[1],
554 RESUME_CHECK_SIZE) == 0) {
555 if (opt.verbose) {
556 printf("Current local and remote file "
557 "appear to be the same. "
558 "Starting download from "
559 "offset %jd\n",
560 (intmax_t)offset_download);
562 } else {
563 fprintf(stderr, "Local and remote file appear "
564 "to be different, not "
565 "doing resume for %s\n",
566 path);
567 smbc_close(remotehandle);
568 close(localhandle);
569 return false;
572 } else {
573 localhandle = STDOUT_FILENO;
574 start_offset = 0;
575 offset_download = 0;
576 offset_check = 0;
579 readbuf = (char *)SMB_MALLOC(opt.blocksize);
580 if (!readbuf) {
581 fprintf(stderr, "Failed to allocate %zu bytes for read "
582 "buffer (%s)", opt.blocksize, strerror(errno));
583 if (localhandle != STDOUT_FILENO) {
584 close(localhandle);
586 return false;
589 /* Now, download all bytes from offset_download to the end */
590 for (curpos = offset_download; curpos < remotestat.st_size;
591 curpos += opt.blocksize) {
592 ssize_t bytesread;
593 ssize_t byteswritten;
595 bytesread = smbc_read(remotehandle, readbuf, opt.blocksize);
596 if(bytesread < 0) {
597 fprintf(stderr,
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) {
602 close(localhandle);
604 free(readbuf);
605 return false;
608 total_bytes += bytesread;
610 byteswritten = write(localhandle, readbuf, bytesread);
611 if (byteswritten != bytesread) {
612 fprintf(stderr,
613 "Can't write %zd bytes to local file %s at "
614 "offset %jd\n", bytesread, path,
615 (intmax_t)curpos);
616 free(readbuf);
617 smbc_close(remotehandle);
618 if (localhandle != STDOUT_FILENO) {
619 close(localhandle);
621 return false;
624 if (opt.dots) {
625 fputc('.', stderr);
626 } else if (!opt.quiet) {
627 print_progress(newpath, start_time, time_mono(NULL),
628 start_offset, curpos,
629 remotestat.st_size);
633 free(readbuf);
635 if (opt.dots) {
636 fputc('\n', stderr);
637 printf("%s downloaded\n", path);
638 } else if (!opt.quiet) {
639 int i;
640 fprintf(stderr, "\r%s", path);
641 if (columns) {
642 for (i = strlen(path); i < columns; i++) {
643 fputc(' ', stderr);
646 fputc('\n', stderr);
649 smbc_close(remotehandle);
650 if (localhandle != STDOUT_FILENO) {
651 close(localhandle);
653 return true;
656 static void clean_exit(void)
658 char bs[100];
659 human_readable(total_bytes, bs, sizeof(bs));
660 if (!opt.quiet) {
661 fprintf(stderr, "Downloaded %s in %lu seconds\n", bs,
662 (unsigned long)(time_mono(NULL) - total_start_time));
664 exit(0);
667 static void signal_quit(int v)
669 clean_exit();
672 static int readrcfile(const char *name, const struct poptOption long_options[])
674 FILE *fd = fopen(name, "r");
675 int lineno = 0, i;
676 char var[101], val[101];
677 bool found;
678 int *intdata;
679 char **stringdata;
680 if (!fd) {
681 fprintf(stderr, "Can't open RC file %s\n", name);
682 return 1;
685 while (!feof(fd)) {
686 lineno++;
687 if (fscanf(fd, "%100s %100s\n", var, val) < 2) {
688 fprintf(stderr,
689 "Can't parse line %d of %s, ignoring.\n",
690 lineno, name);
691 continue;
694 found = false;
696 for (i = 0; long_options[i].argInfo; i++) {
697 if (!long_options[i].longName) {
698 continue;
700 if (strcmp(long_options[i].longName, var)) {
701 continue;
703 if (!long_options[i].arg) {
704 continue;
707 switch (long_options[i].argInfo) {
708 case POPT_ARG_NONE:
709 intdata = (int *)long_options[i].arg;
710 if (!strcmp(val, "on")) {
711 *intdata = 1;
712 } else if (!strcmp(val, "off")) {
713 *intdata = 0;
714 } else {
715 fprintf(stderr, "Illegal value %s for "
716 "%s at line %d in %s\n",
717 val, var, lineno, name);
719 break;
720 case POPT_ARG_INT:
721 intdata = (int *)long_options[i].arg;
722 *intdata = atoi(val);
723 break;
724 case POPT_ARG_STRING:
725 stringdata = (char **)long_options[i].arg;
726 *stringdata = SMB_STRDUP(val);
727 if (long_options[i].shortName == 'U') {
728 char *p;
729 opt.username_specified = true;
730 p = strchr(*stringdata, '%');
731 if (p != NULL) {
732 *p = '\0';
733 opt.password = p + 1;
734 opt.password_specified = true;
737 break;
738 default:
739 fprintf(stderr, "Invalid variable %s at "
740 "line %d in %s\n",
741 var, lineno, name);
742 break;
745 found = true;
747 if (!found) {
748 fprintf(stderr,
749 "Invalid variable %s at line %d in %s\n", var,
750 lineno, name);
754 fclose(fd);
755 return 0;
758 int main(int argc, char **argv)
760 int c = 0;
761 const char *file = NULL;
762 char *rcfile = NULL;
763 bool smb_encrypt = false;
764 int resume = 0, recursive = 0;
765 TALLOC_CTX *frame = talloc_stackframe();
766 bool ret = true;
767 char *p;
768 const char **argv_const = discard_const_p(const char *, argv);
769 struct poptOption long_options[] = {
770 POPT_AUTOHELP
772 {"workgroup", 'w', POPT_ARG_STRING, &opt.workgroup, 'w', "Workgroup to use (optional)" },
773 {"user", 'U', POPT_ARG_STRING, &opt.username, 'U', "Username to use" },
774 {"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" },
776 {"nonprompt", 'n', POPT_ARG_NONE, NULL, 'n', "Don't ask anything (non-interactive)" },
777 {"debuglevel", 'd', POPT_ARG_INT, &opt.debuglevel, 'd', "Debuglevel to use" },
779 {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport" },
780 {"resume", 'r', POPT_ARG_NONE, NULL, 'r', "Automatically resume aborted files" },
781 {"update", 'u', POPT_ARG_NONE, NULL, 'u', "Download only when remote file is newer than local file or local file is missing"},
782 {"recursive", 'R', POPT_ARG_NONE, NULL, 'R', "Recursively download files" },
783 {"blocksize", 'b', POPT_ARG_INT, &opt.blocksize, 'b', "Change number of bytes in a block"},
785 {"outputfile", 'o', POPT_ARG_STRING, &opt.outputfile, 'o', "Write downloaded data to specified file" },
786 {"stdout", 'O', POPT_ARG_NONE, NULL, 'O', "Write data to stdout" },
787 {"dots", 'D', POPT_ARG_NONE, NULL, 'D', "Show dots as progress indication" },
788 {"quiet", 'q', POPT_ARG_NONE, NULL, 'q', "Be quiet" },
789 {"verbose", 'v', POPT_ARG_NONE, NULL, 'v', "Be verbose" },
790 {"rcfile", 'f', POPT_ARG_STRING, NULL, 'f', "Use specified rc file"},
792 POPT_TABLEEND
794 poptContext pc;
796 smb_init_locale();
798 /* only read rcfile if it exists */
799 if (asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME")) == -1) {
800 return 1;
802 if (access(rcfile, F_OK) == 0) {
803 readrcfile(rcfile, long_options);
805 free(rcfile);
807 #ifdef SIGWINCH
808 signal(SIGWINCH, change_columns);
809 #endif
810 signal(SIGINT, signal_quit);
811 signal(SIGTERM, signal_quit);
813 pc = poptGetContext(argv[0], argc, argv_const, long_options, 0);
815 while ((c = poptGetNextOpt(pc)) > 0) {
816 switch (c) {
817 case 'f':
818 readrcfile(poptGetOptArg(pc), long_options);
819 break;
820 case 'a':
821 opt.username_specified = true;
822 opt.username = talloc_strdup(frame, "");
823 opt.password_specified = true;
824 opt.password = talloc_strdup(frame, "");
825 break;
826 case 'e':
827 smb_encrypt = true;
828 break;
829 case 'U':
830 opt.username_specified = true;
831 opt.username = talloc_strdup(frame, opt.username);
832 p = strchr(opt.username,'%');
833 if (p != NULL) {
834 *p = '\0';
835 opt.password = p + 1;
836 opt.password_specified = true;
838 break;
839 case 'n':
840 opt.nonprompt = true;
841 break;
842 case 'r':
843 resume = true;
844 break;
845 case 'u':
846 opt.update = true;
847 break;
848 case 'R':
849 recursive = true;
850 break;
851 case 'O':
852 opt.send_stdout = true;
853 break;
854 case 'D':
855 opt.dots = true;
856 break;
857 case 'q':
858 opt.quiet = true;
859 break;
860 case 'v':
861 opt.verbose = true;
862 break;
866 if (c < -1) {
867 fprintf(stderr, "%s: %s\n",
868 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
869 poptStrerror(c));
870 return 1;
873 if ((opt.send_stdout || resume || opt.outputfile) && opt.update) {
874 fprintf(stderr, "The -o, -R or -O and -U options can not be "
875 "used together.\n");
876 return 1;
878 if ((opt.send_stdout || opt.outputfile) && recursive) {
879 fprintf(stderr, "The -o or -O and -R options can not be "
880 "used together.\n");
881 return 1;
884 if (opt.outputfile && opt.send_stdout) {
885 fprintf(stderr, "The -o and -O options can not be "
886 "used together.\n");
887 return 1;
890 popt_burn_cmdline_password(argc, argv);
892 cmdline_messaging_context(get_dyn_CONFIGFILE());
894 if (smbc_init(get_auth_data, opt.debuglevel) < 0) {
895 fprintf(stderr, "Unable to initialize libsmbclient\n");
896 return 1;
899 if (smb_encrypt) {
900 SMBCCTX *smb_ctx = smbc_set_context(NULL);
901 smbc_option_set(smb_ctx,
902 discard_const_p(char, "smb_encrypt_level"),
903 "require");
906 columns = get_num_cols();
908 total_start_time = time_mono(NULL);
910 while ((file = poptGetArg(pc))) {
911 if (!recursive) {
912 ret = smb_download_file(file, "", recursive, resume,
913 true, opt.outputfile);
914 } else {
915 ret = smb_download_dir(file, "", resume);
919 TALLOC_FREE(frame);
920 if (ret) {
921 clean_exit();
923 return ret?0:1;