1 | /*
|
---|
2 | * Unix SMB/CIFS implementation.
|
---|
3 | * NetApi Samr Support
|
---|
4 | * Copyright (C) Guenther Deschner 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 "lib/netapi/netapi.h"
|
---|
22 | #include "lib/netapi/netapi_private.h"
|
---|
23 |
|
---|
24 | /****************************************************************
|
---|
25 | ****************************************************************/
|
---|
26 |
|
---|
27 | WERROR libnetapi_samr_open_domain(struct libnetapi_ctx *mem_ctx,
|
---|
28 | struct rpc_pipe_client *pipe_cli,
|
---|
29 | uint32_t connect_mask,
|
---|
30 | uint32_t domain_mask,
|
---|
31 | struct policy_handle *connect_handle,
|
---|
32 | struct policy_handle *domain_handle,
|
---|
33 | struct dom_sid2 **domain_sid)
|
---|
34 | {
|
---|
35 | NTSTATUS status;
|
---|
36 | WERROR werr;
|
---|
37 | struct libnetapi_private_ctx *priv;
|
---|
38 | uint32_t resume_handle = 0;
|
---|
39 | uint32_t num_entries = 0;
|
---|
40 | struct samr_SamArray *sam = NULL;
|
---|
41 | const char *domain_name = NULL;
|
---|
42 | struct lsa_String lsa_domain_name;
|
---|
43 | bool domain_found = true;
|
---|
44 | int i;
|
---|
45 |
|
---|
46 | priv = talloc_get_type_abort(mem_ctx->private_data,
|
---|
47 | struct libnetapi_private_ctx);
|
---|
48 |
|
---|
49 | if (is_valid_policy_hnd(&priv->samr.connect_handle)) {
|
---|
50 | if ((priv->samr.connect_mask & connect_mask) == connect_mask) {
|
---|
51 | *connect_handle = priv->samr.connect_handle;
|
---|
52 | } else {
|
---|
53 | libnetapi_samr_close_connect_handle(mem_ctx,
|
---|
54 | &priv->samr.connect_handle);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | if (is_valid_policy_hnd(&priv->samr.domain_handle)) {
|
---|
59 | if ((priv->samr.domain_mask & domain_mask) == domain_mask) {
|
---|
60 | *domain_handle = priv->samr.domain_handle;
|
---|
61 | } else {
|
---|
62 | libnetapi_samr_close_domain_handle(mem_ctx,
|
---|
63 | &priv->samr.domain_handle);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (priv->samr.domain_sid) {
|
---|
68 | *domain_sid = priv->samr.domain_sid;
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (is_valid_policy_hnd(&priv->samr.connect_handle) &&
|
---|
72 | ((priv->samr.connect_mask & connect_mask) == connect_mask) &&
|
---|
73 | is_valid_policy_hnd(&priv->samr.domain_handle) &&
|
---|
74 | (priv->samr.domain_mask & domain_mask) == domain_mask) {
|
---|
75 | return WERR_OK;
|
---|
76 | }
|
---|
77 |
|
---|
78 | if (!is_valid_policy_hnd(connect_handle)) {
|
---|
79 | status = rpccli_try_samr_connects(pipe_cli, mem_ctx,
|
---|
80 | connect_mask,
|
---|
81 | connect_handle);
|
---|
82 | if (!NT_STATUS_IS_OK(status)) {
|
---|
83 | werr = ntstatus_to_werror(status);
|
---|
84 | goto done;
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | status = rpccli_samr_EnumDomains(pipe_cli, mem_ctx,
|
---|
89 | connect_handle,
|
---|
90 | &resume_handle,
|
---|
91 | &sam,
|
---|
92 | 0xffffffff,
|
---|
93 | &num_entries);
|
---|
94 | if (!NT_STATUS_IS_OK(status)) {
|
---|
95 | werr = ntstatus_to_werror(status);
|
---|
96 | goto done;
|
---|
97 | }
|
---|
98 |
|
---|
99 | for (i=0; i<num_entries; i++) {
|
---|
100 |
|
---|
101 | domain_name = sam->entries[i].name.string;
|
---|
102 |
|
---|
103 | if (strequal(domain_name, builtin_domain_name())) {
|
---|
104 | continue;
|
---|
105 | }
|
---|
106 |
|
---|
107 | domain_found = true;
|
---|
108 | break;
|
---|
109 | }
|
---|
110 |
|
---|
111 | if (!domain_found) {
|
---|
112 | werr = WERR_NO_SUCH_DOMAIN;
|
---|
113 | goto done;
|
---|
114 | }
|
---|
115 |
|
---|
116 | init_lsa_String(&lsa_domain_name, domain_name);
|
---|
117 |
|
---|
118 | status = rpccli_samr_LookupDomain(pipe_cli, mem_ctx,
|
---|
119 | connect_handle,
|
---|
120 | &lsa_domain_name,
|
---|
121 | domain_sid);
|
---|
122 | if (!NT_STATUS_IS_OK(status)) {
|
---|
123 | werr = ntstatus_to_werror(status);
|
---|
124 | goto done;
|
---|
125 | }
|
---|
126 |
|
---|
127 | status = rpccli_samr_OpenDomain(pipe_cli, mem_ctx,
|
---|
128 | connect_handle,
|
---|
129 | domain_mask,
|
---|
130 | *domain_sid,
|
---|
131 | domain_handle);
|
---|
132 | if (!NT_STATUS_IS_OK(status)) {
|
---|
133 | werr = ntstatus_to_werror(status);
|
---|
134 | goto done;
|
---|
135 | }
|
---|
136 |
|
---|
137 | priv->samr.cli = pipe_cli;
|
---|
138 |
|
---|
139 | priv->samr.domain_name = domain_name;
|
---|
140 | priv->samr.domain_sid = *domain_sid;
|
---|
141 |
|
---|
142 | priv->samr.connect_mask = connect_mask;
|
---|
143 | priv->samr.connect_handle = *connect_handle;
|
---|
144 |
|
---|
145 | priv->samr.domain_mask = domain_mask;
|
---|
146 | priv->samr.domain_handle = *domain_handle;
|
---|
147 |
|
---|
148 | werr = WERR_OK;
|
---|
149 |
|
---|
150 | done:
|
---|
151 | return werr;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /****************************************************************
|
---|
155 | ****************************************************************/
|
---|
156 |
|
---|
157 | WERROR libnetapi_samr_open_builtin_domain(struct libnetapi_ctx *mem_ctx,
|
---|
158 | struct rpc_pipe_client *pipe_cli,
|
---|
159 | uint32_t connect_mask,
|
---|
160 | uint32_t builtin_mask,
|
---|
161 | struct policy_handle *connect_handle,
|
---|
162 | struct policy_handle *builtin_handle)
|
---|
163 | {
|
---|
164 | NTSTATUS status;
|
---|
165 | WERROR werr;
|
---|
166 | struct libnetapi_private_ctx *priv;
|
---|
167 |
|
---|
168 | priv = talloc_get_type_abort(mem_ctx->private_data,
|
---|
169 | struct libnetapi_private_ctx);
|
---|
170 |
|
---|
171 | if (is_valid_policy_hnd(&priv->samr.connect_handle)) {
|
---|
172 | if ((priv->samr.connect_mask & connect_mask) == connect_mask) {
|
---|
173 | *connect_handle = priv->samr.connect_handle;
|
---|
174 | } else {
|
---|
175 | libnetapi_samr_close_connect_handle(mem_ctx,
|
---|
176 | &priv->samr.connect_handle);
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | if (is_valid_policy_hnd(&priv->samr.builtin_handle)) {
|
---|
181 | if ((priv->samr.builtin_mask & builtin_mask) == builtin_mask) {
|
---|
182 | *builtin_handle = priv->samr.builtin_handle;
|
---|
183 | } else {
|
---|
184 | libnetapi_samr_close_builtin_handle(mem_ctx,
|
---|
185 | &priv->samr.builtin_handle);
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | if (is_valid_policy_hnd(&priv->samr.connect_handle) &&
|
---|
190 | ((priv->samr.connect_mask & connect_mask) == connect_mask) &&
|
---|
191 | is_valid_policy_hnd(&priv->samr.builtin_handle) &&
|
---|
192 | (priv->samr.builtin_mask & builtin_mask) == builtin_mask) {
|
---|
193 | return WERR_OK;
|
---|
194 | }
|
---|
195 |
|
---|
196 | if (!is_valid_policy_hnd(connect_handle)) {
|
---|
197 | status = rpccli_try_samr_connects(pipe_cli, mem_ctx,
|
---|
198 | connect_mask,
|
---|
199 | connect_handle);
|
---|
200 | if (!NT_STATUS_IS_OK(status)) {
|
---|
201 | werr = ntstatus_to_werror(status);
|
---|
202 | goto done;
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | status = rpccli_samr_OpenDomain(pipe_cli, mem_ctx,
|
---|
207 | connect_handle,
|
---|
208 | builtin_mask,
|
---|
209 | CONST_DISCARD(DOM_SID *, &global_sid_Builtin),
|
---|
210 | builtin_handle);
|
---|
211 | if (!NT_STATUS_IS_OK(status)) {
|
---|
212 | werr = ntstatus_to_werror(status);
|
---|
213 | goto done;
|
---|
214 | }
|
---|
215 |
|
---|
216 | priv->samr.cli = pipe_cli;
|
---|
217 |
|
---|
218 | priv->samr.connect_mask = connect_mask;
|
---|
219 | priv->samr.connect_handle = *connect_handle;
|
---|
220 |
|
---|
221 | priv->samr.builtin_mask = builtin_mask;
|
---|
222 | priv->samr.builtin_handle = *builtin_handle;
|
---|
223 |
|
---|
224 | werr = WERR_OK;
|
---|
225 |
|
---|
226 | done:
|
---|
227 | return werr;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /****************************************************************
|
---|
231 | ****************************************************************/
|
---|
232 |
|
---|
233 | void libnetapi_samr_close_domain_handle(struct libnetapi_ctx *ctx,
|
---|
234 | struct policy_handle *handle)
|
---|
235 | {
|
---|
236 | struct libnetapi_private_ctx *priv;
|
---|
237 |
|
---|
238 | if (!is_valid_policy_hnd(handle)) {
|
---|
239 | return;
|
---|
240 | }
|
---|
241 |
|
---|
242 | priv = talloc_get_type_abort(ctx->private_data,
|
---|
243 | struct libnetapi_private_ctx);
|
---|
244 |
|
---|
245 | if (!policy_hnd_equal(handle, &priv->samr.domain_handle)) {
|
---|
246 | return;
|
---|
247 | }
|
---|
248 |
|
---|
249 | rpccli_samr_Close(priv->samr.cli, ctx, handle);
|
---|
250 |
|
---|
251 | ZERO_STRUCT(priv->samr.domain_handle);
|
---|
252 | }
|
---|
253 |
|
---|
254 | /****************************************************************
|
---|
255 | ****************************************************************/
|
---|
256 |
|
---|
257 | void libnetapi_samr_close_builtin_handle(struct libnetapi_ctx *ctx,
|
---|
258 | struct policy_handle *handle)
|
---|
259 | {
|
---|
260 | struct libnetapi_private_ctx *priv;
|
---|
261 |
|
---|
262 | if (!is_valid_policy_hnd(handle)) {
|
---|
263 | return;
|
---|
264 | }
|
---|
265 |
|
---|
266 | priv = talloc_get_type_abort(ctx->private_data,
|
---|
267 | struct libnetapi_private_ctx);
|
---|
268 |
|
---|
269 | if (!policy_hnd_equal(handle, &priv->samr.builtin_handle)) {
|
---|
270 | return;
|
---|
271 | }
|
---|
272 |
|
---|
273 | rpccli_samr_Close(priv->samr.cli, ctx, handle);
|
---|
274 |
|
---|
275 | ZERO_STRUCT(priv->samr.builtin_handle);
|
---|
276 | }
|
---|
277 |
|
---|
278 | /****************************************************************
|
---|
279 | ****************************************************************/
|
---|
280 |
|
---|
281 | void libnetapi_samr_close_connect_handle(struct libnetapi_ctx *ctx,
|
---|
282 | struct policy_handle *handle)
|
---|
283 | {
|
---|
284 | struct libnetapi_private_ctx *priv;
|
---|
285 |
|
---|
286 | if (!is_valid_policy_hnd(handle)) {
|
---|
287 | return;
|
---|
288 | }
|
---|
289 |
|
---|
290 | priv = talloc_get_type_abort(ctx->private_data,
|
---|
291 | struct libnetapi_private_ctx);
|
---|
292 |
|
---|
293 | if (!policy_hnd_equal(handle, &priv->samr.connect_handle)) {
|
---|
294 | return;
|
---|
295 | }
|
---|
296 |
|
---|
297 | rpccli_samr_Close(priv->samr.cli, ctx, handle);
|
---|
298 |
|
---|
299 | ZERO_STRUCT(priv->samr.connect_handle);
|
---|
300 | }
|
---|
301 |
|
---|
302 | /****************************************************************
|
---|
303 | ****************************************************************/
|
---|
304 |
|
---|
305 | void libnetapi_samr_free(struct libnetapi_ctx *ctx)
|
---|
306 | {
|
---|
307 | struct libnetapi_private_ctx *priv;
|
---|
308 |
|
---|
309 | if (!ctx->private_data) {
|
---|
310 | return;
|
---|
311 | }
|
---|
312 |
|
---|
313 | priv = talloc_get_type_abort(ctx->private_data,
|
---|
314 | struct libnetapi_private_ctx);
|
---|
315 |
|
---|
316 | libnetapi_samr_close_domain_handle(ctx, &priv->samr.domain_handle);
|
---|
317 | libnetapi_samr_close_builtin_handle(ctx, &priv->samr.builtin_handle);
|
---|
318 | libnetapi_samr_close_connect_handle(ctx, &priv->samr.connect_handle);
|
---|
319 | }
|
---|