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