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