pyldb: Raise proper exception when attempting to assign a string to a dn
[Samba/fernandojvsilva.git] / source3 / rpc_client / cli_lsarpc.c
blob68fd96faa872e5b518345d55fe0d2c86c1740dfe
1 /*
2 Unix SMB/CIFS implementation.
3 RPC pipe client
4 Copyright (C) Tim Potter 2000-2001,
5 Copyright (C) Andrew Tridgell 1992-1997,2000,
6 Copyright (C) Rafal Szczesniak 2002
7 Copyright (C) Jeremy Allison 2005.
8 Copyright (C) Michael Adam 2007.
9 Copyright (C) Guenther Deschner 2008.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
27 /** @defgroup lsa LSA - Local Security Architecture
28 * @ingroup rpc_client
30 * @{
31 **/
33 /**
34 * @file cli_lsarpc.c
36 * RPC client routines for the LSA RPC pipe. LSA means "local
37 * security authority", which is half of a password database.
38 **/
40 /** Open a LSA policy handle
42 * @param cli Handle on an initialised SMB connection */
44 NTSTATUS rpccli_lsa_open_policy(struct rpc_pipe_client *cli,
45 TALLOC_CTX *mem_ctx,
46 bool sec_qos, uint32 des_access,
47 struct policy_handle *pol)
49 struct lsa_ObjectAttribute attr;
50 struct lsa_QosInfo qos;
51 uint16_t system_name = '\\';
53 ZERO_STRUCT(attr);
55 attr.len = 0x18;
57 if (sec_qos) {
58 qos.len = 0xc;
59 qos.impersonation_level = 2;
60 qos.context_mode = 1;
61 qos.effective_only = 0;
63 attr.sec_qos = &qos;
66 return rpccli_lsa_OpenPolicy(cli, mem_ctx,
67 &system_name,
68 &attr,
69 des_access,
70 pol);
73 /** Open a LSA policy handle
75 * @param cli Handle on an initialised SMB connection
78 NTSTATUS rpccli_lsa_open_policy2(struct rpc_pipe_client *cli,
79 TALLOC_CTX *mem_ctx, bool sec_qos,
80 uint32 des_access, struct policy_handle *pol)
82 struct lsa_ObjectAttribute attr;
83 struct lsa_QosInfo qos;
85 ZERO_STRUCT(attr);
87 attr.len = 0x18;
89 if (sec_qos) {
90 qos.len = 0xc;
91 qos.impersonation_level = 2;
92 qos.context_mode = 1;
93 qos.effective_only = 0;
95 attr.sec_qos = &qos;
98 return rpccli_lsa_OpenPolicy2(cli, mem_ctx,
99 cli->srv_name_slash,
100 &attr,
101 des_access,
102 pol);
105 /* Lookup a list of sids
107 * internal version withOUT memory allocation of the target arrays.
108 * this assumes suffciently sized arrays to store domains, names and types. */
110 static NTSTATUS rpccli_lsa_lookup_sids_noalloc(struct rpc_pipe_client *cli,
111 TALLOC_CTX *mem_ctx,
112 struct policy_handle *pol,
113 int num_sids,
114 const DOM_SID *sids,
115 char **domains,
116 char **names,
117 enum lsa_SidType *types)
119 NTSTATUS result = NT_STATUS_OK;
120 TALLOC_CTX *tmp_ctx = NULL;
121 int i;
122 struct lsa_SidArray sid_array;
123 struct lsa_RefDomainList *ref_domains = NULL;
124 struct lsa_TransNameArray lsa_names;
125 uint32_t count = 0;
126 uint16_t level = 1;
128 ZERO_STRUCT(lsa_names);
130 tmp_ctx = talloc_new(mem_ctx);
131 if (!tmp_ctx) {
132 DEBUG(0, ("rpccli_lsa_lookup_sids_noalloc: out of memory!\n"));
133 result = NT_STATUS_UNSUCCESSFUL;
134 goto done;
137 sid_array.num_sids = num_sids;
138 sid_array.sids = TALLOC_ARRAY(mem_ctx, struct lsa_SidPtr, num_sids);
139 if (!sid_array.sids) {
140 return NT_STATUS_NO_MEMORY;
143 for (i = 0; i<num_sids; i++) {
144 sid_array.sids[i].sid = sid_dup_talloc(mem_ctx, &sids[i]);
145 if (!sid_array.sids[i].sid) {
146 return NT_STATUS_NO_MEMORY;
150 result = rpccli_lsa_LookupSids(cli, mem_ctx,
151 pol,
152 &sid_array,
153 &ref_domains,
154 &lsa_names,
155 level,
156 &count);
158 DEBUG(10, ("LSA_LOOKUPSIDS returned '%s', mapped count = %d'\n",
159 nt_errstr(result), count));
161 if (!NT_STATUS_IS_OK(result) &&
162 !NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
163 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
165 /* An actual error occured */
166 goto done;
169 /* Return output parameters */
171 if (NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) ||
172 (count == 0))
174 for (i = 0; i < num_sids; i++) {
175 (names)[i] = NULL;
176 (domains)[i] = NULL;
177 (types)[i] = SID_NAME_UNKNOWN;
179 result = NT_STATUS_NONE_MAPPED;
180 goto done;
183 for (i = 0; i < num_sids; i++) {
184 const char *name, *dom_name;
185 uint32_t dom_idx = lsa_names.names[i].sid_index;
187 /* Translate optimised name through domain index array */
189 if (dom_idx != 0xffffffff) {
191 dom_name = ref_domains->domains[dom_idx].name.string;
192 name = lsa_names.names[i].name.string;
194 if (name) {
195 (names)[i] = talloc_strdup(mem_ctx, name);
196 if ((names)[i] == NULL) {
197 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
198 result = NT_STATUS_UNSUCCESSFUL;
199 goto done;
201 } else {
202 (names)[i] = NULL;
204 (domains)[i] = talloc_strdup(mem_ctx, dom_name);
205 (types)[i] = lsa_names.names[i].sid_type;
206 if (((domains)[i] == NULL)) {
207 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
208 result = NT_STATUS_UNSUCCESSFUL;
209 goto done;
212 } else {
213 (names)[i] = NULL;
214 (domains)[i] = NULL;
215 (types)[i] = SID_NAME_UNKNOWN;
219 done:
220 TALLOC_FREE(tmp_ctx);
221 return result;
224 /* Lookup a list of sids
226 * do it the right way: there is a limit (of 20480 for w2k3) entries
227 * returned by this call. when the sids list contains more entries,
228 * empty lists are returned. This version of lsa_lookup_sids passes
229 * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
231 /* This constant defines the limit of how many sids to look up
232 * in one call (maximum). the limit from the server side is
233 * at 20480 for win2k3, but we keep it at a save 1000 for now. */
234 #define LOOKUP_SIDS_HUNK_SIZE 1000
236 NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
237 TALLOC_CTX *mem_ctx,
238 struct policy_handle *pol,
239 int num_sids,
240 const DOM_SID *sids,
241 char ***pdomains,
242 char ***pnames,
243 enum lsa_SidType **ptypes)
245 NTSTATUS result = NT_STATUS_OK;
246 int sids_left = 0;
247 int sids_processed = 0;
248 const DOM_SID *hunk_sids = sids;
249 char **hunk_domains;
250 char **hunk_names;
251 enum lsa_SidType *hunk_types;
252 char **domains = NULL;
253 char **names = NULL;
254 enum lsa_SidType *types = NULL;
256 if (num_sids) {
257 if (!(domains = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
258 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
259 result = NT_STATUS_NO_MEMORY;
260 goto fail;
263 if (!(names = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
264 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
265 result = NT_STATUS_NO_MEMORY;
266 goto fail;
269 if (!(types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_sids))) {
270 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
271 result = NT_STATUS_NO_MEMORY;
272 goto fail;
276 sids_left = num_sids;
277 hunk_domains = domains;
278 hunk_names = names;
279 hunk_types = types;
281 while (sids_left > 0) {
282 int hunk_num_sids;
283 NTSTATUS hunk_result = NT_STATUS_OK;
285 hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
286 ? LOOKUP_SIDS_HUNK_SIZE
287 : sids_left);
289 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
290 "%d -- %d of %d.\n",
291 sids_processed,
292 sids_processed + hunk_num_sids - 1,
293 num_sids));
295 hunk_result = rpccli_lsa_lookup_sids_noalloc(cli,
296 mem_ctx,
297 pol,
298 hunk_num_sids,
299 hunk_sids,
300 hunk_domains,
301 hunk_names,
302 hunk_types);
304 if (!NT_STATUS_IS_OK(hunk_result) &&
305 !NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED) &&
306 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED))
308 /* An actual error occured */
309 result = hunk_result;
310 goto fail;
313 /* adapt overall result */
314 if (( NT_STATUS_IS_OK(result) &&
315 !NT_STATUS_IS_OK(hunk_result))
317 ( NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
318 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED)))
320 result = STATUS_SOME_UNMAPPED;
323 sids_left -= hunk_num_sids;
324 sids_processed += hunk_num_sids; /* only used in DEBUG */
325 hunk_sids += hunk_num_sids;
326 hunk_domains += hunk_num_sids;
327 hunk_names += hunk_num_sids;
328 hunk_types += hunk_num_sids;
331 *pdomains = domains;
332 *pnames = names;
333 *ptypes = types;
334 return result;
336 fail:
337 TALLOC_FREE(domains);
338 TALLOC_FREE(names);
339 TALLOC_FREE(types);
340 return result;
343 /** Lookup a list of names */
345 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
346 TALLOC_CTX *mem_ctx,
347 struct policy_handle *pol, int num_names,
348 const char **names,
349 const char ***dom_names,
350 int level,
351 DOM_SID **sids,
352 enum lsa_SidType **types)
354 NTSTATUS result;
355 int i;
356 struct lsa_String *lsa_names = NULL;
357 struct lsa_RefDomainList *domains = NULL;
358 struct lsa_TransSidArray sid_array;
359 uint32_t count = 0;
361 ZERO_STRUCT(sid_array);
363 lsa_names = TALLOC_ARRAY(mem_ctx, struct lsa_String, num_names);
364 if (!lsa_names) {
365 return NT_STATUS_NO_MEMORY;
368 for (i=0; i<num_names; i++) {
369 init_lsa_String(&lsa_names[i], names[i]);
372 result = rpccli_lsa_LookupNames(cli, mem_ctx,
373 pol,
374 num_names,
375 lsa_names,
376 &domains,
377 &sid_array,
378 level,
379 &count);
381 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
382 NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
384 /* An actual error occured */
386 goto done;
389 /* Return output parameters */
391 if (count == 0) {
392 result = NT_STATUS_NONE_MAPPED;
393 goto done;
396 if (num_names) {
397 if (!((*sids = TALLOC_ARRAY(mem_ctx, DOM_SID, num_names)))) {
398 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
399 result = NT_STATUS_NO_MEMORY;
400 goto done;
403 if (!((*types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_names)))) {
404 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
405 result = NT_STATUS_NO_MEMORY;
406 goto done;
409 if (dom_names != NULL) {
410 *dom_names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
411 if (*dom_names == NULL) {
412 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
413 result = NT_STATUS_NO_MEMORY;
414 goto done;
417 } else {
418 *sids = NULL;
419 *types = NULL;
420 if (dom_names != NULL) {
421 *dom_names = NULL;
425 for (i = 0; i < num_names; i++) {
426 uint32_t dom_idx = sid_array.sids[i].sid_index;
427 uint32_t dom_rid = sid_array.sids[i].rid;
428 DOM_SID *sid = &(*sids)[i];
430 /* Translate optimised sid through domain index array */
432 if (dom_idx == 0xffffffff) {
433 /* Nothing to do, this is unknown */
434 ZERO_STRUCTP(sid);
435 (*types)[i] = SID_NAME_UNKNOWN;
436 continue;
439 sid_copy(sid, domains->domains[dom_idx].sid);
441 if (dom_rid != 0xffffffff) {
442 sid_append_rid(sid, dom_rid);
445 (*types)[i] = sid_array.sids[i].sid_type;
447 if (dom_names == NULL) {
448 continue;
451 (*dom_names)[i] = domains->domains[dom_idx].name.string;
454 done:
456 return result;