r25398: Parse loadparm context to all lp_*() functions.
[ira/wip.git] / source4 / client / client.c
index 20542c0183723e1f46bd1561cd53a84794aa6e79..60030a1d27d2ed283a4c1be26c193bd72cbe1338 100644 (file)
@@ -8,7 +8,7 @@
    
    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,
    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/>.
 */
 
+/* 
+ * TODO: remove this ... and don't use talloc_append_string()
+ *
+ * NOTE: I'm not changing the code yet, because I assume there're
+ *       some bugs in the existing code and I'm not sure how to fix
+ *      them correctly.
+ */
+#define TALLOC_DEPRECATED 1
+
 #include "includes.h"
 #include "version.h"
 #include "libcli/libcli.h"
 #include "libcli/util/clilsa.h"
 #include "system/dir.h"
 #include "system/filesys.h"
-#include "dlinklist.h"
+#include "lib/util/dlinklist.h"
 #include "system/readline.h"
+#include "auth/credentials/credentials.h"
 #include "auth/gensec/gensec.h"
 #include "system/time.h" /* needed by some systems for asctime() */
 #include "libcli/resolve/resolve.h"
 #include "libcli/security/security.h"
 #include "lib/smbreadline/smbreadline.h"
 #include "librpc/gen_ndr/ndr_nbt.h"
+#include "param/param.h"
 
 static int io_bufsize = 64512;
 
@@ -71,7 +81,7 @@ static double dir_total;
 /*******************************************************************
  Reduce a file name, removing .. elements.
 ********************************************************************/
