s3:utils/smbget make use of bool for flags
[sfrench/samba-autobuild/.git] / source3 / utils / smbget.c
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>
5
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.
10
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.
15
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/>.  */
18
19 #include "includes.h"
20 #include "system/filesys.h"
21 #include "popt_common.h"
22 #include "libsmbclient.h"
23
24 static int columns = 0;
25
26 static int debuglevel;
27 static char *outputfile;
28
29
30 static time_t total_start_time = 0;
31 static off_t total_bytes = 0;
32
33 #define SMB_MAXPATHLEN MAXPATHLEN
34
35 /*
36  * Number of bytes to read when checking whether local and remote file
37  * are really the same file
38  */
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
44
45 static const char *username = NULL, *password = NULL, *workgroup = NULL;
46 static bool nonprompt = false, quiet = false, dots = false,
47             keep_permissions = false, verbose = false, send_stdout = false,
48             update = false;
49 static unsigned int blocksize = SMB_DEFAULT_BLOCKSIZE;
50
51 static bool smb_download_file(const char *base, const char *name,
52                               bool recursive, bool resume, bool toplevel,
53                               char *outfile);
54
55 static int get_num_cols(void)
56 {
57 #ifdef TIOCGWINSZ
58         struct winsize ws;
59         if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
60                 return 0;
61         }
62         return ws.ws_col;
63 #else
64 #warning No support for TIOCGWINSZ
65         char *cols = getenv("COLUMNS");
66         if (!cols) {
67                 return 0;
68         }
69         return atoi(cols);
70 #endif
71 }
72
73 static void change_columns(int sig)
74 {
75         columns = get_num_cols();
76 }
77
78 static void human_readable(off_t s, char *buffer, int l)
79 {
80         if (s > 1024 * 1024 * 1024) {
81                 snprintf(buffer, l, "%.2fGB", 1.0 * s / (1024 * 1024 * 1024));
82         } else if (s > 1024 * 1024) {
83                 snprintf(buffer, l, "%.2fMB", 1.0 * s / (1024 * 1024));
84         } else if (s > 1024) {
85                 snprintf(buffer, l, "%.2fkB", 1.0 * s / 1024);
86         } else {
87                 snprintf(buffer, l, "%jdb", (intmax_t)s);
88         }
89 }
90
91 static void get_auth_data(const char *srv, const char *shr, char *wg, int wglen,
92                           char *un, int unlen, char *pw, int pwlen)
93 {
94         static bool hasasked = false;
95         static char *savedwg;
96         static char *savedun;
97         static char *savedpw;
98         char tmp[128];
99
100         if (hasasked) {
101                 strncpy(wg, savedwg, wglen - 1);
102                 strncpy(un, savedun, unlen - 1);
103                 strncpy(pw, savedpw, pwlen - 1);
104                 return;
105         }
106         hasasked = true;
107
108         if (!nonprompt && !username) {
109                 printf("Username for %s at %s [guest] ", shr, srv);
110                 if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
111                         return;
112                 }
113                 if ((strlen(tmp) > 0) && (tmp[strlen(tmp) - 1] == '\n')) {
114                         tmp[strlen(tmp) - 1] = '\0';
115                 }
116                 strncpy(un, tmp, unlen - 1);
117         } else if (username) {
118                 strncpy(un, username, unlen - 1);
119         }
120
121         if (!nonprompt && !password) {
122                 char *prompt;
123                 if (asprintf(&prompt, "Password for %s at %s: ", shr, srv) ==
124                     -1) {
125                         return;
126                 }
127                 (void)samba_getpass(prompt, pw, pwlen, false, false);
128                 free(prompt);
129         } else if (password) {
130                 strncpy(pw, password, pwlen - 1);
131         }
132
133         if (workgroup) {
134                 strncpy(wg, workgroup, wglen - 1);
135         }
136
137         /* save the values found for later */
138         savedwg = SMB_STRDUP(wg);
139         savedun = SMB_STRDUP(un);
140         savedpw = SMB_STRDUP(pw);
141
142         if (!quiet) {
143                 char *wgtmp, *usertmp;
144                 wgtmp = SMB_STRNDUP(wg, wglen);
145                 usertmp = SMB_STRNDUP(un, unlen);
146                 printf("Using workgroup %s, %s%s\n", wgtmp,
147                        *usertmp ? "user " : "guest user", usertmp);
148                 free(wgtmp);
149                 free(usertmp);
150         }
151 }
152
153 /* Return 1 on error, 0 on success. */
154
155 static int smb_download_dir(const char *base, const char *name, int resume)
156 {
157         char path[SMB_MAXPATHLEN];
158         int dirhandle;
159         struct smbc_dirent *dirent;
160         const char *relname = name;
161         char *tmpname;
162         struct stat remotestat;
163         int ret = 0;
164
165         snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
166                  (base[0] && name[0] && name[0] != '/' &&
167                   base[strlen(base)-1] != '/') ? "/" : "",
168                  name);
169
170         /* List files in directory and call smb_download_file on them */
171         dirhandle = smbc_opendir(path);
172         if (dirhandle < 1) {
173                 if (errno == ENOTDIR) {
174                         return smb_download_file(base, name, true, resume,
175                                                  false, NULL);
176                 }
177                 fprintf(stderr, "Can't open directory %s: %s\n", path,
178                         strerror(errno));
179                 return 1;
180         }
181
182         while (*relname == '/') {
183                 relname++;
184         }
185         mkdir(relname, 0755);
186
187         tmpname = SMB_STRDUP(name);
188
189         while ((dirent = smbc_readdir(dirhandle))) {
190                 char *newname;
191                 if (!strcmp(dirent->name, ".") || !strcmp(dirent->name, "..")) {
192                         continue;
193                 }
194                 if (asprintf(&newname, "%s/%s", tmpname, dirent->name) == -1) {
195                         free(tmpname);
196                         return 1;
197                 }
198                 switch (dirent->smbc_type) {
199                 case SMBC_DIR:
200                         ret = smb_download_dir(base, newname, resume);
201                         break;
202
203                 case SMBC_WORKGROUP:
204                         ret = smb_download_dir("smb://", dirent->name, resume);
205                         break;
206
207                 case SMBC_SERVER:
208                         ret = smb_download_dir("smb://", dirent->name, resume);
209                         break;
210
211                 case SMBC_FILE:
212                         ret = smb_download_file(base, newname, true, resume,
213                                                 false, NULL);
214                         break;
215
216                 case SMBC_FILE_SHARE:
217                         ret = smb_download_dir(base, newname, resume);
218                         break;
219
220                 case SMBC_PRINTER_SHARE:
221                         if (!quiet) {
222                                 printf("Ignoring printer share %s\n",
223                                        dirent->name);
224                         }
225                         break;
226
227                 case SMBC_COMMS_SHARE:
228                         if (!quiet) {
229                                 printf("Ignoring comms share %s\n",
230                                        dirent->name);
231                         }
232                         break;
233
234                 case SMBC_IPC_SHARE:
235                         if (!quiet) {
236                                 printf("Ignoring ipc$ share %s\n",
237                                        dirent->name);
238                         }
239                         break;
240
241                 default:
242                         fprintf(stderr, "Ignoring file '%s' of type '%d'\n",
243                                 newname, dirent->smbc_type);
244                         break;
245                 }
246                 free(newname);
247         }
248         free(tmpname);
249
250         if (keep_permissions) {
251                 if (smbc_fstat(dirhandle, &remotestat) < 0) {
252                         fprintf(stderr,
253                                 "Unable to get stats on %s on remote server\n",
254                                 path);
255                         smbc_closedir(dirhandle);
256                         return 1;
257                 }
258
259                 if (chmod(relname, remotestat.st_mode) < 0) {
260                         fprintf(stderr,
261                                 "Unable to change mode of local dir %s to %o\n",
262                                 relname, (unsigned int)remotestat.st_mode);
263                         smbc_closedir(dirhandle);
264                         return 1;
265                 }
266         }
267
268         smbc_closedir(dirhandle);
269         return ret;
270 }
271
272 static char *print_time(long t)
273 {
274         static char buffer[100];
275         int secs, mins, hours;
276         if (t < -1) {
277                 strncpy(buffer, "Unknown", sizeof(buffer));
278                 return buffer;
279         }
280
281         secs = (int)t % 60;
282         mins = (int)t / 60 % 60;
283         hours = (int)t / (60 * 60);
284         snprintf(buffer, sizeof(buffer) - 1, "%02d:%02d:%02d", hours, mins,
285                  secs);
286         return buffer;
287 }
288
289 static void print_progress(const char *name, time_t start, time_t now,
290                            off_t start_pos, off_t pos, off_t total)
291 {
292         double avg = 0.0;
293         long eta = -1;
294         double prcnt = 0.0;
295         char hpos[20], htotal[20], havg[20];
296         char *status, *filename;
297         int len;
298         if (now - start) {
299                 avg = 1.0 * (pos - start_pos) / (now - start);
300         }
301         eta = (total - pos) / avg;
302         if (total) {
303                 prcnt = 100.0 * pos / total;
304         }
305
306         human_readable(pos, hpos, sizeof(hpos));
307         human_readable(total, htotal, sizeof(htotal));
308         human_readable(avg, havg, sizeof(havg));
309
310         len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos,
311                        htotal, prcnt, havg, print_time(eta));
312         if (len == -1) {
313                 return;
314         }
315
316         if (columns) {
317                 int required = strlen(name),
318                     available = columns - len - strlen("[] ");
319                 if (required > available) {
320                         if (asprintf(&filename, "...%s",
321                                      name + required - available + 3) == -1) {
322                                 return;
323                         }
324                 } else {
325                         filename = SMB_STRNDUP(name, available);
326                 }
327         } else {
328                 filename = SMB_STRDUP(name);
329         }
330
331         fprintf(stderr, "\r[%s] %s", filename, status);
332
333         free(filename);
334         free(status);
335 }
336
337 /* Return false on error, true on success. */
338
339 static bool smb_download_file(const char *base, const char *name,
340                               bool recursive, bool resume, bool toplevel,
341                               char *outfile)
342 {
343         int remotehandle, localhandle;
344         time_t start_time = time_mono(NULL);
345         const char *newpath;
346         char path[SMB_MAXPATHLEN];
347         char checkbuf[2][RESUME_CHECK_SIZE];
348         char *readbuf = NULL;
349         off_t offset_download = 0, offset_check = 0, curpos = 0,
350               start_offset = 0;
351         struct stat localstat, remotestat;
352
353         snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
354                  (*base && *name && name[0] != '/' &&
355                   base[strlen(base)-1] != '/') ? "/" : "",
356                  name);
357
358         remotehandle = smbc_open(path, O_RDONLY, 0755);
359
360         if (remotehandle < 0) {
361                 switch (errno) {
362                 case EISDIR:
363                         if (!recursive) {
364                                 fprintf(stderr,
365                                         "%s is a directory. Specify -R "
366                                         "to download recursively\n",
367                                         path);
368                                 return false;
369                         }
370                         return smb_download_dir(base, name, resume);
371
372                 case ENOENT:
373                         fprintf(stderr,
374                                 "%s can't be found on the remote server\n",
375                                 path);
376                         return false;
377
378                 case ENOMEM:
379                         fprintf(stderr, "Not enough memory\n");
380                         return false;
381
382                 case ENODEV:
383                         fprintf(stderr,
384                                 "The share name used in %s does not exist\n",
385                                 path);
386                         return false;
387
388                 case EACCES:
389                         fprintf(stderr, "You don't have enough permissions "
390                                 "to access %s\n",
391                                 path);
392                         return false;
393
394                 default:
395                         perror("smbc_open");
396                         return false;
397                 }
398         }
399
400         if (smbc_fstat(remotehandle, &remotestat) < 0) {
401                 fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
402                 return false;
403         }
404
405         if (outfile) {
406                 newpath = outfile;
407         } else if (!name[0]) {
408                 newpath = strrchr(base, '/');
409                 if (newpath) {
410                         newpath++;
411                 } else {
412                         newpath = base;
413                 }
414         } else {
415                 newpath = name;
416         }
417
418         if (!toplevel && (newpath[0] == '/')) {
419                 newpath++;
420         }
421
422         /* Open local file according to the mode */
423         if (update) {
424                 /* if it is up-to-date, skip */
425                 if (stat(newpath, &localstat) == 0 &&
426                     localstat.st_mtime >= remotestat.st_mtime) {
427                         if (verbose) {
428                                 printf("%s is up-to-date, skipping\n", newpath);
429                         }
430                         smbc_close(remotehandle);
431                         return true;
432                 }
433                 /* else open it for writing and truncate if it exists */
434                 localhandle = open(
435                     newpath, O_CREAT | O_NONBLOCK | O_RDWR | O_TRUNC, 0775);
436                 if (localhandle < 0) {
437                         fprintf(stderr, "Can't open %s : %s\n", newpath,
438                                 strerror(errno));
439                         smbc_close(remotehandle);
440                         return false;
441                 }
442                 /* no offset */
443         } else if (!send_stdout) {
444                 localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR |
445                                                 (!resume ? O_EXCL : 0),
446                                    0755);
447                 if (localhandle < 0) {
448                         fprintf(stderr, "Can't open %s: %s\n", newpath,
449                                 strerror(errno));
450                         smbc_close(remotehandle);
451                         return false;
452                 }
453
454                 if (fstat(localhandle, &localstat) != 0) {
455                         fprintf(stderr, "Can't fstat %s: %s\n", newpath,
456                                 strerror(errno));
457                         smbc_close(remotehandle);
458                         close(localhandle);
459                         return false;
460                 }
461
462                 start_offset = localstat.st_size;
463
464                 if (localstat.st_size &&
465                     localstat.st_size == remotestat.st_size) {
466                         if (verbose) {
467                                 fprintf(stderr, "%s is already downloaded "
468                                         "completely.\n",
469                                         path);
470                         } else if (!quiet) {
471                                 fprintf(stderr, "%s\n", path);
472                         }
473                         smbc_close(remotehandle);
474                         close(localhandle);
475                         return true;
476                 }
477
478                 if (localstat.st_size > RESUME_CHECK_OFFSET &&
479                     remotestat.st_size > RESUME_CHECK_OFFSET) {
480                         offset_download =
481                             localstat.st_size - RESUME_DOWNLOAD_OFFSET;
482                         offset_check = localstat.st_size - RESUME_CHECK_OFFSET;
483                         if (verbose) {
484                                 printf("Trying to start resume of %s at %jd\n"
485                                        "At the moment %jd of %jd bytes have "
486                                        "been retrieved\n",
487                                        newpath, (intmax_t)offset_check,
488                                        (intmax_t)localstat.st_size,
489                                        (intmax_t)remotestat.st_size);
490                         }
491                 }
492
493                 if (offset_check) {
494                         off_t off1, off2;
495                         /* First, check all bytes from offset_check to
496                          * offset_download */
497                         off1 = lseek(localhandle, offset_check, SEEK_SET);
498                         if (off1 < 0) {
499                                 fprintf(stderr,
500                                         "Can't seek to %jd in local file %s\n",
501                                         (intmax_t)offset_check, newpath);
502                                 smbc_close(remotehandle);
503                                 close(localhandle);
504                                 return false;
505                         }
506
507                         off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET);
508                         if (off2 < 0) {
509                                 fprintf(stderr,
510                                         "Can't seek to %jd in remote file %s\n",
511                                         (intmax_t)offset_check, newpath);
512                                 smbc_close(remotehandle);
513                                 close(localhandle);
514                                 return false;
515                         }
516
517                         if (off1 != off2) {
518                                 fprintf(stderr, "Offset in local and remote "
519                                         "files are different "
520                                         "(local: %jd, remote: %jd)\n",
521                                         (intmax_t)off1, (intmax_t)off2);
522                                 smbc_close(remotehandle);
523                                 close(localhandle);
524                                 return false;
525                         }
526
527                         if (smbc_read(remotehandle, checkbuf[0],
528                                       RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
529                                 fprintf(stderr, "Can't read %d bytes from "
530                                         "remote file %s\n",
531                                         RESUME_CHECK_SIZE, path);
532                                 smbc_close(remotehandle);
533                                 close(localhandle);
534                                 return false;
535                         }
536
537                         if (read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) !=
538                             RESUME_CHECK_SIZE) {
539                                 fprintf(stderr, "Can't read %d bytes from "
540                                         "local file %s\n",
541                                         RESUME_CHECK_SIZE, name);
542                                 smbc_close(remotehandle);
543                                 close(localhandle);
544                                 return false;
545                         }
546
547                         if (memcmp(checkbuf[0], checkbuf[1],
548                                    RESUME_CHECK_SIZE) == 0) {
549                                 if (verbose) {
550                                         printf("Current local and remote file "
551                                                "appear to be the same. "
552                                                "Starting download from "
553                                                "offset %jd\n",
554                                                (intmax_t)offset_download);
555                                 }
556                         } else {
557                                 fprintf(stderr, "Local and remote file appear "
558                                         "to be different, not "
559                                         "doing resume for %s\n",
560                                         path);
561                                 smbc_close(remotehandle);
562                                 close(localhandle);
563                                 return false;
564                         }
565                 }
566         } else {
567                 localhandle = STDOUT_FILENO;
568                 start_offset = 0;
569                 offset_download = 0;
570                 offset_check = 0;
571         }
572
573         readbuf = (char *)SMB_MALLOC(blocksize);
574         if (!readbuf) {
575                 if (localhandle != STDOUT_FILENO) {
576                         close(localhandle);
577                 }
578                 return false;
579         }
580
581         /* Now, download all bytes from offset_download to the end */
582         for (curpos = offset_download; curpos < remotestat.st_size;
583              curpos += blocksize) {
584                 ssize_t bytesread = smbc_read(remotehandle, readbuf, blocksize);
585                 if(bytesread < 0) {
586                         fprintf(stderr,
587                                 "Can't read %u bytes at offset %jd, file %s\n",
588                                 blocksize, (intmax_t)curpos, path);
589                         smbc_close(remotehandle);
590                         if (localhandle != STDOUT_FILENO) {
591                                 close(localhandle);
592                         }
593                         free(readbuf);
594                         return false;
595                 }
596
597                 total_bytes += bytesread;
598
599                 if(write(localhandle, readbuf, bytesread) < 0) {
600                         fprintf(stderr,
601                                 "Can't write %zd bytes to local file %s at "
602                                 "offset %jd\n", bytesread, path,
603                                 (intmax_t)curpos);
604                         free(readbuf);
605                         smbc_close(remotehandle);
606                         if (localhandle != STDOUT_FILENO) {
607                                 close(localhandle);
608                         }
609                         return false;
610                 }
611
612                 if (dots) {
613                         fputc('.', stderr);
614                 } else if (!quiet) {
615                         print_progress(newpath, start_time, time_mono(NULL),
616                                        start_offset, curpos,
617                                        remotestat.st_size);
618                 }
619         }
620
621         free(readbuf);
622
623         if (dots) {
624                 fputc('\n', stderr);
625                 printf("%s downloaded\n", path);
626         } else if (!quiet) {
627                 int i;
628                 fprintf(stderr, "\r%s", path);
629                 if (columns) {
630                         for (i = strlen(path); i < columns; i++) {
631                                 fputc(' ', stderr);
632                         }
633                 }
634                 fputc('\n', stderr);
635         }
636
637         if (keep_permissions && !send_stdout) {
638                 if (fchmod(localhandle, remotestat.st_mode) < 0) {
639                         fprintf(stderr, "Unable to change mode of local "
640                                 "file %s to %o\n",
641                                 path, (unsigned int)remotestat.st_mode);
642                         smbc_close(remotehandle);
643                         close(localhandle);
644                         return false;
645                 }
646         }
647
648         smbc_close(remotehandle);
649         if (localhandle != STDOUT_FILENO) {
650                 close(localhandle);
651         }
652         return true;
653 }
654
655 static void clean_exit(void)
656 {
657         char bs[100];
658         human_readable(total_bytes, bs, sizeof(bs));
659         if (!quiet) {
660                 fprintf(stderr, "Downloaded %s in %lu seconds\n", bs,
661                         (unsigned long)(time_mono(NULL) - total_start_time));
662         }
663         exit(0);
664 }
665
666 static void signal_quit(int v)
667 {
668         clean_exit();
669 }
670
671 static int readrcfile(const char *name, const struct poptOption long_options[])
672 {
673         FILE *fd = fopen(name, "r");
674         int lineno = 0, i;
675         char var[101], val[101];
676         bool found;
677         int *intdata;
678         char **stringdata;
679         if (!fd) {
680                 fprintf(stderr, "Can't open RC file %s\n", name);
681                 return 1;
682         }
683
684         while (!feof(fd)) {
685                 lineno++;
686                 if (fscanf(fd, "%100s %100s\n", var, val) < 2) {
687                         fprintf(stderr,
688                                 "Can't parse line %d of %s, ignoring.\n",
689                                 lineno, name);
690                         continue;
691                 }
692
693                 found = false;
694
695                 for (i = 0; long_options[i].shortName; i++) {
696                         if (!long_options[i].longName) {
697                                 continue;
698                         }
699                         if (strcmp(long_options[i].longName, var)) {
700                                 continue;
701                         }
702                         if (!long_options[i].arg) {
703                                 continue;
704                         }
705
706                         switch (long_options[i].argInfo) {
707                         case POPT_ARG_NONE:
708                                 intdata = (int *)long_options[i].arg;
709                                 if (!strcmp(val, "on")) {
710                                         *intdata = 1;
711                                 } else if (!strcmp(val, "off")) {
712                                         *intdata = 0;
713                                 } else {
714                                         fprintf(stderr, "Illegal value %s for "
715                                                 "%s at line %d in %s\n",
716                                                 val, var, lineno, name);
717                                 }
718                                 break;
719                         case POPT_ARG_INT:
720                                 intdata = (int *)long_options[i].arg;
721                                 *intdata = atoi(val);
722                                 break;
723                         case POPT_ARG_STRING:
724                                 stringdata = (char **)long_options[i].arg;
725                                 *stringdata = SMB_STRDUP(val);
726                                 break;
727                         default:
728                                 fprintf(stderr, "Invalid variable %s at "
729                                         "line %d in %s\n",
730                                         var, lineno, name);
731                                 break;
732                         }
733
734                         found = true;
735                 }
736                 if (!found) {
737                         fprintf(stderr,
738                                 "Invalid variable %s at line %d in %s\n", var,
739                                 lineno, name);
740                 }
741         }
742
743         fclose(fd);
744         return 0;
745 }
746
747 int main(int argc, const char **argv)
748 {
749         int c = 0;
750         const char *file = NULL;
751         char *rcfile = NULL;
752         bool smb_encrypt = false;
753         int resume = 0, recursive = 0;
754         TALLOC_CTX *frame = talloc_stackframe();
755         bool ret = true;
756         struct poptOption long_options[] = {
757                 {"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" },
758                 {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport" },
759                 {"resume", 'r', POPT_ARG_NONE, &resume, 0, "Automatically resume aborted files" },
760                 {"update", 'U',  POPT_ARG_NONE, &update, 0, "Download only when remote file is newer than local file or local file is missing"},
761                 {"recursive", 'R',  POPT_ARG_NONE, &recursive, 0, "Recursively download files" },
762                 {"username", 'u', POPT_ARG_STRING, &username, 'u', "Username to use" },
763                 {"password", 'p', POPT_ARG_STRING, &password, 'p', "Password to use" },
764                 {"workgroup", 'w', POPT_ARG_STRING, &workgroup, 'w', "Workgroup to use (optional)" },
765                 {"nonprompt", 'n', POPT_ARG_NONE, &nonprompt, 'n', "Don't ask anything (non-interactive)" },
766                 {"debuglevel", 'd', POPT_ARG_INT, &debuglevel, 'd', "Debuglevel to use" },
767                 {"outputfile", 'o', POPT_ARG_STRING, &outputfile, 'o', "Write downloaded data to specified file" },
768                 {"stdout", 'O', POPT_ARG_NONE, &send_stdout, 'O', "Write data to stdout" },
769                 {"dots", 'D', POPT_ARG_NONE, &dots, 'D', "Show dots as progress indication" },
770                 {"quiet", 'q', POPT_ARG_NONE, &quiet, 'q', "Be quiet" },
771                 {"verbose", 'v', POPT_ARG_NONE, &verbose, 'v', "Be verbose" },
772                 {"keep-permissions", 'P', POPT_ARG_NONE, &keep_permissions, 'P', "Keep permissions" },
773                 {"blocksize", 'b', POPT_ARG_INT, &blocksize, 'b', "Change number of bytes in a block"},
774                 {"rcfile", 'f', POPT_ARG_STRING, NULL, 'f', "Use specified rc file"},
775                 POPT_AUTOHELP
776                 POPT_TABLEEND
777         };
778         poptContext pc;
779
780         smb_init_locale();
781
782         /* only read rcfile if it exists */
783         if (asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME")) == -1) {
784                 return 1;
785         }
786         if (access(rcfile, F_OK) == 0) {
787                 readrcfile(rcfile, long_options);
788         }
789         free(rcfile);
790
791 #ifdef SIGWINCH
792         signal(SIGWINCH, change_columns);
793 #endif
794         signal(SIGINT, signal_quit);
795         signal(SIGTERM, signal_quit);
796
797         pc = poptGetContext(argv[0], argc, argv, long_options, 0);
798
799         while ((c = poptGetNextOpt(pc)) >= 0) {
800                 switch (c) {
801                 case 'f':
802                         readrcfile(poptGetOptArg(pc), long_options);
803                         break;
804                 case 'a':
805                         username = "";
806                         password = "";
807                         break;
808                 case 'e':
809                         smb_encrypt = true;
810                         break;
811                 }
812         }
813
814         if ((send_stdout || resume || outputfile) && update) {
815                 fprintf(stderr, "The -o, -R or -O and -U options can not be "
816                         "used together.\n");
817                 return 1;
818         }
819         if ((send_stdout || outputfile) && recursive) {
820                 fprintf(stderr, "The -o or -O and -R options can not be "
821                         "used together.\n");
822                 return 1;
823         }
824
825         if (outputfile && send_stdout) {
826                 fprintf(stderr, "The -o and -O options can not be "
827                         "used together.\n");
828                 return 1;
829         }
830
831         if (smbc_init(get_auth_data, debuglevel) < 0) {
832                 fprintf(stderr, "Unable to initialize libsmbclient\n");
833                 return 1;
834         }
835
836         if (smb_encrypt) {
837                 SMBCCTX *smb_ctx = smbc_set_context(NULL);
838                 smbc_option_set(smb_ctx,
839                                 discard_const_p(char, "smb_encrypt_level"),
840                                 "require");
841         }
842
843         columns = get_num_cols();
844
845         total_start_time = time_mono(NULL);
846
847         while ((file = poptGetArg(pc))) {
848                 if (!recursive) {
849                         ret = smb_download_file(file, "", recursive, resume,
850                                                 true, outputfile);
851                 } else {
852                         ret = smb_download_dir(file, "", resume);
853                 }
854         }
855
856         TALLOC_FREE(frame);
857         if (ret) {
858                 clean_exit();
859         }
860         return ret?0:1;
861 }