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/>.
21 #include "lib/netapi/netapi.h"
22 #include "lib/netapi/netapi_private.h"
23 #include "../librpc/gen_ndr/cli_samr.h"
25 /****************************************************************
26 ****************************************************************/
28 WERROR
libnetapi_samr_open_domain(struct libnetapi_ctx
*mem_ctx
,
29 struct rpc_pipe_client
*pipe_cli
,
30 uint32_t connect_mask
,
32 struct policy_handle
*connect_handle
,
33 struct policy_handle
*domain_handle
,
34 struct dom_sid2
**domain_sid
)
38 struct libnetapi_private_ctx
*priv
;
39 uint32_t resume_handle
= 0;
40 uint32_t num_entries
= 0;
41 struct samr_SamArray
*sam
= NULL
;
42 const char *domain_name
= NULL
;
43 struct lsa_String lsa_domain_name
;
44 bool domain_found
= true;
47 priv
= talloc_get_type_abort(mem_ctx
->private_data
,
48 struct libnetapi_private_ctx
);
50 if (is_valid_policy_hnd(&priv
->samr
.connect_handle
)) {
51 if ((priv
->samr
.connect_mask
& connect_mask
) == connect_mask
) {
52 *connect_handle
= priv
->samr
.connect_handle
;
54 libnetapi_samr_close_connect_handle(mem_ctx
,
55 &priv
->samr
.connect_handle
);
59 if (is_valid_policy_hnd(&priv
->samr
.domain_handle
)) {
60 if ((priv
->samr
.domain_mask
& domain_mask
) == domain_mask
) {
61 *domain_handle
= priv
->samr
.domain_handle
;
63 libnetapi_samr_close_domain_handle(mem_ctx
,
64 &priv
->samr
.domain_handle
);
68 if (priv
->samr
.domain_sid
) {
69 *domain_sid
= priv
->samr
.domain_sid
;
72 if (is_valid_policy_hnd(&priv
->samr
.connect_handle
) &&
73 ((priv
->samr
.connect_mask
& connect_mask
) == connect_mask
) &&
74 is_valid_policy_hnd(&priv
->samr
.domain_handle
) &&
75 (priv
->samr
.domain_mask
& domain_mask
) == domain_mask
) {
79 if (!is_valid_policy_hnd(connect_handle
)) {
80 status
= rpccli_try_samr_connects(pipe_cli
, mem_ctx
,
83 if (!NT_STATUS_IS_OK(status
)) {
84 werr
= ntstatus_to_werror(status
);
89 status
= rpccli_samr_EnumDomains(pipe_cli
, mem_ctx
,
95 if (!NT_STATUS_IS_OK(status
)) {
96 werr
= ntstatus_to_werror(status
);
100 for (i
=0; i
<num_entries
; i
++) {
102 domain_name
= sam
->entries
[i
].name
.string
;
104 if (strequal(domain_name
, builtin_domain_name())) {
113 werr
= WERR_NO_SUCH_DOMAIN
;
117 init_lsa_String(&lsa_domain_name
, domain_name
);
119 status
= rpccli_samr_LookupDomain(pipe_cli
, mem_ctx
,
123 if (!NT_STATUS_IS_OK(status
)) {
124 werr
= ntstatus_to_werror(status
);
128 status
= rpccli_samr_OpenDomain(pipe_cli
, mem_ctx
,
133 if (!NT_STATUS_IS_OK(status
)) {
134 werr
= ntstatus_to_werror(status
);
138 priv
->samr
.cli
= pipe_cli
;
140 priv
->samr
.domain_name
= domain_name
;
141 priv
->samr
.domain_sid
= *domain_sid
;
143 priv
->samr
.connect_mask
= connect_mask
;
144 priv
->samr
.connect_handle
= *connect_handle
;
146 priv
->samr
.domain_mask
= domain_mask
;
147 priv
->samr
.domain_handle
= *domain_handle
;
155 /****************************************************************
156 ****************************************************************/
158 WERROR
libnetapi_samr_open_builtin_domain(struct libnetapi_ctx
*mem_ctx
,
159 struct rpc_pipe_client
*pipe_cli
,
160 uint32_t connect_mask
,
161 uint32_t builtin_mask
,
162 struct policy_handle
*connect_handle
,
163 struct policy_handle
*builtin_handle
)
167 struct libnetapi_private_ctx
*priv
;
169 priv
= talloc_get_type_abort(mem_ctx
->private_data
,
170 struct libnetapi_private_ctx
);
172 if (is_valid_policy_hnd(&priv
->samr
.connect_handle
)) {
173 if ((priv
->samr
.connect_mask
& connect_mask
) == connect_mask
) {
174 *connect_handle
= priv
->samr
.connect_handle
;
176 libnetapi_samr_close_connect_handle(mem_ctx
,
177 &priv
->samr
.connect_handle
);
181 if (is_valid_policy_hnd(&priv
->samr
.builtin_handle
)) {
182 if ((priv
->samr
.builtin_mask
& builtin_mask
) == builtin_mask
) {
183 *builtin_handle
= priv
->samr
.builtin_handle
;
185 libnetapi_samr_close_builtin_handle(mem_ctx
,
186 &priv
->samr
.builtin_handle
);
190 if (is_valid_policy_hnd(&priv
->samr
.connect_handle
) &&
191 ((priv
->samr
.connect_mask
& connect_mask
) == connect_mask
) &&
192 is_valid_policy_hnd(&priv
->samr
.builtin_handle
) &&
193 (priv
->samr
.builtin_mask
& builtin_mask
) == builtin_mask
) {
197 if (!is_valid_policy_hnd(connect_handle
)) {
198 status
= rpccli_try_samr_connects(pipe_cli
, mem_ctx
,
201 if (!NT_STATUS_IS_OK(status
)) {
202 werr
= ntstatus_to_werror(status
);
207 status
= rpccli_samr_OpenDomain(pipe_cli
, mem_ctx
,
210 CONST_DISCARD(DOM_SID
*, &global_sid_Builtin
),
212 if (!NT_STATUS_IS_OK(status
)) {
213 werr
= ntstatus_to_werror(status
);
217 priv
->samr
.cli
= pipe_cli
;
219 priv
->samr
.connect_mask
= connect_mask
;
220 priv
->samr
.connect_handle
= *connect_handle
;
222 priv
->samr
.builtin_mask
= builtin_mask
;
223 priv
->samr
.builtin_handle
= *builtin_handle
;
231 /****************************************************************
232 ****************************************************************/
234 void libnetapi_samr_close_domain_handle(struct libnetapi_ctx
*ctx
,
235 struct policy_handle
*handle
)
237 struct libnetapi_private_ctx
*priv
;
239 if (!is_valid_policy_hnd(handle
)) {
243 priv
= talloc_get_type_abort(ctx
->private_data
,
244 struct libnetapi_private_ctx
);
246 if (!policy_hnd_equal(handle
, &priv
->samr
.domain_handle
)) {
250 rpccli_samr_Close(priv
->samr
.cli
, ctx
, handle
);
252 ZERO_STRUCT(priv
->samr
.domain_handle
);
255 /****************************************************************
256 ****************************************************************/
258 void libnetapi_samr_close_builtin_handle(struct libnetapi_ctx
*ctx
,
259 struct policy_handle
*handle
)
261 struct libnetapi_private_ctx
*priv
;
263 if (!is_valid_policy_hnd(handle
)) {
267 priv
= talloc_get_type_abort(ctx
->private_data
,
268 struct libnetapi_private_ctx
);
270 if (!policy_hnd_equal(handle
, &priv
->samr
.builtin_handle
)) {
274 rpccli_samr_Close(priv
->samr
.cli
, ctx
, handle
);
276 ZERO_STRUCT(priv
->samr
.builtin_handle
);
279 /****************************************************************
280 ****************************************************************/
282 void libnetapi_samr_close_connect_handle(struct libnetapi_ctx
*ctx
,
283 struct policy_handle
*handle
)
285 struct libnetapi_private_ctx
*priv
;
287 if (!is_valid_policy_hnd(handle
)) {
291 priv
= talloc_get_type_abort(ctx
->private_data
,
292 struct libnetapi_private_ctx
);
294 if (!policy_hnd_equal(handle
, &priv
->samr
.connect_handle
)) {
298 rpccli_samr_Close(priv
->samr
.cli
, ctx
, handle
);
300 ZERO_STRUCT(priv
->samr
.connect_handle
);
303 /****************************************************************
304 ****************************************************************/
306 void libnetapi_samr_free(struct libnetapi_ctx
*ctx
)
308 struct libnetapi_private_ctx
*priv
;
310 if (!ctx
->private_data
) {
314 priv
= talloc_get_type_abort(ctx
->private_data
,
315 struct libnetapi_private_ctx
);
317 libnetapi_samr_close_domain_handle(ctx
, &priv
->samr
.domain_handle
);
318 libnetapi_samr_close_builtin_handle(ctx
, &priv
->samr
.builtin_handle
);
319 libnetapi_samr_close_connect_handle(ctx
, &priv
->samr
.connect_handle
);