set of changes in the beginning of bringing rpcclient changes
authorGerald Carter <jerry@samba.org>
Wed, 14 Mar 2001 20:22:57 +0000 (20:22 +0000)
committerGerald Carter <jerry@samba.org>
Wed, 14 Mar 2001 20:22:57 +0000 (20:22 +0000)
back to working order.  The main change is that the cli_*() RPC
functions from libsmb/*.c now should accept a struct cli_state*.

The reason for this is that rpcclient should establish the
connection to the server at startup so that it is not necessary
to keep the clear test or password hash in memory for each command.

enumports and enumprinters now works as well.  lsa* functions
have been tested.  SAMR calls may or may not work (one of the core
dumps I know), but it compiles :-)

jerry
(This used to be commit d98ac8852ae6b39b6fcff92c346ba56d9e63c518)

source3/include/rpcclient.h
source3/libsmb/cli_spoolss.c
source3/libsmb/cliconnect.c
source3/libsmb/pwd_cache.c
source3/rpcclient/cmd_lsarpc.c
source3/rpcclient/cmd_samr.c
source3/rpcclient/cmd_spoolss.c
source3/rpcclient/rpcclient.c

index f2aa57b2dfe47956739cb6be6fda0681fe57ff86..f8e5d2d9b15e3b2a0ab6bc67f57fc125666be404 100644 (file)
@@ -25,7 +25,7 @@
 
 struct cmd_set {
        char *name;
-       uint32 (*fn)(int argc, char **argv);
+       uint32 (*fn)(struct cli_state*, int argc, char **argv);
        char *description;
 };
 
index d8e7b0cce65007bf1c5513dfb366042924d36fe8..af12f102d7619ec36feadc905c7b3efc947596bd 100644 (file)
@@ -25,7 +25,6 @@
 #include "includes.h"
 
 /* Opens a SMB connection to the SPOOLSS pipe */
-
 struct cli_state *cli_spoolss_initialise(struct cli_state *cli, 
                                         char *system_name,
                                         struct ntuser_creds *creds)
