2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Guenther Deschner 2008
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "lib/netapi/netapi.h"
23 #include "lib/netapi/netapi_private.h"
24 #include "libsmb/libsmb.h"
25 #include "rpc_client/cli_pipe.h"
26 #include "../libcli/smb/smbXcli_base.h"
28 /********************************************************************
29 ********************************************************************/
31 struct client_ipc_connection
{
32 struct client_ipc_connection
*prev
, *next
;
33 struct cli_state
*cli
;
34 struct client_pipe_connection
*pipe_connections
;
37 struct client_pipe_connection
{
38 struct client_pipe_connection
*prev
, *next
;
39 struct rpc_pipe_client
*pipe
;
42 /********************************************************************
43 ********************************************************************/
45 static struct client_ipc_connection
*ipc_cm_find(
46 struct libnetapi_private_ctx
*priv_ctx
, const char *server_name
)
48 struct client_ipc_connection
*p
;
50 for (p
= priv_ctx
->ipc_connections
; p
; p
= p
->next
) {
51 const char *remote_name
= smbXcli_conn_remote_name(p
->cli
->conn
);
53 if (strequal(remote_name
, server_name
)) {
61 /********************************************************************
62 ********************************************************************/
64 static WERROR
libnetapi_open_ipc_connection(struct libnetapi_ctx
*ctx
,
65 const char *server_name
,
66 struct client_ipc_connection
**pp
)
68 struct libnetapi_private_ctx
*priv_ctx
;
69 struct cli_state
*cli_ipc
= NULL
;
70 struct client_ipc_connection
*p
;
72 const char *username
= NULL
;
73 const char *password
= NULL
;
75 enum credentials_use_kerberos krb5_state
;
77 if (!ctx
|| !pp
|| !server_name
) {
78 return WERR_INVALID_PARAMETER
;
81 priv_ctx
= (struct libnetapi_private_ctx
*)ctx
->private_data
;
83 p
= ipc_cm_find(priv_ctx
, server_name
);
89 rc
= libnetapi_get_username(ctx
, &username
);
91 return WERR_INTERNAL_ERROR
;
94 rc
= libnetapi_get_password(ctx
, &password
);
96 return WERR_INTERNAL_ERROR
;
99 if (password
== NULL
) {
100 cli_credentials_set_cmdline_callbacks(ctx
->creds
);
103 krb5_state
= cli_credentials_get_kerberos_state(ctx
->creds
);
105 if (username
!= NULL
&& username
[0] != '\0' &&
106 password
!= NULL
&& password
[0] != '\0' &&
107 krb5_state
== CRED_USE_KERBEROS_REQUIRED
) {
108 cli_credentials_set_kerberos_state(ctx
->creds
,
109 CRED_USE_KERBEROS_DESIRED
,
113 status
= cli_cm_open(ctx
, NULL
,
116 NULL
, 0, 0x20, &cli_ipc
);
117 if (!NT_STATUS_IS_OK(status
)) {
122 libnetapi_set_error_string(ctx
,
123 "Failed to connect to IPC$ share on %s", server_name
);
124 return WERR_CAN_NOT_COMPLETE
;
127 p
= talloc_zero(ctx
, struct client_ipc_connection
);
129 return WERR_NOT_ENOUGH_MEMORY
;
133 DLIST_ADD(priv_ctx
->ipc_connections
, p
);
140 /********************************************************************
141 ********************************************************************/
143 WERROR
libnetapi_shutdown_cm(struct libnetapi_ctx
*ctx
)
145 struct libnetapi_private_ctx
*priv_ctx
=
146 (struct libnetapi_private_ctx
*)ctx
->private_data
;
147 struct client_ipc_connection
*p
;
149 for (p
= priv_ctx
->ipc_connections
; p
; p
= p
->next
) {
150 cli_shutdown(p
->cli
);
156 /********************************************************************
157 ********************************************************************/
159 static NTSTATUS
pipe_cm_find(struct client_ipc_connection
*ipc
,
160 const struct ndr_interface_table
*table
,
161 struct rpc_pipe_client
**presult
)
163 struct client_pipe_connection
*p
;
165 for (p
= ipc
->pipe_connections
; p
; p
= p
->next
) {
166 struct dcerpc_binding_handle
*bh
= NULL
;
167 const struct dcerpc_binding
*bd
= NULL
;
168 const char *ipc_remote_name
;
169 struct ndr_syntax_id syntax
;
171 if (!rpccli_is_connected(p
->pipe
)) {
172 return NT_STATUS_PIPE_EMPTY
;
175 ipc_remote_name
= smbXcli_conn_remote_name(ipc
->cli
->conn
);
177 if (!strequal(ipc_remote_name
, p
->pipe
->desthost
)) {
181 bh
= p
->pipe
->binding_handle
;
182 bd
= dcerpc_binding_handle_get_binding(bh
);
183 syntax
= dcerpc_binding_get_abstract_syntax(bd
);
185 if (ndr_syntax_id_equal(&syntax
, &table
->syntax_id
)) {
191 return NT_STATUS_PIPE_NOT_AVAILABLE
;
194 /********************************************************************
195 ********************************************************************/
197 static NTSTATUS
pipe_cm_connect(TALLOC_CTX
*mem_ctx
,
198 struct client_ipc_connection
*ipc
,
199 const struct ndr_interface_table
*table
,
200 struct rpc_pipe_client
**presult
)
202 struct client_pipe_connection
*p
;
205 p
= talloc_zero_array(mem_ctx
, struct client_pipe_connection
, 1);
207 return NT_STATUS_NO_MEMORY
;
210 status
= cli_rpc_pipe_open_noauth(ipc
->cli
, table
, &p
->pipe
);
211 if (!NT_STATUS_IS_OK(status
)) {
216 DLIST_ADD(ipc
->pipe_connections
, p
);
222 /********************************************************************
223 ********************************************************************/
225 static NTSTATUS
pipe_cm_open(TALLOC_CTX
*ctx
,
226 struct client_ipc_connection
*ipc
,
227 const struct ndr_interface_table
*table
,
228 struct rpc_pipe_client
**presult
)
230 if (NT_STATUS_IS_OK(pipe_cm_find(ipc
, table
, presult
))) {
234 return pipe_cm_connect(ctx
, ipc
, table
, presult
);
237 /********************************************************************
238 ********************************************************************/
240 WERROR
libnetapi_open_pipe(struct libnetapi_ctx
*ctx
,
241 const char *server_name
,
242 const struct ndr_interface_table
*table
,
243 struct rpc_pipe_client
**presult
)
245 struct rpc_pipe_client
*result
= NULL
;
248 struct client_ipc_connection
*ipc
= NULL
;
251 return WERR_INVALID_PARAMETER
;
254 werr
= libnetapi_open_ipc_connection(ctx
, server_name
, &ipc
);
255 if (!W_ERROR_IS_OK(werr
)) {
259 status
= pipe_cm_open(ctx
, ipc
, table
, &result
);
260 if (!NT_STATUS_IS_OK(status
)) {
261 libnetapi_set_error_string(ctx
, "failed to open PIPE %s: %s",
263 get_friendly_nt_error_msg(status
));
264 return WERR_NERR_DESTNOTFOUND
;
272 /********************************************************************
273 ********************************************************************/
275 WERROR
libnetapi_get_binding_handle(struct libnetapi_ctx
*ctx
,
276 const char *server_name
,
277 const struct ndr_interface_table
*table
,
278 struct dcerpc_binding_handle
**binding_handle
)
280 struct rpc_pipe_client
*pipe_cli
;
283 *binding_handle
= NULL
;
285 result
= libnetapi_open_pipe(ctx
, server_name
, table
, &pipe_cli
);
286 if (!W_ERROR_IS_OK(result
)) {
290 *binding_handle
= pipe_cli
->binding_handle
;