first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[kai/samba.git] / source3 / rpc_client / cli_connect.c
index 6b7fba41c5dfef068f0b4871e10e86d3a37df0c3..f935b530444811ba05f142f2c35347d89d3e6792 100644 (file)
@@ -23,9 +23,8 @@
 #define NO_SYSLOG
 
 #include "includes.h"
-#include "trans2.h"
 
-struct user_credentials *usr_creds = NULL;
+struct ntuser_creds *usr_creds = NULL;
 
 extern int DEBUGLEVEL;
 extern pstring scope;
@@ -36,7 +35,7 @@ struct cli_connection
        uint32 num_connections;
        char *srv_name;
        char *pipe_name;
-       struct user_credentials usr_creds;
+       struct ntuser_creds usr_creds;
        struct cli_state *cli;
        uint16 fnum;
 };
@@ -48,6 +47,8 @@ void init_connections(void)
 {
        con_list = NULL;
        num_cons = 0;
+
+       init_cli_use();
 }
 
 static void free_con_array(uint32 num_entries, struct cli_connection **entries)
@@ -67,10 +68,13 @@ static struct cli_connection* add_con_to_array(uint32 *len,
 void free_connections(void)
 {
        free_con_array(num_cons, con_list);
+       free_cli_use();
+
+       init_connections();
 }
 
 static struct cli_connection *cli_con_get(const char* srv_name,
-                               const char* pipe_name)
+                               const char* pipe_name, BOOL reuse)
 {
        struct cli_connection *con = NULL;
 
@@ -92,28 +96,14 @@ static struct cli_connection *cli_con_get(const char* srv_name,
                con->pipe_name = strdup(pipe_name);
        }
 
-       con->cli = cli_initialise(NULL);
-       con->fnum = 0xffff;
-
-       memcpy(&con->usr_creds, usr_creds, sizeof(*usr_creds));
+       con->cli = cli_net_use_add(srv_name, usr_creds, True, reuse);
 
        if (con->cli == NULL)
        {
                cli_connection_free(con);
                return NULL;
        }
-
-       /*
-        * initialise
-        */
-
-       con->cli->capabilities |= CAP_NT_SMBS | CAP_STATUS32;
-       cli_init_creds(con->cli, usr_creds);
-
-       con->cli->use_ntlmv2 = lp_client_ntlmv2();
-
        add_con_to_array(&num_cons, &con_list, con);
-
        return con;
 }
 
@@ -122,21 +112,53 @@ terminate client connection
 ****************************************************************************/
 void cli_connection_free(struct cli_connection *con)
 {
-       cli_nt_session_close(con->cli, con->fnum);
-       cli_shutdown(con->cli);
-       free(con->cli);
+       BOOL closed;
+       int i;
+
+       if (con->cli != NULL)
+       {
+               cli_nt_session_close(con->cli, con->fnum);
+               cli_net_use_del(con->srv_name, &con->usr_creds, False, &closed);
+       }
+
+       if (closed)
+       {
+               for (i = 0; i < num_cons; i++)
+               {
+                       if (con_list[i] != NULL &&
+                           con != con_list[i] &&
+                           con_list[i]->cli == con->cli)
+                       {
+                               /* WHOOPS! fnum already open: too bad!!! */
+                               con_list[i]->cli = NULL;
+                               con_list[i]->fnum = 0xffff;
+                       }
+               }
+       }
+
+       con->cli = NULL;
 
        if (con->srv_name != NULL)
        {
                free(con->srv_name);
+               con->srv_name = NULL;
        }
        if (con->pipe_name != NULL)
        {
                free(con->pipe_name);
+               con->pipe_name = NULL;
        }
 
        memset(&con->usr_creds, 0, sizeof(con->usr_creds));
 
+       for (i = 0; i < num_cons; i++)
+       {
+               if (con == con_list[i])
+               {
+                       con_list[i] = NULL;
+               }
+       }
+
        free(con);
 }
 
