c344ece3858c00d97f1071f27b8eeb5c5f7251d1
[ira/wip.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_connect_byname(server, 0, NULL, NULL);
36
37         if (sock == NULL) return False;
38         
39         cli->transport = smbcli_transport_init(sock, cli, True);
40         if (!cli->transport) {
41                 return False;
42         }
43
44         return True;
45 }
46
47 /* wrapper around smbcli_transport_connect() */
48 BOOL smbcli_transport_establish(struct smbcli_state *cli, 
49                                 struct nbt_name *calling,
50                                 struct nbt_name *called)
51 {
52         return smbcli_transport_connect(cli->transport, calling, called);
53 }
54
55 /* wrapper around smb_raw_negotiate() */
56 NTSTATUS smbcli_negprot(struct smbcli_state *cli)
57 {
58         return smb_raw_negotiate(cli->transport, lp_maxprotocol());
59 }
60
61 /* wrapper around smb_raw_sesssetup() */
62 NTSTATUS smbcli_session_setup(struct smbcli_state *cli, 
63                               struct cli_credentials *credentials)
64 {
65         struct smb_composite_sesssetup setup;
66         NTSTATUS status;
67
68         cli->session = smbcli_session_init(cli->transport, cli, True);
69         if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
70
71         setup.in.sesskey = cli->transport->negotiate.sesskey;
72         setup.in.capabilities = cli->transport->negotiate.capabilities;
73         setup.in.credentials = credentials;
74         setup.in.workgroup = lp_workgroup();
75
76         status = smb_composite_sesssetup(cli->session, &setup);
77
78         cli->session->vuid = setup.out.vuid;
79
80         return status;
81 }
82
83 /* wrapper around smb_raw_tcon() */
84 NTSTATUS smbcli_tconX(struct smbcli_state *cli, const char *sharename, 
85                       const char *devtype, const char *password)
86 {
87         union smb_tcon tcon;
88         TALLOC_CTX *mem_ctx;
89         NTSTATUS status;
90
91         cli->tree = smbcli_tree_init(cli->session, cli, True);
92         if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
93
94         mem_ctx = talloc_init("tcon");
95         if (!mem_ctx) {
96                 return NT_STATUS_NO_MEMORY;
97         }
98
99         /* setup a tree connect */
100         tcon.generic.level = RAW_TCON_TCONX;
101         tcon.tconx.in.flags = 0;
102         if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
103                 tcon.tconx.in.password = data_blob(NULL, 0);
104         } else if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
105                 tcon.tconx.in.password = data_blob_talloc(mem_ctx, NULL, 24);
106                 if (cli->transport->negotiate.secblob.length < 8) {
107                         return NT_STATUS_INVALID_PARAMETER;
108                 }
109                 SMBencrypt(password, cli->transport->negotiate.secblob.data, tcon.tconx.in.password.data);
110         } else {
111                 tcon.tconx.in.password = data_blob_talloc(mem_ctx, password, strlen(password)+1);
112         }
113         tcon.tconx.in.path = sharename;
114         tcon.tconx.in.device = devtype;
115         
116         status = smb_raw_tcon(cli->tree, mem_ctx, &tcon);
117
118         cli->tree->tid = tcon.tconx.out.tid;
119
120         talloc_free(mem_ctx);
121
122         return status;
123 }
124
125
126 /*
127   easy way to get to a fully connected smbcli_state in one call
128 */
129 NTSTATUS smbcli_full_connection(TALLOC_CTX *parent_ctx,
130                                 struct smbcli_state **ret_cli, 
131                                 const char *host,
132                                 const char *sharename,
133                                 const char *devtype,
134                                 struct cli_credentials *credentials,
135                                 struct event_context *ev)
136 {
137         struct smbcli_tree *tree;
138         NTSTATUS status;
139
140         *ret_cli = NULL;
141
142         status = smbcli_tree_full_connection(parent_ctx,
143                                              &tree, host, 0, sharename, devtype,
144                                              credentials, ev);
145         if (!NT_STATUS_IS_OK(status)) {
146                 goto done;
147         }
148
149         (*ret_cli) = smbcli_state_init(parent_ctx);
150
151         (*ret_cli)->tree = tree;
152         (*ret_cli)->session = tree->session;
153         (*ret_cli)->transport = tree->session->transport;
154
155         talloc_steal(*ret_cli, tree);
156         
157 done:
158         return status;
159 }
160
161
162 /*
163   disconnect the tree
164 */
165 NTSTATUS smbcli_tdis(struct smbcli_state *cli)
166 {
167         return smb_tree_disconnect(cli->tree);
168 }
169
170 /****************************************************************************
171  Initialise a client state structure.
172 ****************************************************************************/
173 struct smbcli_state *smbcli_state_init(TALLOC_CTX *mem_ctx)
174 {
175         return talloc_zero(mem_ctx, struct smbcli_state);
176 }
177
178 /*
179   parse a //server/share type UNC name
180 */
181 BOOL smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
182                       const char **hostname, const char **sharename)
183 {
184         char *p;
185
186         if (strncmp(unc_name, "\\\\", 2) &&
187             strncmp(unc_name, "//", 2)) {
188                 return False;
189         }
190
191         *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
192         p = strchr_m(&(*hostname)[2],'/');
193         if (!p) {
194                 p = strchr_m(&(*hostname)[2],'\\');
195                 if (!p) return False;
196         }
197         *p = 0;
198         *sharename = talloc_strdup(mem_ctx, p+1);
199
200         return True;
201 }
202
203
204