r10504: - seperate implementation specific stuff, from the generic composite
[amitay/samba.git] / source4 / libcli / cliconnect.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    client connect/disconnect routines
5
6    Copyright (C) Andrew Tridgell 2003-2005
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/composite/composite.h"
26 #include "libcli/smb_composite/smb_composite.h"
27
28 /*
29   wrapper around smbcli_sock_connect()
30 */
31 BOOL smbcli_socket_connect(struct smbcli_state *cli, const char *server)
32 {
33         struct smbcli_socket *sock;
34
35         sock = smbcli_sock_init(cli, NULL);
36         if (!sock) return False;
37
38         if (!smbcli_sock_connect_byname(sock, server, 0)) {
39                 talloc_free(sock);
40                 return False;
41         }
42         
43         cli->transport = smbcli_transport_init(sock, cli, True);
44         if (!cli->transport) {
45                 return False;
46         }
47
48         return True;
49 }
50
51 /* wrapper around smbcli_transport_connect() */
52 BOOL smbcli_transport_establish(struct smbcli_state *cli, 
53                                 struct nbt_name *calling,
54                                 struct nbt_name *called)
55 {
56         return smbcli_transport_connect(cli->transport, calling, called);
57 }
58
59 /* wrapper around smb_raw_negotiate() */
60 NTSTATUS smbcli_negprot(struct smbcli_state *cli)
61 {
62         return smb_raw_negotiate(cli->transport, lp_maxprotocol());
63 }
64
65 /* wrapper around smb_raw_sesssetup() */
66 NTSTATUS smbcli_session_setup(struct smbcli_state *cli, 
67                               struct cli_credentials *credentials)
68 {
69         struct smb_composite_sesssetup setup;
70         NTSTATUS status;
71
72         cli->session = smbcli_session_init(cli->transport, cli, True);
73         if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
74
75         setup.in.sesskey = cli->transport->negotiate.sesskey;
76         setup.in.capabilities = cli->transport->negotiate.capabilities;
77         setup.in.credentials = credentials;
78         setup.in.workgroup = lp_workgroup();
79
80         status = smb_composite_sesssetup(cli->session, &setup);
81
82         cli->session->vuid = setup.out.vuid;
83
84         return status;
85 }
86
87 /* wrapper around smb_raw_tcon() */
88 NTSTATUS smbcli_tconX(struct smbcli_state *cli, const char *sharename, 
89                       const char *devtype, const char *password)
90 {
91         union smb_tcon tcon;
92         TALLOC_CTX *mem_ctx;
93         NTSTATUS status;
94
95         cli->tree = smbcli_tree_init(cli->session, cli, True);
96         if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
97
98         mem_ctx = talloc_init("tcon");
99         if (!mem_ctx) {
100                 return NT_STATUS_NO_MEMORY;
101         }
102
103         /* setup a tree connect */
104         tcon.generic.level = RAW_TCON_TCONX;
105         tcon.tconx.in.flags = 0;
106         if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
107                 tcon.tconx.in.password = data_blob(NULL, 0);
108         } else if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
109                 tcon.tconx.in.password = data_blob_talloc(mem_ctx, NULL, 24);
110                 if (cli->transport->negotiate.secblob.length < 8) {
111                         return NT_STATUS_INVALID_PARAMETER;
112                 }
113                 SMBencrypt(password, cli->transport->negotiate.secblob.data, tcon.tconx.in.password.data);
114         } else {
115                 tcon.tconx.in.password = data_blob_talloc(mem_ctx, password, strlen(password)+1);
116         }
117         tcon.tconx.in.path = sharename;
118         tcon.tconx.in.device = devtype;
119         
120         status = smb_raw_tcon(cli->tree, mem_ctx, &tcon);
121
122         cli->tree->tid = tcon.tconx.out.tid;
123
124         talloc_free(mem_ctx);
125
126         return status;
127 }
128
129
130 /*
131   easy way to get to a fully connected smbcli_state in one call
132 */
133 NTSTATUS smbcli_full_connection(TALLOC_CTX *parent_ctx,
134                                 struct smbcli_state **ret_cli, 
135                                 const char *host,
136                                 const char *sharename,
137                                 const char *devtype,
138                                 struct cli_credentials *credentials,
139                                 struct event_context *ev)
140 {
141         struct smbcli_tree *tree;
142         NTSTATUS status;
143
144         *ret_cli = NULL;
145
146         status = smbcli_tree_full_connection(parent_ctx,
147                                              &tree, host, 0, sharename, devtype,
148                                              credentials, ev);
149         if (!NT_STATUS_IS_OK(status)) {
150                 goto done;
151         }
152
153         (*ret_cli) = smbcli_state_init(parent_ctx);
154
155         (*ret_cli)->tree = tree;
156         (*ret_cli)->session = tree->session;
157         (*ret_cli)->transport = tree->session->transport;
158
159         talloc_steal(*ret_cli, tree);
160         
161 done:
162         return status;
163 }
164
165
166 /*
167   disconnect the tree
168 */
169 NTSTATUS smbcli_tdis(struct smbcli_state *cli)
170 {
171         return smb_tree_disconnect(cli->tree);
172 }
173
174 /****************************************************************************
175  Initialise a client state structure.
176 ****************************************************************************/
177 struct smbcli_state *smbcli_state_init(TALLOC_CTX *mem_ctx)
178 {
179         return talloc_zero(mem_ctx, struct smbcli_state);
180 }
181
182 /*
183   parse a //server/share type UNC name
184 */
185 BOOL smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
186                       const char **hostname, const char **sharename)
187 {
188         char *p;
189
190         if (strncmp(unc_name, "\\\\", 2) &&
191             strncmp(unc_name, "//", 2)) {
192                 return False;
193         }
194
195         *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
196         p = strchr_m(&(*hostname)[2],'/');
197         if (!p) {
198                 p = strchr_m(&(*hostname)[2],'\\');
199                 if (!p) return False;
200         }
201         *p = 0;
202         *sharename = talloc_strdup(mem_ctx, p+1);
203
204         return True;
205 }
206
207
208