smbget: Fix file descriptor leak.
[mat/samba.git] / source3 / utils / smbget.c
index 4a2670e0c169b5241c1a93c6c8ec25c3d1730277..5cb6849bf7dcfd9ea700875d0e1e0e27079c7180 100644 (file)
@@ -5,19 +5,20 @@
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "includes.h"
+#include "system/filesys.h"
+#include "popt_common.h"
 #include "libsmbclient.h"
 
 #if _FILE_OFFSET_BITS==64
 #define OFF_T_FORMAT_CAST long
 #endif
 
-int columns = 0;
+static int columns = 0;
 
-static int _resume, _recursive, debuglevel;
+static int debuglevel, update;
 static char *outputfile;
 
 
-time_t total_start_time = 0;
-off_t total_bytes = 0;
+static time_t total_start_time = 0;
+static off_t total_bytes = 0;
 
 #define SMB_MAXPATHLEN MAXPATHLEN
 
@@ -46,13 +47,14 @@ off_t total_bytes = 0;
 /* Number of bytes to read at once */
 #define SMB_DEFAULT_BLOCKSIZE                                  64000
 
-const char *username = NULL, *password = NULL, *workgroup = NULL;
-int nonprompt = 0, quiet = 0, dots = 0, keep_permissions = 0, verbose = 0, send_stdout = 0;
-int blocksize = SMB_DEFAULT_BLOCKSIZE;
+static const char *username = NULL, *password = NULL, *workgroup = NULL;
+static int nonprompt = 0, quiet = 0, dots = 0, keep_permissions = 0, verbose = 0, send_stdout = 0;
+static int blocksize = SMB_DEFAULT_BLOCKSIZE;
 
-int smb_download_file(const char *base, const char *name, int recursive, int resume, char *outfile);
+static int smb_download_file(const char *base, const char *name, int recursive,
+                            int resume, int toplevel, char *outfile);
 
-int get_num_cols(void)
+static int get_num_cols(void)
 {
 #ifdef TIOCGWINSZ
        struct winsize ws;
@@ -68,20 +70,25 @@ int get_num_cols(void)
 #endif
 }
 
-void change_columns(int sig)
+static void change_columns(int sig)
 {
        columns = get_num_cols();
 }
 
-void human_readable(off_t s, char *buffer, int l)
+static void human_readable(off_t s, char *buffer, int l)
 {
-       if(s > 1024 * 1024 * 1024) snprintf(buffer, l, "%.2fGb", 1.0 * s / (1024 * 1024 * 1024));
-       else if(s > 1024 * 1024) snprintf(buffer, l, "%.2fMb", 1.0 * s / (1024 * 1024));
-       else if(s > 1024) snprintf(buffer, l, "%.2fkb", 1.0 * s / 1024);
-       else snprintf(buffer, l, OFF_T_FORMAT"b", (OFF_T_FORMAT_CAST)s);
+       if (s > 1024 * 1024 * 1024) {
+               snprintf(buffer, l, "%.2fGB", 1.0 * s / (1024 * 1024 * 1024));
+       } else if (s > 1024 * 1024) {
+               snprintf(buffer, l, "%.2fMB", 1.0 * s / (1024 * 1024));
+       } else if (s > 1024) {
+               snprintf(buffer, l, "%.2fkB", 1.0 * s / 1024);
+       } else {
+               snprintf(buffer, l, OFF_T_FORMAT"b", (OFF_T_FORMAT_CAST)s);
+       }
 }
 
