1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Samba 4-compatible DCE/RPC API on top of the Samba 3 DCE/RPC client library.
|
---|
4 | Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
|
---|
5 |
|
---|
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.
|
---|
10 |
|
---|
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.
|
---|
15 |
|
---|
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/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "includes.h"
|
---|
21 | #include "librpc/rpc/dcerpc.h"
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Send a struct-based RPC request using the Samba 3 RPC client library.
|
---|
25 | */
|
---|
26 | struct rpc_request *dcerpc_ndr_request_send(struct dcerpc_pipe *p, const struct GUID *object,
|
---|
27 | const struct ndr_interface_table *table, uint32_t opnum,
|
---|
28 | TALLOC_CTX *mem_ctx, void *r)
|
---|
29 | {
|
---|
30 | const struct ndr_interface_call *call;
|
---|
31 | struct ndr_push *push;
|
---|
32 | struct rpc_request *ret = talloc(mem_ctx, struct rpc_request);
|
---|
33 | enum ndr_err_code ndr_err;
|
---|
34 | DATA_BLOB blob;
|
---|
35 |
|
---|
36 | if (ret == NULL)
|
---|
37 | return NULL;
|
---|
38 |
|
---|
39 | SMB_ASSERT(p->table->num_calls > opnum);
|
---|
40 |
|
---|
41 | call = &p->table->calls[opnum];
|
---|
42 |
|
---|
43 | ret->call = call;
|
---|
44 | ret->r = r;
|
---|
45 |
|
---|
46 | push = ndr_push_init_ctx(mem_ctx);
|
---|
47 | if (!push) {
|
---|
48 | return NULL;
|
---|
49 | }
|
---|
50 |
|
---|
51 | ndr_err = call->ndr_push(push, NDR_IN, r);
|
---|
52 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
---|
53 | /* FIXME: ndr_map_error2ntstatus(ndr_err); */
|
---|
54 | return NULL;
|
---|
55 | }
|
---|
56 |
|
---|
57 | blob = ndr_push_blob(push);
|
---|
58 |
|
---|
59 | if (!prs_init_data_blob(&ret->q_ps, &blob, mem_ctx)) {
|
---|
60 | return NULL;
|
---|
61 | }
|
---|
62 |
|
---|
63 | talloc_free(push);
|
---|
64 |
|
---|
65 | ret->opnum = opnum;
|
---|
66 |
|
---|
67 | ret->pipe = p;
|
---|
68 |
|
---|
69 | return ret;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Wait for a DCE/RPC request.
|
---|
74 | *
|
---|
75 | * @note at the moment this is still sync, even though the API is async.
|
---|
76 | */
|
---|
77 | NTSTATUS dcerpc_ndr_request_recv(struct rpc_request *req)
|
---|
78 | {
|
---|
79 | prs_struct r_ps;
|
---|
80 | struct ndr_pull *pull;
|
---|
81 | NTSTATUS status;
|
---|
82 | DATA_BLOB blob;
|
---|
83 | enum ndr_err_code ndr_err;
|
---|
84 |
|
---|
85 | prs_init_empty( &r_ps, req, UNMARSHALL );
|
---|
86 |
|
---|
87 | status = rpc_api_pipe_req(req->pipe->rpc_cli, req->opnum, &req->q_ps, &r_ps);
|
---|
88 |
|
---|
89 | prs_mem_free( &req->q_ps );
|
---|
90 |
|
---|
91 | if (!NT_STATUS_IS_OK(status)) {
|
---|
92 | prs_mem_free( &r_ps );
|
---|
93 | return status;
|
---|
94 | }
|
---|
95 |
|
---|
96 | if (!prs_data_blob(&r_ps, &blob, req)) {
|
---|
97 | prs_mem_free( &r_ps );
|
---|
98 | return NT_STATUS_NO_MEMORY;
|
---|
99 | }
|
---|
100 |
|
---|
101 | prs_mem_free( &r_ps );
|
---|
102 |
|
---|
103 | pull = ndr_pull_init_blob(&blob, req);
|
---|
104 | if (pull == NULL) {
|
---|
105 | return NT_STATUS_NO_MEMORY;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /* have the ndr parser alloc memory for us */
|
---|
109 | pull->flags |= LIBNDR_FLAG_REF_ALLOC;
|
---|
110 | ndr_err = req->call->ndr_pull(pull, NDR_OUT, req->r);
|
---|
111 | talloc_free(pull);
|
---|
112 |
|
---|
113 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
---|
114 | return ndr_map_error2ntstatus(ndr_err);
|
---|
115 | }
|
---|
116 |
|
---|
117 | return NT_STATUS_OK;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Connect to a DCE/RPC interface.
|
---|
122 | *
|
---|
123 | * @note lp_ctx and ev are ignored at the moment but present
|
---|
124 | * for API compatibility.
|
---|
125 | */
|
---|
126 | _PUBLIC_ NTSTATUS dcerpc_pipe_connect(TALLOC_CTX *parent_ctx, struct dcerpc_pipe **pp,
|
---|
127 | const char *binding_string, const struct ndr_interface_table *table,
|
---|
128 | struct cli_credentials *credentials, struct event_context *ev,
|
---|
129 | struct loadparm_context *lp_ctx)
|
---|
130 | {
|
---|
131 | struct dcerpc_pipe *p = talloc(parent_ctx, struct dcerpc_pipe);
|
---|
132 | struct dcerpc_binding *binding;
|
---|
133 | NTSTATUS nt_status;
|
---|
134 |
|
---|
135 | nt_status = dcerpc_parse_binding(p, binding_string, &binding);
|
---|
136 |
|
---|
137 | if (NT_STATUS_IS_ERR(nt_status)) {
|
---|
138 | DEBUG(1, ("Unable to parse binding string '%s'", binding_string));
|
---|
139 | talloc_free(p);
|
---|
140 | return nt_status;
|
---|
141 | }
|
---|
142 |
|
---|
143 | if (binding->transport != NCACN_NP) {
|
---|
144 | DEBUG(0, ("Only ncacn_np supported"));
|
---|
145 | talloc_free(p);
|
---|
146 | return NT_STATUS_NOT_SUPPORTED;
|
---|
147 | }
|
---|
148 |
|
---|
149 | /* FIXME: Actually use loadparm_context.. */
|
---|
150 |
|
---|
151 | /* FIXME: actually use credentials */
|
---|
152 |
|
---|
153 | nt_status = cli_full_connection(&p->cli, global_myname(), binding->host,
|
---|
154 | NULL, 0,
|
---|
155 | "IPC$", "IPC",
|
---|
156 | get_cmdline_auth_info_username(),
|
---|
157 | lp_workgroup(),
|
---|
158 | get_cmdline_auth_info_password(),
|
---|
159 | get_cmdline_auth_info_use_kerberos() ? CLI_FULL_CONNECTION_USE_KERBEROS : 0,
|
---|
160 | get_cmdline_auth_info_signing_state(), NULL);
|
---|
161 |
|
---|
162 | if (NT_STATUS_IS_ERR(nt_status)) {
|
---|
163 | talloc_free(p);
|
---|
164 | return nt_status;
|
---|
165 | }
|
---|
166 |
|
---|
167 | nt_status = cli_rpc_pipe_open_noauth(p->cli, &table->syntax_id,
|
---|
168 | &p->rpc_cli);
|
---|
169 |
|
---|
170 | if (!NT_STATUS_IS_OK(nt_status)) {
|
---|
171 | talloc_free(p);
|
---|
172 | return nt_status;
|
---|
173 | }
|
---|
174 |
|
---|
175 | p->table = table;
|
---|
176 |
|
---|
177 | *pp = p;
|
---|
178 |
|
---|
179 | return nt_status;
|
---|
180 | }
|
---|