1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 |
|
---|
4 | client connect/disconnect routines
|
---|
5 |
|
---|
6 | Copyright (C) Andrew Tridgell 2003-2005
|
---|
7 | Copyright (C) James Peach 2005
|
---|
8 |
|
---|
9 | This program is free software; you can redistribute it and/or modify
|
---|
10 | it under the terms of the GNU General Public License as published by
|
---|
11 | the Free Software Foundation; either version 3 of the License, or
|
---|
12 | (at your option) any later version.
|
---|
13 |
|
---|
14 | This program is distributed in the hope that it will be useful,
|
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | GNU General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU General Public License
|
---|
20 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include "includes.h"
|
---|
24 | #include "libcli/libcli.h"
|
---|
25 | #include "libcli/raw/libcliraw.h"
|
---|
26 | #include "libcli/raw/raw_proto.h"
|
---|
27 | #include "libcli/auth/libcli_auth.h"
|
---|
28 | #include "libcli/smb_composite/smb_composite.h"
|
---|
29 | #include "libcli/smb/smbXcli_base.h"
|
---|
30 |
|
---|
31 | /*
|
---|
32 | wrapper around smbcli_sock_connect()
|
---|
33 | */
|
---|
34 | bool smbcli_socket_connect(struct smbcli_state *cli, const char *server,
|
---|
35 | const char **ports,
|
---|
36 | struct tevent_context *ev_ctx,
|
---|
37 | struct resolve_context *resolve_ctx,
|
---|
38 | struct smbcli_options *options,
|
---|
39 | const char *socket_options,
|
---|
40 | struct nbt_name *calling,
|
---|
41 | struct nbt_name *called)
|
---|
42 | {
|
---|
43 | NTSTATUS status;
|
---|
44 |
|
---|
45 | cli->options = *options;
|
---|
46 |
|
---|
47 | status = smbcli_sock_connect(cli,
|
---|
48 | NULL, /* host_addr */
|
---|
49 | ports,
|
---|
50 | server,
|
---|
51 | resolve_ctx,
|
---|
52 | ev_ctx,
|
---|
53 | socket_options,
|
---|
54 | calling,
|
---|
55 | called,
|
---|
56 | &cli->sock);
|
---|
57 | if (!NT_STATUS_IS_OK(status)) {
|
---|
58 | return false;
|
---|
59 | }
|
---|
60 |
|
---|
61 | return true;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /* wrapper around smb_raw_negotiate() */
|
---|
65 | NTSTATUS smbcli_negprot(struct smbcli_state *cli, bool unicode, int maxprotocol)
|
---|
66 | {
|
---|
67 | if (unicode) {
|
---|
68 | cli->options.unicode = 1;
|
---|
69 | } else {
|
---|
70 | cli->options.unicode = 0;
|
---|
71 | }
|
---|
72 |
|
---|
73 | cli->transport = smbcli_transport_init(cli->sock, cli,
|
---|
74 | true, &cli->options);
|
---|
75 | cli->sock = NULL;
|
---|
76 | if (!cli->transport) {
|
---|
77 | return NT_STATUS_NO_MEMORY;
|
---|
78 | }
|
---|
79 |
|
---|
80 | return smb_raw_negotiate(cli->transport, unicode, PROTOCOL_CORE, maxprotocol);
|
---|
81 | }
|
---|
82 |
|
---|
83 | /* wrapper around smb_raw_sesssetup() */
|
---|
84 | NTSTATUS smbcli_session_setup(struct smbcli_state *cli,
|
---|
85 | struct cli_credentials *credentials,
|
---|
86 | const char *workgroup,
|
---|
87 | struct smbcli_session_options options,
|
---|
88 | struct gensec_settings *gensec_settings)
|
---|
89 | {
|
---|
90 | struct smb_composite_sesssetup setup;
|
---|
91 | NTSTATUS status;
|
---|
92 |
|
---|
93 | cli->session = smbcli_session_init(cli->transport, cli, true,
|
---|
94 | options);
|
---|
95 | if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
|
---|
96 |
|
---|
97 | setup.in.sesskey = cli->transport->negotiate.sesskey;
|
---|
98 | setup.in.capabilities = cli->transport->negotiate.capabilities;
|
---|
99 | setup.in.credentials = credentials;
|
---|
100 | setup.in.workgroup = workgroup;
|
---|
101 | setup.in.gensec_settings = gensec_settings;
|
---|
102 |
|
---|
103 | status = smb_composite_sesssetup(cli->session, &setup);
|
---|
104 |
|
---|
105 | cli->session->vuid = setup.out.vuid;
|
---|
106 |
|
---|
107 | return status;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /* wrapper around smb_raw_tcon() */
|
---|
111 | NTSTATUS smbcli_tconX(struct smbcli_state *cli, const char *sharename,
|
---|
112 | const char *devtype, const char *password)
|
---|
113 | {
|
---|
114 | union smb_tcon tcon;
|
---|
115 | TALLOC_CTX *mem_ctx;
|
---|
116 | NTSTATUS status;
|
---|
117 |
|
---|
118 | cli->tree = smbcli_tree_init(cli->session, cli, true);
|
---|
119 | if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
|
---|
120 |
|
---|
121 | mem_ctx = talloc_init("tcon");
|
---|
122 | if (!mem_ctx) {
|
---|
123 | return NT_STATUS_NO_MEMORY;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /* setup a tree connect */
|
---|
127 | tcon.generic.level = RAW_TCON_TCONX;
|
---|
128 | tcon.tconx.in.flags = TCONX_FLAG_EXTENDED_RESPONSE;
|
---|
129 | tcon.tconx.in.flags |= TCONX_FLAG_EXTENDED_SIGNATURES;
|
---|
130 | if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
|
---|
131 | tcon.tconx.in.password = data_blob(NULL, 0);
|
---|
132 | } else if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
|
---|
133 | tcon.tconx.in.password = data_blob_talloc(mem_ctx, NULL, 24);
|
---|
134 | if (cli->transport->negotiate.secblob.length < 8) {
|
---|
135 | return NT_STATUS_INVALID_PARAMETER;
|
---|
136 | }
|
---|
137 | SMBencrypt(password, cli->transport->negotiate.secblob.data, tcon.tconx.in.password.data);
|
---|
138 | } else {
|
---|
139 | tcon.tconx.in.password = data_blob_talloc(mem_ctx, password, strlen(password)+1);
|
---|
140 | }
|
---|
141 | tcon.tconx.in.path = sharename;
|
---|
142 | tcon.tconx.in.device = devtype;
|
---|
143 |
|
---|
144 | status = smb_raw_tcon(cli->tree, mem_ctx, &tcon);
|
---|
145 |
|
---|
146 | cli->tree->tid = tcon.tconx.out.tid;
|
---|
147 |
|
---|
148 | if (tcon.tconx.out.options & SMB_EXTENDED_SIGNATURES) {
|
---|
149 | smb1cli_session_protect_session_key(cli->tree->session->smbXcli);
|
---|
150 | }
|
---|
151 |
|
---|
152 | talloc_free(mem_ctx);
|
---|
153 |
|
---|
154 | return status;
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | /*
|
---|
159 | easy way to get to a fully connected smbcli_state in one call
|
---|
160 | */
|
---|
161 | NTSTATUS smbcli_full_connection(TALLOC_CTX *parent_ctx,
|
---|
162 | struct smbcli_state **ret_cli,
|
---|
163 | const char *host,
|
---|
164 | const char **ports,
|
---|
165 | const char *sharename,
|
---|
166 | const char *devtype,
|
---|
167 | const char *socket_options,
|
---|
168 | struct cli_credentials *credentials,
|
---|
169 | struct resolve_context *resolve_ctx,
|
---|
170 | struct tevent_context *ev,
|
---|
171 | struct smbcli_options *options,
|
---|
172 | struct smbcli_session_options *session_options,
|
---|
173 | struct gensec_settings *gensec_settings)
|
---|
174 | {
|
---|
175 | struct smbcli_tree *tree;
|
---|
176 | NTSTATUS status;
|
---|
177 |
|
---|
178 | *ret_cli = NULL;
|
---|
179 |
|
---|
180 | status = smbcli_tree_full_connection(parent_ctx,
|
---|
181 | &tree, host, ports,
|
---|
182 | sharename, devtype,
|
---|
183 | socket_options,
|
---|
184 | credentials, resolve_ctx, ev,
|
---|
185 | options,
|
---|
186 | session_options,
|
---|
187 | gensec_settings);
|
---|
188 | if (!NT_STATUS_IS_OK(status)) {
|
---|
189 | goto done;
|
---|
190 | }
|
---|
191 |
|
---|
192 | (*ret_cli) = smbcli_state_init(parent_ctx);
|
---|
193 |
|
---|
194 | (*ret_cli)->tree = tree;
|
---|
195 | (*ret_cli)->session = tree->session;
|
---|
196 | (*ret_cli)->transport = tree->session->transport;
|
---|
197 |
|
---|
198 | talloc_steal(*ret_cli, tree);
|
---|
199 |
|
---|
200 | done:
|
---|
201 | return status;
|
---|
202 | }
|
---|
203 |
|
---|
204 |
|
---|
205 | /*
|
---|
206 | disconnect the tree
|
---|
207 | */
|
---|
208 | NTSTATUS smbcli_tdis(struct smbcli_state *cli)
|
---|
209 | {
|
---|
210 | return smb_tree_disconnect(cli->tree);
|
---|
211 | }
|
---|
212 |
|
---|
213 | /****************************************************************************
|
---|
214 | Initialise a client state structure.
|
---|
215 | ****************************************************************************/
|
---|
216 | struct smbcli_state *smbcli_state_init(TALLOC_CTX *mem_ctx)
|
---|
217 | {
|
---|
218 | return talloc_zero(mem_ctx, struct smbcli_state);
|
---|
219 | }
|
---|
220 |
|
---|
221 | /* Insert a NULL at the first separator of the given path and return a pointer
|
---|
222 | * to the remainder of the string.
|
---|
223 | */
|
---|
224 | static char *
|
---|
225 | terminate_path_at_separator(char * path)
|
---|
226 | {
|
---|
227 | char * p;
|
---|
228 |
|
---|
229 | if (!path) {
|
---|
230 | return NULL;
|
---|
231 | }
|
---|
232 |
|
---|
233 | if ((p = strchr_m(path, '/'))) {
|
---|
234 | *p = '\0';
|
---|
235 | return p + 1;
|
---|
236 | }
|
---|
237 |
|
---|
238 | if ((p = strchr_m(path, '\\'))) {
|
---|
239 | *p = '\0';
|
---|
240 | return p + 1;
|
---|
241 | }
|
---|
242 |
|
---|
243 | /* No separator. */
|
---|
244 | return NULL;
|
---|
245 | }
|
---|
246 |
|
---|
247 | /*
|
---|
248 | parse a //server/share type UNC name
|
---|
249 | */
|
---|
250 | bool smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
|
---|
251 | char **hostname, char **sharename)
|
---|
252 | {
|
---|
253 | char *p;
|
---|
254 |
|
---|
255 | if (strncmp(unc_name, "\\\\", 2) &&
|
---|
256 | strncmp(unc_name, "//", 2)) {
|
---|
257 | return false;
|
---|
258 | }
|
---|
259 |
|
---|
260 | *hostname = *sharename = NULL;
|
---|
261 |
|
---|
262 | *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
|
---|
263 | p = terminate_path_at_separator(*hostname);
|
---|
264 |
|
---|
265 | if (p != NULL && *p) {
|
---|
266 | *sharename = talloc_strdup(mem_ctx, p);
|
---|
267 | terminate_path_at_separator(*sharename);
|
---|
268 | }
|
---|
269 |
|
---|
270 | if (*hostname && *sharename) {
|
---|
271 | return true;
|
---|
272 | }
|
---|
273 |
|
---|
274 | talloc_free(*hostname);
|
---|
275 | talloc_free(*sharename);
|
---|
276 | *hostname = *sharename = NULL;
|
---|
277 | return false;
|
---|
278 | }
|
---|
279 |
|
---|
280 |
|
---|
281 |
|
---|