browse_fn: FIXME: If the remote machine returns non-ascii characters
[ira/wip.git] / source3 / client / client.c
index 2cdc3ee6eca3bb255856d37a41ad99fec22475c0..61a123b9e54fbd98010640995a2c4134815af125 100644 (file)
 #define REGISTER 0
 #endif
 
+const char prog_name[] = "smbclient";
+
 struct cli_state *cli;
 extern BOOL in_client;
+extern BOOL AllowDebugChange;
 static int port = 0;
 pstring cur_dir = "\\";
 pstring cd_path = "";
@@ -43,7 +46,6 @@ static char *cmdstr;
 static BOOL got_pass;
 static int io_bufsize = 64512;
 static BOOL use_kerberos;
-extern struct in_addr ipzero;
 
 static int name_type = 0x20;
 static int max_protocol = PROTOCOL_NT1;
@@ -62,8 +64,6 @@ static int cmd_help(void);
 time_t newer_than = 0;
 int archive_level = 0;
 
-extern pstring debugf;
-
 BOOL translation = False;
 
 static BOOL have_ip;
@@ -971,6 +971,8 @@ static int cmd_quit(void)
 {
        cli_shutdown(cli);
        exit(0);
+       /* NOTREACHED */
+       return 0;
 }
 
 
@@ -1019,7 +1021,7 @@ static int cmd_mkdir(void)
 /****************************************************************************
   show alt name
   ****************************************************************************/
-static void cmd_altname(void)
+static int cmd_altname(void)
 {
        pstring name;
        fstring buf;
@@ -1029,11 +1031,13 @@ static void cmd_altname(void)
 
        if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
                d_printf("altname <file>\n");
-               return;
+               return 1;
        }
        pstrcat(name,p);
 
        do_altname(name);
+
+       return 0;
 }
 
 
@@ -1747,6 +1751,9 @@ static void browse_fn(const char *name, uint32 m,
           case STYPE_IPC:
             fstrcpy(typestr,"IPC"); break;
         }
+       /* FIXME: If the remote machine returns non-ascii characters
+          in any of these fields, they can corrupt the output.  We
+          should remove them. */
        d_printf("\t%-15.15s%-10.10s%s\n",
                name,typestr,comment);
 }
@@ -2079,7 +2086,6 @@ struct cli_state *do_connect(const char *server, const char *share)
        struct nmb_name called, calling;
        const char *server_n;
        struct in_addr ip;
-       extern struct in_addr ipzero;
        fstring servicename;
        char *sharename;
        
@@ -2096,13 +2102,13 @@ struct cli_state *do_connect(const char *server, const char *share)
 
        server_n = server;
        
-       ip = ipzero;
+       zero_ip(&ip);
 
        make_nmb_name(&calling, global_myname, 0x0);
        make_nmb_name(&called , server, name_type);
 
  again:
-       ip = ipzero;
+       zero_ip(&ip);
        if (have_ip) ip = dest_ip;
 
        /* have to open a new connection */
@@ -2113,17 +2119,13 @@ struct cli_state *do_connect(const char *server, const char *share)
        }
 
        c->protocol = max_protocol;
-
-       if (use_kerberos) {
-               c->use_spnego = True;
-       }
+       c->use_kerberos = use_kerberos;
 
        if (!cli_session_request(c, &calling, &called)) {
                char *p;
                d_printf("session request to %s failed (%s)\n", 
                         called.name, cli_errstr(c));
                cli_shutdown(c);
-               SAFE_FREE(c);
                if ((p=strchr_m(called.name, '.'))) {
                        *p = 0;
                        goto again;
@@ -2140,7 +2142,6 @@ struct cli_state *do_connect(const char *server, const char *share)
        if (!cli_negprot(c)) {
                d_printf("protocol negotiation failed\n");
                cli_shutdown(c);
-               SAFE_FREE(c);
                return NULL;
        }
 
@@ -2156,27 +2157,21 @@ struct cli_state *do_connect(const char *server, const char *share)
                               password, strlen(password),
                               workgroup)) {
                /* if a password was not supplied then try again with a null username */
-               if (password[0] || !username[0] || 
+               if (password[0] || !username[0] || use_kerberos ||
                    !cli_session_setup(c, "", "", 0, "", 0, workgroup)) { 
                        d_printf("session setup failed: %s\n", cli_errstr(c));
                        cli_shutdown(c);
-                       SAFE_FREE(c);
                        return NULL;
                }
                d_printf("Anonymous login successful\n");
        }
 
-       /*
-        * These next two lines are needed to emulate
-        * old client behaviour for people who have
-        * scripts based on client output.
-        * QUESTION ? Do we want to have a 'client compatibility
-        * mode to turn these on/off ? JRA.
-        */
-
-       if (*c->server_domain || *c->server_os || *c->server_type){
+       if (*c->server_domain) {
                DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
                        c->server_domain,c->server_os,c->server_type));
+       } else if (*c->server_os || *c->server_type){
+               DEBUG(1,("OS=[%s] Server=[%s]\n",
+                        c->server_os,c->server_type));
        }               
        
        DEBUG(4,(" session setup ok\n"));
