3d4a71b71d1d1e9a51d2abd7964bebbfd037a892
[ira/wip.git] / source3 / utils / smbget.c
1 /*
2    smbget: a wget-like utility with support for recursive downloading and 
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 "libsmbclient.h"
21
22 #if _FILE_OFFSET_BITS==64
23 #define OFF_T_FORMAT "%lld"
24 #define OFF_T_FORMAT_CAST long long
25 #else
26 #define OFF_T_FORMAT "%ld"
27 #define OFF_T_FORMAT_CAST long
28 #endif
29
30 static int columns = 0;
31
32 static int debuglevel, update;
33 static char *outputfile;
34
35
36 static time_t total_start_time = 0;
37 static off_t total_bytes = 0;
38
39 #define SMB_MAXPATHLEN MAXPATHLEN
40
41 /* Number of bytes to read when checking whether local and remote file are really the same file */
42 #define RESUME_CHECK_SIZE                               512
43 #define RESUME_DOWNLOAD_OFFSET                  1024
44 #define RESUME_CHECK_OFFSET                             RESUME_DOWNLOAD_OFFSET+RESUME_CHECK_SIZE
45 /* Number of bytes to read at once */
46 #define SMB_DEFAULT_BLOCKSIZE                                   64000
47
48 static const char *username = NULL, *password = NULL, *workgroup = NULL;
49 static int nonprompt = 0, quiet = 0, dots = 0, keep_permissions = 0, verbose = 0, send_stdout = 0;
50 static int blocksize = SMB_DEFAULT_BLOCKSIZE;
51
52 static int smb_download_file(const char *base, const char *name, int recursive, int resume, char *outfile);
53
54 static int get_num_cols(void)
55 {
56 #ifdef TIOCGWINSZ
57         struct winsize ws;
58         if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
59                 return 0;
60         }
61         return ws.ws_col;
62 #else
63 #warning No support for TIOCGWINSZ
64         char *cols = getenv("COLUMNS");
65         if(!cols) return 0;
66         return atoi(cols);
67 #endif
68 }
69
70 static void change_columns(int sig)
71 {
72         columns = get_num_cols();
73 }
74
75 static void human_readable(off_t s, char *buffer, int l)
76 {
77         if(s > 1024 * 1024 * 1024) snprintf(buffer, l, "%.2fGb", 1.0 * s / (1024 * 1024 * 1024));
78         else if(s > 1024 * 1024) snprintf(buffer, l, "%.2fMb", 1.0 * s / (1024 * 1024));
79         else if(s > 1024) snprintf(buffer, l, "%.2fkb", 1.0 * s / 1024);
80         else snprintf(buffer, l, OFF_T_FORMAT"b", (OFF_T_FORMAT_CAST)s);
81 }
82
83 static void get_auth_data(const char *srv, const char *shr, char *wg, int wglen, char *un, int unlen, char *pw, int pwlen)
84 {
85         static char hasasked = 0;
86         char *wgtmp, *usertmp;
87         char tmp[128];
88
89         if(hasasked) return;
90         hasasked = 1;
91
92         if(!nonprompt && !username) {
93                 printf("Username for %s at %s [guest] ", shr, srv);
94                 fgets(tmp, sizeof(tmp), stdin);
95                 if(tmp[strlen(tmp)-1] == '\n')tmp[strlen(tmp)-1] = '\0';
96                 strncpy(un, tmp, unlen-1);
97         } else if(username) strncpy(un, username, unlen-1);
98
99         if(!nonprompt && !password) {
100                 char *prompt, *pass;
101                 asprintf(&prompt, "Password for %s at %s: ", shr, srv);
102                 pass = getpass(prompt);
103                 free(prompt);
104                 strncpy(pw, pass, pwlen-1);
105         } else if(password) strncpy(pw, password, pwlen-1);
106
107         if(workgroup)strncpy(wg, workgroup, wglen-1);
108
109         wgtmp = SMB_STRNDUP(wg, wglen); 
110         usertmp = SMB_STRNDUP(un, unlen);
111         if(!quiet)printf("Using workgroup %s, %s%s\n", wgtmp, *usertmp?"user ":"guest user", usertmp);
112         free(wgtmp); free(usertmp);
113 }
114
115 static int smb_download_dir(const char *base, const char *name, int resume)
116 {
117         char path[SMB_MAXPATHLEN];
118         int dirhandle;
119         struct smbc_dirent *dirent;
120         const char *relname = name;
121         char *tmpname;
122         struct stat remotestat;
123         snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (base[0] && name[0] && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name);
124
125         /* List files in directory and call smb_download_file on them */
126         dirhandle = smbc_opendir(path);
127         if(dirhandle < 1) {
128                 if(errno == ENOTDIR) return smb_download_file(base, name, 1, resume, NULL);
129                 fprintf(stderr, "Can't open directory %s: %s\n", path, strerror(errno));
130                 return 0;
131         }
132
133         while(*relname == '/')relname++;
134         mkdir(relname, 0755);
135         
136         tmpname = SMB_STRDUP(name);
137
138         while((dirent = smbc_readdir(dirhandle))) {
139                 char *newname;
140                 if(!strcmp(dirent->name, ".") || !strcmp(dirent->name, ".."))continue;
141                 asprintf(&newname, "%s/%s", tmpname, dirent->name);
142                 switch(dirent->smbc_type) {
143                 case SMBC_DIR:
144                         smb_download_dir(base, newname, resume);
145                         break;
146
147                 case SMBC_WORKGROUP:
148                         smb_download_dir("smb://", dirent->name, resume);
149                         break;
150
151                 case SMBC_SERVER:
152                         smb_download_dir("smb://", dirent->name, resume);
153                         break;
154
155                 case SMBC_FILE:
156                         smb_download_file(base, newname, 1, resume, NULL);
157                         break;
158
159                 case SMBC_FILE_SHARE:
160                         smb_download_dir(base, newname, resume);
161                         break;
162
163                 case SMBC_PRINTER_SHARE:
164                         if(!quiet)printf("Ignoring printer share %s\n", dirent->name);
165                         break;
166
167                 case SMBC_COMMS_SHARE:
168                         if(!quiet)printf("Ignoring comms share %s\n", dirent->name);
169                         break;
170                         
171                 case SMBC_IPC_SHARE:
172                         if(!quiet)printf("Ignoring ipc$ share %s\n", dirent->name);
173                         break;
174
175                 default:
176                         fprintf(stderr, "Ignoring file '%s' of type '%d'\n", newname, dirent->smbc_type);
177                         break;
178                 }
179                 free(newname);
180         }
181         free(tmpname);
182
183         if(keep_permissions) {
184                 if(smbc_fstat(dirhandle, &remotestat) < 0) {
185                         fprintf(stderr, "Unable to get stats on %s on remote server\n", path);
186                         smbc_closedir(dirhandle);
187                         return 0;
188                 }
189                 
190                 if(chmod(relname, remotestat.st_mode) < 0) {
191                         fprintf(stderr, "Unable to change mode of local dir %s to %o\n", relname, remotestat.st_mode);
192                         smbc_closedir(dirhandle);
193                         return 0;
194                 }
195         }
196
197         smbc_closedir(dirhandle);
198         return 1;
199 }
200
201 static char *print_time(long t)
202 {
203         static char buffer[100];
204         int secs, mins, hours;
205         if(t < -1) {
206                 strncpy(buffer, "Unknown", sizeof(buffer));
207                 return buffer;
208         }
209
210         secs = (int)t % 60;
211         mins = (int)t / 60 % 60;
212         hours = (int)t / (60 * 60);
213         snprintf(buffer, sizeof(buffer)-1, "%02d:%02d:%02d", hours, mins, secs);
214         return buffer;
215 }
216
217 static void print_progress(const char *name, time_t start, time_t now, off_t start_pos, off_t pos, off_t total)
218 {
219         double avg = 0.0;
220         long  eta = -1; 
221         double prcnt = 0.0;
222         char hpos[20], htotal[20], havg[20];
223         char *status, *filename;
224         int len;
225         if(now - start)avg = 1.0 * (pos - start_pos) / (now - start);
226         eta = (total - pos) / avg;
227         if(total)prcnt = 100.0 * pos / total;
228
229         human_readable(pos, hpos, sizeof(hpos));
230         human_readable(total, htotal, sizeof(htotal));
231         human_readable(avg, havg, sizeof(havg));
232
233         len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos, htotal, prcnt, havg, print_time(eta));
234         
235         if(columns) {
236                 int required = strlen(name), available = columns - len - strlen("[] ");
237                 if(required > available) asprintf(&filename, "...%s", name + required - available + 3);
238                 else filename = SMB_STRNDUP(name, available);
239         } else filename = SMB_STRDUP(name);
240
241         fprintf(stderr, "\r[%s] %s", filename, status);
242
243         free(filename); free(status);
244 }
245
246 static int smb_download_file(const char *base, const char *name, int recursive, int resume, char *outfile) {
247         int remotehandle, localhandle;
248         time_t start_time = time(NULL);
249         const char *newpath;
250         char path[SMB_MAXPATHLEN];
251         char checkbuf[2][RESUME_CHECK_SIZE];
252         char *readbuf = NULL;
253         off_t offset_download = 0, offset_check = 0, curpos = 0, start_offset = 0;
254         struct stat localstat, remotestat;
255
256         snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (*base && *name && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name);
257         
258         remotehandle = smbc_open(path, O_RDONLY, 0755);
259
260         if(remotehandle < 0) {
261                 switch(errno) {
262                 case EISDIR: 
263                         if(!recursive) {
264                                 fprintf(stderr, "%s is a directory. Specify -R to download recursively\n", path);
265                                 return 0;
266                         }
267                         smb_download_dir(base, name, resume);
268                         return 0;
269
270                 case ENOENT:
271                         fprintf(stderr, "%s can't be found on the remote server\n", path);
272                         return 0;
273
274                 case ENOMEM:
275                         fprintf(stderr, "Not enough memory\n");
276                         exit(1);
277                         return 0;
278
279                 case ENODEV:
280                         fprintf(stderr, "The share name used in %s does not exist\n", path);
281                         return 0;
282
283                 case EACCES:
284                         fprintf(stderr, "You don't have enough permissions to access %s\n", path);
285                         return 0;
286
287                 default:
288                         perror("smbc_open");
289                         return 0;
290                 }
291         } 
292
293         if(smbc_fstat(remotehandle, &remotestat) < 0) {
294                 fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
295                 return 0;
296         }
297
298         if(outfile) newpath = outfile;
299         else if(!name[0]) {
300                 newpath = strrchr(base, '/');
301                 if(newpath)newpath++; else newpath = base;
302         } else newpath = name;
303
304         if(newpath[0] == '/')newpath++;
305         
306         /* Open local file according to the mode */
307         if(update) {
308                 /* if it is up-to-date, skip */
309                 if(stat(newpath, &localstat) == 0 &&
310                                 localstat.st_mtime >= remotestat.st_mtime) {
311                         if(verbose)
312                                 printf("%s is up-to-date, skipping\n", newpath);
313                         smbc_close(remotehandle);
314                         return 0;
315                 }
316                 /* else open it for writing and truncate if it exists */
317                 localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | O_TRUNC, 0775);
318                 if(localhandle < 0) {
319                         fprintf(stderr, "Can't open %s : %s\n", newpath,
320                                         strerror(errno));
321                         smbc_close(remotehandle);
322                         return 0;
323                 }
324                 /* no offset */
325         } else if(!send_stdout) {
326                 localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | (!resume?O_EXCL:0), 0755);
327                 if(localhandle < 0) {
328                         fprintf(stderr, "Can't open %s: %s\n", newpath, strerror(errno));
329                         smbc_close(remotehandle);
330                         return 0;
331                 }
332         
333                 if (fstat(localhandle, &localstat) != 0) {
334                         fprintf(stderr, "Can't fstat %s: %s\n", newpath, strerror(errno));
335                         smbc_close(remotehandle);
336                         close(localhandle);
337                         return 0;
338                 }
339
340                 start_offset = localstat.st_size;
341
342                 if(localstat.st_size && localstat.st_size == remotestat.st_size) {
343                         if(verbose)fprintf(stderr, "%s is already downloaded completely.\n", path);
344                         else if(!quiet)fprintf(stderr, "%s\n", path);
345                         smbc_close(remotehandle);
346                         close(localhandle);
347                         return 1;
348                 }
349
350                 if(localstat.st_size > RESUME_CHECK_OFFSET && remotestat.st_size > RESUME_CHECK_OFFSET) {
351                         offset_download = localstat.st_size - RESUME_DOWNLOAD_OFFSET;
352                         offset_check = localstat.st_size - RESUME_CHECK_OFFSET;
353                         if(verbose)printf("Trying to start resume of %s at "OFF_T_FORMAT"\n"
354                                    "At the moment "OFF_T_FORMAT" of "OFF_T_FORMAT" bytes have been retrieved\n",
355                                 newpath, (OFF_T_FORMAT_CAST)offset_check, 
356                                 (OFF_T_FORMAT_CAST)localstat.st_size,
357                                 (OFF_T_FORMAT_CAST)remotestat.st_size);
358                 }
359
360                 if(offset_check) { 
361                         off_t off1, off2;
362                         /* First, check all bytes from offset_check to offset_download */
363                         off1 = lseek(localhandle, offset_check, SEEK_SET);
364                         if(off1 < 0) {
365                                 fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in local file %s\n",
366                                         (OFF_T_FORMAT_CAST)offset_check, newpath);
367                                 smbc_close(remotehandle); close(localhandle);
368                                 return 0;
369                         }
370
371                         off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET); 
372                         if(off2 < 0) {
373                                 fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in remote file %s\n",
374                                         (OFF_T_FORMAT_CAST)offset_check, newpath);
375                                 smbc_close(remotehandle); close(localhandle);
376                                 return 0;
377                         }
378
379                         if(off1 != off2) {
380                                 fprintf(stderr, "Offset in local and remote files is different (local: "OFF_T_FORMAT", remote: "OFF_T_FORMAT")\n",
381                                         (OFF_T_FORMAT_CAST)off1,
382                                         (OFF_T_FORMAT_CAST)off2);
383                                 return 0;
384                         }
385
386                         if(smbc_read(remotehandle, checkbuf[0], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
387                                 fprintf(stderr, "Can't read %d bytes from remote file %s\n", RESUME_CHECK_SIZE, path);
388                                 smbc_close(remotehandle); close(localhandle);
389                                 return 0;
390                         }
391
392                         if(read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
393                                 fprintf(stderr, "Can't read %d bytes from local file %s\n", RESUME_CHECK_SIZE, name);
394                                 smbc_close(remotehandle); close(localhandle);
395                                 return 0;
396                         }
397
398                         if(memcmp(checkbuf[0], checkbuf[1], RESUME_CHECK_SIZE) == 0) {
399                                 if(verbose)printf("Current local and remote file appear to be the same. Starting download from offset "OFF_T_FORMAT"\n", (OFF_T_FORMAT_CAST)offset_download);
400                         } else {
401                                 fprintf(stderr, "Local and remote file appear to be different, not doing resume for %s\n", path);
402                                 smbc_close(remotehandle); close(localhandle);
403                                 return 0;
404                         }
405                 }
406         } else {
407                 localhandle = STDOUT_FILENO;
408                 start_offset = 0;
409                 offset_download = 0;
410                 offset_check = 0;
411         }
412
413         readbuf = (char *)SMB_MALLOC(blocksize);
414
415         /* Now, download all bytes from offset_download to the end */
416         for(curpos = offset_download; curpos < remotestat.st_size; curpos+=blocksize) {
417                 ssize_t bytesread = smbc_read(remotehandle, readbuf, blocksize);
418                 if(bytesread < 0) {
419                         fprintf(stderr, "Can't read %u bytes at offset "OFF_T_FORMAT", file %s\n", (unsigned int)blocksize, (OFF_T_FORMAT_CAST)curpos, path);
420                         smbc_close(remotehandle);
421                         if (localhandle != STDOUT_FILENO) close(localhandle);
422                         free(readbuf);
423                         return 0;
424                 }
425
426                 total_bytes += bytesread;
427
428                 if(write(localhandle, readbuf, bytesread) < 0) {
429                         fprintf(stderr, "Can't write %u bytes to local file %s at offset "OFF_T_FORMAT"\n", (unsigned int)bytesread, path, (OFF_T_FORMAT_CAST)curpos);
430                         free(readbuf);
431                         smbc_close(remotehandle);
432                         if (localhandle != STDOUT_FILENO) close(localhandle);
433                         return 0;
434                 }
435
436                 if(dots)fputc('.', stderr);
437                 else if(!quiet) {
438                         print_progress(newpath, start_time, time(NULL), start_offset, curpos, remotestat.st_size);
439                 }
440         }
441
442         free(readbuf);
443
444         if(dots){
445                 fputc('\n', stderr);
446                 printf("%s downloaded\n", path);
447         } else if(!quiet) {
448                 int i;
449                 fprintf(stderr, "\r%s", path);
450                 if(columns) {
451                         for(i = strlen(path); i < columns; i++) {
452                                 fputc(' ', stderr);
453                         }
454                 }
455                 fputc('\n', stderr);
456         }
457
458         if(keep_permissions && !send_stdout) {
459                 if(fchmod(localhandle, remotestat.st_mode) < 0) {
460                         fprintf(stderr, "Unable to change mode of local file %s to %o\n", path, remotestat.st_mode);
461                         smbc_close(remotehandle);
462                         close(localhandle);
463                         return 0;
464                 }
465         }
466
467         smbc_close(remotehandle);
468         if (localhandle != STDOUT_FILENO) close(localhandle);
469         return 1;
470 }
471
472 static void clean_exit(void)
473 {
474         char bs[100];
475         human_readable(total_bytes, bs, sizeof(bs));
476         if(!quiet)fprintf(stderr, "Downloaded %s in %lu seconds\n", bs, time(NULL) - total_start_time);
477         exit(0);
478 }
479
480 static void signal_quit(int v)
481 {
482         clean_exit();
483 }
484
485 static int readrcfile(const char *name, const struct poptOption long_options[])
486 {
487         FILE *fd = fopen(name, "r");
488         int lineno = 0, i;
489         char var[101], val[101];
490         char found;
491         int *intdata; char **stringdata;
492         if(!fd) {
493                 fprintf(stderr, "Can't open RC file %s\n", name);
494                 return 1;
495         }
496
497         while(!feof(fd)) {
498                 lineno++;
499                 if(fscanf(fd, "%100s %100s\n", var, val) < 2) {
500                         fprintf(stderr, "Can't parse line %d of %s, ignoring.\n", lineno, name);
501                         continue;
502                 }
503
504                 found = 0;
505
506                 for(i = 0; long_options[i].shortName; i++) {
507                         if(!long_options[i].longName)continue;
508                         if(strcmp(long_options[i].longName, var)) continue;
509                         if(!long_options[i].arg)continue;
510
511                         switch(long_options[i].argInfo) {
512                         case POPT_ARG_NONE:
513                                 intdata = (int *)long_options[i].arg;
514                                 if(!strcmp(val, "on")) *intdata = 1;
515                                 else if(!strcmp(val, "off")) *intdata = 0;
516                                 else fprintf(stderr, "Illegal value %s for %s at line %d in %s\n", val, var, lineno, name);
517                                 break;
518                         case POPT_ARG_INT:
519                                 intdata = (int *)long_options[i].arg;
520                                 *intdata = atoi(val);
521                                 break;
522                         case POPT_ARG_STRING:
523                                 stringdata = (char **)long_options[i].arg;
524                                 *stringdata = SMB_STRDUP(val);
525                                 break;
526                         default:
527                                 fprintf(stderr, "Invalid variable %s at line %d in %s\n", var, lineno, name);
528                                 break;
529                         }
530
531                         found = 1;
532                 }
533                 if(!found) {
534                         fprintf(stderr, "Invalid variable %s at line %d in %s\n", var, lineno, name);
535                 }
536         }
537
538         fclose(fd);
539         return 0;
540 }
541
542 int main(int argc, const char **argv)
543 {
544         int c = 0;
545         const char *file = NULL;
546         char *rcfile = NULL;
547         bool smb_encrypt = false;
548         int resume = 0, recursive = 0;
549         TALLOC_CTX *frame = talloc_stackframe();
550         struct poptOption long_options[] = {
551                 {"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" },        
552                 {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" },      
553                 {"resume", 'r', POPT_ARG_NONE, &resume, 0, "Automatically resume aborted files" },
554                 {"update", 'U',  POPT_ARG_NONE, &update, 0, "Download only when remote file is newer than local file or local file is missing"},
555                 {"recursive", 'R',  POPT_ARG_NONE, &recursive, 0, "Recursively download files" },
556                 {"username", 'u', POPT_ARG_STRING, &username, 'u', "Username to use" },
557                 {"password", 'p', POPT_ARG_STRING, &password, 'p', "Password to use" },
558                 {"workgroup", 'w', POPT_ARG_STRING, &workgroup, 'w', "Workgroup to use (optional)" },
559                 {"nonprompt", 'n', POPT_ARG_NONE, &nonprompt, 'n', "Don't ask anything (non-interactive)" },
560                 {"debuglevel", 'd', POPT_ARG_INT, &debuglevel, 'd', "Debuglevel to use" },
561                 {"outputfile", 'o', POPT_ARG_STRING, &outputfile, 'o', "Write downloaded data to specified file" },
562                 {"stdout", 'O', POPT_ARG_NONE, &send_stdout, 'O', "Write data to stdout" },
563                 {"dots", 'D', POPT_ARG_NONE, &dots, 'D', "Show dots as progress indication" },
564                 {"quiet", 'q', POPT_ARG_NONE, &quiet, 'q', "Be quiet" },
565                 {"verbose", 'v', POPT_ARG_NONE, &verbose, 'v', "Be verbose" },
566                 {"keep-permissions", 'P', POPT_ARG_NONE, &keep_permissions, 'P', "Keep permissions" },
567                 {"blocksize", 'b', POPT_ARG_INT, &blocksize, 'b', "Change number of bytes in a block"},
568                 {"rcfile", 'f', POPT_ARG_STRING, NULL, 0, "Use specified rc file"},
569                 POPT_AUTOHELP
570                 POPT_TABLEEND
571         };
572         poptContext pc;
573
574         load_case_tables();
575
576         /* only read rcfile if it exists */
577         asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME"));
578         if(access(rcfile, F_OK) == 0) 
579                 readrcfile(rcfile, long_options);
580         free(rcfile);
581
582 #ifdef SIGWINCH
583         signal(SIGWINCH, change_columns);
584 #endif
585         signal(SIGINT, signal_quit);
586         signal(SIGTERM, signal_quit);
587
588         pc = poptGetContext(argv[0], argc, argv, long_options, 0);
589
590         while((c = poptGetNextOpt(pc)) >= 0) {
591                 switch(c) {
592                 case 'f':
593                         readrcfile(poptGetOptArg(pc), long_options);
594                         break;
595                 case 'a':
596                         username = ""; password = "";
597                         break;
598                 case 'e':
599                         smb_encrypt = true;
600                         break;
601                 }
602         }
603
604         if((send_stdout || resume || outputfile) && update) {
605                 fprintf(stderr, "The -o, -R or -O and -U options can not be used together.\n");
606                 return 1;
607         }
608         if((send_stdout || outputfile) && recursive) {
609                 fprintf(stderr, "The -o or -O and -R options can not be used together.\n");
610                 return 1;
611         }
612
613         if(outputfile && send_stdout) {
614                 fprintf(stderr, "The -o and -O options cannot be used together.\n");
615                 return 1;
616         }
617
618         if(smbc_init(get_auth_data, debuglevel) < 0) {
619                 fprintf(stderr, "Unable to initialize libsmbclient\n");
620                 return 1;
621         }
622
623         if (smb_encrypt) {
624                 SMBCCTX *smb_ctx = smbc_set_context(NULL);
625                 smbc_option_set(smb_ctx,
626                         CONST_DISCARD(char *, "smb_encrypt_level"),
627                         "require");
628         }
629         
630         columns = get_num_cols();
631
632         total_start_time = time(NULL);
633
634         while ( (file = poptGetArg(pc)) ) {
635                 if (!recursive) 
636                         return smb_download_file(file, "", recursive, resume, outputfile);
637                 else 
638                         return smb_download_dir(file, "", resume);
639         }
640
641         clean_exit();
642         TALLOC_FREE(frame);
643         return 0;
644 }