netapi: remove unrequired headers.
[kai/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
22 /****************************************************************
23 ****************************************************************/
24
25 WERROR libnetapi_samr_open_domain(TALLOC_CTX *mem_ctx,
26                                   struct rpc_pipe_client *pipe_cli,
27                                   uint32_t connect_mask,
28                                   uint32_t domain_mask,
29                                   struct policy_handle *connect_handle,
30                                   struct policy_handle *domain_handle,
31                                   struct dom_sid2 **domain_sid)
32 {
33         NTSTATUS status;
34         WERROR werr;
35         uint32_t resume_handle = 0;
36         uint32_t num_entries = 0;
37         struct samr_SamArray *sam = NULL;
38         const char *domain_name = NULL;
39         struct lsa_String lsa_domain_name;
40         bool domain_found = true;
41         int i;
42
43         if (!is_valid_policy_hnd(connect_handle)) {
44                 status = rpccli_try_samr_connects(pipe_cli, mem_ctx,
45                                                   connect_mask,
46                                                   connect_handle);
47                 if (!NT_STATUS_IS_OK(status)) {
48                         werr = ntstatus_to_werror(status);
49                         goto done;
50                 }
51         }
52
53         status = rpccli_samr_EnumDomains(pipe_cli, mem_ctx,
54                                          connect_handle,
55                                          &resume_handle,
56                                          &sam,
57                                          0xffffffff,
58                                          &num_entries);
59         if (!NT_STATUS_IS_OK(status)) {
60                 werr = ntstatus_to_werror(status);
61                 goto done;
62         }
63
64         for (i=0; i<num_entries; i++) {
65
66                 domain_name = sam->entries[i].name.string;
67
68                 if (strequal(domain_name, builtin_domain_name())) {
69                         continue;
70                 }
71
72                 domain_found = true;
73                 break;
74         }
75
76         if (!domain_found) {
77                 werr = WERR_NO_SUCH_DOMAIN;
78                 goto done;
79         }
80
81         init_lsa_String(&lsa_domain_name, domain_name);
82
83         status = rpccli_samr_LookupDomain(pipe_cli, mem_ctx,
84                                           connect_handle,
85                                           &lsa_domain_name,
86                                           domain_sid);
87         if (!NT_STATUS_IS_OK(status)) {
88                 werr = ntstatus_to_werror(status);
89                 goto done;
90         }
91
92         status = rpccli_samr_OpenDomain(pipe_cli, mem_ctx,
93                                         connect_handle,
94                                         domain_mask,
95                                         *domain_sid,
96                                         domain_handle);
97         if (!NT_STATUS_IS_OK(status)) {
98                 werr = ntstatus_to_werror(status);
99                 goto done;
100         }
101
102         werr = WERR_OK;
103
104  done:
105         return werr;
106 }
107
108 /****************************************************************
109 ****************************************************************/
110
111 WERROR libnetapi_samr_open_builtin_domain(TALLOC_CTX *mem_ctx,
112                                           struct rpc_pipe_client *pipe_cli,
113                                           uint32_t connect_mask,
114                                           uint32_t builtin_mask,
115                                           struct policy_handle *connect_handle,
116                                           struct policy_handle *builtin_handle)
117 {
118         NTSTATUS status;
119         WERROR werr;
120
121         if (!is_valid_policy_hnd(connect_handle)) {
122                 status = rpccli_try_samr_connects(pipe_cli, mem_ctx,
123                                                   connect_mask,
124                                                   connect_handle);
125                 if (!NT_STATUS_IS_OK(status)) {
126                         werr = ntstatus_to_werror(status);
127                         goto done;
128                 }
129         }
130
131         status = rpccli_samr_OpenDomain(pipe_cli, mem_ctx,
132                                         connect_handle,
133                                         builtin_mask,
134                                         CONST_DISCARD(DOM_SID *, &global_sid_Builtin),
135                                         builtin_handle);
136         if (!NT_STATUS_IS_OK(status)) {
137                 werr = ntstatus_to_werror(status);
138                 goto done;
139         }
140
141         werr = WERR_OK;
142
143  done:
144         return werr;
145 }