modules: Add dependency on tirpc to vfs_nfs4acl_xattr
[samba.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_cmdline.h"
22 #include "libsmbclient.h"
23 #include "cmdline_contexts.h"
24
25 static int columns = 0;
26
27 static time_t total_start_time = 0;
28 static off_t total_bytes = 0;
29
30 #define SMB_MAXPATHLEN MAXPATHLEN
31
32 /*
33  * Number of bytes to read when checking whether local and remote file
34  * are really the same file
35  */
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
41
42 struct opt {
43         char *workgroup;
44         bool username_specified;
45         char *username;
46         bool password_specified;
47         char *password;
48
49         char *outputfile;
50         size_t blocksize;
51
52         bool nonprompt;
53         bool quiet;
54         bool dots;
55         bool verbose;
56         bool send_stdout;
57         bool update;
58         int debuglevel;
59 };
60 static struct opt opt = { .blocksize = SMB_DEFAULT_BLOCKSIZE };
61
62 static bool smb_download_file(const char *base, const char *name,
63                               bool recursive, bool resume, bool toplevel,
64                               char *outfile);
65
66 static int get_num_cols(void)
67 {
68 #ifdef TIOCGWINSZ
69         struct winsize ws;
70         if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
71                 return 0;
72         }
73         return ws.ws_col;
74 #else
75 #warning No support for TIOCGWINSZ
76         char *cols = getenv("COLUMNS");
77         if (!cols) {
78                 return 0;
79         }
80         return atoi(cols);
81 #endif
82 }
83
84 static void change_columns(int sig)
85 {
86         columns = get_num_cols();
87 }
88
89 static void human_readable(off_t s, char *buffer, int l)
90 {
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);
99         }
100 }
101
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)
104 {
105         static bool hasasked = false;
106         static char *savedwg;
107         static char *savedun;
108         static char *savedpw;
109
110         if (hasasked) {
111                 strncpy(wg, savedwg, wglen - 1);
112                 strncpy(un, savedun, unlen - 1);
113                 strncpy(pw, savedpw, pwlen - 1);
114                 return;
115         }
116         hasasked = true;
117
118         /*
119          * If no user has been specified un is initialized with the current
120          * username of the user who started smbget.
121          */
122         if (opt.username_specified) {
123                 strncpy(un, opt.username, unlen - 1);
124         }
125
126         if (!opt.nonprompt && !opt.password_specified && pw[0] == '\0') {
127                 char *prompt;
128                 int rc;
129
130                 rc = asprintf(&prompt,
131                               "Password for [%s] connecting to //%s/%s: ",
132                               un, shr, srv);
133                 if (rc == -1) {
134                         return;
135                 }
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);
140         }
141
142         if (opt.workgroup != NULL) {
143                 strncpy(wg, opt.workgroup, wglen-1);
144         }
145
146         /* save the values found for later */
147         savedwg = SMB_STRDUP(wg);
148         savedun = SMB_STRDUP(un);
149         savedpw = SMB_STRDUP(pw);
150
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);
161         }
162 }
163
164 static bool smb_download_dir(const char *base, const char *name, int resume)
165 {
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;
172
173         snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
174                  (base[0] && name[0] && name[0] != '/' &&
175                   base[strlen(base)-1] != '/') ? "/" : "",
176                  name);
177
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);
184                 }
185                 fprintf(stderr, "Can't open directory %s: %s\n", path,
186                         strerror(errno));
187                 return false;
188         }
189
190         while (*relname == '/') {
191                 relname++;
192         }
193
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;
200                 }
201         }
202
203         tmpname = SMB_STRDUP(name);
204
205         while ((dirent = smbc_readdir(dirhandle))) {
206                 char *newname;
207                 if (!strcmp(dirent->name, ".") || !strcmp(dirent->name, "..")) {
208                         ok = true;
209                         continue;
210                 }
211                 if (asprintf(&newname, "%s/%s", tmpname, dirent->name) == -1) {
212                         free(tmpname);
213                         return false;
214                 }
215                 switch (dirent->smbc_type) {
216                 case SMBC_DIR:
217                         ok = smb_download_dir(base, newname, resume);
218                         break;
219
220                 case SMBC_WORKGROUP:
221                         ok = smb_download_dir("smb://", dirent->name, resume);
222                         break;
223
224                 case SMBC_SERVER:
225                         ok = smb_download_dir("smb://", dirent->name, resume);
226                         break;
227
228                 case SMBC_FILE:
229                         ok = smb_download_file(base, newname, true, resume,
230                                                 false, NULL);
231                         break;
232
233                 case SMBC_FILE_SHARE:
234                         ok = smb_download_dir(base, newname, resume);
235                         break;
236
237                 case SMBC_PRINTER_SHARE:
238                         if (!opt.quiet) {
239                                 printf("Ignoring printer share %s\n",
240                                        dirent->name);
241                         }
242                         break;
243
244                 case SMBC_COMMS_SHARE:
245                         if (!opt.quiet) {
246                                 printf("Ignoring comms share %s\n",
247                                        dirent->name);
248                         }
249                         break;
250
251                 case SMBC_IPC_SHARE:
252                         if (!opt.quiet) {
253                                 printf("Ignoring ipc$ share %s\n",
254                                        dirent->name);
255                         }
256                         break;
257
258                 default:
259                         fprintf(stderr, "Ignoring file '%s' of type '%d'\n",
260                                 newname, dirent->smbc_type);
261                         break;
262                 }
263
264                 if (!ok) {
265                         fprintf(stderr, "Failed to download %s: %s\n",
266                                 newname, strerror(errno));
267                         free(tmpname);
268                         return false;
269                 }
270                 free(newname);
271         }
272         free(tmpname);
273
274         smbc_closedir(dirhandle);
275         return ok;
276 }
277
278 static char *print_time(long t)
279 {
280         static char buffer[100];
281         int secs, mins, hours;
282         if (t < -1) {
283                 strncpy(buffer, "Unknown", sizeof(buffer));
284                 return buffer;
285         }
286
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;
293 }
294
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)
297 {
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);
306         }
307         eta = (total - pos) / avg;
308         if (total) {
309                 prcnt = 100.0 * pos / total;
310         }
311
312         human_readable(pos, hpos, sizeof(hpos));
313         human_readable(total, htotal, sizeof(htotal));
314         human_readable(avg, havg, sizeof(havg));
315
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;
320         }
321
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;
329                         }
330                 } else {
331                         filename = SMB_STRNDUP(name, available);
332                 }
333         } else {
334                 filename = SMB_STRDUP(name);
335         }
336
337         fprintf(stderr, "\r[%s] %s", filename, status);
338
339         free(filename);
340         free(status);
341 }
342
343 /* Return false on error, true on success. */
344
345 static bool smb_download_file(const char *base, const char *name,
346                               bool recursive, bool resume, bool toplevel,
347                               char *outfile)
348 {
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;
358
359         snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
360                  (*base && *name && name[0] != '/' &&
361                   base[strlen(base)-1] != '/') ? "/" : "",
362                  name);
363
364         remotehandle = smbc_open(path, O_RDONLY, 0755);
365
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;
375                         }
376                         return smb_download_dir(base, name, resume);
377
378                 case ENOENT:
379                         fprintf(stderr,
380                                 "%s can't be found on the remote server\n",
381                                 path);
382                         return false;
383
384                 case ENOMEM:
385                         fprintf(stderr, "Not enough memory\n");
386                         return false;
387
388                 case ENODEV:
389                         fprintf(stderr,
390                                 "The share name used in %s does not exist\n",
391                                 path);
392                         return false;
393
394                 case EACCES:
395                         fprintf(stderr, "You don't have enough permissions "
396                                 "to access %s\n",
397                                 path);
398                         return false;
399
400                 default:
401                         perror("smbc_open");
402                         return false;
403                 }
404         }
405
406         if (smbc_fstat(remotehandle, &remotestat) < 0) {
407                 fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
408                 return false;
409         }
410
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;
419                 }
420         } else {
421                 newpath = name;
422         }
423
424         if (!toplevel && (newpath[0] == '/')) {
425                 newpath++;
426         }
427
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);
435                         }
436                         smbc_close(remotehandle);
437                         return true;
438                 }
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;
447                 }
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;
458                 }
459
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;
466                 }
467
468                 start_offset = localstat.st_size;
469
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);
478                         }
479                         smbc_close(remotehandle);
480                         close(localhandle);
481                         return true;
482                 }
483
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);
496                         }
497                 }
498
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;
511                         }
512
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;
521                         }
522
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;
531                         }
532
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;
541                         }
542
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;
551                         }
552
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);
561                                 }
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;
570                         }
571                 }
572         } else {
573                 localhandle = STDOUT_FILENO;
574                 start_offset = 0;
575                 offset_download = 0;
576                 offset_check = 0;
577         }
578
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);
585                 }
586                 return false;
587         }
588
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;
594
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);
603                         }
604                         free(readbuf);
605                         return false;
606                 }
607
608                 total_bytes += bytesread;
609
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);
620                         }
621                         return false;
622                 }
623
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);
630                 }
631         }
632
633         free(readbuf);
634
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);
644                         }
645                 }
646                 fputc('\n', stderr);
647         }
648
649         smbc_close(remotehandle);
650         if (localhandle != STDOUT_FILENO) {
651                 close(localhandle);
652         }
653         return true;
654 }
655
656 static void clean_exit(void)
657 {
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));
663         }
664         exit(0);
665 }
666
667 static void signal_quit(int v)
668 {
669         clean_exit();
670 }
671
672 static int readrcfile(const char *name, const struct poptOption long_options[])
673 {
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;
683         }
684
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;
692                 }
693
694                 found = false;
695
696                 for (i = 0; long_options[i].argInfo; i++) {
697                         if (!long_options[i].longName) {
698                                 continue;
699                         }
700                         if (strcmp(long_options[i].longName, var)) {
701                                 continue;
702                         }
703                         if (!long_options[i].arg) {
704                                 continue;
705                         }
706
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);
718                                 }
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;
735                                         }
736                                 }
737                                 break;
738                         default:
739                                 fprintf(stderr, "Invalid variable %s at "
740                                         "line %d in %s\n",
741                                         var, lineno, name);
742                                 break;
743                         }
744
745                         found = true;
746                 }
747                 if (!found) {
748                         fprintf(stderr,
749                                 "Invalid variable %s at line %d in %s\n", var,
750                                 lineno, name);
751                 }
752         }
753
754         fclose(fd);
755         return 0;
756 }
757
758 int main(int argc, char **argv)
759 {
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
771
772                 {
773                         .longName   = "workgroup",
774                         .shortName  = 'w',
775                         .argInfo    = POPT_ARG_STRING,
776                         .arg        = &opt.workgroup,
777                         .val        = 'w',
778                         .descrip    = "Workgroup to use (optional)"
779                 },
780                 {
781                         .longName   = "user",
782                         .shortName  = 'U',
783                         .argInfo    = POPT_ARG_STRING,
784                         .arg        = &opt.username,
785                         .val        = 'U',
786                         .descrip    = "Username to use"
787                 },
788                 {
789                         .longName   = "guest",
790                         .shortName  = 'a',
791                         .argInfo    = POPT_ARG_NONE,
792                         .arg        = NULL,
793                         .val        = 'a',
794                         .descrip    = "Work as user guest"
795                 },
796
797                 {
798                         .longName   = "nonprompt",
799                         .shortName  = 'n',
800                         .argInfo    = POPT_ARG_NONE,
801                         .arg        = NULL,
802                         .val        = 'n',
803                         .descrip    = "Don't ask anything (non-interactive)"
804                 },
805                 {
806                         .longName   = "debuglevel",
807                         .shortName  = 'd',
808                         .argInfo    = POPT_ARG_INT,
809                         .arg        = &opt.debuglevel,
810                         .val        = 'd',
811                         .descrip    = "Debuglevel to use"
812                 },
813
814                 {
815                         .longName   = "encrypt",
816                         .shortName  = 'e',
817                         .argInfo    = POPT_ARG_NONE,
818                         .arg        = NULL,
819                         .val        = 'e',
820                         .descrip    = "Encrypt SMB transport"
821                 },
822                 {
823                         .longName   = "resume",
824                         .shortName  = 'r',
825                         .argInfo    = POPT_ARG_NONE,
826                         .arg        = NULL,
827                         .val        = 'r',
828                         .descrip    = "Automatically resume aborted files"
829                 },
830                 {
831                         .longName   = "update",
832                         .shortName  = 'u',
833                         .argInfo    = POPT_ARG_NONE,
834                         .arg        = NULL,
835                         .val        = 'u',
836                         .descrip    = "Download only when remote file is "
837                                       "newer than local file or local file "
838                                       "is missing"
839                 },
840                 {
841                         .longName   = "recursive",
842                         .shortName  = 'R',
843                         .argInfo    = POPT_ARG_NONE,
844                         .arg        = NULL,
845                         .val        = 'R',
846                         .descrip    = "Recursively download files"
847                 },
848                 {
849                         .longName   = "blocksize",
850                         .shortName  = 'b',
851                         .argInfo    = POPT_ARG_INT,
852                         .arg        = &opt.blocksize,
853                         .val        = 'b',
854                         .descrip    = "Change number of bytes in a block"
855                 },
856
857                 {
858                         .longName   = "outputfile",
859                         .shortName  = 'o',
860                         .argInfo    = POPT_ARG_STRING,
861                         .arg        = &opt.outputfile,
862                         .val        = 'o',
863                         .descrip    = "Write downloaded data to specified file"
864                 },
865                 {
866                         .longName   = "stdout",
867                         .shortName  = 'O',
868                         .argInfo    = POPT_ARG_NONE,
869                         .arg        = NULL,
870                         .val        = 'O',
871                         .descrip    = "Write data to stdout"
872                 },
873                 {
874                         .longName   = "dots",
875                         .shortName  = 'D',
876                         .argInfo    = POPT_ARG_NONE,
877                         .arg        = NULL,
878                         .val        = 'D',
879                         .descrip    = "Show dots as progress indication"
880                 },
881                 {
882                         .longName   = "quiet",
883                         .shortName  = 'q',
884                         .argInfo    = POPT_ARG_NONE,
885                         .arg        = NULL,
886                         .val        = 'q',
887                         .descrip    = "Be quiet"
888                 },
889                 {
890                         .longName   = "verbose",
891                         .shortName  = 'v',
892                         .argInfo    = POPT_ARG_NONE,
893                         .arg        = NULL,
894                         .val        = 'v',
895                         .descrip    = "Be verbose"
896                 },
897                 {
898                         .longName   = "rcfile",
899                         .shortName  = 'f',
900                         .argInfo    = POPT_ARG_STRING,
901                         .arg        = NULL,
902                         .val        = 'f',
903                         .descrip    = "Use specified rc file"
904                 },
905
906                 POPT_TABLEEND
907         };
908         poptContext pc;
909
910         smb_init_locale();
911
912         /* only read rcfile if it exists */
913         if (asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME")) == -1) {
914                 return 1;
915         }
916         if (access(rcfile, F_OK) == 0) {
917                 readrcfile(rcfile, long_options);
918         }
919         free(rcfile);
920
921 #ifdef SIGWINCH
922         signal(SIGWINCH, change_columns);
923 #endif
924         signal(SIGINT, signal_quit);
925         signal(SIGTERM, signal_quit);
926
927         pc = poptGetContext(argv[0], argc, argv_const, long_options, 0);
928
929         while ((c = poptGetNextOpt(pc)) > 0) {
930                 switch (c) {
931                 case 'f':
932                         readrcfile(poptGetOptArg(pc), long_options);
933                         break;
934                 case 'a':
935                         opt.username_specified = true;
936                         opt.username = talloc_strdup(frame, "");
937                         opt.password_specified = true;
938                         opt.password = talloc_strdup(frame, "");
939                         break;
940                 case 'e':
941                         smb_encrypt = true;
942                         break;
943                 case 'U':
944                         opt.username_specified = true;
945                         opt.username = talloc_strdup(frame, opt.username);
946                         p = strchr(opt.username,'%');
947                         if (p != NULL) {
948                                 *p = '\0';
949                                 opt.password = p + 1;
950                                 opt.password_specified = true;
951                         }
952                         break;
953                 case 'n':
954                         opt.nonprompt = true;
955                         break;
956                 case 'r':
957                         resume = true;
958                         break;
959                 case 'u':
960                         opt.update = true;
961                         break;
962                 case 'R':
963                         recursive = true;
964                         break;
965                 case 'O':
966                         opt.send_stdout = true;
967                         break;
968                 case 'D':
969                         opt.dots = true;
970                         break;
971                 case 'q':
972                         opt.quiet = true;
973                         break;
974                 case 'v':
975                         opt.verbose = true;
976                         break;
977                 }
978         }
979
980         if (c < -1) {
981                 fprintf(stderr, "%s: %s\n",
982                         poptBadOption(pc, POPT_BADOPTION_NOALIAS),
983                         poptStrerror(c));
984                 return 1;
985         }
986
987         if ((opt.send_stdout || resume || opt.outputfile) && opt.update) {
988                 fprintf(stderr, "The -o, -R or -O and -U options can not be "
989                         "used together.\n");
990                 return 1;
991         }
992         if ((opt.send_stdout || opt.outputfile) && recursive) {
993                 fprintf(stderr, "The -o or -O and -R options can not be "
994                         "used together.\n");
995                 return 1;
996         }
997
998         if (opt.outputfile && opt.send_stdout) {
999                 fprintf(stderr, "The -o and -O options can not be "
1000                         "used together.\n");
1001                 return 1;
1002         }
1003
1004         popt_burn_cmdline_password(argc, argv);
1005
1006         cmdline_messaging_context(get_dyn_CONFIGFILE());
1007
1008         if (smbc_init(get_auth_data, opt.debuglevel) < 0) {
1009                 fprintf(stderr, "Unable to initialize libsmbclient\n");
1010                 return 1;
1011         }
1012
1013         if (smb_encrypt) {
1014                 SMBCCTX *smb_ctx = smbc_set_context(NULL);
1015                 smbc_option_set(smb_ctx,
1016                                 discard_const_p(char, "smb_encrypt_level"),
1017                                 "require");
1018         }
1019
1020         columns = get_num_cols();
1021
1022         total_start_time = time_mono(NULL);
1023
1024         while ((file = poptGetArg(pc))) {
1025                 if (!recursive) {
1026                         ret = smb_download_file(file, "", recursive, resume,
1027                                                 true, opt.outputfile);
1028                 } else {
1029                         ret = smb_download_dir(file, "", resume);
1030                 }
1031         }
1032
1033         TALLOC_FREE(frame);
1034         if (ret) {
1035                 clean_exit();
1036         }
1037         return ret?0:1;
1038 }