s3:utils: Fix setting the debug level
[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 "lib/cmdline/cmdline.h"
22 #include "lib/param/param.h"
23 #include "libsmbclient.h"
24 #include "cmdline_contexts.h"
25 #include "auth/credentials/credentials.h"
26 #include "auth/gensec/gensec.h"
27
28 static int columns = 0;
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 struct opt {
46         char *outputfile;
47         size_t blocksize;
48
49         int quiet;
50         int dots;
51         int verbose;
52         int send_stdout;
53         int update;
54         unsigned limit_rate;
55 };
56 static struct opt opt = { .blocksize = SMB_DEFAULT_BLOCKSIZE };
57
58 static bool smb_download_file(const char *base, const char *name,
59                               bool recursive, bool resume, bool toplevel,
60                               char *outfile);
61
62 static int get_num_cols(void)
63 {
64 #ifdef TIOCGWINSZ
65         struct winsize ws;
66         if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
67                 return 0;
68         }
69         return ws.ws_col;
70 #else
71 #warning No support for TIOCGWINSZ
72         char *cols = getenv("COLUMNS");
73         if (!cols) {
74                 return 0;
75         }
76         return atoi(cols);
77 #endif
78 }
79
80 static void change_columns(int sig)
81 {
82         columns = get_num_cols();
83 }
84
85 static void human_readable(off_t s, char *buffer, int l)
86 {
87         if (s > 1024 * 1024 * 1024) {
88                 snprintf(buffer, l, "%.2fGB", 1.0 * s / (1024 * 1024 * 1024));
89         } else if (s > 1024 * 1024) {
90                 snprintf(buffer, l, "%.2fMB", 1.0 * s / (1024 * 1024));
91         } else if (s > 1024) {
92                 snprintf(buffer, l, "%.2fkB", 1.0 * s / 1024);
93         } else {
94                 snprintf(buffer, l, "%jdb", (intmax_t)s);
95         }
96 }
97
98 /*
99  * Authentication callback for libsmbclient.
100  *
101  * The command line parser will take care asking for a password interactively!
102  */
103 static void get_auth_data_with_context_fn(SMBCCTX *ctx,
104                                           const char *srv,
105                                           const char *shr,
106                                           char *dom,
107                                           int dom_len,
108                                           char *usr,
109                                           int usr_len,
110                                           char *pwd,
111                                           int pwd_len)
112 {
113         struct cli_credentials *creds = samba_cmdline_get_creds();
114         const char *username = NULL;
115         const char *password = NULL;
116         const char *domain = NULL;
117
118         username = cli_credentials_get_username(creds);
119         if (username != NULL) {
120                 strncpy(usr, username, usr_len - 1);
121         }
122
123         password = cli_credentials_get_password(creds);
124         if (password != NULL) {
125                 strncpy(pwd, password, pwd_len - 1);
126         }
127
128         domain = cli_credentials_get_domain(creds);
129         if (domain != NULL) {
130                 strncpy(dom, domain, dom_len - 1);
131         }
132
133         smbc_set_credentials_with_fallback(ctx, domain, username, password);
134
135         if (!opt.quiet && username != NULL) {
136                 if (username[0] == '\0') {
137                         printf("Using guest user\n");
138                 } else {
139                         printf("Using domain: %s, user: %s\n",
140                                 domain,
141                                 username);
142                 }
143         }
144 }
145
146 static bool smb_download_dir(const char *base, const char *name, int resume)
147 {
148         char path[SMB_MAXPATHLEN];
149         int dirhandle;
150         struct smbc_dirent *dirent;
151         const char *relname = name;
152         char *tmpname;
153         bool ok = false;
154
155         snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
156                  (base[0] && name[0] && name[0] != '/' &&
157                   base[strlen(base)-1] != '/') ? "/" : "",
158                  name);
159
160         /* List files in directory and call smb_download_file on them */
161         dirhandle = smbc_opendir(path);
162         if (dirhandle < 1) {
163                 if (errno == ENOTDIR) {
164                         return smb_download_file(base, name, true, resume,
165                                                  false, NULL);
166                 }
167                 fprintf(stderr, "Can't open directory %s: %s\n", path,
168                         strerror(errno));
169                 return false;
170         }
171
172         while (*relname == '/') {
173                 relname++;
174         }
175
176         if (strlen(relname) > 0) {
177                 int rc = mkdir(relname, 0755);
178                 if (rc == -1 && errno != EEXIST) {
179                         fprintf(stderr, "Can't create directory %s: %s\n",
180                                 relname, strerror(errno));
181                         return false;
182                 }
183         }
184
185         tmpname = SMB_STRDUP(name);
186
187         while ((dirent = smbc_readdir(dirhandle))) {
188                 char *newname;
189                 if (!strcmp(dirent->name, ".") || !strcmp(dirent->name, "..")) {
190                         ok = true;
191                         continue;
192                 }
193                 if (asprintf(&newname, "%s/%s", tmpname, dirent->name) == -1) {
194                         free(tmpname);
195                         return false;
196                 }
197                 switch (dirent->smbc_type) {
198                 case SMBC_DIR:
199                         ok = smb_download_dir(base, newname, resume);
200                         break;
201
202                 case SMBC_WORKGROUP:
203                         ok = smb_download_dir("smb://", dirent->name, resume);
204                         break;
205
206                 case SMBC_SERVER:
207                         ok = smb_download_dir("smb://", dirent->name, resume);
208                         break;
209
210                 case SMBC_FILE:
211                         ok = smb_download_file(base, newname, true, resume,
212                                                 false, NULL);
213                         break;
214
215                 case SMBC_FILE_SHARE:
216                         ok = smb_download_dir(base, newname, resume);
217                         break;
218
219                 case SMBC_PRINTER_SHARE:
220                         if (!opt.quiet) {
221                                 printf("Ignoring printer share %s\n",
222                                        dirent->name);
223                         }
224                         break;
225
226                 case SMBC_COMMS_SHARE:
227                         if (!opt.quiet) {
228                                 printf("Ignoring comms share %s\n",
229                                        dirent->name);
230                         }
231                         break;
232
233                 case SMBC_IPC_SHARE:
234                         if (!opt.quiet) {
235                                 printf("Ignoring ipc$ share %s\n",
236                                        dirent->name);
237                         }
238                         break;
239
240                 default:
241                         fprintf(stderr, "Ignoring file '%s' of type '%d'\n",
242                                 newname, dirent->smbc_type);
243                         break;
244                 }
245
246                 if (!ok) {
247                         fprintf(stderr, "Failed to download %s: %s\n",
248                                 newname, strerror(errno));
249                         free(newname);
250                         free(tmpname);
251                         return false;
252                 }
253                 free(newname);
254         }
255         free(tmpname);
256
257         smbc_closedir(dirhandle);
258         return ok;
259 }
260
261 static char *print_time(long t)
262 {
263         static char buffer[100];
264         int secs, mins, hours;
265         if (t < -1) {
266                 strncpy(buffer, "Unknown", sizeof(buffer));
267                 return buffer;
268         }
269
270         secs = (int)t % 60;
271         mins = (int)t / 60 % 60;
272         hours = (int)t / (60 * 60);
273         snprintf(buffer, sizeof(buffer) - 1, "%02d:%02d:%02d", hours, mins,
274                  secs);
275         return buffer;
276 }
277
278 static void print_progress(const char *name, time_t start, time_t now,
279                            off_t start_pos, off_t pos, off_t total)
280 {
281         double avg = 0.0;
282         long eta = -1;
283         double prcnt = 0.0;
284         char hpos[22], htotal[22], havg[22];
285         char *status, *filename;
286         int len;
287         if (now - start) {
288                 avg = 1.0 * (pos - start_pos) / (now - start);
289         }
290         eta = (total - pos) / avg;
291         if (total) {
292                 prcnt = 100.0 * pos / total;
293         }
294
295         human_readable(pos, hpos, sizeof(hpos));
296         human_readable(total, htotal, sizeof(htotal));
297         human_readable(avg, havg, sizeof(havg));
298
299         len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos,
300                        htotal, prcnt, havg, print_time(eta));
301         if (len == -1) {
302                 return;
303         }
304
305         if (columns) {
306                 int required = strlen(name),
307                     available = columns - len - strlen("[] ");
308                 if (required > available) {
309                         if (asprintf(&filename, "...%s",
310                                      name + required - available + 3) == -1) {
311                                 return;
312                         }
313                 } else {
314                         filename = SMB_STRNDUP(name, available);
315                 }
316         } else {
317                 filename = SMB_STRDUP(name);
318         }
319
320         fprintf(stderr, "\r[%s] %s", filename, status);
321
322         free(filename);
323         free(status);
324 }
325
326 /* Return false on error, true on success. */
327
328 static bool smb_download_file(const char *base, const char *name,
329                               bool recursive, bool resume, bool toplevel,
330                               char *outfile)
331 {
332         int remotehandle, localhandle;
333         time_t start_time = time_mono(NULL);
334         const char *newpath;
335         char path[SMB_MAXPATHLEN];
336         char checkbuf[2][RESUME_CHECK_SIZE];
337         char *readbuf = NULL;
338         off_t offset_download = 0, offset_check = 0, curpos = 0,
339               start_offset = 0;
340         struct stat localstat, remotestat;
341         clock_t start_of_bucket_ticks = 0;
342         size_t bytes_in_bucket = 0;
343         size_t bucket_size = 0;
344         clock_t ticks_to_fill_bucket = 0;
345
346         snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
347                  (*base && *name && name[0] != '/' &&
348                   base[strlen(base)-1] != '/') ? "/" : "",
349                  name);
350
351         remotehandle = smbc_open(path, O_RDONLY, 0755);
352
353         if (remotehandle < 0) {
354                 switch (errno) {
355                 case EISDIR:
356                         if (!recursive) {
357                                 fprintf(stderr,
358                                         "%s is a directory. Specify -R "
359                                         "to download recursively\n",
360                                         path);
361                                 return false;
362                         }
363                         return smb_download_dir(base, name, resume);
364
365                 case ENOENT:
366                         fprintf(stderr,
367                                 "%s can't be found on the remote server\n",
368                                 path);
369                         return false;
370
371                 case ENOMEM:
372                         fprintf(stderr, "Not enough memory\n");
373                         return false;
374
375                 case ENODEV:
376                         fprintf(stderr,
377                                 "The share name used in %s does not exist\n",
378                                 path);
379                         return false;
380
381                 case EACCES:
382                         fprintf(stderr, "You don't have enough permissions "
383                                 "to access %s\n",
384                                 path);
385                         return false;
386
387                 default:
388                         perror("smbc_open");
389                         return false;
390                 }
391         }
392
393         if (smbc_fstat(remotehandle, &remotestat) < 0) {
394                 fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
395                 return false;
396         }
397
398         if (outfile) {
399                 newpath = outfile;
400         } else if (!name[0]) {
401                 newpath = strrchr(base, '/');
402                 if (newpath) {
403                         newpath++;
404                 } else {
405                         newpath = base;
406                 }
407         } else {
408                 newpath = name;
409         }
410
411         if (!toplevel && (newpath[0] == '/')) {
412                 newpath++;
413         }
414
415         /* Open local file according to the mode */
416         if (opt.update) {
417                 /* if it is up-to-date, skip */
418                 if (stat(newpath, &localstat) == 0 &&
419                     localstat.st_mtime >= remotestat.st_mtime) {
420                         if (opt.verbose) {
421                                 printf("%s is up-to-date, skipping\n", newpath);
422                         }
423                         smbc_close(remotehandle);
424                         return true;
425                 }
426                 /* else open it for writing and truncate if it exists */
427                 localhandle = open(
428                     newpath, O_CREAT | O_NONBLOCK | O_RDWR | O_TRUNC, 0775);
429                 if (localhandle < 0) {
430                         fprintf(stderr, "Can't open %s : %s\n", newpath,
431                                 strerror(errno));
432                         smbc_close(remotehandle);
433                         return false;
434                 }
435                 /* no offset */
436         } else if (!opt.send_stdout) {
437                 localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR |
438                                                 (!resume ? O_EXCL : 0),
439                                    0755);
440                 if (localhandle < 0) {
441                         fprintf(stderr, "Can't open %s: %s\n", newpath,
442                                 strerror(errno));
443                         smbc_close(remotehandle);
444                         return false;
445                 }
446
447                 if (fstat(localhandle, &localstat) != 0) {
448                         fprintf(stderr, "Can't fstat %s: %s\n", newpath,
449                                 strerror(errno));
450                         smbc_close(remotehandle);
451                         close(localhandle);
452                         return false;
453                 }
454
455                 start_offset = localstat.st_size;
456
457                 if (localstat.st_size &&
458                     localstat.st_size == remotestat.st_size) {
459                         if (opt.verbose) {
460                                 fprintf(stderr, "%s is already downloaded "
461                                         "completely.\n",
462                                         path);
463                         } else if (!opt.quiet) {
464                                 fprintf(stderr, "%s\n", path);
465                         }
466                         smbc_close(remotehandle);
467                         close(localhandle);
468                         return true;
469                 }
470
471                 if (localstat.st_size > RESUME_CHECK_OFFSET &&
472                     remotestat.st_size > RESUME_CHECK_OFFSET) {
473                         offset_download =
474                             localstat.st_size - RESUME_DOWNLOAD_OFFSET;
475                         offset_check = localstat.st_size - RESUME_CHECK_OFFSET;
476                         if (opt.verbose) {
477                                 printf("Trying to start resume of %s at %jd\n"
478                                        "At the moment %jd of %jd bytes have "
479                                        "been retrieved\n",
480                                        newpath, (intmax_t)offset_check,
481                                        (intmax_t)localstat.st_size,
482                                        (intmax_t)remotestat.st_size);
483                         }
484                 }
485
486                 if (offset_check) {
487                         off_t off1, off2;
488                         /* First, check all bytes from offset_check to
489                          * offset_download */
490                         off1 = lseek(localhandle, offset_check, SEEK_SET);
491                         if (off1 < 0) {
492                                 fprintf(stderr,
493                                         "Can't seek to %jd in local file %s\n",
494                                         (intmax_t)offset_check, newpath);
495                                 smbc_close(remotehandle);
496                                 close(localhandle);
497                                 return false;
498                         }
499
500                         off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET);
501                         if (off2 < 0) {
502                                 fprintf(stderr,
503                                         "Can't seek to %jd in remote file %s\n",
504                                         (intmax_t)offset_check, newpath);
505                                 smbc_close(remotehandle);
506                                 close(localhandle);
507                                 return false;
508                         }
509
510                         if (off1 != off2) {
511                                 fprintf(stderr, "Offset in local and remote "
512                                         "files are different "
513                                         "(local: %jd, remote: %jd)\n",
514                                         (intmax_t)off1, (intmax_t)off2);
515                                 smbc_close(remotehandle);
516                                 close(localhandle);
517                                 return false;
518                         }
519
520                         if (smbc_read(remotehandle, checkbuf[0],
521                                       RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
522                                 fprintf(stderr, "Can't read %d bytes from "
523                                         "remote file %s\n",
524                                         RESUME_CHECK_SIZE, path);
525                                 smbc_close(remotehandle);
526                                 close(localhandle);
527                                 return false;
528                         }
529
530                         if (read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) !=
531                             RESUME_CHECK_SIZE) {
532                                 fprintf(stderr, "Can't read %d bytes from "
533                                         "local file %s\n",
534                                         RESUME_CHECK_SIZE, name);
535                                 smbc_close(remotehandle);
536                                 close(localhandle);
537                                 return false;
538                         }
539
540                         if (memcmp(checkbuf[0], checkbuf[1],
541                                    RESUME_CHECK_SIZE) == 0) {
542                                 if (opt.verbose) {
543                                         printf("Current local and remote file "
544                                                "appear to be the same. "
545                                                "Starting download from "
546                                                "offset %jd\n",
547                                                (intmax_t)offset_download);
548                                 }
549                         } else {
550                                 fprintf(stderr, "Local and remote file appear "
551                                         "to be different, not "
552                                         "doing resume for %s\n",
553                                         path);
554                                 smbc_close(remotehandle);
555                                 close(localhandle);
556                                 return false;
557                         }
558                 }
559         } else {
560                 localhandle = STDOUT_FILENO;
561                 start_offset = 0;
562                 offset_download = 0;
563                 offset_check = 0;
564         }
565
566         /* We implement rate limiting by filling up a bucket with bytes and
567          * checking, once the bucket is filled, if it was filled too fast.
568          * If so, we sleep for some time to get an average transfer rate that
569          * equals to the one set by the user.
570          *
571          * The bucket size directly affects the traffic characteristics.
572          * The smaller the bucket the more frequent the pause/resume cycle.
573          * A large bucket can result in burst of high speed traffic and large
574          * pauses. A cycle of 100ms looks like a good value. This value (in
575          * ticks) is held in `ticks_to_fill_bucket`. The `bucket_size` is
576          * calculated as:
577          * `limit_rate * 1024 * / (CLOCKS_PER_SEC / ticks_to_fill_bucket)`
578          *
579          * After selecting the bucket size we also need to check the blocksize
580          * of the transfer, since this is the minimum unit of traffic that we
581          * can observe. Achieving a ~10% precision requires a blocksize with a
582          * maximum size of `bucket_size / 10`.
583          */
584         if (opt.limit_rate > 0) {
585                 unsigned max_block_size;
586                 /* This is the time that the bucket should take to fill. */
587                 ticks_to_fill_bucket = 100 /*ms*/ * CLOCKS_PER_SEC / 1000;
588                 /* This is the size of the bucket in bytes.
589                  * If we fill the bucket too quickly we should pause */
590                 bucket_size = opt.limit_rate * 1024 / (CLOCKS_PER_SEC / ticks_to_fill_bucket);
591                 max_block_size = bucket_size / 10;
592                 max_block_size = max_block_size > 0 ? max_block_size : 1;
593                 if (opt.blocksize > max_block_size) {
594                         if (opt.blocksize != SMB_DEFAULT_BLOCKSIZE) {
595                                 fprintf(stderr,
596                                         "Warning: Overriding block size to %d \
597                                          due to limit-rate", max_block_size);
598                         }
599                         opt.blocksize = max_block_size;
600                 }
601                 start_of_bucket_ticks = clock();
602         }
603
604         readbuf = (char *)SMB_MALLOC(opt.blocksize);
605         if (!readbuf) {
606                 fprintf(stderr, "Failed to allocate %zu bytes for read "
607                                 "buffer (%s)", opt.blocksize, strerror(errno));
608                 if (localhandle != STDOUT_FILENO) {
609                         close(localhandle);
610                 }
611                 return false;
612         }
613
614         /* Now, download all bytes from offset_download to the end */
615         for (curpos = offset_download; curpos < remotestat.st_size;
616              curpos += opt.blocksize) {
617                 ssize_t bytesread;
618                 ssize_t byteswritten;
619
620                 /* Rate limiting. This pauses the transfer to limit traffic. */
621                 if (opt.limit_rate > 0) {
622                         if (bytes_in_bucket > bucket_size) {
623                                 clock_t now_ticks = clock();
624                                 clock_t diff_ticks = now_ticks
625                                                      - start_of_bucket_ticks;
626                                 /* Check if the bucket filled up too fast. */
627                                 if (diff_ticks < ticks_to_fill_bucket) {
628                                         /* Pause until `ticks_to_fill_bucket` */
629                                         double sleep_us
630                                          = (ticks_to_fill_bucket - diff_ticks)
631                                           * 1000000.0 / CLOCKS_PER_SEC;
632                                         usleep(sleep_us);
633                                 }
634                                 /* Reset the byte counter and the ticks. */
635                                 bytes_in_bucket = 0;
636                                 start_of_bucket_ticks = clock();
637                         }
638                 }
639
640                 bytesread = smbc_read(remotehandle, readbuf, opt.blocksize);
641                 if (opt.limit_rate > 0) {
642                         bytes_in_bucket += bytesread;
643                 }
644                 if(bytesread < 0) {
645                         fprintf(stderr,
646                                 "Can't read %zu bytes at offset %jd, file %s\n",
647                                 opt.blocksize, (intmax_t)curpos, path);
648                         smbc_close(remotehandle);
649                         if (localhandle != STDOUT_FILENO) {
650                                 close(localhandle);
651                         }
652                         free(readbuf);
653                         return false;
654                 }
655
656                 total_bytes += bytesread;
657
658                 byteswritten = write(localhandle, readbuf, bytesread);
659                 if (byteswritten != bytesread) {
660                         fprintf(stderr,
661                                 "Can't write %zd bytes to local file %s at "
662                                 "offset %jd\n", bytesread, path,
663                                 (intmax_t)curpos);
664                         free(readbuf);
665                         smbc_close(remotehandle);
666                         if (localhandle != STDOUT_FILENO) {
667                                 close(localhandle);
668                         }
669                         return false;
670                 }
671
672                 if (opt.dots) {
673                         fputc('.', stderr);
674                 } else if (!opt.quiet) {
675                         print_progress(newpath, start_time, time_mono(NULL),
676                                        start_offset, curpos,
677                                        remotestat.st_size);
678                 }
679         }
680
681         free(readbuf);
682
683         if (opt.dots) {
684                 fputc('\n', stderr);
685                 printf("%s downloaded\n", path);
686         } else if (!opt.quiet) {
687                 int i;
688                 fprintf(stderr, "\r%s", path);
689                 if (columns) {
690                         for (i = strlen(path); i < columns; i++) {
691                                 fputc(' ', stderr);
692                         }
693                 }
694                 fputc('\n', stderr);
695         }
696
697         smbc_close(remotehandle);
698         if (localhandle != STDOUT_FILENO) {
699                 close(localhandle);
700         }
701         return true;
702 }
703
704 static void clean_exit(void)
705 {
706         char bs[100];
707         human_readable(total_bytes, bs, sizeof(bs));
708         if (!opt.quiet) {
709                 fprintf(stderr, "Downloaded %s in %lu seconds\n", bs,
710                         (unsigned long)(time_mono(NULL) - total_start_time));
711         }
712         exit(0);
713 }
714
715 static void signal_quit(int v)
716 {
717         clean_exit();
718 }
719
720 int main(int argc, char **argv)
721 {
722         int c = 0;
723         const char *file = NULL;
724         int smb_encrypt = false;
725         int resume = 0, recursive = 0;
726         TALLOC_CTX *frame = talloc_stackframe();
727         bool ok = false;
728         const char **argv_const = discard_const_p(const char *, argv);
729         struct poptOption long_options[] = {
730                 POPT_AUTOHELP
731
732                 {
733                         .longName   = "guest",
734                         .shortName  = 'a',
735                         .argInfo    = POPT_ARG_NONE,
736                         .arg        = NULL,
737                         .val        = 'a',
738                         .descrip    = "Work as user guest"
739                 },
740                 {
741                         .longName   = "encrypt",
742                         .shortName  = 'e',
743                         .argInfo    = POPT_ARG_NONE,
744                         .arg        = &smb_encrypt,
745                         .val        = 1,
746                         .descrip    = "Encrypt SMB transport"
747                 },
748                 {
749                         .longName   = "resume",
750                         .shortName  = 'r',
751                         .argInfo    = POPT_ARG_NONE,
752                         .arg        = &resume,
753                         .val        = 1,
754                         .descrip    = "Automatically resume aborted files"
755                 },
756                 {
757                         .longName   = "update",
758                         .shortName  = 'u',
759                         .argInfo    = POPT_ARG_NONE,
760                         .arg        = &opt.update,
761                         .val        = 1,
762                         .descrip    = "Download only when remote file is "
763                                       "newer than local file or local file "
764                                       "is missing"
765                 },
766                 {
767                         .longName   = "recursive",
768                         .shortName  = 0,
769                         .argInfo    = POPT_ARG_NONE,
770                         .arg        = &recursive,
771                         .val        = true,
772                         .descrip    = "Recursively download files"
773                 },
774                 {
775                         .longName   = "blocksize",
776                         .shortName  = 'b',
777                         .argInfo    = POPT_ARG_INT,
778                         .arg        = &opt.blocksize,
779                         .val        = 'b',
780                         .descrip    = "Change number of bytes in a block"
781                 },
782
783                 {
784                         .longName   = "outputfile",
785                         .shortName  = 'o',
786                         .argInfo    = POPT_ARG_STRING,
787                         .arg        = &opt.outputfile,
788                         .val        = 'o',
789                         .descrip    = "Write downloaded data to specified file"
790                 },
791                 {
792                         .longName   = "stdout",
793                         .shortName  = 0,
794                         .argInfo    = POPT_ARG_NONE,
795                         .arg        = &opt.send_stdout,
796                         .val        = true,
797                         .descrip    = "Write data to stdout"
798                 },
799                 {
800                         .longName   = "dots",
801                         .shortName  = 'D',
802                         .argInfo    = POPT_ARG_NONE,
803                         .arg        = &opt.dots,
804                         .val        = 1,
805                         .descrip    = "Show dots as progress indication"
806                 },
807                 {
808                         .longName   = "quiet",
809                         .shortName  = 'q',
810                         .argInfo    = POPT_ARG_NONE,
811                         .arg        = &opt.quiet,
812                         .val        = 1,
813                         .descrip    = "Be quiet"
814                 },
815                 {
816                         .longName   = "verbose",
817                         .shortName  = 'v',
818                         .argInfo    = POPT_ARG_NONE,
819                         .arg        = &opt.verbose,
820                         .val        = 1,
821                         .descrip    = "Be verbose"
822                 },
823                 {
824                         .longName   = "limit-rate",
825                         .shortName  = 0,
826                         .argInfo    = POPT_ARG_INT,
827                         .arg        = &opt.limit_rate,
828                         .val        = 'l',
829                         .descrip    = "Limit download speed to this many KB/s"
830                 },
831
832                 POPT_COMMON_SAMBA
833                 POPT_COMMON_CONNECTION
834                 POPT_COMMON_CREDENTIALS
835                 POPT_LEGACY_S3
836                 POPT_COMMON_VERSION
837                 POPT_TABLEEND
838         };
839         poptContext pc = NULL;
840         struct cli_credentials *creds = NULL;
841         enum smb_encryption_setting encryption_state = SMB_ENCRYPTION_DEFAULT;
842         enum credentials_use_kerberos use_kerberos = CRED_USE_KERBEROS_DESIRED;
843         smbc_smb_encrypt_level encrypt_level = SMBC_ENCRYPTLEVEL_DEFAULT;
844 #if 0
845         enum smb_signing_setting signing_state = SMB_SIGNING_DEFAULT;
846         const char *use_signing = "auto";
847 #endif
848         bool is_nt_hash = false;
849         uint32_t gensec_features;
850         bool use_wbccache = false;
851         SMBCCTX *smb_ctx = NULL;
852         int dbg_lvl = -1;
853         int rc;
854
855         smb_init_locale();
856
857         ok = samba_cmdline_init(frame,
858                                 SAMBA_CMDLINE_CONFIG_CLIENT,
859                                 false);
860         if (!ok) {
861                 goto done;
862         }
863
864 #ifdef SIGWINCH
865         signal(SIGWINCH, change_columns);
866 #endif
867         signal(SIGINT, signal_quit);
868         signal(SIGTERM, signal_quit);
869
870         pc = samba_popt_get_context(getprogname(),
871                                     argc,
872                                     argv_const,
873                                     long_options,
874                                     0);
875         if (pc == NULL) {
876                 ok = false;
877                 goto done;
878         }
879
880         creds = samba_cmdline_get_creds();
881
882         while ((c = poptGetNextOpt(pc)) != -1) {
883                 switch (c) {
884                 case 'a':
885                         cli_credentials_set_anonymous(creds);
886                         break;
887                 case POPT_ERROR_BADOPT:
888                         fprintf(stderr, "\nInvalid option %s: %s\n\n",
889                                 poptBadOption(pc, 0), poptStrerror(c));
890                         poptPrintUsage(pc, stderr, 0);
891                         ok = false;
892                         goto done;
893                 }
894
895                 if (c < -1) {
896                         fprintf(stderr, "%s: %s\n",
897                                 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
898                                 poptStrerror(c));
899                         ok = false;
900                         goto done;
901                 }
902         }
903
904         if ((opt.send_stdout || resume || opt.outputfile) && opt.update) {
905                 fprintf(stderr, "The -o, -R or -O and -U options can not be "
906                         "used together.\n");
907                 ok = true;
908                 goto done;
909         }
910         if ((opt.send_stdout || opt.outputfile) && recursive) {
911                 fprintf(stderr, "The -o or -O and -R options can not be "
912                         "used together.\n");
913                 ok = true;
914                 goto done;
915         }
916
917         if (opt.outputfile && opt.send_stdout) {
918                 fprintf(stderr, "The -o and -O options can not be "
919                         "used together.\n");
920                 ok = true;
921                 goto done;
922         }
923
924         samba_cmdline_burn(argc, argv);
925
926         /* smbc_new_context() will set the log level to 0 */
927         dbg_lvl = debuglevel_get();
928
929         smb_ctx = smbc_new_context();
930         if (smb_ctx == NULL) {
931                 fprintf(stderr, "Unable to initialize libsmbclient\n");
932                 ok = false;
933                 goto done;
934         }
935         smbc_setDebug(smb_ctx, dbg_lvl);
936
937         rc = smbc_setConfiguration(smb_ctx, lp_default_path());
938         if (rc < 0) {
939                 ok = false;
940                 goto done;
941         }
942
943         smbc_setFunctionAuthDataWithContext(smb_ctx,
944                                             get_auth_data_with_context_fn);
945
946         ok = smbc_init_context(smb_ctx);
947         if (!ok) {
948                 goto done;
949         }
950         smbc_set_context(smb_ctx);
951
952         encryption_state = cli_credentials_get_smb_encryption(creds);
953         switch (encryption_state) {
954         case SMB_ENCRYPTION_REQUIRED:
955                 encrypt_level = SMBC_ENCRYPTLEVEL_REQUIRE;
956                 break;
957         case SMB_ENCRYPTION_DESIRED:
958         case SMB_ENCRYPTION_IF_REQUIRED:
959                 encrypt_level = SMBC_ENCRYPTLEVEL_REQUEST;
960                 break;
961         case SMB_ENCRYPTION_OFF:
962                 encrypt_level = SMBC_ENCRYPTLEVEL_NONE;
963                 break;
964         case SMB_ENCRYPTION_DEFAULT:
965                 encrypt_level = SMBC_ENCRYPTLEVEL_DEFAULT;
966                 break;
967         }
968         if (smb_encrypt) {
969                 encrypt_level = SMBC_ENCRYPTLEVEL_REQUIRE;
970         }
971         smbc_setOptionSmbEncryptionLevel(smb_ctx, encrypt_level);
972
973 #if 0
974         signing_state = cli_credentials_get_smb_signing(creds);
975         if (encryption_state >= SMB_ENCRYPTION_DESIRED) {
976                 signing_state = SMB_SIGNING_REQUIRED;
977         }
978         switch (signing_state) {
979         case SMB_SIGNING_REQUIRED:
980                 use_signing = "required";
981                 break;
982         case SMB_SIGNING_DEFAULT:
983         case SMB_SIGNING_DESIRED:
984         case SMB_SIGNING_IF_REQUIRED:
985                 use_signing = "yes";
986                 break;
987         case SMB_SIGNING_OFF:
988                 use_signing = "off";
989                 break;
990         default:
991                 use_signing = "auto";
992                 break;
993         }
994         /* FIXME: There is no libsmbclient function to set signing state */
995 #endif
996
997         use_kerberos = cli_credentials_get_kerberos_state(creds);
998         switch (use_kerberos) {
999         case CRED_USE_KERBEROS_REQUIRED:
1000                 smbc_setOptionUseKerberos(smb_ctx, true);
1001                 smbc_setOptionFallbackAfterKerberos(smb_ctx, false);
1002                 break;
1003         case CRED_USE_KERBEROS_DESIRED:
1004                 smbc_setOptionUseKerberos(smb_ctx, true);
1005                 smbc_setOptionFallbackAfterKerberos(smb_ctx, true);
1006                 break;
1007         case CRED_USE_KERBEROS_DISABLED:
1008                 smbc_setOptionUseKerberos(smb_ctx, false);
1009                 break;
1010         }
1011
1012         /* Check if the password supplied is an NT hash */
1013         is_nt_hash = cli_credentials_is_password_nt_hash(creds);
1014         smbc_setOptionUseNTHash(smb_ctx, is_nt_hash);
1015
1016         /* Check if we should use the winbind ccache */
1017         gensec_features = cli_credentials_get_gensec_features(creds);
1018         use_wbccache = (gensec_features & GENSEC_FEATURE_NTLM_CCACHE);
1019         smbc_setOptionUseCCache(smb_ctx, use_wbccache);
1020
1021         columns = get_num_cols();
1022
1023         total_start_time = time_mono(NULL);
1024
1025         while ((file = poptGetArg(pc))) {
1026                 if (!recursive) {
1027                         ok = smb_download_file(file, "", recursive, resume,
1028                                                 true, opt.outputfile);
1029                 } else {
1030                         ok = smb_download_dir(file, "", resume);
1031                 }
1032         }
1033
1034 done:
1035         poptFreeContext(pc);
1036         TALLOC_FREE(frame);
1037         if (ok) {
1038                 clean_exit();
1039         }
1040         return ok ? 0 : 1;
1041 }