s3-utils/smbget: Fix reading the rcfile
[Samba.git] / source3 / utils / smbget.c
blobe332b2d99b42ee74c736710f232759c68664e25e
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 keep_permissions;
55 bool verbose;
56 bool send_stdout;
57 bool update;
58 int debuglevel;
60 static struct opt opt;
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;
109 char tmp[128];
111 if (hasasked) {
112 strncpy(wg, savedwg, wglen - 1);
113 strncpy(un, savedun, unlen - 1);
114 strncpy(pw, savedpw, pwlen - 1);
115 return;
117 hasasked = true;
119 if (!opt.nonprompt && !opt.username_specified) {
120 printf("Username for %s at %s [guest] ", shr, srv);
121 if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
122 return;
124 if ((strlen(tmp) > 0) && (tmp[strlen(tmp) - 1] == '\n')) {
125 tmp[strlen(tmp) - 1] = '\0';
127 strncpy(un, tmp, unlen - 1);
128 } else if (opt.username != NULL) {
129 strncpy(un, opt.username, unlen - 1);
132 if (!opt.nonprompt && !opt.password_specified) {
133 char *prompt;
134 if (asprintf(&prompt, "Password for %s at %s: ", shr, srv) ==
135 -1) {
136 return;
138 (void)samba_getpass(prompt, pw, pwlen, false, false);
139 free(prompt);
140 } else if (opt.password != NULL) {
141 strncpy(pw, opt.password, pwlen-1);
144 if (opt.workgroup != NULL) {
145 strncpy(wg, opt.workgroup, wglen-1);
148 /* save the values found for later */
149 savedwg = SMB_STRDUP(wg);
150 savedun = SMB_STRDUP(un);
151 savedpw = SMB_STRDUP(pw);
153 if (!opt.quiet) {
154 char *wgtmp, *usertmp;
155 wgtmp = SMB_STRNDUP(wg, wglen);
156 usertmp = SMB_STRNDUP(un, unlen);
157 printf("Using workgroup %s, %s%s\n",
158 wgtmp,
159 *usertmp ? "user " : "guest user",
160 usertmp);
161 free(wgtmp);
162 free(usertmp);
166 /* Return 1 on error, 0 on success. */
168 static int smb_download_dir(const char *base, const char *name, int resume)
170 char path[SMB_MAXPATHLEN];
171 int dirhandle;
172 struct smbc_dirent *dirent;
173 const char *relname = name;
174 char *tmpname;
175 struct stat remotestat;
176 int ret = 0;
178 snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
179 (base[0] && name[0] && name[0] != '/' &&
180 base[strlen(base)-1] != '/') ? "/" : "",
181 name);
183 /* List files in directory and call smb_download_file on them */
184 dirhandle = smbc_opendir(path);
185 if (dirhandle < 1) {
186 if (errno == ENOTDIR) {
187 return smb_download_file(base, name, true, resume,
188 false, NULL);
190 fprintf(stderr, "Can't open directory %s: %s\n", path,
191 strerror(errno));
192 return 1;
195 while (*relname == '/') {
196 relname++;
198 mkdir(relname, 0755);
200 tmpname = SMB_STRDUP(name);
202 while ((dirent = smbc_readdir(dirhandle))) {
203 char *newname;
204 if (!strcmp(dirent->name, ".") || !strcmp(dirent->name, "..")) {
205 continue;
207 if (asprintf(&newname, "%s/%s", tmpname, dirent->name) == -1) {
208 free(tmpname);
209 return 1;
211 switch (dirent->smbc_type) {
212 case SMBC_DIR:
213 ret = smb_download_dir(base, newname, resume);
214 break;
216 case SMBC_WORKGROUP:
217 ret = smb_download_dir("smb://", dirent->name, resume);
218 break;
220 case SMBC_SERVER:
221 ret = smb_download_dir("smb://", dirent->name, resume);
222 break;
224 case SMBC_FILE:
225 ret = smb_download_file(base, newname, true, resume,
226 false, NULL);
227 break;
229 case SMBC_FILE_SHARE:
230 ret = smb_download_dir(base, newname, resume);
231 break;
233 case SMBC_PRINTER_SHARE:
234 if (!opt.quiet) {
235 printf("Ignoring printer share %s\n",
236 dirent->name);
238 break;
240 case SMBC_COMMS_SHARE:
241 if (!opt.quiet) {
242 printf("Ignoring comms share %s\n",
243 dirent->name);
245 break;
247 case SMBC_IPC_SHARE:
248 if (!opt.quiet) {
249 printf("Ignoring ipc$ share %s\n",
250 dirent->name);
252 break;
254 default:
255 fprintf(stderr, "Ignoring file '%s' of type '%d'\n",
256 newname, dirent->smbc_type);
257 break;
259 free(newname);
261 free(tmpname);
263 if (opt.keep_permissions) {
264 if (smbc_fstat(dirhandle, &remotestat) < 0) {
265 fprintf(stderr,
266 "Unable to get stats on %s on remote server\n",
267 path);
268 smbc_closedir(dirhandle);
269 return 1;
272 if (chmod(relname, remotestat.st_mode) < 0) {
273 fprintf(stderr,
274 "Unable to change mode of local dir %s to %o\n",
275 relname, (unsigned int)remotestat.st_mode);
276 smbc_closedir(dirhandle);
277 return 1;
281 smbc_closedir(dirhandle);
282 return ret;
285 static char *print_time(long t)
287 static char buffer[100];
288 int secs, mins, hours;
289 if (t < -1) {
290 strncpy(buffer, "Unknown", sizeof(buffer));
291 return buffer;
294 secs = (int)t % 60;
295 mins = (int)t / 60 % 60;
296 hours = (int)t / (60 * 60);
297 snprintf(buffer, sizeof(buffer) - 1, "%02d:%02d:%02d", hours, mins,
298 secs);
299 return buffer;
302 static void print_progress(const char *name, time_t start, time_t now,
303 off_t start_pos, off_t pos, off_t total)
305 double avg = 0.0;
306 long eta = -1;
307 double prcnt = 0.0;
308 char hpos[20], htotal[20], havg[20];
309 char *status, *filename;
310 int len;
311 if (now - start) {
312 avg = 1.0 * (pos - start_pos) / (now - start);
314 eta = (total - pos) / avg;
315 if (total) {
316 prcnt = 100.0 * pos / total;
319 human_readable(pos, hpos, sizeof(hpos));
320 human_readable(total, htotal, sizeof(htotal));
321 human_readable(avg, havg, sizeof(havg));
323 len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos,
324 htotal, prcnt, havg, print_time(eta));
325 if (len == -1) {
326 return;
329 if (columns) {
330 int required = strlen(name),
331 available = columns - len - strlen("[] ");
332 if (required > available) {
333 if (asprintf(&filename, "...%s",
334 name + required - available + 3) == -1) {
335 return;
337 } else {
338 filename = SMB_STRNDUP(name, available);
340 } else {
341 filename = SMB_STRDUP(name);
344 fprintf(stderr, "\r[%s] %s", filename, status);
346 free(filename);
347 free(status);
350 /* Return false on error, true on success. */
352 static bool smb_download_file(const char *base, const char *name,
353 bool recursive, bool resume, bool toplevel,
354 char *outfile)
356 int remotehandle, localhandle;
357 time_t start_time = time_mono(NULL);
358 const char *newpath;
359 char path[SMB_MAXPATHLEN];
360 char checkbuf[2][RESUME_CHECK_SIZE];
361 char *readbuf = NULL;
362 off_t offset_download = 0, offset_check = 0, curpos = 0,
363 start_offset = 0;
364 struct stat localstat, remotestat;
366 snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
367 (*base && *name && name[0] != '/' &&
368 base[strlen(base)-1] != '/') ? "/" : "",
369 name);
371 remotehandle = smbc_open(path, O_RDONLY, 0755);
373 if (remotehandle < 0) {
374 switch (errno) {
375 case EISDIR:
376 if (!recursive) {
377 fprintf(stderr,
378 "%s is a directory. Specify -R "
379 "to download recursively\n",
380 path);
381 return false;
383 return smb_download_dir(base, name, resume);
385 case ENOENT:
386 fprintf(stderr,
387 "%s can't be found on the remote server\n",
388 path);
389 return false;
391 case ENOMEM:
392 fprintf(stderr, "Not enough memory\n");
393 return false;
395 case ENODEV:
396 fprintf(stderr,
397 "The share name used in %s does not exist\n",
398 path);
399 return false;
401 case EACCES:
402 fprintf(stderr, "You don't have enough permissions "
403 "to access %s\n",
404 path);
405 return false;
407 default:
408 perror("smbc_open");
409 return false;
413 if (smbc_fstat(remotehandle, &remotestat) < 0) {
414 fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
415 return false;
418 if (outfile) {
419 newpath = outfile;
420 } else if (!name[0]) {
421 newpath = strrchr(base, '/');
422 if (newpath) {
423 newpath++;
424 } else {
425 newpath = base;
427 } else {
428 newpath = name;
431 if (!toplevel && (newpath[0] == '/')) {
432 newpath++;
435 /* Open local file according to the mode */
436 if (opt.update) {
437 /* if it is up-to-date, skip */
438 if (stat(newpath, &localstat) == 0 &&
439 localstat.st_mtime >= remotestat.st_mtime) {
440 if (opt.verbose) {
441 printf("%s is up-to-date, skipping\n", newpath);
443 smbc_close(remotehandle);
444 return true;
446 /* else open it for writing and truncate if it exists */
447 localhandle = open(
448 newpath, O_CREAT | O_NONBLOCK | O_RDWR | O_TRUNC, 0775);
449 if (localhandle < 0) {
450 fprintf(stderr, "Can't open %s : %s\n", newpath,
451 strerror(errno));
452 smbc_close(remotehandle);
453 return false;
455 /* no offset */
456 } else if (!opt.send_stdout) {
457 localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR |
458 (!resume ? O_EXCL : 0),
459 0755);
460 if (localhandle < 0) {
461 fprintf(stderr, "Can't open %s: %s\n", newpath,
462 strerror(errno));
463 smbc_close(remotehandle);
464 return false;
467 if (fstat(localhandle, &localstat) != 0) {
468 fprintf(stderr, "Can't fstat %s: %s\n", newpath,
469 strerror(errno));
470 smbc_close(remotehandle);
471 close(localhandle);
472 return false;
475 start_offset = localstat.st_size;
477 if (localstat.st_size &&
478 localstat.st_size == remotestat.st_size) {
479 if (opt.verbose) {
480 fprintf(stderr, "%s is already downloaded "
481 "completely.\n",
482 path);
483 } else if (!opt.quiet) {
484 fprintf(stderr, "%s\n", path);
486 smbc_close(remotehandle);
487 close(localhandle);
488 return true;
491 if (localstat.st_size > RESUME_CHECK_OFFSET &&
492 remotestat.st_size > RESUME_CHECK_OFFSET) {
493 offset_download =
494 localstat.st_size - RESUME_DOWNLOAD_OFFSET;
495 offset_check = localstat.st_size - RESUME_CHECK_OFFSET;
496 if (opt.verbose) {
497 printf("Trying to start resume of %s at %jd\n"
498 "At the moment %jd of %jd bytes have "
499 "been retrieved\n",
500 newpath, (intmax_t)offset_check,
501 (intmax_t)localstat.st_size,
502 (intmax_t)remotestat.st_size);
506 if (offset_check) {
507 off_t off1, off2;
508 /* First, check all bytes from offset_check to
509 * offset_download */
510 off1 = lseek(localhandle, offset_check, SEEK_SET);
511 if (off1 < 0) {
512 fprintf(stderr,
513 "Can't seek to %jd in local file %s\n",
514 (intmax_t)offset_check, newpath);
515 smbc_close(remotehandle);
516 close(localhandle);
517 return false;
520 off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET);
521 if (off2 < 0) {
522 fprintf(stderr,
523 "Can't seek to %jd in remote file %s\n",
524 (intmax_t)offset_check, newpath);
525 smbc_close(remotehandle);
526 close(localhandle);
527 return false;
530 if (off1 != off2) {
531 fprintf(stderr, "Offset in local and remote "
532 "files are different "
533 "(local: %jd, remote: %jd)\n",
534 (intmax_t)off1, (intmax_t)off2);
535 smbc_close(remotehandle);
536 close(localhandle);
537 return false;
540 if (smbc_read(remotehandle, checkbuf[0],
541 RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
542 fprintf(stderr, "Can't read %d bytes from "
543 "remote file %s\n",
544 RESUME_CHECK_SIZE, path);
545 smbc_close(remotehandle);
546 close(localhandle);
547 return false;
550 if (read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) !=
551 RESUME_CHECK_SIZE) {
552 fprintf(stderr, "Can't read %d bytes from "
553 "local file %s\n",
554 RESUME_CHECK_SIZE, name);
555 smbc_close(remotehandle);
556 close(localhandle);
557 return false;
560 if (memcmp(checkbuf[0], checkbuf[1],
561 RESUME_CHECK_SIZE) == 0) {
562 if (opt.verbose) {
563 printf("Current local and remote file "
564 "appear to be the same. "
565 "Starting download from "
566 "offset %jd\n",
567 (intmax_t)offset_download);
569 } else {
570 fprintf(stderr, "Local and remote file appear "
571 "to be different, not "
572 "doing resume for %s\n",
573 path);
574 smbc_close(remotehandle);
575 close(localhandle);
576 return false;
579 } else {
580 localhandle = STDOUT_FILENO;
581 start_offset = 0;
582 offset_download = 0;
583 offset_check = 0;
586 readbuf = (char *)SMB_MALLOC(opt.blocksize);
587 if (!readbuf) {
588 if (localhandle != STDOUT_FILENO) {
589 close(localhandle);
591 return false;
594 /* Now, download all bytes from offset_download to the end */
595 for (curpos = offset_download; curpos < remotestat.st_size;
596 curpos += opt.blocksize) {
597 ssize_t bytesread = smbc_read(remotehandle,
598 readbuf,
599 opt.blocksize);
600 if(bytesread < 0) {
601 fprintf(stderr,
602 "Can't read %zu bytes at offset %jd, file %s\n",
603 opt.blocksize, (intmax_t)curpos, path);
604 smbc_close(remotehandle);
605 if (localhandle != STDOUT_FILENO) {
606 close(localhandle);
608 free(readbuf);
609 return false;
612 total_bytes += bytesread;
614 if(write(localhandle, readbuf, bytesread) < 0) {
615 fprintf(stderr,
616 "Can't write %zd bytes to local file %s at "
617 "offset %jd\n", bytesread, path,
618 (intmax_t)curpos);
619 free(readbuf);
620 smbc_close(remotehandle);
621 if (localhandle != STDOUT_FILENO) {
622 close(localhandle);
624 return false;
627 if (opt.dots) {
628 fputc('.', stderr);
629 } else if (!opt.quiet) {
630 print_progress(newpath, start_time, time_mono(NULL),
631 start_offset, curpos,
632 remotestat.st_size);
636 free(readbuf);
638 if (opt.dots) {
639 fputc('\n', stderr);
640 printf("%s downloaded\n", path);
641 } else if (!opt.quiet) {
642 int i;
643 fprintf(stderr, "\r%s", path);
644 if (columns) {
645 for (i = strlen(path); i < columns; i++) {
646 fputc(' ', stderr);
649 fputc('\n', stderr);
652 if (opt.keep_permissions && !opt.send_stdout) {
653 if (fchmod(localhandle, remotestat.st_mode) < 0) {
654 fprintf(stderr, "Unable to change mode of local "
655 "file %s to %o\n",
656 path, (unsigned int)remotestat.st_mode);
657 smbc_close(remotehandle);
658 close(localhandle);
659 return false;
663 smbc_close(remotehandle);
664 if (localhandle != STDOUT_FILENO) {
665 close(localhandle);
667 return true;
670 static void clean_exit(void)
672 char bs[100];
673 human_readable(total_bytes, bs, sizeof(bs));
674 if (!opt.quiet) {
675 fprintf(stderr, "Downloaded %s in %lu seconds\n", bs,
676 (unsigned long)(time_mono(NULL) - total_start_time));
678 exit(0);
681 static void signal_quit(int v)
683 clean_exit();
686 static int readrcfile(const char *name, const struct poptOption long_options[])
688 FILE *fd = fopen(name, "r");
689 int lineno = 0, i;
690 char var[101], val[101];
691 bool found;
692 int *intdata;
693 char **stringdata;
694 if (!fd) {
695 fprintf(stderr, "Can't open RC file %s\n", name);
696 return 1;
699 while (!feof(fd)) {
700 lineno++;
701 if (fscanf(fd, "%100s %100s\n", var, val) < 2) {
702 fprintf(stderr,
703 "Can't parse line %d of %s, ignoring.\n",
704 lineno, name);
705 continue;
708 found = false;
710 for (i = 0; long_options[i].argInfo; i++) {
711 if (!long_options[i].longName) {
712 continue;
714 if (strcmp(long_options[i].longName, var)) {
715 continue;
717 if (!long_options[i].arg) {
718 continue;
721 switch (long_options[i].argInfo) {
722 case POPT_ARG_NONE:
723 intdata = (int *)long_options[i].arg;
724 if (!strcmp(val, "on")) {
725 *intdata = 1;
726 } else if (!strcmp(val, "off")) {
727 *intdata = 0;
728 } else {
729 fprintf(stderr, "Illegal value %s for "
730 "%s at line %d in %s\n",
731 val, var, lineno, name);
733 break;
734 case POPT_ARG_INT:
735 intdata = (int *)long_options[i].arg;
736 *intdata = atoi(val);
737 break;
738 case POPT_ARG_STRING:
739 stringdata = (char **)long_options[i].arg;
740 *stringdata = SMB_STRDUP(val);
741 break;
742 default:
743 fprintf(stderr, "Invalid variable %s at "
744 "line %d in %s\n",
745 var, lineno, name);
746 break;
749 found = true;
751 if (!found) {
752 fprintf(stderr,
753 "Invalid variable %s at line %d in %s\n", var,
754 lineno, name);
758 fclose(fd);
759 return 0;
762 int main(int argc, char **argv)
764 int c = 0;
765 const char *file = NULL;
766 char *rcfile = NULL;
767 bool smb_encrypt = false;
768 int resume = 0, recursive = 0;
769 TALLOC_CTX *frame = talloc_stackframe();
770 bool ret = true;
771 char *p;
772 const char **argv_const = discard_const_p(const char *, argv);
773 struct poptOption long_options[] = {
774 POPT_AUTOHELP
776 {"workgroup", 'w', POPT_ARG_STRING, &opt.workgroup, 'w', "Workgroup to use (optional)" },
777 {"user", 'U', POPT_ARG_STRING, &opt.username, 'U', "Username to use" },
778 {"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" },
780 {"nonprompt", 'n', POPT_ARG_NONE, &opt.nonprompt, 'n', "Don't ask anything (non-interactive)" },
781 {"debuglevel", 'd', POPT_ARG_INT, &opt.debuglevel, 'd', "Debuglevel to use" },
783 {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport" },
784 {"resume", 'r', POPT_ARG_NONE, &resume, 0, "Automatically resume aborted files" },
785 {"update", 'u', POPT_ARG_NONE, &opt.update, 0, "Download only when remote file is newer than local file or local file is missing"},
786 {"recursive", 'R', POPT_ARG_NONE, &recursive, 0, "Recursively download files" },
787 {"keep-permissions", 'P', POPT_ARG_NONE, &opt.keep_permissions, 'P', "Keep permissions" },
788 {"blocksize", 'b', POPT_ARG_INT, &opt.blocksize, 'b', "Change number of bytes in a block"},
790 {"outputfile", 'o', POPT_ARG_STRING, &opt.outputfile, 'o', "Write downloaded data to specified file" },
791 {"stdout", 'O', POPT_ARG_NONE, &opt.send_stdout, 'O', "Write data to stdout" },
792 {"dots", 'D', POPT_ARG_NONE, &opt.dots, 'D', "Show dots as progress indication" },
793 {"quiet", 'q', POPT_ARG_NONE, &opt.quiet, 'q', "Be quiet" },
794 {"verbose", 'v', POPT_ARG_NONE, &opt.verbose, 'v', "Be verbose" },
795 {"rcfile", 'f', POPT_ARG_STRING, NULL, 'f', "Use specified rc file"},
797 POPT_TABLEEND
799 poptContext pc;
801 smb_init_locale();
803 /* only read rcfile if it exists */
804 if (asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME")) == -1) {
805 return 1;
807 if (access(rcfile, F_OK) == 0) {
808 readrcfile(rcfile, long_options);
810 free(rcfile);
812 #ifdef SIGWINCH
813 signal(SIGWINCH, change_columns);
814 #endif
815 signal(SIGINT, signal_quit);
816 signal(SIGTERM, signal_quit);
818 pc = poptGetContext(argv[0], argc, argv_const, long_options, 0);
820 while ((c = poptGetNextOpt(pc)) >= 0) {
821 switch (c) {
822 case 'f':
823 readrcfile(poptGetOptArg(pc), long_options);
824 break;
825 case 'a':
826 opt.username_specified = true;
827 opt.username = talloc_strdup(frame, "");
828 opt.password_specified = true;
829 opt.password = talloc_strdup(frame, "");
830 break;
831 case 'e':
832 smb_encrypt = true;
833 break;
834 case 'U':
835 opt.username_specified = true;
836 opt.username = talloc_strdup(frame, opt.username);
837 p = strchr(opt.username,'%');
838 if (p != NULL) {
839 *p = '\0';
840 opt.password = p + 1;
841 opt.password_specified = true;
843 break;
847 if ((opt.send_stdout || resume || opt.outputfile) && opt.update) {
848 fprintf(stderr, "The -o, -R or -O and -U options can not be "
849 "used together.\n");
850 return 1;
852 if ((opt.send_stdout || opt.outputfile) && recursive) {
853 fprintf(stderr, "The -o or -O and -R options can not be "
854 "used together.\n");
855 return 1;
858 if (opt.outputfile && opt.send_stdout) {
859 fprintf(stderr, "The -o and -O options can not be "
860 "used together.\n");
861 return 1;
864 popt_burn_cmdline_password(argc, argv);
866 if (smbc_init(get_auth_data, opt.debuglevel) < 0) {
867 fprintf(stderr, "Unable to initialize libsmbclient\n");
868 return 1;
871 if (smb_encrypt) {
872 SMBCCTX *smb_ctx = smbc_set_context(NULL);
873 smbc_option_set(smb_ctx,
874 discard_const_p(char, "smb_encrypt_level"),
875 "require");
878 columns = get_num_cols();
880 total_start_time = time_mono(NULL);
882 while ((file = poptGetArg(pc))) {
883 if (!recursive) {
884 ret = smb_download_file(file, "", recursive, resume,
885 true, opt.outputfile);
886 } else {
887 ret = smb_download_dir(file, "", resume);
891 TALLOC_FREE(frame);
892 if (ret) {
893 clean_exit();
895 return ret?0:1;