-void dos_clean_name(char *s)
+static void dos_clean_name(char *s)
 {
        char *p=NULL,*r;
 
@@ -97,7 +107,7 @@ number taken from the buffer. This may not equal the number written.
 ****************************************************************************/
 static int writefile(int f, const void *_b, int n, BOOL translation)
 {
-       const uint8_t *b = _b;
+       const uint8_t *b = (const uint8_t *)_b;
        int i;
 
        if (!translation) {
@@ -125,7 +135,7 @@ static int writefile(int f, const void *_b, int n, BOOL translation)
 ****************************************************************************/
 static int readfile(void *_b, int n, XFILE *f, BOOL translation)
 {
-       uint8_t *b = _b;
+       uint8_t *b = (uint8_t *)_b;
        int i;
        int c;
 
@@ -280,11 +290,11 @@ static int cmd_cd(struct smbclient_context *ctx, const char **args)
 }
 
 
-BOOL mask_match(struct smbcli_state *c, const char *string, const char *pattern
-               BOOL is_case_sensitive)
+static bool mask_match(struct smbcli_state *c, const char *string
+               const char *pattern, bool is_case_sensitive)
 {
        char *p2, *s2;
-       BOOL ret;
+       bool ret;
 
        if (ISDOTDOT(string))
                string = ".";
@@ -396,7 +406,7 @@ static void init_do_list_queue(void)
 {
        reset_do_list_queue();
        do_list_queue_size = 1024;
-       do_list_queue = malloc(do_list_queue_size);
+       do_list_queue = malloc_array_p(char, do_list_queue_size);
        if (do_list_queue == 0) { 
                d_printf("malloc fail for size %d\n",
                         (int)do_list_queue_size);
@@ -490,7 +500,7 @@ a helper for do_list
   ****************************************************************************/
 static void do_list_helper(struct clilist_file_info *f, const char *mask, void *state)
 {
-       struct smbclient_context *ctx = state;
+       struct smbclient_context *ctx = (struct smbclient_context *)state;
 
        if (f->attrib & FILE_ATTRIBUTE_DIRECTORY) {
                if (do_list_dirs && do_this_one(ctx, f)) {
@@ -506,7 +516,7 @@ static void do_list_helper(struct clilist_file_info *f, const char *mask, void *
                        p = strrchr_m(mask2,'\\');
                        if (!p) return;
                        p[1] = 0;
-                       mask2 = talloc_asprintf_append(mask2, "%s\\*", f->name);
+                       mask2 = talloc_asprintf_append_buffer(mask2, "%s\\*", f->name);
                        add_to_do_list_queue(mask2);
                }
                return;
@@ -860,7 +870,7 @@ static void do_mget(struct smbclient_context *ctx, struct clilist_file_info *fin
        /* handle directories */
        saved_curdir = talloc_strdup(NULL, ctx->remote_cur_dir);
 
-       ctx->remote_cur_dir = talloc_asprintf_append(NULL, "%s\\", finfo->name);
+       ctx->remote_cur_dir = talloc_asprintf_append_buffer(NULL, "%s\\", finfo->name);
 
        string_replace(discard_const_p(char, finfo->name), '\\', '/');
        if (ctx->lowercase) {
@@ -1194,16 +1204,16 @@ static int cmd_put(struct smbclient_context *ctx, const char **args)
        char *rname;
        
        if (!args[1]) {
-               d_printf("put <filename>\n");
+               d_printf("put <filename> [<remotename>]\n");
                return 1;
        }
 
-       lname = talloc_asprintf(ctx, "%s\\%s", ctx->remote_cur_dir, args[1]);
+       lname = talloc_strdup(ctx, args[1]);
   
        if (args[2])
                rname = talloc_strdup(ctx, args[2]);
        else
-               rname = talloc_strdup(ctx, lname);
+               rname = talloc_asprintf(ctx, "%s\\%s", ctx->remote_cur_dir, lname);
        
        dos_clean_name(rname);
 
@@ -1725,6 +1735,7 @@ static int cmd_allinfo(struct smbclient_context *ctx, const char **args)
        char *fname;
        union smb_fileinfo finfo;
        NTSTATUS status;
+       int fnum;
 
        if (!args[1]) {
                d_printf("allinfo <filename>\n");
@@ -1808,6 +1819,41 @@ static int cmd_allinfo(struct smbclient_context *ctx, const char **args)
                d_printf("\tcluster_shift   %ld\n", (long)finfo.compression_info.out.cluster_shift);
        }
 
+       /* shadow copies if available */
+       fnum = smbcli_open(ctx->cli->tree, fname, O_RDONLY, DENY_NONE);
+       if (fnum != -1) {
+               struct smb_shadow_copy info;
+               int i;
+               info.in.file.fnum = fnum;
+               info.in.max_data = ~0;
+               status = smb_raw_shadow_data(ctx->cli->tree, ctx, &info);
+               if (NT_STATUS_IS_OK(status)) {
+                       d_printf("\tshadow_copy: %u volumes  %u names\n",
+                                info.out.num_volumes, info.out.num_names);
+                       for (i=0;i<info.out.num_names;i++) {
+                               d_printf("\t%s\n", info.out.names[i]);
+                               finfo.generic.level = RAW_FILEINFO_ALL_INFO;
+                               finfo.generic.in.file.path = talloc_asprintf(ctx, "%s%s", 
+                                                                            info.out.names[i], fname); 
+                               status = smb_raw_pathinfo(ctx->cli->tree, ctx, &finfo);
+                               if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_PATH_NOT_FOUND) ||
+                                   NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
+                                       continue;
+                               }
+                               if (!NT_STATUS_IS_OK(status)) {
+                                       d_printf("%s - %s\n", finfo.generic.in.file.path, 
+                                                nt_errstr(status));
+                                       return 1;
+                               }
+                               
+                               d_printf("\t\tcreate_time:    %s\n", nt_time_string(ctx, finfo.all_info.out.create_time));
+                               d_printf("\t\twrite_time:     %s\n", nt_time_string(ctx, finfo.all_info.out.write_time));
+                               d_printf("\t\tchange_time:    %s\n", nt_time_string(ctx, finfo.all_info.out.change_time));
+                               d_printf("\t\tsize:           %lu\n", (unsigned long)finfo.all_info.out.size);
+                       }
+               }
+       }
+       
        return 0;
 }
 
@@ -2388,7 +2434,7 @@ history
 ****************************************************************************/
 static int cmd_history(struct smbclient_context *ctx, const char **args)
 {
-#if defined(HAVE_LIBREADLINE)
+#if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
        HIST_ENTRY **hlist;
        int i;
 
@@ -2512,7 +2558,7 @@ static BOOL browse_host(const char *query_host)
        binding = talloc_asprintf(mem_ctx, "ncacn_np:%s", query_host);
 
        status = dcerpc_pipe_connect(mem_ctx, &p, binding, 
-                                        &dcerpc_table_srvsvc,
+                                        &ndr_table_srvsvc,
                                     cmdline_credentials, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("Failed to connect to %s - %s\n", 
@@ -2794,7 +2840,7 @@ static char **remote_completion(const char *text, int len)
        if (info.count == 2)
                info.matches[0] = strdup(info.matches[1]);
        else {
-               info.matches[0] = malloc(info.samelen+1);
+               info.matches[0] = malloc_array_p(char, info.samelen+1);
                if (!info.matches[0])
                        goto cleanup;
                strncpy(info.matches[0], info.matches[1], info.samelen);
@@ -2876,7 +2922,7 @@ static char **completion_fn(const char *text, int start, int end)
                        matches[0] = strdup(matches[1]);
                        break;
                default:
-                       matches[0] = malloc(samelen+1);
+                       matches[0] = malloc_array_p(char, samelen+1);
                        if (!matches[0])
                                goto cleanup;
                        strncpy(matches[0], matches[1], samelen);
@@ -2886,9 +2932,10 @@ static char **completion_fn(const char *text, int start, int end)
                return matches;
 
 cleanup:
-               while (i >= 0) {
-                       free(matches[i]);
-                       i--;
+               count--;
+               while (count >= 0) {
+                       free(matches[count]);
+                       count--;
                }
                free(matches);
                return NULL;
@@ -2993,7 +3040,8 @@ static struct smbclient_context *do_connect(TALLOC_CTX *mem_ctx,
        ctx->remote_cur_dir = talloc_strdup(ctx, "\\");
        
        status = smbcli_full_connection(ctx, &ctx->cli, server,
-                                       share, NULL, cred, NULL);
+                                       share, NULL, cred, 
+                                       cli_credentials_get_event_context(cred));
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("Connection to \\\\%s\\%s failed - %s\n", 
                         server, share, nt_errstr(status));
@@ -3007,10 +3055,10 @@ static struct smbclient_context *do_connect(TALLOC_CTX *mem_ctx,
 /****************************************************************************
 handle a -L query
 ****************************************************************************/
-static int do_host_query(const char *query_host)
+static int do_host_query(const char *query_host, const char *workgroup)
 {
        browse_host(query_host);
-       list_servers(lp_workgroup());
+       list_servers(workgroup);
        return(0);
 }
 
@@ -3024,7 +3072,7 @@ static int do_message_op(const char *desthost, const char *destip, int name_type
        const char *server_name;
        struct smbcli_state *cli;
 
-       make_nbt_name_client(&calling, lp_netbios_name());
+       make_nbt_name_client(&calling, lp_netbios_name(global_loadparm));
 
        nbt_choose_called_name(NULL, &called, desthost, name_type);
 
@@ -3090,7 +3138,7 @@ static int do_message_op(const char *desthost, const char *destip, int name_type
                POPT_COMMON_CONNECTION
                POPT_COMMON_CREDENTIALS
                POPT_COMMON_VERSION
-               POPT_TABLEEND
+               { NULL }
        };
        
        mem_ctx = talloc_init("client.c/main");
@@ -3171,7 +3219,7 @@ static int do_message_op(const char *desthost, const char *destip, int name_type
        }
   
        if (query_host) {
-               return do_host_query(query_host);
+               return do_host_query(query_host, lp_workgroup(global_loadparm));
        }
 
        if (message) {