@@ -155,91 +177,23 @@ void cli_connection_unlink(struct cli_connection *con)
 /****************************************************************************
 init client state
 ****************************************************************************/
-BOOL cli_connection_init_list(char* servers, const char* pipe_name,
-                               struct cli_connection **con)
-{
-       BOOL res = True;
-
-       /*
-        * allocate
-        */
-
-       *con = cli_con_get(servers, pipe_name);
-
-       if ((*con) == NULL)
-       {
-               return False;
-       }
-
-       if (!cli_connect_serverlist((*con)->cli, servers))
-       {
-               DEBUG(0,("cli_state_init: connection failed\n"));
-               cli_connection_free((*con));
-               return False;
-       }
-
-       (*con)->cli->ntlmssp_cli_flgs = 0x0;
-
-       res = res ? cli_nt_session_open((*con)->cli, pipe_name,
-                                      &(*con)->fnum) : False;
-
-       return res;
-}
-
-/****************************************************************************
-init client state
-****************************************************************************/
-BOOL cli_connection_init(const char* server_name, const char* pipe_name,
+BOOL cli_connection_init(const char* srv_name, const char* pipe_name,
                                struct cli_connection **con)
 {
-       struct nmb_name calling;
-       struct nmb_name called;
-       struct in_addr *dest_ip = NULL;
-       fstring dest_host;
-       struct in_addr ip;
-
        BOOL res = True;
+       BOOL reuse = False;
 
        /*
         * allocate
         */
 
-       *con = cli_con_get(server_name, pipe_name);
+       *con = cli_con_get(srv_name, pipe_name, reuse);
 
        if ((*con) == NULL)
        {
                return False;
        }
 
-       if (resolve_srv_name(server_name, dest_host, &ip))
-       {
-               dest_ip = &ip;
-       }
-       else
-       {
-               return False;
-       }
-
-       make_nmb_name(&called , dns_to_netbios_name(dest_host    ), 32, scope);
-       make_nmb_name(&calling, dns_to_netbios_name(global_myname),  0, scope);
-
-       /*
-        * connect
-        */
-
-       if (!cli_establish_connection((*con)->cli, 
-                                 dest_host, dest_ip,
-                                 &calling, &called,
-                                 "IPC$", "IPC",
-                                 False, True))
-       {
-               DEBUG(0,("cli_state_init: connection failed\n"));
-               cli_connection_free((*con));
-               return False;
-       }
-
-       (*con)->cli->ntlmssp_cli_flgs = 0x0;
-
        res = res ? cli_nt_session_open((*con)->cli, pipe_name,
                                       &(*con)->fnum) : False;
 
@@ -294,6 +248,21 @@ BOOL cli_pol_link(POLICY_HND *to, const POLICY_HND *from)
        return register_policy_hnd(to) && set_policy_con(to, con, NULL);
 }
 
+/****************************************************************************
+get a user session key associated with a connection associated with a
+policy handle.
+****************************************************************************/
+BOOL cli_get_con_usr_sesskey(struct cli_connection *con, uchar usr_sess_key[16])
+{
+       if (con == NULL)
+       {
+               return False;
+       }
+       memcpy(usr_sess_key, con->cli->usr.pwd.sess_key, 16);
+
+       return True;
+}
+
 /****************************************************************************
 get a user session key associated with a connection associated with a
 policy handle.
@@ -333,6 +302,22 @@ BOOL cli_con_get_srvname(struct cli_connection *con, char *srv_name)
        return True;
 }
 
+/****************************************************************************
+get a user session key associated with a connection associated with a
+policy handle.
+****************************************************************************/
+BOOL cli_get_usr_sesskey(const POLICY_HND *pol, uchar usr_sess_key[16])
+{
+       struct cli_connection *con = NULL;
+
+       if (!cli_connection_get(pol, &con))
+       {
+               return False;
+       }
+
+       return cli_get_con_usr_sesskey(con, usr_sess_key);
+}
+
 /****************************************************************************
 get a user session key associated with a connection associated with a
 policy handle.