2 Unix SMB/CIFS implementation.
4 client connect/disconnect routines
6 Copyright (C) Andrew Tridgell 2003-2005
7 Copyright (C) James Peach 2005
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 2 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "libcli/libcli.h"
26 #include "libcli/raw/libcliraw.h"
27 #include "libcli/auth/libcli_auth.h"
28 #include "libcli/smb_composite/smb_composite.h"
31 wrapper around smbcli_sock_connect()
33 BOOL
smbcli_socket_connect(struct smbcli_state
*cli
, const char *server
)
35 struct smbcli_socket
*sock
;
37 sock
= smbcli_sock_connect_byname(server
, 0, NULL
, NULL
);
39 if (sock
== NULL
) return False
;
41 cli
->transport
= smbcli_transport_init(sock
, cli
, True
);
42 if (!cli
->transport
) {
49 /* wrapper around smbcli_transport_connect() */
50 BOOL
smbcli_transport_establish(struct smbcli_state
*cli
,
51 struct nbt_name
*calling
,
52 struct nbt_name
*called
)
54 return smbcli_transport_connect(cli
->transport
, calling
, called
);
57 /* wrapper around smb_raw_negotiate() */
58 NTSTATUS
smbcli_negprot(struct smbcli_state
*cli
)
60 return smb_raw_negotiate(cli
->transport
, lp_cli_maxprotocol());
63 /* wrapper around smb_raw_sesssetup() */
64 NTSTATUS
smbcli_session_setup(struct smbcli_state
*cli
,
65 struct cli_credentials
*credentials
)
67 struct smb_composite_sesssetup setup
;
70 cli
->session
= smbcli_session_init(cli
->transport
, cli
, True
);
71 if (!cli
->session
) return NT_STATUS_UNSUCCESSFUL
;
73 setup
.in
.sesskey
= cli
->transport
->negotiate
.sesskey
;
74 setup
.in
.capabilities
= cli
->transport
->negotiate
.capabilities
;
75 setup
.in
.credentials
= credentials
;
76 setup
.in
.workgroup
= lp_workgroup();
78 status
= smb_composite_sesssetup(cli
->session
, &setup
);
80 cli
->session
->vuid
= setup
.out
.vuid
;
85 /* wrapper around smb_raw_tcon() */
86 NTSTATUS
smbcli_tconX(struct smbcli_state
*cli
, const char *sharename
,
87 const char *devtype
, const char *password
)
93 cli
->tree
= smbcli_tree_init(cli
->session
, cli
, True
);
94 if (!cli
->tree
) return NT_STATUS_UNSUCCESSFUL
;
96 mem_ctx
= talloc_init("tcon");
98 return NT_STATUS_NO_MEMORY
;
101 /* setup a tree connect */
102 tcon
.generic
.level
= RAW_TCON_TCONX
;
103 tcon
.tconx
.in
.flags
= 0;
104 if (cli
->transport
->negotiate
.sec_mode
& NEGOTIATE_SECURITY_USER_LEVEL
) {
105 tcon
.tconx
.in
.password
= data_blob(NULL
, 0);
106 } else if (cli
->transport
->negotiate
.sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) {
107 tcon
.tconx
.in
.password
= data_blob_talloc(mem_ctx
, NULL
, 24);
108 if (cli
->transport
->negotiate
.secblob
.length
< 8) {
109 return NT_STATUS_INVALID_PARAMETER
;
111 SMBencrypt(password
, cli
->transport
->negotiate
.secblob
.data
, tcon
.tconx
.in
.password
.data
);
113 tcon
.tconx
.in
.password
= data_blob_talloc(mem_ctx
, password
, strlen(password
)+1);
115 tcon
.tconx
.in
.path
= sharename
;
116 tcon
.tconx
.in
.device
= devtype
;
118 status
= smb_raw_tcon(cli
->tree
, mem_ctx
, &tcon
);
120 cli
->tree
->tid
= tcon
.tconx
.out
.tid
;
122 talloc_free(mem_ctx
);
129 easy way to get to a fully connected smbcli_state in one call
131 NTSTATUS
smbcli_full_connection(TALLOC_CTX
*parent_ctx
,
132 struct smbcli_state
**ret_cli
,
134 const char *sharename
,
136 struct cli_credentials
*credentials
,
137 struct event_context
*ev
)
139 struct smbcli_tree
*tree
;
144 status
= smbcli_tree_full_connection(parent_ctx
,
145 &tree
, host
, 0, sharename
, devtype
,
147 if (!NT_STATUS_IS_OK(status
)) {
151 (*ret_cli
) = smbcli_state_init(parent_ctx
);
153 (*ret_cli
)->tree
= tree
;
154 (*ret_cli
)->session
= tree
->session
;
155 (*ret_cli
)->transport
= tree
->session
->transport
;
157 talloc_steal(*ret_cli
, tree
);
167 NTSTATUS
smbcli_tdis(struct smbcli_state
*cli
)
169 return smb_tree_disconnect(cli
->tree
);
172 /****************************************************************************
173 Initialise a client state structure.
174 ****************************************************************************/
175 struct smbcli_state
*smbcli_state_init(TALLOC_CTX
*mem_ctx
)
177 return talloc_zero(mem_ctx
, struct smbcli_state
);
180 /* Insert a NULL at the first separator of the given path and return a pointer
181 * to the remainder of the string.
184 terminate_path_at_separator(char * path
)
192 if ((p
= strchr_m(path
, '/'))) {
197 if ((p
= strchr_m(path
, '\\'))) {
207 parse a //server/share type UNC name
209 BOOL
smbcli_parse_unc(const char *unc_name
, TALLOC_CTX
*mem_ctx
,
210 char **hostname
, char **sharename
)
214 *hostname
= *sharename
= NULL
;
216 if (strncmp(unc_name
, "\\\\", 2) &&
217 strncmp(unc_name
, "//", 2)) {
221 *hostname
= talloc_strdup(mem_ctx
, &unc_name
[2]);
222 p
= terminate_path_at_separator(*hostname
);
225 *sharename
= talloc_strdup(mem_ctx
, p
);
226 terminate_path_at_separator(*sharename
);
229 if (*hostname
&& *sharename
) {
233 talloc_free(*hostname
);
234 talloc_free(*sharename
);
235 *hostname
= *sharename
= NULL
;