2 Unix SMB/CIFS implementation.
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/>.
27 /** @defgroup lsa LSA - Local Security Architecture
36 * RPC client routines for the LSA RPC pipe. LSA means "local
37 * security authority", which is half of a password database.
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
,
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
= '\\';
59 qos
.impersonation_level
= 2;
61 qos
.effective_only
= 0;
66 return rpccli_lsa_OpenPolicy(cli
, mem_ctx
,
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
;
91 qos
.impersonation_level
= 2;
93 qos
.effective_only
= 0;
98 return rpccli_lsa_OpenPolicy2(cli
, mem_ctx
,
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
,
112 struct policy_handle
*pol
,
117 enum lsa_SidType
*types
)
119 NTSTATUS result
= NT_STATUS_OK
;
120 TALLOC_CTX
*tmp_ctx
= NULL
;
122 struct lsa_SidArray sid_array
;
123 struct lsa_RefDomainList
*ref_domains
= NULL
;
124 struct lsa_TransNameArray lsa_names
;
128 ZERO_STRUCT(lsa_names
);
130 tmp_ctx
= talloc_new(mem_ctx
);
132 DEBUG(0, ("rpccli_lsa_lookup_sids_noalloc: out of memory!\n"));
133 result
= NT_STATUS_UNSUCCESSFUL
;
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
,
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 */
169 /* Return output parameters */
171 if (NT_STATUS_EQUAL(result
, NT_STATUS_NONE_MAPPED
) ||
174 for (i
= 0; i
< num_sids
; i
++) {
177 (types
)[i
] = SID_NAME_UNKNOWN
;
179 result
= NT_STATUS_NONE_MAPPED
;
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
;
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
;
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
;
215 (types
)[i
] = SID_NAME_UNKNOWN
;
220 TALLOC_FREE(tmp_ctx
);
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
,
238 struct policy_handle
*pol
,
243 enum lsa_SidType
**ptypes
)
245 NTSTATUS result
= NT_STATUS_OK
;
247 int sids_processed
= 0;
248 const DOM_SID
*hunk_sids
= sids
;
251 enum lsa_SidType
*hunk_types
;
252 char **domains
= NULL
;
254 enum lsa_SidType
*types
= NULL
;
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
;
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
;
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
;
276 sids_left
= num_sids
;
277 hunk_domains
= domains
;
281 while (sids_left
> 0) {
283 NTSTATUS hunk_result
= NT_STATUS_OK
;
285 hunk_num_sids
= ((sids_left
> LOOKUP_SIDS_HUNK_SIZE
)
286 ? LOOKUP_SIDS_HUNK_SIZE
289 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
292 sids_processed
+ hunk_num_sids
- 1,
295 hunk_result
= rpccli_lsa_lookup_sids_noalloc(cli
,
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
;
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
;
337 TALLOC_FREE(domains
);
343 /** Lookup a list of names */
345 NTSTATUS
rpccli_lsa_lookup_names(struct rpc_pipe_client
*cli
,
347 struct policy_handle
*pol
, int num_names
,
349 const char ***dom_names
,
352 enum lsa_SidType
**types
)
356 struct lsa_String
*lsa_names
= NULL
;
357 struct lsa_RefDomainList
*domains
= NULL
;
358 struct lsa_TransSidArray sid_array
;
361 ZERO_STRUCT(sid_array
);
363 lsa_names
= TALLOC_ARRAY(mem_ctx
, struct lsa_String
, num_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
,
381 if (!NT_STATUS_IS_OK(result
) && NT_STATUS_V(result
) !=
382 NT_STATUS_V(STATUS_SOME_UNMAPPED
)) {
384 /* An actual error occured */
389 /* Return output parameters */
392 result
= NT_STATUS_NONE_MAPPED
;
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
;
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
;
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
;
420 if (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 */
435 (*types
)[i
] = SID_NAME_UNKNOWN
;
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
) {
451 (*dom_names
)[i
] = domains
->domains
[dom_idx
].name
.string
;