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