@@ -321,10 +320,14 @@ uint32 cli_spoolss_enum_printers(struct cli_state *cli, uint32 flags,
        NEW_BUFFER buffer;
        uint32 needed = 100;
        uint32 result;
+       fstring server;
 
        ZERO_STRUCT(q);
        ZERO_STRUCT(r);
 
+       fstrcpy (server, cli->desthost);
+       strupper (server);
+       
        do {
                /* Initialise input parameters */
 
@@ -333,46 +336,39 @@ uint32 cli_spoolss_enum_printers(struct cli_state *cli, uint32 flags,
                prs_init(&qbuf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
                prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
 
-               make_spoolss_q_enumprinters(&q, flags, "", level, &buffer, 
+               make_spoolss_q_enumprinters(&q, flags, server, level, &buffer, 
                                            needed);
 
                /* Marshall data and send request */
 
                if (!spoolss_io_q_enumprinters("", &q, &qbuf, 0) ||
-                   !rpc_api_pipe_req(cli, SPOOLSS_ENUMPRINTERS, &qbuf,
-                                     &rbuf)) {
+                   !rpc_api_pipe_req(cli, SPOOLSS_ENUMPRINTERS, &qbuf, &rbuf)) {
                        result = NT_STATUS_UNSUCCESSFUL;
                        goto done;
                }
 
                /* Unmarshall response */
-
-               prs_switch_type(&buffer.prs, UNMARSHALL);
-               prs_set_offset(&buffer.prs, 0);         
-               r.buffer = &buffer;
-
                if (new_spoolss_io_r_enumprinters("", &r, &rbuf, 0)) {
                        needed = r.needed;
                }
                
                /* Return output parameters */
 
-               if ((result = r.status) == NT_STATUS_NOPROBLEMO &&
-                   r.returned > 0) {
+               if ((result = r.status) == NT_STATUS_NOPROBLEMO && r.returned > 0) {
 
                        *returned = r.returned;
 
                        switch (level) {
                        case 1:
-                               decode_printer_info_1(&buffer, r.returned, 
+                               decode_printer_info_1(r.buffer, r.returned, 
                                                      &ctr->printers_1);
                                break;
                        case 2:
-                               decode_printer_info_2(&buffer, r.returned, 
+                               decode_printer_info_2(r.buffer, r.returned, 
                                                      &ctr->printers_2);
                                break;
                        case 3:
-                               decode_printer_info_3(&buffer, r.returned, 
+                               decode_printer_info_3(r.buffer, r.returned, 
                                                      &ctr->printers_3);
                                break;
                        }                       
@@ -398,10 +394,14 @@ uint32 cli_spoolss_enum_ports(struct cli_state *cli, uint32 level,
        NEW_BUFFER buffer;
        uint32 needed = 100;
        uint32 result;
+       fstring server;
 
        ZERO_STRUCT(q);
        ZERO_STRUCT(r);
 
+       fstrcpy (server, cli->desthost);
+       strupper (server);
+
        do {
                /* Initialise input parameters */
 
@@ -410,23 +410,20 @@ uint32 cli_spoolss_enum_ports(struct cli_state *cli, uint32 level,
                prs_init(&qbuf, MAX_PDU_FRAG_LEN, cli->mem_ctx, MARSHALL);
                prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL);
 
+               /* NT4 will return NT_STATUS_CTL_FILE_NOT_SUPPORTED is we
+                  set the servername here in the query.  Not sure why  \
+                  --jerry */
                make_spoolss_q_enumports(&q, "", level, &buffer, needed);
 
                /* Marshall data and send request */
 
                if (!spoolss_io_q_enumports("", &q, &qbuf, 0) ||
-                   !rpc_api_pipe_req(cli, SPOOLSS_ENUMPORTS, &qbuf,
-                                     &rbuf)) {
+                   !rpc_api_pipe_req(cli, SPOOLSS_ENUMPORTS, &qbuf, &rbuf)) {
                        result = NT_STATUS_UNSUCCESSFUL;
                        goto done;
                }
 
                /* Unmarshall response */
-
-               prs_switch_type(&buffer.prs, UNMARSHALL);
-               prs_set_offset(&buffer.prs, 0);         
-               r.buffer = &buffer;
-
                if (new_spoolss_io_r_enumports("", &r, &rbuf, 0)) {
                        needed = r.needed;
                }
@@ -440,11 +437,11 @@ uint32 cli_spoolss_enum_ports(struct cli_state *cli, uint32 level,
 
                        switch (level) {
                        case 1:
-                               decode_port_info_1(&buffer, r.returned, 
+                               decode_port_info_1(r.buffer, r.returned, 
                                                   &ctr->port.info_1);
                                break;
                        case 2:
-                               decode_port_info_2(&buffer, r.returned, 
+                               decode_port_info_2(r.buffer, r.returned, 
                                                   &ctr->port.info_2);
                                break;
                        }                       
@@ -495,11 +492,6 @@ uint32 cli_spoolss_getprinter(struct cli_state *cli, POLICY_HND *pol,
                }
 
                /* Unmarshall response */
-
-               prs_switch_type(&buffer.prs, UNMARSHALL);
-               prs_set_offset(&buffer.prs, 0);         
-               r.buffer = &buffer;
-
                if (spoolss_io_r_getprinter("", &r, &rbuf, 0)) {
                        needed = r.needed;
                }
@@ -510,19 +502,19 @@ uint32 cli_spoolss_getprinter(struct cli_state *cli, POLICY_HND *pol,
 
                        switch (level) {
                        case 0:
-                               decode_printer_info_0(&buffer, 1, 
+                               decode_printer_info_0(r.buffer, 1, 
                                                      &ctr->printers_0);
                                break;
                        case 1:
-                               decode_printer_info_1(&buffer, 1, 
+                               decode_printer_info_1(r.buffer, 1, 
                                                      &ctr->printers_1);
                                break;
                        case 2:
-                               decode_printer_info_2(&buffer, 1,
+                               decode_printer_info_2(r.buffer, 1,
                                                      &ctr->printers_2);
                                break;
                        case 3:
-                               decode_printer_info_3(&buffer, 1,
+                               decode_printer_info_3(r.buffer, 1,
                                                      &ctr->printers_3);
                                break;
                        }                       
index 46a63dc5f19dadd311f9354571956162b21a084e..06f283c321a57510b8a2d77acf4f996119aaa785 100644 (file)
@@ -669,7 +669,7 @@ BOOL cli_establish_connection(struct cli_state *cli,
        {
                DEBUG(1,("failed session request\n"));
                if (do_shutdown)
-          cli_shutdown(cli);
+                       cli_shutdown(cli);
                return False;
        }
 
index 26b1d192f09c1eaa09df484996b02f4948c1aaff..420b49ed2e74f045b5cc21e14176d49cbe080da6 100644 (file)
@@ -103,11 +103,21 @@ void pwd_read(struct pwd_info *pwd, char *passwd_report, BOOL do_encrypt)
 
        user_pass = (char*)getpass(passwd_report);
 
+       /*
+        * Do not assume that an empty string is a NULL password.
+        * If you do this will break the session key generation for
+        * and account with an emtpy password.  If you wish to use
+        * a NULL password, use the -N option to smbclient and rpcclient
+        * --jerry
+        */
+#if 0
        if (user_pass == NULL || user_pass[0] == 0)
        {
                pwd_set_nullpwd(pwd);
        }
        else if (do_encrypt)
+#endif
+       if (do_encrypt)
        {
                pwd_make_lm_nt_16(pwd, user_pass);
        }
index 694dd4d81a6342668130a1cb729755298c6e682f..3ae0b1e11d686be1fc2a48874c3802a3e9cf5b18 100644 (file)
@@ -26,13 +26,10 @@ extern int DEBUGLEVEL;
 extern pstring server;
 
 /* Look up domain related information on a remote host */
-
-static uint32 cmd_lsa_query_info_policy(int argc, char **argv) 
+static uint32 cmd_lsa_query_info_policy(struct cli_state *cli, int argc, char **argv) 
 {
-       struct cli_state cli;
        POLICY_HND pol;
        uint32 result = NT_STATUS_UNSUCCESSFUL;
-       struct ntuser_creds creds;
        BOOL got_policy_hnd = False;
        DOM_SID dom_sid;
        fstring sid_str, domain_name;
@@ -47,16 +44,13 @@ static uint32 cmd_lsa_query_info_policy(int argc, char **argv)
                info_class = atoi(argv[1]);
        }
        
-       /* Open a lsa handle */
-
-       ZERO_STRUCT(cli);
-       init_rpcclient_creds(&creds);
-
-       if (cli_lsa_initialise(&cli, server, &creds) == NULL) {
-               goto done;
+       /* Initialise RPC connection */
+       if (!cli_nt_session_open (cli, PIPE_LSARPC)) {
+               fprintf (stderr, "Could not initialize samr pipe!\n");
+               return NT_STATUS_UNSUCCESSFUL;
        }
 
-       if ((result = cli_lsa_open_policy(&cli, True, 
+       if ((result = cli_lsa_open_policy(cli, True, 
                                          SEC_RIGHTS_MAXIMUM_ALLOWED,
                                          &pol)) != NT_STATUS_NOPROBLEMO) {
                goto done;
@@ -66,7 +60,7 @@ static uint32 cmd_lsa_query_info_policy(int argc, char **argv)
 
        /* Lookup info policy */
 
-       if ((result = cli_lsa_query_info_policy(&cli, &pol, info_class, 
+       if ((result = cli_lsa_query_info_policy(cli, &pol, info_class, 
                                                domain_name, &dom_sid)) 
            != NT_STATUS_NOPROBLEMO) {
                goto done;
@@ -76,23 +70,21 @@ static uint32 cmd_lsa_query_info_policy(int argc, char **argv)
 
        printf("domain %s has sid %s\n", domain_name, sid_str);
 
- done:
+done:
 
        if (got_policy_hnd) {
-               cli_lsa_close(&cli, &pol);
+               cli_lsa_close(cli, &pol);
        }
 
-       cli_lsa_shutdown(&cli);
+       cli_nt_session_close(cli);
 
        return result;
 }
 
 /* Resolve a list of names to a list of sids */
 
-static uint32 cmd_lsa_lookup_names(int argc, char **argv)
+static uint32 cmd_lsa_lookup_names(struct cli_state *cli, int argc, char **argv)
 {
-       struct cli_state cli;
-       struct ntuser_creds creds;
        POLICY_HND pol;
        uint32 result = NT_STATUS_UNSUCCESSFUL;
        BOOL got_policy_hnd = False;
@@ -105,16 +97,14 @@ static uint32 cmd_lsa_lookup_names(int argc, char **argv)
                return 0;
        }
 
-       /* Open a lsa handle */
-
-       ZERO_STRUCT(cli);
-       init_rpcclient_creds(&creds);
-
-       if (cli_lsa_initialise(&cli, server, &creds) == NULL) {
-               goto done;
+       /* Initialise RPC connection */
+       if (!cli_nt_session_open (cli, PIPE_LSARPC)) {
+               fprintf (stderr, "Could not initialize samr pipe!\n");
+               return NT_STATUS_UNSUCCESSFUL;
        }
 
-       if ((result = cli_lsa_open_policy(&cli, True, 
+
+       if ((result = cli_lsa_open_policy(cli, True, 
                                          SEC_RIGHTS_MAXIMUM_ALLOWED,
                                          &pol)) != NT_STATUS_NOPROBLEMO) {
                goto done;
@@ -125,7 +115,7 @@ static uint32 cmd_lsa_lookup_names(int argc, char **argv)
        /* Lookup the names */
 
        if ((result = cli_lsa_lookup_names(
-               &cli, &pol, argc - 1, &argv[1], &sids, &types, &num_names) !=
+               cli, &pol, argc - 1, &argv[1], &sids, &types, &num_names) !=
             NT_STATUS_NOPROBLEMO)) {
                goto done;
        }
@@ -146,22 +136,20 @@ static uint32 cmd_lsa_lookup_names(int argc, char **argv)
  done:
 
        if (got_policy_hnd) {
-               cli_lsa_close(&cli, &pol);
+               cli_lsa_close(cli, &pol);
        }
 
-       cli_lsa_shutdown(&cli);
+       cli_nt_session_close(cli);
 
        return result;
 }
 
 /* Resolve a list of SIDs to a list of names */
 
-static uint32 cmd_lsa_lookup_sids(int argc, char **argv)
+static uint32 cmd_lsa_lookup_sids(struct cli_state *cli, int argc, char **argv)
 {
-       struct cli_state cli;
        POLICY_HND pol;
        uint32 result = NT_STATUS_UNSUCCESSFUL;
-       struct ntuser_creds creds;
        BOOL got_policy_hnd = False;
        DOM_SID *sids;
        char **names;
@@ -173,16 +161,13 @@ static uint32 cmd_lsa_lookup_sids(int argc, char **argv)
                return 0;
        }
 
-       /* Open a lsa handle */
-
-       ZERO_STRUCT(cli);
-       init_rpcclient_creds(&creds);
-
-       if (cli_lsa_initialise(&cli, server, &creds) == NULL) {
-               goto done;
+       /* Initialise RPC connection */
+       if (!cli_nt_session_open (cli, PIPE_LSARPC)) {
+               fprintf (stderr, "Could not initialize samr pipe!\n");
+               return NT_STATUS_UNSUCCESSFUL;
        }
 
-       if ((result = cli_lsa_open_policy(&cli, True, 
+       if ((result = cli_lsa_open_policy(cli, True, 
                                          SEC_RIGHTS_MAXIMUM_ALLOWED,
                                          &pol)) != NT_STATUS_NOPROBLEMO) {
                goto done;
@@ -205,7 +190,7 @@ static uint32 cmd_lsa_lookup_sids(int argc, char **argv)
 
        /* Lookup the SIDs */
 
-       if ((result = cli_lsa_lookup_sids(&cli, &pol, argc - 1, sids, 
+       if ((result = cli_lsa_lookup_sids(cli, &pol, argc - 1, sids, 
                                          &names, &types, &num_names) !=
             NT_STATUS_NOPROBLEMO)) {
                goto done;
@@ -233,22 +218,20 @@ static uint32 cmd_lsa_lookup_sids(int argc, char **argv)
  done:
 
        if (got_policy_hnd) {
-               cli_lsa_close(&cli, &pol);
+               cli_lsa_close(cli, &pol);
        }
 
-       cli_lsa_shutdown(&cli);
+       cli_nt_session_close(cli);
 
        return result;
 }
 
 /* Enumerate list of trusted domains */
 
-static uint32 cmd_lsa_enum_trust_dom(int argc, char **argv)
+static uint32 cmd_lsa_enum_trust_dom(struct cli_state *cli, int argc, char **argv)
 {
-       struct cli_state cli;
        POLICY_HND pol;
        uint32 result = NT_STATUS_UNSUCCESSFUL;
-       struct ntuser_creds creds;
        BOOL got_policy_hnd = False;
        DOM_SID *domain_sids;
        char **domain_names;
@@ -259,16 +242,13 @@ static uint32 cmd_lsa_enum_trust_dom(int argc, char **argv)
                return 0;
        }
 
-       /* Open a lsa handle */
-
-       ZERO_STRUCT(cli);
-       init_rpcclient_creds(&creds);
-
-       if (cli_lsa_initialise(&cli, server, &creds) == NULL) {
-               goto done;
+       /* Initialise RPC connection */
+       if (!cli_nt_session_open (cli, PIPE_LSARPC)) {
+               fprintf (stderr, "Could not initialize samr pipe!\n");
+               return NT_STATUS_UNSUCCESSFUL;
        }
 
-       if ((result = cli_lsa_open_policy(&cli, True, 
+       if ((result = cli_lsa_open_policy(cli, True, 
                                          SEC_RIGHTS_MAXIMUM_ALLOWED,
                                          &pol)) != NT_STATUS_NOPROBLEMO) {
                goto done;
@@ -278,7 +258,7 @@ static uint32 cmd_lsa_enum_trust_dom(int argc, char **argv)
 
        /* Lookup list of trusted domains */
 
-       if ((result = cli_lsa_enum_trust_dom(&cli, &pol, &enum_ctx,
+       if ((result = cli_lsa_enum_trust_dom(cli, &pol, &enum_ctx,
                                             &num_domains, &domain_names,
                                             &domain_sids) 
             != NT_STATUS_NOPROBLEMO)) {
@@ -306,10 +286,10 @@ static uint32 cmd_lsa_enum_trust_dom(int argc, char **argv)
  done:
 
        if (got_policy_hnd) {
-               cli_lsa_close(&cli, &pol);
+               cli_lsa_close(cli, &pol);
        }
 
-       cli_lsa_shutdown(&cli);
+       cli_nt_session_close(cli);
 
        return result;
 }
@@ -317,9 +297,9 @@ static uint32 cmd_lsa_enum_trust_dom(int argc, char **argv)
 /* List of commands exported by this module */
 
 struct cmd_set lsarpc_commands[] = {
-       { "lsaquery", cmd_lsa_query_info_policy, "Query info policy" },
-       { "lookupsids", cmd_lsa_lookup_sids, "Convert SIDs to names" },
-       { "lookupnames", cmd_lsa_lookup_names, "Convert names to SIDs" },
-       { "enumtrust", cmd_lsa_enum_trust_dom, "Enumerate trusted domains" },
+       { "lsaquery",   cmd_lsa_query_info_policy,      "Query info policy" },
+       { "lookupsids", cmd_lsa_lookup_sids,            "Convert SIDs to names" },
+       { "lookupnames",cmd_lsa_lookup_names,           "Convert names to SIDs" },
+       { "enumtrust",  cmd_lsa_enum_trust_dom,         "Enumerate trusted domains" },
        { NULL, NULL, NULL }
 };
index 59830e77426b1c45a49f8d882e0785105f64eec2..c7bed1368e9751c5f3d3aecb5b4412918e5756f0 100644 (file)
@@ -95,15 +95,16 @@ static void display_sam_user_info_21(SAM_USER_INFO_21 *usr)
        }
 }
 
-/* Query user information */
-
-static uint32 cmd_samr_query_user(int argc, char **argv) 
+/**********************************************************************
+ * Query user information 
+ */
+static uint32 cmd_samr_query_user(struct cli_state *cli, int argc, char **argv) 
 {
-       struct cli_state cli;
        POLICY_HND connect_pol, domain_pol, user_pol;
-       uint32 result = NT_STATUS_UNSUCCESSFUL, info_level = 21;
-       struct ntuser_creds creds;
-       BOOL got_connect_pol = False, got_domain_pol = False,
+       uint32  result = NT_STATUS_UNSUCCESSFUL, 
+               info_level = 21;
+       BOOL    got_connect_pol = False, 
+               got_domain_pol = False,
                got_user_pol = False;
        SAM_USERINFO_CTR user_ctr;
        SAM_USER_INFO_21 info_21;
@@ -113,25 +114,22 @@ static uint32 cmd_samr_query_user(int argc, char **argv)
                return 0;
        }
 
-       /* Open a lsa handle */
-
-       ZERO_STRUCT(cli);
-       init_rpcclient_creds(&creds);
-
-       if (!cli_samr_initialise(&cli, server, &creds)) {
-               goto done;
+       /* Initialise RPC connection */
+       if (!cli_nt_session_open (cli, PIPE_SAMR)) {
+               fprintf (stderr, "Could not initialize samr pipe!\n");
+               return NT_STATUS_UNSUCCESSFUL;
        }
        
-       if ((result = cli_samr_connect(&cli, server, MAXIMUM_ALLOWED_ACCESS,
+       if ((result = cli_samr_connect(cli, server, MAXIMUM_ALLOWED_ACCESS,
                                       &connect_pol)) !=
            NT_STATUS_NOPROBLEMO) {
                goto done;
        }
 
        got_connect_pol = True;
-       fetch_domain_sid();
+       fetch_domain_sid(cli);
 
-       if ((result = cli_samr_open_domain(&cli, &connect_pol,
+       if ((result = cli_samr_open_domain(cli, &connect_pol,
                                           MAXIMUM_ALLOWED_ACCESS,
                                           &domain_sid, &domain_pol))
             != NT_STATUS_NOPROBLEMO) {
@@ -140,7 +138,7 @@ static uint32 cmd_samr_query_user(int argc, char **argv)
 
        got_domain_pol = True;
 
-       if ((result = cli_samr_open_user(&cli, &domain_pol,
+       if ((result = cli_samr_open_user(cli, &domain_pol,
                                         MAXIMUM_ALLOWED_ACCESS,
                                         0x1f4, &user_pol))
            != NT_STATUS_NOPROBLEMO) {
@@ -154,7 +152,7 @@ static uint32 cmd_samr_query_user(int argc, char **argv)
 
        user_ctr.info.id21 = &info_21;
 
-       if ((result = cli_samr_query_userinfo(&cli, &user_pol, info_level,
+       if ((result = cli_samr_query_userinfo(cli, &user_pol, info_level,
                                              &user_ctr)) 
            != NT_STATUS_NOPROBLEMO) {
                goto done;
@@ -162,12 +160,12 @@ static uint32 cmd_samr_query_user(int argc, char **argv)
 
        display_sam_user_info_21(&info_21);
 
- done:
-       if (got_user_pol) cli_samr_close(&cli, &user_pol);
-       if (got_domain_pol) cli_samr_close(&cli, &domain_pol);
-       if (got_connect_pol) cli_samr_close(&cli, &connect_pol);
+done:
+       if (got_user_pol) cli_samr_close(cli, &user_pol);
+       if (got_domain_pol) cli_samr_close(cli, &domain_pol);
+       if (got_connect_pol) cli_samr_close(cli, &connect_pol);
 
-       cli_samr_shutdown(&cli);
+       cli_nt_session_close(cli);
 
        return result;
 }
@@ -215,14 +213,13 @@ static void display_group_info_ctr(GROUP_INFO_CTR *ctr)
        }
 }
 
-/* Query group information */
-
-static uint32 cmd_samr_query_group(int argc, char **argv) 
+/***********************************************************************
+ * Query group information 
+ */
+static uint32 cmd_samr_query_group(struct cli_state *cli, int argc, char **argv) 
 {
-       struct cli_state cli;
        POLICY_HND connect_pol, domain_pol, group_pol;
        uint32 result = NT_STATUS_UNSUCCESSFUL, info_level = 1;
-       struct ntuser_creds creds;
        BOOL got_connect_pol = False, got_domain_pol = False,
                got_group_pol = False;
        GROUP_INFO_CTR group_ctr;
@@ -232,25 +229,22 @@ static uint32 cmd_samr_query_group(int argc, char **argv)
                return 0;
        }
 
-       /* Open a lsa handle */
-
-       ZERO_STRUCT(cli);
-       init_rpcclient_creds(&creds);
-
-       if (!cli_samr_initialise(&cli, server, &creds)) {
-               goto done;
+       /* Initialise RPC connection */
+       if (!cli_nt_session_open (cli, PIPE_SAMR)) {
+               fprintf (stderr, "Could not initialize samr pipe!\n");
+               return NT_STATUS_UNSUCCESSFUL;
        }
        
-       if ((result = cli_samr_connect(&cli, server, MAXIMUM_ALLOWED_ACCESS,
+       if ((result = cli_samr_connect(cli, server, MAXIMUM_ALLOWED_ACCESS,
                                       &connect_pol)) !=
            NT_STATUS_NOPROBLEMO) {
                goto done;
        }
 
        got_connect_pol = True;
-       fetch_domain_sid();
+       fetch_domain_sid(cli);
 
-       if ((result = cli_samr_open_domain(&cli, &connect_pol,
+       if ((result = cli_samr_open_domain(cli, &connect_pol,
                                           MAXIMUM_ALLOWED_ACCESS,
                                           &domain_sid, &domain_pol))
             != NT_STATUS_NOPROBLEMO) {
@@ -259,7 +253,7 @@ static uint32 cmd_samr_query_group(int argc, char **argv)
 
        got_domain_pol = True;
 
-       if ((result = cli_samr_open_group(&cli, &domain_pol,
+       if ((result = cli_samr_open_group(cli, &domain_pol,
                                          MAXIMUM_ALLOWED_ACCESS,
                                          0x202, &group_pol))
            != NT_STATUS_NOPROBLEMO) {
@@ -270,7 +264,7 @@ static uint32 cmd_samr_query_group(int argc, char **argv)
 
        ZERO_STRUCT(group_ctr);
 
-       if ((result = cli_samr_query_groupinfo(&cli, &group_pol, info_level,
+       if ((result = cli_samr_query_groupinfo(cli, &group_pol, info_level,
                                               &group_ctr)) 
            != NT_STATUS_NOPROBLEMO) {
                goto done;
@@ -278,24 +272,22 @@ static uint32 cmd_samr_query_group(int argc, char **argv)
 
        display_group_info_ctr(&group_ctr);
 
- done:
-       if (got_group_pol) cli_samr_close(&cli, &group_pol);
-       if (got_domain_pol) cli_samr_close(&cli, &domain_pol);
-       if (got_connect_pol) cli_samr_close(&cli, &connect_pol);
+done:
+       if (got_group_pol) cli_samr_close(cli, &group_pol);
+       if (got_domain_pol) cli_samr_close(cli, &domain_pol);
+       if (got_connect_pol) cli_samr_close(cli, &connect_pol);
 
-       cli_samr_shutdown(&cli);
+       cli_nt_session_close(cli);
 
        return result;
 }
 
 /* Query groups a user is a member of */
 
-static uint32 cmd_samr_query_usergroups(int argc, char **argv) 
+static uint32 cmd_samr_query_usergroups(struct cli_state *cli, int argc, char **argv) 
 {
-       struct cli_state cli;
        POLICY_HND connect_pol, domain_pol, user_pol;
        uint32 result = NT_STATUS_UNSUCCESSFUL;
-       struct ntuser_creds creds;
        BOOL got_connect_pol = False, got_domain_pol = False,
                got_user_pol = False;
        uint32 num_groups, user_rid;
@@ -309,25 +301,22 @@ static uint32 cmd_samr_query_usergroups(int argc, char **argv)
 
        sscanf(argv[1], "%i", &user_rid);
 
-       /* Open a lsa handle */
-
-       ZERO_STRUCT(cli);
-       init_rpcclient_creds(&creds);
-
-       if (!cli_samr_initialise(&cli, server, &creds)) {
-               goto done;
+       /* Initialise RPC connection */
+       if (!cli_nt_session_open (cli, PIPE_SAMR)) {
+               fprintf (stderr, "Could not initialize samr pipe!\n");
+               return NT_STATUS_UNSUCCESSFUL;
        }
        
-       if ((result = cli_samr_connect(&cli, server, MAXIMUM_ALLOWED_ACCESS,
+       if ((result = cli_samr_connect(cli, server, MAXIMUM_ALLOWED_ACCESS,
                                       &connect_pol)) !=
            NT_STATUS_NOPROBLEMO) {
                goto done;
        }
 
        got_connect_pol = True;
-       fetch_domain_sid();
+       fetch_domain_sid(cli);
 
-       if ((result = cli_samr_open_domain(&cli, &connect_pol,
+       if ((result = cli_samr_open_domain(cli, &connect_pol,
                                           MAXIMUM_ALLOWED_ACCESS,
                                           &domain_sid, &domain_pol))
             != NT_STATUS_NOPROBLEMO) {
@@ -336,7 +325,7 @@ static uint32 cmd_samr_query_usergroups(int argc, char **argv)
 
        got_domain_pol = True;
 
-       if ((result = cli_samr_open_user(&cli, &domain_pol,
+       if ((result = cli_samr_open_user(cli, &domain_pol,
                                         MAXIMUM_ALLOWED_ACCESS,
                                         user_rid, &user_pol))
            != NT_STATUS_NOPROBLEMO) {
@@ -345,7 +334,7 @@ static uint32 cmd_samr_query_usergroups(int argc, char **argv)
 
        got_user_pol = True;
 
-       if ((result = cli_samr_query_usergroups(&cli, &user_pol,
+       if ((result = cli_samr_query_usergroups(cli, &user_pol,
                                                &num_groups, &user_gids))
            != NT_STATUS_NOPROBLEMO) {
                goto done;
@@ -357,24 +346,23 @@ static uint32 cmd_samr_query_usergroups(int argc, char **argv)
        }
 
  done:
-       if (got_user_pol) cli_samr_close(&cli, &user_pol);
-       if (got_domain_pol) cli_samr_close(&cli, &domain_pol);
-       if (got_connect_pol) cli_samr_close(&cli, &connect_pol);
+       if (got_user_pol) cli_samr_close(cli, &user_pol);
+       if (got_domain_pol) cli_samr_close(cli, &domain_pol);
+       if (got_connect_pol) cli_samr_close(cli, &connect_pol);
 
-       cli_samr_shutdown(&cli);
+       cli_nt_session_close(cli);
 
        return result;
 }
 
 /* Query members of a group */
 
-static uint32 cmd_samr_query_groupmem(int argc, char **argv) 
+static uint32 cmd_samr_query_groupmem(struct cli_state *cli, int argc, char **argv) 
 {
-       struct cli_state cli;
        POLICY_HND connect_pol, domain_pol, group_pol;
        uint32 result = NT_STATUS_UNSUCCESSFUL;
-       struct ntuser_creds creds;
-       BOOL got_connect_pol = False, got_domain_pol = False,
+       BOOL    got_connect_pol = False, 
+               got_domain_pol = False,
                got_group_pol = False;
        uint32 num_members, *group_rids, *group_attrs, group_rid;
        int i;
@@ -386,25 +374,22 @@ static uint32 cmd_samr_query_groupmem(int argc, char **argv)
 
        sscanf(argv[1], "%i", &group_rid);
 
-       /* Open a lsa handle */
-
-       ZERO_STRUCT(cli);
-       init_rpcclient_creds(&creds);
-
-       if (!cli_samr_initialise(&cli, server, &creds)) {
-               goto done;
+       /* Initialise RPC connection */
+       if (!cli_nt_session_open (cli, PIPE_SAMR)) {
+               fprintf (stderr, "Could not initialize samr pipe!\n");
+               return NT_STATUS_UNSUCCESSFUL;
        }
-       
-       if ((result = cli_samr_connect(&cli, server, MAXIMUM_ALLOWED_ACCESS,
+
+       if ((result = cli_samr_connect(cli, server, MAXIMUM_ALLOWED_ACCESS,
                                       &connect_pol)) !=
            NT_STATUS_NOPROBLEMO) {
                goto done;
        }
 
        got_connect_pol = True;
-       fetch_domain_sid();
+       fetch_domain_sid(cli);
 
-       if ((result = cli_samr_open_domain(&cli, &connect_pol,
+       if ((result = cli_samr_open_domain(cli, &connect_pol,
                                           MAXIMUM_ALLOWED_ACCESS,
                                           &domain_sid, &domain_pol))
             != NT_STATUS_NOPROBLEMO) {
@@ -413,7 +398,7 @@ static uint32 cmd_samr_query_groupmem(int argc, char **argv)
 
        got_domain_pol = True;
 
-       if ((result = cli_samr_open_group(&cli, &domain_pol,
+       if ((result = cli_samr_open_group(cli, &domain_pol,
                                          MAXIMUM_ALLOWED_ACCESS,
                                          group_rid, &group_pol))
            != NT_STATUS_NOPROBLEMO) {
@@ -422,7 +407,7 @@ static uint32 cmd_samr_query_groupmem(int argc, char **argv)
 
        got_group_pol = True;
 
-       if ((result = cli_samr_query_groupmem(&cli, &group_pol,
+       if ((result = cli_samr_query_groupmem(cli, &group_pol,
                                              &num_members, &group_rids,
                                              &group_attrs))
            != NT_STATUS_NOPROBLEMO) {
@@ -435,11 +420,11 @@ static uint32 cmd_samr_query_groupmem(int argc, char **argv)
        }
 
  done:
-       if (got_group_pol) cli_samr_close(&cli, &group_pol);
-       if (got_domain_pol) cli_samr_close(&cli, &domain_pol);
-       if (got_connect_pol) cli_samr_close(&cli, &connect_pol);
+       if (got_group_pol) cli_samr_close(cli, &group_pol);
+       if (got_domain_pol) cli_samr_close(cli, &domain_pol);
+       if (got_connect_pol) cli_samr_close(cli, &connect_pol);
 
-       cli_samr_shutdown(&cli);
+       cli_nt_session_close(cli);
 
        return result;
 }
@@ -447,9 +432,10 @@ static uint32 cmd_samr_query_groupmem(int argc, char **argv)
 /* List of commands exported by this module */
 
 struct cmd_set samr_commands[] = {
-       { "queryuser", cmd_samr_query_user, "Query user info" },
-       { "querygroup", cmd_samr_query_group, "Query group info" },
-       { "queryusergroups", cmd_samr_query_usergroups, "Query user groups" },
-       { "querygroupmem", cmd_samr_query_groupmem, "Query group membership" },
+       { "queryuser",          cmd_samr_query_user,            "Query user info" },
+       { "querygroup",         cmd_samr_query_group,           "Query group info" },
+       { "queryusergroups",    cmd_samr_query_usergroups,      "Query user groups" },
+       { "querygroupmem",      cmd_samr_query_groupmem,        "Query group membership" },
        { NULL, NULL, NULL }
 };
+
index a312bc8889efd2be74b587fbf65979ff51906943..c7989d6882b17ea7aa01b8f964f51c85ee896025 100644 (file)
@@ -224,11 +224,9 @@ static void display_print_info_3(PRINTER_INFO_3 *i3)
 
 /* Enumerate printers */
 
-static uint32 cmd_spoolss_enum_printers(int argc, char **argv)
+static uint32 cmd_spoolss_enum_printers(struct cli_state *cli, int argc, char **argv)
 {
        uint32 result = NT_STATUS_UNSUCCESSFUL, info_level = 1;
-       struct cli_state cli;
-       struct ntuser_creds creds;
        PRINTER_INFO_CTR ctr;
        int returned;
        
@@ -242,19 +240,14 @@ static uint32 cmd_spoolss_enum_printers(int argc, char **argv)
        }
 
        /* Initialise RPC connection */
-
-       ZERO_STRUCT(cli);
-       init_rpcclient_creds(&creds);
-
-       if (cli_spoolss_initialise(&cli, server, &creds) == NULL) {
-               goto done;
+       if (!cli_nt_session_open (cli, PIPE_SPOOLSS)) {
+               fprintf (stderr, "Could not initialize spoolss pipe!\n");
+               return NT_STATUS_UNSUCCESSFUL;
        }
 
        /* Enumerate printers */
-
        ZERO_STRUCT(ctr);
-
-       result = cli_spoolss_enum_printers(&cli, PRINTER_ENUM_LOCAL, 
+       result = cli_spoolss_enum_printers(cli, PRINTER_ENUM_LOCAL, 
                                           info_level, &returned, &ctr);
 
        if (result == NT_STATUS_NOPROBLEMO) {
@@ -277,8 +270,7 @@ static uint32 cmd_spoolss_enum_printers(int argc, char **argv)
                }
        }
 
- done:
-       cli_spoolss_shutdown(&cli);
+       cli_nt_session_close(cli);
 
        return result;
 }
@@ -314,11 +306,9 @@ static void display_port_info_2(PORT_INFO_2 *i2)
 
 /* Enumerate ports */
 
-static uint32 cmd_spoolss_enum_ports(int argc, char **argv)
+static uint32 cmd_spoolss_enum_ports(struct cli_state *cli, int argc, char **argv)
 {
        uint32 result = NT_STATUS_UNSUCCESSFUL, info_level = 1;
-       struct cli_state cli;
-       struct ntuser_creds creds;
        PORT_INFO_CTR ctr;
        int returned;
        
@@ -332,19 +322,15 @@ static uint32 cmd_spoolss_enum_ports(int argc, char **argv)
        }
 
        /* Initialise RPC connection */
-
-       ZERO_STRUCT(cli);
-       init_rpcclient_creds(&creds);
-
-       if (cli_spoolss_initialise(&cli, server, &creds) == NULL) {
-               goto done;
+       if (!cli_nt_session_open (cli, PIPE_SPOOLSS)) {
+               fprintf (stderr, "Could not initialize spoolss pipe!\n");
+               return NT_STATUS_UNSUCCESSFUL;
        }
 
-       /* Enumerate printers */
-
+       /* Enumerate ports */
        ZERO_STRUCT(ctr);
 
-       result = cli_spoolss_enum_ports(&cli, info_level, &returned, &ctr);
+       result = cli_spoolss_enum_ports(cli, info_level, &returned, &ctr);
 
        if (result == NT_STATUS_NOPROBLEMO) {
                int i;
@@ -364,21 +350,18 @@ static uint32 cmd_spoolss_enum_ports(int argc, char **argv)
                }
        }
 
- done:
-       cli_spoolss_shutdown(&cli);
+       cli_nt_session_close(cli);
 
        return result;
 }
 
 /* Get printer information */
 
-static uint32 cmd_spoolss_getprinter(int argc, char **argv)
+static uint32 cmd_spoolss_getprinter(struct cli_state *cli, int argc, char **argv)
 {
-       struct cli_state cli;
        POLICY_HND pol;
        uint32 result, info_level = 1;
        BOOL opened_hnd = False;
-       struct ntuser_creds creds;
        PRINTER_INFO_CTR ctr;
        fstring printer_name, station_name;
 
@@ -388,12 +371,9 @@ static uint32 cmd_spoolss_getprinter(int argc, char **argv)
        }
 
        /* Initialise RPC connection */
-
-       ZERO_STRUCT(cli);
-       init_rpcclient_creds(&creds);
-
-       if (cli_spoolss_initialise(&cli, server, &creds) == NULL) {
-               goto done;
+       if (!cli_nt_session_open (cli, PIPE_SPOOLSS)) {
+               fprintf (stderr, "Could not initialize spoolss pipe!\n");
+               return NT_STATUS_UNSUCCESSFUL;
        }
 
        /* Open a printer handle */
@@ -408,7 +388,7 @@ static uint32 cmd_spoolss_getprinter(int argc, char **argv)
        slprintf(station_name, sizeof(fstring), "\\\\%s", global_myname);
 
        if ((result = cli_spoolss_open_printer_ex(
-               &cli, printer_name, "", MAXIMUM_ALLOWED_ACCESS, station_name,
+               cli, printer_name, "", MAXIMUM_ALLOWED_ACCESS, station_name,
                username, &pol)) != NT_STATUS_NOPROBLEMO) {
                goto done;
        }
@@ -416,8 +396,7 @@ static uint32 cmd_spoolss_getprinter(int argc, char **argv)
        opened_hnd = True;
 
        /* Get printer info */
-
-       if ((result = cli_spoolss_getprinter(&cli, &pol, info_level, &ctr))
+       if ((result = cli_spoolss_getprinter(cli, &pol, info_level, &ctr))
            != NT_STATUS_NOPROBLEMO) {
                goto done;
        }
@@ -443,9 +422,9 @@ static uint32 cmd_spoolss_getprinter(int argc, char **argv)
        }
 
  done: 
-       if (opened_hnd) cli_spoolss_closeprinter(&cli, &pol);
+       if (opened_hnd) cli_spoolss_closeprinter(cli, &pol);
 
-       cli_spoolss_shutdown(&cli);
+       cli_nt_session_close(cli);
 
        return result;
 }
@@ -454,9 +433,9 @@ static uint32 cmd_spoolss_getprinter(int argc, char **argv)
 
 struct cmd_set spoolss_commands[] = {
 
-       { "enumprinters", cmd_spoolss_enum_printers, "Enumerate printers" },
-       { "enumports", cmd_spoolss_enum_ports, "Enumerate printer ports" },
-       { "getprinter", cmd_spoolss_getprinter, "Get printer info" },
+       { "enumprinters",       cmd_spoolss_enum_printers,      "Enumerate printers" },
+       { "enumports",          cmd_spoolss_enum_ports,         "Enumerate printer ports" },
+       { "getprinter",         cmd_spoolss_getprinter,         "Get printer info" },
 
        { NULL, NULL, NULL }
 };
index fce66b6b9b81fafb1f658780a4e5110f5ffd21b6..7cdd73a592755263f4d77ba2cf907a8cf8409303 100644 (file)
 #include "includes.h"
 
 extern int DEBUGLEVEL;
-       
-/* Connect info */
+extern fstring debugf;
+
+/* Various pipe commands */
+extern struct cmd_set lsarpc_commands[];
+extern struct cmd_set samr_commands[];
+extern struct cmd_set spoolss_commands[];
 
 pstring password;
 pstring username;
 pstring workgroup;
 pstring server;
 
-/* Various pipe commands */
-
-extern struct cmd_set lsarpc_commands[];
-extern struct cmd_set samr_commands[];
-extern struct cmd_set spoolss_commands[];
 
 DOM_SID domain_sid;
 
-/* Fetch the SID for this domain */
 
-void fetch_domain_sid(void)
+/* Fetch the SID for this domain */
+void fetch_domain_sid(struct cli_state *cli)
 {
-       struct cli_state cli;
        POLICY_HND pol;
        uint32 result = 0, info_class = 5;
-       struct ntuser_creds creds;
        fstring domain_name;
        static BOOL got_domain_sid;
 
        if (got_domain_sid) return;
 
-       ZERO_STRUCT(cli);
-       init_rpcclient_creds(&creds);
 
-       if (cli_lsa_initialise(&cli, server, &creds) == NULL) {
+       if (!cli_nt_session_open (cli, PIPE_LSARPC)) {
                fprintf(stderr, "could not initialise lsa pipe\n");
                goto error;
        }
-
-       if ((result = cli_lsa_open_policy(&cli, True, 
+       
+       if ((result = cli_lsa_open_policy(cli, True, 
                                          SEC_RIGHTS_MAXIMUM_ALLOWED,
                                          &pol) != NT_STATUS_NOPROBLEMO)) {
                goto error;
        }
 
-       if ((result = cli_lsa_query_info_policy(&cli, &pol, info_class, 
+       if ((result = cli_lsa_query_info_policy(cli, &pol, info_class, 
                                                domain_name, &domain_sid))
            != NT_STATUS_NOPROBLEMO) {
                goto error;
@@ -74,8 +69,8 @@ void fetch_domain_sid(void)
 
        got_domain_sid = True;
 
-       cli_lsa_close(&cli, &pol);
-       cli_lsa_shutdown(&cli);
+       cli_lsa_close(cli, &pol);
+       cli_nt_session_close(cli);
 
        return;
 
@@ -91,7 +86,8 @@ void fetch_domain_sid(void)
 
 /* Initialise client credentials for authenticated pipe access */
 
-void init_rpcclient_creds(struct ntuser_creds *creds)
+void init_rpcclient_creds(struct ntuser_creds *creds, char* username,
+                         char* workgroup, char* password)
 {
        ZERO_STRUCTP(creds);
        
@@ -112,7 +108,7 @@ static struct cmd_list {
        struct cmd_set *cmd_set;
 } *cmd_list;
 
-static uint32 cmd_help(int argc, char **argv)
+static uint32 cmd_help(struct cli_state *cli, int argc, char **argv)
 {
        struct cmd_list *temp_list;
 
@@ -129,7 +125,7 @@ static uint32 cmd_help(int argc, char **argv)
        return 0;
 }
 
-static uint32 cmd_debuglevel(int argc, char **argv)
+static uint32 cmd_debuglevel(struct cli_state *cli, int argc, char **argv)
 {
        if (argc > 2) {
                printf("Usage: %s [debuglevel]\n", argv[0]);
@@ -145,7 +141,7 @@ static uint32 cmd_debuglevel(int argc, char **argv)
        return NT_STATUS_NOPROBLEMO;
 }
 
-static uint32 cmd_quit(int argc, char **argv)
+static uint32 cmd_quit(struct cli_state *cli, int argc, char **argv)
 {
        exit(0);
 }
@@ -153,10 +149,10 @@ static uint32 cmd_quit(int argc, char **argv)
 /* Build in rpcclient commands */
 
 static struct cmd_set rpcclient_commands[] = {
-       { "help", cmd_help, "Print list of commands" },
+       { "help",       cmd_help,       "Print list of commands" },
        { "debuglevel", cmd_debuglevel, "Set debug level" },
-       { "quit", cmd_quit, "Exit program" },
-       { "?", cmd_help, "Print list of commands" },
+       { "quit",       cmd_quit,       "Exit program" },
+       { "?",          cmd_help,       "Print list of commands" },
 
        { NULL, NULL, NULL }
 };
@@ -176,7 +172,7 @@ void add_command_set(struct cmd_set *cmd_set)
        DLIST_ADD(cmd_list, entry);
 }
 
-static uint32 do_cmd(struct cmd_set *cmd_entry, char *cmd)
+static uint32 do_cmd(struct cli_state *cli, struct cmd_set *cmd_entry, char *cmd)
 {
        char *p = cmd, **argv = NULL;
        uint32 result;
@@ -218,7 +214,7 @@ static uint32 do_cmd(struct cmd_set *cmd_entry, char *cmd)
 
        /* Call the function */
 
-       result = cmd_entry->fn(argc, argv);
+       result = cmd_entry->fn(cli, argc, argv);
                                
        /* Cleanup */
 
@@ -233,7 +229,7 @@ static uint32 do_cmd(struct cmd_set *cmd_entry, char *cmd)
 
 /* Process a command entered at the prompt or as part of -c */
 
-static uint32 process_cmd(char *cmd)
+static uint32 process_cmd(struct cli_state *cli, char *cmd)
 {
        struct cmd_list *temp_list;
        BOOL found = False;
@@ -253,7 +249,7 @@ static uint32 process_cmd(char *cmd)
                while(temp_set->name) {
                        if (strequal(buf, temp_set->name)) {
                                found = True;
-                               result = do_cmd(temp_set, cmd);
+                               result = do_cmd(cli, temp_set, cmd);
                                goto done;
                        }
                        temp_set++;
@@ -273,18 +269,59 @@ static uint32 process_cmd(char *cmd)
        return result;
 }
 
-/* Print usage information */
+/************************************************************************/
+struct cli_state *setup_connection(struct cli_state *cli, char *system_name,
+                                  struct ntuser_creds *creds)
+{
+       struct in_addr dest_ip;
+       struct nmb_name calling, called;
+       fstring dest_host;
+       extern pstring global_myname;
+       struct ntuser_creds anon;
+
+       /* Initialise cli_state information */
+       if (!cli_initialise(cli)) {
+               return NULL;
+       }
 
+       if (!creds) {
+               ZERO_STRUCT(anon);
+               anon.pwd.null_pwd = 1;
+               creds = &anon;
+       }
+
+       cli_init_creds(cli, creds);
+
+       /* Establish a SMB connection */
+       if (!resolve_srv_name(system_name, dest_host, &dest_ip)) {
+               return NULL;
+       }
+
+       make_nmb_name(&called, dns_to_netbios_name(dest_host), 0x20);
+       make_nmb_name(&calling, dns_to_netbios_name(global_myname), 0);
+
+       if (!cli_establish_connection(cli, dest_host, &dest_ip, &calling, 
+                                     &called, "IPC$", "IPC", False, True)) {
+               return NULL;
+       }
+       
+       return cli;
+}
+
+
+/* Print usage information */
 static void usage(char *pname)
 {
        printf("Usage: %s server [options]\n", pname);
 
-       printf("\t-N                    don't ask for a password\n");
+       printf("\t-c \"command string\"   execute semicolon separated cmds\n");
        printf("\t-d debuglevel         set the debuglevel\n");
+       printf("\t-l logfile            name of logfile to use as opposed to stdout\n");
        printf("\t-h                    Print this help message.\n");
+       printf("\t-N                    don't ask for a password\n");
+       printf("\t-s configfile         specify an alternative config file\n");
        printf("\t-U username           set the network username\n");
        printf("\t-W workgroup          set the workgroup name\n");
-       printf("\t-c command string     execute semicolon separated cmds\n");
        printf("\n");
 }
 
@@ -297,41 +334,24 @@ static void usage(char *pname)
        struct in_addr dest_ip;
        extern pstring global_myname;
        BOOL got_pass = False;
+       BOOL interactive = True;
        BOOL have_ip = False;
        int opt;
+       int olddebug;
        pstring cmdstr = "", servicesf = CONFIGFILE;
-       extern FILE *dbf;
+       struct ntuser_creds     creds;
+       struct cli_state        cli;
 
        setlinebuf(stdout);
-       dbf = stderr;
-
-       setup_logging(argv[0], True);
 
 #ifdef HAVE_LIBREADLINE
        /* Allow conditional parsing of the ~/.inputrc file. */
        rl_readline_name = "rpcclient";
 #endif    
        
-       DEBUGLEVEL = 2;
-
-       /* Load smb.conf file */
-
-       charset_initialise();
-
-       if (!lp_load(servicesf,True,False,False)) {
-               fprintf(stderr, "Can't load %s\n", servicesf);
-       }
-
-       codepage_initialise(lp_client_code_page());
-       load_interfaces();
-
-       TimeInit();
-
-       get_myname((*global_myname)?NULL:global_myname);
-       strupper(global_myname);
+       DEBUGLEVEL = 1;
 
        /* Parse options */
-
        if (argc < 2) {
                usage(argv[0]);
                return 0;
@@ -342,21 +362,35 @@ static void usage(char *pname)
        argv++;
        argc--;
 
-       while ((opt = getopt(argc, argv, "s:Nd:I:U:W:c:")) != EOF) {
+       while ((opt = getopt(argc, argv, "s:Nd:I:U:W:c:l:")) != EOF) {
                switch (opt) {
-               case 's':
-                       pstrcpy(servicesf, optarg);
-                       break;
-               case 'N':
+               case 'c':
+                       pstrcpy(cmdstr, optarg);
                        got_pass = True;
                        break;
+
                case 'd':
                        DEBUGLEVEL = atoi(optarg);
                        break;
+
                case 'I':
                        dest_ip = *interpret_addr2(optarg);
                        have_ip = True;
                        break;
+                       
+               case 'l':
+                       slprintf(debugf, sizeof(debugf) - 1, "%s.client", optarg);
+                       interactive = False;
+                       break;
+
+               case 'N':
+                       got_pass = True;
+                       break;
+                       
+               case 's':
+                       pstrcpy(servicesf, optarg);
+                       break;
+
                case 'U': {
                        char *lp;
                        pstrcpy(username,optarg);
@@ -368,50 +402,95 @@ static void usage(char *pname)
                        }
                        break;
                }
+               
                case 'W':
                        pstrcpy(workgroup, optarg);
                        break;
-               case 'c':
-                       pstrcpy(cmdstr, optarg);
-                       got_pass = True;
-                       break;
+                       
                case 'h':
                default:
                        usage(argv[0]);
                        exit(1);
                }
        }
+       
+       /* the following functions are part of the Samba debugging
+          facilities.  See lib/debug.c */
+       setup_logging (argv[0], interactive);
+       if (!interactive) 
+               reopen_logs();
 
-       /* Load command lists */
+       charset_initialise();
+       
+       /* FIXME!  How to get this DEBUGLEVEL to last over lp_load()? */
+       olddebug = DEBUGLEVEL;
 
+       /* Load smb.conf file */
+       if (!lp_load(servicesf,True,False,False)) {
+               fprintf(stderr, "Can't load %s\n", servicesf);
+       }
+       DEBUGLEVEL = olddebug;
+
+       codepage_initialise(lp_client_code_page());
+       load_interfaces();
+
+       TimeInit();
+
+       get_myname((*global_myname)?NULL:global_myname);
+       strupper(global_myname);
+       
+       /*
+        * initialize the credentials struct.  Get password
+        * from stdin if necessary
+        */
+       if (!got_pass) {
+               init_rpcclient_creds (&creds, username, workgroup, "");
+               pwd_read(&creds.pwd, "Password : ", lp_encrypted_passwords());
+       }
+       else {
+               init_rpcclient_creds (&creds, username, workgroup, password);
+       }
+       memset(password,'X',strlen(password));
+
+       /* open a connection to the specified server */
+       ZERO_STRUCTP (&cli);
+       if (!setup_connection (&cli, server, &creds)) {
+               return 0;
+       }
+       
+       /* There are no pointers in ntuser_creds struct so zero it out */
+       ZERO_STRUCTP (&creds);
+       
+
+       /* Load command lists */
        add_command_set(rpcclient_commands);
+       add_command_set(spoolss_commands);
        add_command_set(lsarpc_commands);
        add_command_set(samr_commands);
-       add_command_set(spoolss_commands);
 
-       /* Do anything specified with -c */
 
+       /* Do anything specified with -c */
        if (cmdstr[0]) {
                pstring cmd;
                char *p = cmdstr;
                uint32 result;
 
                while(next_token(&p, cmd, ";", sizeof(pstring))) {
-                       result = process_cmd(cmd);
+                       result = process_cmd(&cli, cmd);
                }
 
                return 0;
        }
 
-       /* Loop around accepting commands */
 
+       /* Loop around accepting commands */
        while(1) {
                pstring prompt, cmd;
                uint32 result;
 
                ZERO_STRUCT(cmd);
                
-               slprintf(prompt, sizeof(prompt) - 1, "rpcclient> ");
+               slprintf(prompt, sizeof(prompt) - 1, "rpcclient $> ");
 
 #if HAVE_READLINE
                cmd = readline(prompt);
@@ -424,8 +503,9 @@ static void usage(char *pname)
 
                cmd[strlen(cmd) - 1] = '\0';
 #endif
-               result = process_cmd(cmd);
+               result = process_cmd(&cli, cmd);
        }
-
+       
        return 0;
 }
+