647ae653268e213d44d45d6658e8da6d6ce9063e
[samba.git] / source3 / lib / netapi / samr.c
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 #include "../librpc/gen_ndr/ndr_samr_c.h"
24 #include "rpc_client/cli_samr.h"
25 #include "rpc_client/init_lsa.h"
26 #include "../libcli/security/security.h"
27
28 /****************************************************************
29 ****************************************************************/
30
31 WERROR libnetapi_samr_open_domain(struct libnetapi_ctx *mem_ctx,
32                                   struct rpc_pipe_client *pipe_cli,
33                                   uint32_t connect_mask,
34                                   uint32_t domain_mask,
35                                   struct policy_handle *connect_handle,
36                                   struct policy_handle *domain_handle,
37                                   struct dom_sid2 **domain_sid)
38 {
39         NTSTATUS status, result;
40         WERROR werr;
41         struct libnetapi_private_ctx *priv;
42         uint32_t resume_handle = 0;
43         uint32_t num_entries = 0;
44         struct samr_SamArray *sam = NULL;
45         const char *domain_name = NULL;
46         struct lsa_String lsa_domain_name;
47         bool domain_found = true;
48         int i;
49         struct dcerpc_binding_handle *b = pipe_cli->binding_handle;
50
51         priv = talloc_get_type_abort(mem_ctx->private_data,
52                 struct libnetapi_private_ctx);
53
54         if (is_valid_policy_hnd(&priv->samr.connect_handle)) {
55                 if ((priv->samr.connect_mask & connect_mask) == connect_mask) {
56                         *connect_handle = priv->samr.connect_handle;
57                 } else {
58                         libnetapi_samr_close_connect_handle(mem_ctx,
59                                 &priv->samr.connect_handle);
60                 }
61         }
62
63         if (is_valid_policy_hnd(&priv->samr.domain_handle)) {
64                 if ((priv->samr.domain_mask & domain_mask) == domain_mask) {
65                         *domain_handle = priv->samr.domain_handle;
66                 } else {
67                         libnetapi_samr_close_domain_handle(mem_ctx,
68                                 &priv->samr.domain_handle);
69                 }
70         }
71
72         if (priv->samr.domain_sid) {
73                 *domain_sid = priv->samr.domain_sid;
74         }
75
76         if (is_valid_policy_hnd(&priv->samr.connect_handle) &&
77             ((priv->samr.connect_mask & connect_mask) == connect_mask) &&
78             is_valid_policy_hnd(&priv->samr.domain_handle) &&
79             (priv->samr.domain_mask & domain_mask) == domain_mask) {
80                 return WERR_OK;
81         }
82
83         if (!is_valid_policy_hnd(connect_handle)) {
84                 status = dcerpc_try_samr_connects(pipe_cli->binding_handle, mem_ctx,
85                                                   pipe_cli->srv_name_slash,
86                                                   connect_mask,
87                                                   connect_handle,
88                                                   &result);
89                 if (!NT_STATUS_IS_OK(status)) {
90                         werr = ntstatus_to_werror(status);
91                         goto done;
92                 }
93                 if (!NT_STATUS_IS_OK(result)) {
94                         werr = ntstatus_to_werror(result);
95                         goto done;
96                 }
97         }
98
99         status = dcerpc_samr_EnumDomains(b, mem_ctx,
100                                          connect_handle,
101                                          &resume_handle,
102                                          &sam,
103                                          0xffffffff,
104                                          &num_entries,
105                                          &result);
106         if (!NT_STATUS_IS_OK(status)) {
107                 werr = ntstatus_to_werror(status);
108                 goto done;
109         }
110         if (!NT_STATUS_IS_OK(result)) {
111                 werr = ntstatus_to_werror(result);
112                 goto done;
113         }
114
115         for (i=0; i<num_entries; i++) {
116
117                 domain_name = sam->entries[i].name.string;
118
119                 if (strequal(domain_name, builtin_domain_name())) {
120                         continue;
121                 }
122
123                 domain_found = true;
124                 break;
125         }
126
127         if (!domain_found) {
128                 werr = WERR_NO_SUCH_DOMAIN;
129                 goto done;
130         }
131
132         init_lsa_String(&lsa_domain_name, domain_name);
133
134         status = dcerpc_samr_LookupDomain(b, mem_ctx,
135                                           connect_handle,
136                                           &lsa_domain_name,
137                                           domain_sid,
138                                           &result);
139         if (!NT_STATUS_IS_OK(status)) {
140                 werr = ntstatus_to_werror(status);
141                 goto done;
142         }
143         if (!NT_STATUS_IS_OK(result)) {
144                 werr = ntstatus_to_werror(result);
145                 goto done;
146         }
147
148         status = dcerpc_samr_OpenDomain(b, mem_ctx,
149                                         connect_handle,
150                                         domain_mask,
151                                         *domain_sid,
152                                         domain_handle,
153                                         &result);
154         if (!NT_STATUS_IS_OK(status)) {
155                 werr = ntstatus_to_werror(status);
156                 goto done;
157         }
158         if (!NT_STATUS_IS_OK(result)) {
159                 werr = ntstatus_to_werror(result);
160                 goto done;
161         }
162
163         priv->samr.cli                  = pipe_cli;
164
165         priv->samr.domain_name          = domain_name;
166         priv->samr.domain_sid           = *domain_sid;
167
168         priv->samr.connect_mask         = connect_mask;
169         priv->samr.connect_handle       = *connect_handle;
170
171         priv->samr.domain_mask          = domain_mask;
172         priv->samr.domain_handle        = *domain_handle;
173
174         werr = WERR_OK;
175
176  done:
177         return werr;
178 }
179
180 /****************************************************************
181 ****************************************************************/
182
183 WERROR libnetapi_samr_open_builtin_domain(struct libnetapi_ctx *mem_ctx,
184                                           struct rpc_pipe_client *pipe_cli,
185                                           uint32_t connect_mask,
186                                           uint32_t builtin_mask,
187                                           struct policy_handle *connect_handle,
188                                           struct policy_handle *builtin_handle)
189 {
190         NTSTATUS status, result;
191         WERROR werr;
192         struct libnetapi_private_ctx *priv;
193         struct dcerpc_binding_handle *b = pipe_cli->binding_handle;
194
195         priv = talloc_get_type_abort(mem_ctx->private_data,
196                 struct libnetapi_private_ctx);
197
198         if (is_valid_policy_hnd(&priv->samr.connect_handle)) {
199                 if ((priv->samr.connect_mask & connect_mask) == connect_mask) {
200                         *connect_handle = priv->samr.connect_handle;
201                 } else {
202                         libnetapi_samr_close_connect_handle(mem_ctx,
203                                 &priv->samr.connect_handle);
204                 }
205         }
206
207         if (is_valid_policy_hnd(&priv->samr.builtin_handle)) {
208                 if ((priv->samr.builtin_mask & builtin_mask) == builtin_mask) {
209                         *builtin_handle = priv->samr.builtin_handle;
210                 } else {
211                         libnetapi_samr_close_builtin_handle(mem_ctx,
212                                 &priv->samr.builtin_handle);
213                 }
214         }
215
216         if (is_valid_policy_hnd(&priv->samr.connect_handle) &&
217             ((priv->samr.connect_mask & connect_mask) == connect_mask) &&
218             is_valid_policy_hnd(&priv->samr.builtin_handle) &&
219             (priv->samr.builtin_mask & builtin_mask) == builtin_mask) {
220                 return WERR_OK;
221         }
222
223         if (!is_valid_policy_hnd(connect_handle)) {
224                 status = dcerpc_try_samr_connects(pipe_cli->binding_handle, mem_ctx,
225                                                   pipe_cli->srv_name_slash,
226                                                   connect_mask,
227                                                   connect_handle,
228                                                   &result);
229                 if (!NT_STATUS_IS_OK(status)) {
230                         werr = ntstatus_to_werror(status);
231                         goto done;
232                 }
233                 if (!NT_STATUS_IS_OK(result)) {
234                         werr = ntstatus_to_werror(result);
235                         goto done;
236                 }
237         }
238
239         status = dcerpc_samr_OpenDomain(b, mem_ctx,
240                                         connect_handle,
241                                         builtin_mask,
242                                         CONST_DISCARD(struct dom_sid *, &global_sid_Builtin),
243                                         builtin_handle,
244                                         &result);
245         if (!NT_STATUS_IS_OK(status)) {
246                 werr = ntstatus_to_werror(status);
247                 goto done;
248         }
249         if (!NT_STATUS_IS_OK(result)) {
250                 werr = ntstatus_to_werror(result);
251                 goto done;
252         }
253
254         priv->samr.cli                  = pipe_cli;
255
256         priv->samr.connect_mask         = connect_mask;
257         priv->samr.connect_handle       = *connect_handle;
258
259         priv->samr.builtin_mask         = builtin_mask;
260         priv->samr.builtin_handle       = *builtin_handle;
261
262         werr = WERR_OK;
263
264  done:
265         return werr;
266 }
267
268 /****************************************************************
269 ****************************************************************/
270
271 void libnetapi_samr_close_domain_handle(struct libnetapi_ctx *ctx,
272                                         struct policy_handle *handle)
273 {
274         struct libnetapi_private_ctx *priv;
275         struct dcerpc_binding_handle *b;
276         NTSTATUS result;
277
278         if (!is_valid_policy_hnd(handle)) {
279                 return;
280         }
281
282         priv = talloc_get_type_abort(ctx->private_data,
283                 struct libnetapi_private_ctx);
284
285         if (!policy_handle_equal(handle, &priv->samr.domain_handle)) {
286                 return;
287         }
288
289         b = priv->samr.cli->binding_handle;
290
291         dcerpc_samr_Close(b, ctx, handle, &result);
292
293         ZERO_STRUCT(priv->samr.domain_handle);
294 }
295
296 /****************************************************************
297 ****************************************************************/
298
299 void libnetapi_samr_close_builtin_handle(struct libnetapi_ctx *ctx,
300                                          struct policy_handle *handle)
301 {
302         struct libnetapi_private_ctx *priv;
303         struct dcerpc_binding_handle *b;
304         NTSTATUS result;
305
306         if (!is_valid_policy_hnd(handle)) {
307                 return;
308         }
309
310         priv = talloc_get_type_abort(ctx->private_data,
311                 struct libnetapi_private_ctx);
312
313         if (!policy_handle_equal(handle, &priv->samr.builtin_handle)) {
314                 return;
315         }
316
317         b = priv->samr.cli->binding_handle;
318
319         dcerpc_samr_Close(b, ctx, handle, &result);
320
321         ZERO_STRUCT(priv->samr.builtin_handle);
322 }
323
324 /****************************************************************
325 ****************************************************************/
326
327 void libnetapi_samr_close_connect_handle(struct libnetapi_ctx *ctx,
328                                          struct policy_handle *handle)
329 {
330         struct libnetapi_private_ctx *priv;
331         struct dcerpc_binding_handle *b;
332         NTSTATUS result;
333
334         if (!is_valid_policy_hnd(handle)) {
335                 return;
336         }
337
338         priv = talloc_get_type_abort(ctx->private_data,
339                 struct libnetapi_private_ctx);
340
341         if (!policy_handle_equal(handle, &priv->samr.connect_handle)) {
342                 return;
343         }
344
345         b = priv->samr.cli->binding_handle;
346
347         dcerpc_samr_Close(b, ctx, handle, &result);
348
349         ZERO_STRUCT(priv->samr.connect_handle);
350 }
351
352 /****************************************************************
353 ****************************************************************/
354
355 void libnetapi_samr_free(struct libnetapi_ctx *ctx)
356 {
357         struct libnetapi_private_ctx *priv;
358
359         if (!ctx->private_data) {
360                 return;
361         }
362
363         priv = talloc_get_type_abort(ctx->private_data,
364                 struct libnetapi_private_ctx);
365
366         libnetapi_samr_close_domain_handle(ctx, &priv->samr.domain_handle);
367         libnetapi_samr_close_builtin_handle(ctx, &priv->samr.builtin_handle);
368         libnetapi_samr_close_connect_handle(ctx, &priv->samr.connect_handle);
369 }