-void get_auth_data(const char *srv, const char *shr, char *wg, int wglen, char *un, int unlen, char *pw, int pwlen)
+static void get_auth_data(const char *srv, const char *shr, char *wg, int wglen, char *un, int unlen, char *pw, int pwlen)
 {
        static char hasasked = 0;
        char *wgtmp, *usertmp;
@@ -92,17 +99,22 @@ void get_auth_data(const char *srv, const char *shr, char *wg, int wglen, char *
 
        if(!nonprompt && !username) {
                printf("Username for %s at %s [guest] ", shr, srv);
-               fgets(tmp, sizeof(tmp), stdin);
-               if(tmp[strlen(tmp)-1] == '\n')tmp[strlen(tmp)-1] = '\0';
+               if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
+                       return;
+               }
+               if ((strlen(tmp) > 0) && (tmp[strlen(tmp)-1] == '\n')) {
+                       tmp[strlen(tmp)-1] = '\0';
+               }
                strncpy(un, tmp, unlen-1);
        } else if(username) strncpy(un, username, unlen-1);
 
        if(!nonprompt && !password) {
-               char *prompt, *pass;
-               asprintf(&prompt, "Password for %s at %s: ", shr, srv);
-               pass = getpass(prompt);
+               char *prompt;
+               if (asprintf(&prompt, "Password for %s at %s: ", shr, srv) == -1) {
+                       return;
+               }
+               (void) samba_getpass(prompt, pw, pwlen, false, false);
                free(prompt);
-               strncpy(pw, pass, pwlen-1);
        } else if(password) strncpy(pw, password, pwlen-1);
 
        if(workgroup)strncpy(wg, workgroup, wglen-1);
@@ -113,7 +125,9 @@ void get_auth_data(const char *srv, const char *shr, char *wg, int wglen, char *
        free(wgtmp); free(usertmp);
 }
 
-int smb_download_dir(const char *base, const char *name, int resume)
+/* Return 1 on error, 0 on success. */
+
+static int smb_download_dir(const char *base, const char *name, int resume)
 {
        char path[SMB_MAXPATHLEN];
        int dirhandle;
@@ -121,44 +135,52 @@ int smb_download_dir(const char *base, const char *name, int resume)
        const char *relname = name;
        char *tmpname;
        struct stat remotestat;
+       int ret = 0;
+
        snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (base[0] && name[0] && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name);
 
        /* List files in directory and call smb_download_file on them */
        dirhandle = smbc_opendir(path);
        if(dirhandle < 1) {
-               if(errno == ENOTDIR) return smb_download_file(base, name, 1, resume, NULL);
+               if (errno == ENOTDIR) {
+                       return smb_download_file(base, name, 1, resume,
+                                                0, NULL);
+               }
                fprintf(stderr, "Can't open directory %s: %s\n", path, strerror(errno));
-               return 0;
+               return 1;
        }
 
        while(*relname == '/')relname++;
        mkdir(relname, 0755);
-       
+
        tmpname = SMB_STRDUP(name);
 
        while((dirent = smbc_readdir(dirhandle))) {
                char *newname;
                if(!strcmp(dirent->name, ".") || !strcmp(dirent->name, ".."))continue;
-               asprintf(&newname, "%s/%s", tmpname, dirent->name);
+               if (asprintf(&newname, "%s/%s", tmpname, dirent->name) == -1) {
+                       return 1;
+               }
                switch(dirent->smbc_type) {
                case SMBC_DIR:
-                       smb_download_dir(base, newname, resume);
+                       ret = smb_download_dir(base, newname, resume);
                        break;
 
                case SMBC_WORKGROUP:
-                       smb_download_dir("smb://", dirent->name, resume);
+                       ret = smb_download_dir("smb://", dirent->name, resume);
                        break;
 
                case SMBC_SERVER:
-                       smb_download_dir("smb://", dirent->name, resume);
+                       ret = smb_download_dir("smb://", dirent->name, resume);
                        break;
 
                case SMBC_FILE:
-                       smb_download_file(base, newname, 1, resume, NULL);
+                       ret = smb_download_file(base, newname, 1, resume, 0,
+                                               NULL);
                        break;
 
                case SMBC_FILE_SHARE:
-                       smb_download_dir(base, newname, resume);
+                       ret = smb_download_dir(base, newname, resume);
                        break;
 
                case SMBC_PRINTER_SHARE:
@@ -168,7 +190,7 @@ int smb_download_dir(const char *base, const char *name, int resume)
                case SMBC_COMMS_SHARE:
                        if(!quiet)printf("Ignoring comms share %s\n", dirent->name);
                        break;
-                       
+
                case SMBC_IPC_SHARE:
                        if(!quiet)printf("Ignoring ipc$ share %s\n", dirent->name);
                        break;
@@ -185,21 +207,22 @@ int smb_download_dir(const char *base, const char *name, int resume)
                if(smbc_fstat(dirhandle, &remotestat) < 0) {
                        fprintf(stderr, "Unable to get stats on %s on remote server\n", path);
                        smbc_closedir(dirhandle);
-                       return 0;
+                       return 1;
                }
-               
+
                if(chmod(relname, remotestat.st_mode) < 0) {
-                       fprintf(stderr, "Unable to change mode of local dir %s to %o\n", relname, remotestat.st_mode);
+                       fprintf(stderr, "Unable to change mode of local dir %s to %o\n", relname,
+                               (unsigned int)remotestat.st_mode);
                        smbc_closedir(dirhandle);
-                       return 0;
+                       return 1;
                }
        }
 
        smbc_closedir(dirhandle);
-       return 1;
+       return ret;
 }
 
-char *print_time(long t)
+static char *print_time(long t)
 {
        static char buffer[100];
        int secs, mins, hours;
@@ -215,7 +238,7 @@ char *print_time(long t)
        return buffer;
 }
 
-void print_progress(const char *name, time_t start, time_t now, off_t start_pos, off_t pos, off_t total)
+static void print_progress(const char *name, time_t start, time_t now, off_t start_pos, off_t pos, off_t total)
 {
        double avg = 0.0;
        long  eta = -1; 
@@ -232,11 +255,19 @@ void print_progress(const char *name, time_t start, time_t now, off_t start_pos,
        human_readable(avg, havg, sizeof(havg));
 
        len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos, htotal, prcnt, havg, print_time(eta));
-       
+       if (len == -1) {
+               return;
+       }
+
        if(columns) {
                int required = strlen(name), available = columns - len - strlen("[] ");
-               if(required > available) asprintf(&filename, "...%s", name + required - available + 3);
-               else filename = SMB_STRNDUP(name, available);
+               if(required > available) {
+                       if (asprintf(&filename, "...%s", name + required - available + 3) == -1) {
+                               return;
+                       }
+               } else {
+                       filename = SMB_STRNDUP(name, available);
+               }
        } else filename = SMB_STRDUP(name);
 
        fprintf(stderr, "\r[%s] %s", filename, status);
@@ -244,9 +275,13 @@ void print_progress(const char *name, time_t start, time_t now, off_t start_pos,
        free(filename); free(status);
 }
 
-int smb_download_file(const char *base, const char *name, int recursive, int resume, char *outfile) {
+/* Return 1 on error, 0 on success. */
+
+static int smb_download_file(const char *base, const char *name, int recursive,
+                            int resume, int toplevel, char *outfile)
+{
        int remotehandle, localhandle;
-       time_t start_time = time(NULL);
+       time_t start_time = time_mono(NULL);
        const char *newpath;
        char path[SMB_MAXPATHLEN];
        char checkbuf[2][RESUME_CHECK_SIZE];
@@ -255,7 +290,7 @@ int smb_download_file(const char *base, const char *name, int recursive, int res
        struct stat localstat, remotestat;
 
        snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (*base && *name && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name);
-       
+
        remotehandle = smbc_open(path, O_RDONLY, 0755);
 
        if(remotehandle < 0) {
@@ -263,37 +298,35 @@ int smb_download_file(const char *base, const char *name, int recursive, int res
                case EISDIR: 
                        if(!recursive) {
                                fprintf(stderr, "%s is a directory. Specify -R to download recursively\n", path);
-                               return 0;
+                               return 1;
                        }
-                       smb_download_dir(base, name, resume);
-                       return 0;
+                       return smb_download_dir(base, name, resume);
 
                case ENOENT:
                        fprintf(stderr, "%s can't be found on the remote server\n", path);
-                       return 0;
+                       return 1;
 
                case ENOMEM:
                        fprintf(stderr, "Not enough memory\n");
-                       exit(1);
-                       return 0;
+                       return 1;
 
                case ENODEV:
                        fprintf(stderr, "The share name used in %s does not exist\n", path);
-                       return 0;
+                       return 1;
 
                case EACCES:
                        fprintf(stderr, "You don't have enough permissions to access %s\n", path);
-                       return 0;
+                       return 1;
 
                default:
                        perror("smbc_open");
-                       return 0;
+                       return 1;
                }
        } 
 
        if(smbc_fstat(remotehandle, &remotestat) < 0) {
                fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
-               return 0;
+               return 1;
        }
 
        if(outfile) newpath = outfile;
@@ -302,18 +335,43 @@ int smb_download_file(const char *base, const char *name, int recursive, int res
                if(newpath)newpath++; else newpath = base;
        } else newpath = name;
 
-       if(newpath[0] == '/')newpath++;
-       
-       /* Open local file and, if necessary, resume */
-       if(!send_stdout) {
+       if (!toplevel && (newpath[0] == '/')) {
+               newpath++;
+       }
+
+       /* Open local file according to the mode */
+       if(update) {
+               /* if it is up-to-date, skip */
+               if(stat(newpath, &localstat) == 0 &&
+                               localstat.st_mtime >= remotestat.st_mtime) {
+                       if(verbose)
+                               printf("%s is up-to-date, skipping\n", newpath);
+                       smbc_close(remotehandle);
+                       return 0;
+               }
+               /* else open it for writing and truncate if it exists */
+               localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | O_TRUNC, 0775);
+               if(localhandle < 0) {
+                       fprintf(stderr, "Can't open %s : %s\n", newpath,
+                                       strerror(errno));
+                       smbc_close(remotehandle);
+                       return 1;
+               }
+               /* no offset */
+       } else if(!send_stdout) {
                localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | (!resume?O_EXCL:0), 0755);
                if(localhandle < 0) {
                        fprintf(stderr, "Can't open %s: %s\n", newpath, strerror(errno));
                        smbc_close(remotehandle);
-                       return 0;
+                       return 1;
+               }
+
+               if (fstat(localhandle, &localstat) != 0) {
+                       fprintf(stderr, "Can't fstat %s: %s\n", newpath, strerror(errno));
+                       smbc_close(remotehandle);
+                       close(localhandle);
+                       return 1;
                }
-       
-               fstat(localhandle, &localstat);
 
                start_offset = localstat.st_size;
 
@@ -322,7 +380,7 @@ int smb_download_file(const char *base, const char *name, int recursive, int res
                        else if(!quiet)fprintf(stderr, "%s\n", path);
                        smbc_close(remotehandle);
                        close(localhandle);
-                       return 1;
+                       return 0;
                }
 
                if(localstat.st_size > RESUME_CHECK_OFFSET && remotestat.st_size > RESUME_CHECK_OFFSET) {
@@ -343,7 +401,7 @@ int smb_download_file(const char *base, const char *name, int recursive, int res
                                fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in local file %s\n",
                                        (OFF_T_FORMAT_CAST)offset_check, newpath);
                                smbc_close(remotehandle); close(localhandle);
-                               return 0;
+                               return 1;
                        }
 
                        off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET); 
@@ -351,26 +409,27 @@ int smb_download_file(const char *base, const char *name, int recursive, int res
                                fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in remote file %s\n",
                                        (OFF_T_FORMAT_CAST)offset_check, newpath);
                                smbc_close(remotehandle); close(localhandle);
-                               return 0;
+                               return 1;
                        }
 
                        if(off1 != off2) {
                                fprintf(stderr, "Offset in local and remote files is different (local: "OFF_T_FORMAT", remote: "OFF_T_FORMAT")\n",
                                        (OFF_T_FORMAT_CAST)off1,
                                        (OFF_T_FORMAT_CAST)off2);
-                               return 0;
+                               smbc_close(remotehandle); close(localhandle);
+                               return 1;
                        }
 
                        if(smbc_read(remotehandle, checkbuf[0], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
                                fprintf(stderr, "Can't read %d bytes from remote file %s\n", RESUME_CHECK_SIZE, path);
                                smbc_close(remotehandle); close(localhandle);
-                               return 0;
+                               return 1;
                        }
 
                        if(read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
                                fprintf(stderr, "Can't read %d bytes from local file %s\n", RESUME_CHECK_SIZE, name);
                                smbc_close(remotehandle); close(localhandle);
-                               return 0;
+                               return 1;
                        }
 
                        if(memcmp(checkbuf[0], checkbuf[1], RESUME_CHECK_SIZE) == 0) {
@@ -378,7 +437,7 @@ int smb_download_file(const char *base, const char *name, int recursive, int res
                        } else {
                                fprintf(stderr, "Local and remote file appear to be different, not doing resume for %s\n", path);
                                smbc_close(remotehandle); close(localhandle);
-                               return 0;
+                               return 1;
                        }
                }
        } else {
@@ -388,7 +447,13 @@ int smb_download_file(const char *base, const char *name, int recursive, int res
                offset_check = 0;
        }
 
-       readbuf = SMB_MALLOC(blocksize);
+       readbuf = (char *)SMB_MALLOC(blocksize);
+       if (!readbuf) {
+               if (localhandle != STDOUT_FILENO) {
+                       close(localhandle);
+               }
+               return 1;
+       }
 
        /* Now, download all bytes from offset_download to the end */
        for(curpos = offset_download; curpos < remotestat.st_size; curpos+=blocksize) {
@@ -398,7 +463,7 @@ int smb_download_file(const char *base, const char *name, int recursive, int res
                        smbc_close(remotehandle);
                        if (localhandle != STDOUT_FILENO) close(localhandle);
                        free(readbuf);
-                       return 0;
+                       return 1;
                }
 
                total_bytes += bytesread;
@@ -408,12 +473,13 @@ int smb_download_file(const char *base, const char *name, int recursive, int res
                        free(readbuf);
                        smbc_close(remotehandle);
                        if (localhandle != STDOUT_FILENO) close(localhandle);
-                       return 0;
+                       return 1;
                }
 
                if(dots)fputc('.', stderr);
                else if(!quiet) {
-                       print_progress(newpath, start_time, time(NULL), start_offset, curpos, remotestat.st_size);
+                       print_progress(newpath, start_time, time_mono(NULL),
+                                       start_offset, curpos, remotestat.st_size);
                }
        }
 
@@ -435,32 +501,34 @@ int smb_download_file(const char *base, const char *name, int recursive, int res
 
        if(keep_permissions && !send_stdout) {
                if(fchmod(localhandle, remotestat.st_mode) < 0) {
-                       fprintf(stderr, "Unable to change mode of local file %s to %o\n", path, remotestat.st_mode);
+                       fprintf(stderr, "Unable to change mode of local file %s to %o\n", path,
+                               (unsigned int)remotestat.st_mode);
                        smbc_close(remotehandle);
                        close(localhandle);
-                       return 0;
+                       return 1;
                }
        }
 
        smbc_close(remotehandle);
        if (localhandle != STDOUT_FILENO) close(localhandle);
-       return 1;
+       return 0;
 }
 
-void clean_exit(void)
+static void clean_exit(void)
 {
        char bs[100];
        human_readable(total_bytes, bs, sizeof(bs));
-       if(!quiet)fprintf(stderr, "Downloaded %s in %lu seconds\n", bs, time(NULL) - total_start_time);
+       if(!quiet)fprintf(stderr, "Downloaded %s in %lu seconds\n", bs,
+               (unsigned long)(time_mono(NULL) - total_start_time));
        exit(0);
 }
 
-void signal_quit(int v)
+static void signal_quit(int v)
 {
        clean_exit();
 }
 
-int readrcfile(const char *name, const struct poptOption long_options[])
+static int readrcfile(const char *name, const struct poptOption long_options[])
 {
        FILE *fd = fopen(name, "r");
        int lineno = 0, i;
@@ -522,10 +590,16 @@ int main(int argc, const char **argv)
        int c = 0;
        const char *file = NULL;
        char *rcfile = NULL;
+       bool smb_encrypt = false;
+       int resume = 0, recursive = 0;
+       TALLOC_CTX *frame = talloc_stackframe();
+       int ret = 0;
        struct poptOption long_options[] = {
                {"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" },        
-               {"resume", 'r', POPT_ARG_NONE, &_resume, 0, "Automatically resume aborted files" },
-               {"recursive", 'R',  POPT_ARG_NONE, &_recursive, 0, "Recursively download files" },
+               {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" },      
+               {"resume", 'r', POPT_ARG_NONE, &resume, 0, "Automatically resume aborted files" },
+               {"update", 'U',  POPT_ARG_NONE, &update, 0, "Download only when remote file is newer than local file or local file is missing"},
+               {"recursive", 'R',  POPT_ARG_NONE, &recursive, 0, "Recursively download files" },
                {"username", 'u', POPT_ARG_STRING, &username, 'u', "Username to use" },
                {"password", 'p', POPT_ARG_STRING, &password, 'p', "Password to use" },
                {"workgroup", 'w', POPT_ARG_STRING, &workgroup, 'w', "Workgroup to use (optional)" },
@@ -538,7 +612,7 @@ int main(int argc, const char **argv)
                {"verbose", 'v', POPT_ARG_NONE, &verbose, 'v', "Be verbose" },
                {"keep-permissions", 'P', POPT_ARG_NONE, &keep_permissions, 'P', "Keep permissions" },
                {"blocksize", 'b', POPT_ARG_INT, &blocksize, 'b', "Change number of bytes in a block"},
-               {"rcfile", 'f', POPT_ARG_STRING, NULL, 0, "Use specified rc file"},
+               {"rcfile", 'f', POPT_ARG_STRING, NULL, 'f', "Use specified rc file"},
                POPT_AUTOHELP
                POPT_TABLEEND
        };
@@ -547,7 +621,9 @@ int main(int argc, const char **argv)
        load_case_tables();
 
        /* only read rcfile if it exists */
-       asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME"));
+       if (asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME")) == -1) {
+               return 1;
+       }
        if(access(rcfile, F_OK) == 0) 
                readrcfile(rcfile, long_options);
        free(rcfile);
@@ -568,10 +644,17 @@ int main(int argc, const char **argv)
                case 'a':
                        username = ""; password = "";
                        break;
+               case 'e':
+                       smb_encrypt = true;
+                       break;
                }
        }
 
-       if((send_stdout || outputfile) && _recursive) {
+       if((send_stdout || resume || outputfile) && update) {
+               fprintf(stderr, "The -o, -R or -O and -U options can not be used together.\n");
+               return 1;
+       }
+       if((send_stdout || outputfile) && recursive) {
                fprintf(stderr, "The -o or -O and -R options can not be used together.\n");
                return 1;
        }
@@ -586,18 +669,28 @@ int main(int argc, const char **argv)
                return 1;
        }
 
+       if (smb_encrypt) {
+               SMBCCTX *smb_ctx = smbc_set_context(NULL);
+               smbc_option_set(smb_ctx,
+                       discard_const_p(char, "smb_encrypt_level"),
+                       "require");
+       }
+
        columns = get_num_cols();
 
-       total_start_time = time(NULL);
+       total_start_time = time_mono(NULL);
 
        while ( (file = poptGetArg(pc)) ) {
-               if (!_recursive) 
-                       return smb_download_file(file, "", _recursive, _resume, outputfile);
+               if (!recursive) 
+                       ret = smb_download_file(file, "", recursive, resume,
+                                               1, outputfile);
                else 
-                       return smb_download_dir(file, "", _resume);
+                       ret = smb_download_dir(file, "", resume);
        }
 
-       clean_exit();
-
-       return 0;
+       TALLOC_FREE(frame);
+       if ( ret == 0){
+               clean_exit();
+       }
+       return ret;
 }