@@ -2185,7 +2180,6 @@ struct cli_state *do_connect(const char *server, const char *share)
                            password, strlen(password)+1)) {
                d_printf("tree connect failed: %s\n", cli_errstr(c));
                cli_shutdown(c);
-               SAFE_FREE(c);
                return NULL;
        }
 
@@ -2204,7 +2198,7 @@ static int process(char *base_directory)
 
        cli = do_connect(desthost, service);
        if (!cli) {
-               return(False);
+               return 1;
        }
 
        if (*base_directory) do_cd(base_directory);
@@ -2363,12 +2357,12 @@ static int do_message_op(void)
        struct in_addr ip;
        struct nmb_name called, calling;
 
-       ip = ipzero;
+        zero_ip(&ip);
 
        make_nmb_name(&calling, global_myname, 0x0);
        make_nmb_name(&called , desthost, name_type);
 
-       ip = ipzero;
+        zero_ip(&ip);
        if (have_ip) ip = dest_ip;
 
        if (!(cli=cli_initialise(NULL)) || (cli_set_port(cli, port) != port) || !cli_connect(cli, desthost, &ip)) {
@@ -2403,9 +2397,9 @@ static int do_message_op(void)
        pstring query_host;
        BOOL message = False;
        extern char tar_type;
-       static pstring servicesf = CONFIGFILE;
        pstring term_code;
        pstring new_name_resolve_order;
+       pstring logfile;
        char *p;
        int rc = 0;
 
@@ -2421,6 +2415,7 @@ static int do_message_op(void)
        *new_name_resolve_order = 0;
 
        DEBUGLEVEL = 2;
+       AllowDebugChange = False;
  
        setup_logging(pname,True);
 
@@ -2435,12 +2430,12 @@ static int do_message_op(void)
                        dbf = x_stderr;
                else if(strncmp(argv[opt], "-s", 2) == 0) {
                        if(argv[opt][2] != '\0')
-                               pstrcpy(servicesf, &argv[opt][2]);
+                               pstrcpy(dyn_CONFIGFILE, &argv[opt][2]);
                        else if(argv[opt+1] != NULL) {
                                /*
                                 * At least one more arg left.
                                 */
-                               pstrcpy(servicesf, argv[opt+1]);
+                               pstrcpy(dyn_CONFIGFILE, argv[opt+1]);
                        } else {
                                usage(pname);
                                exit(1);
@@ -2448,13 +2443,12 @@ static int do_message_op(void)
                }
        }
 
-       TimeInit();
-
        in_client = True;   /* Make sure that we tell lp_load we are */
 
        old_debug = DEBUGLEVEL;
-       if (!lp_load(servicesf,True,False,False)) {
-               fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
+       if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
+               fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
+                       prog_name, dyn_CONFIGFILE);
        }
        DEBUGLEVEL = old_debug;
        
@@ -2508,7 +2502,13 @@ static int do_message_op(void)
                usage(pname);
                exit(1);
        }
-  
+
+       /* FIXME: At the moment, if the user should happen to give the
+        * options ahead of the service name (in standard Unix
+        * fashion) then smbclient just spits out the usage message
+        * with no explanation of what in particular was wrong.  Is
+        * there any reason we can't just parse out the service name
+        * and password after running getopt?? -- mbp */
        if (*argv[1] != '-') {
                pstrcpy(service,argv[1]);  
                /* Convert any '/' characters in the service name to '\' characters */
@@ -2535,7 +2535,7 @@ static int do_message_op(void)
                getopt(argc, argv,"s:O:R:M:i:Nn:d:Pp:l:hI:EU:L:t:m:W:T:D:c:b:A:k")) != EOF) {
                switch (opt) {
                case 's':
-                       pstrcpy(servicesf, optarg);
+                       pstrcpy(dyn_CONFIGFILE, optarg);
                        break;
                case 'O':
                        pstrcpy(user_socket_options,optarg);
@@ -2574,7 +2574,8 @@ static int do_message_op(void)
                        port = atoi(optarg);
                        break;
                case 'l':
-                       slprintf(debugf,sizeof(debugf)-1, "%s.client",optarg);
+                       slprintf(logfile,sizeof(logfile)-1, "%s.client",optarg);
+                       lp_set_logfile(logfile);
                        break;
                case 'h':
                        usage(pname);
@@ -2583,7 +2584,7 @@ static int do_message_op(void)
                case 'I':
                        {
                                dest_ip = *interpret_addr2(optarg);
-                               if (zero_ip(dest_ip))
+                               if (is_zero_ip(dest_ip))
                                        exit(1);
                                have_ip = True;
                        }
@@ -2653,7 +2654,8 @@ static int do_message_op(void)
                                        }
                                        else if (strwicmp("username", param) == 0)
                                                pstrcpy(username, val);
-                                               
+                                       else if (strwicmp("domain", param) == 0)
+                                               pstrcpy(workgroup,val);
                                        memset(buf, 0, sizeof(buf));
                                }
                                x_fclose(auth);
@@ -2691,7 +2693,7 @@ static int do_message_op(void)
                        io_bufsize = MAX(1, atoi(optarg));
                        break;
                case 'k':
-#if HAVE_KRB5
+#ifdef HAVE_KRB5
                        use_kerberos = True;
                        got_pass = True;
 #else