2 Unix SMB/CIFS implementation.
4 endpoint server for the lsarpc pipe
6 Copyright (C) Andrew Tridgell 2004
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "librpc/gen_ndr/ndr_lsa.h"
26 #include "librpc/gen_ndr/ndr_samr.h"
27 #include "rpc_server/dcerpc_server.h"
28 #include "rpc_server/common/common.h"
29 #include "lib/ldb/include/ldb.h"
30 #include "system/time.h"
32 #include "auth/auth.h"
35 this type allows us to distinguish handle types
41 LSA_HANDLE_TRUSTED_DOMAIN
45 state associated with a lsa_OpenPolicy() operation
47 struct lsa_policy_state {
48 struct dcesrv_handle *handle;
49 struct ldb_context *sam_ldb;
50 struct sidmap_context *sidmap;
52 const struct ldb_dn *domain_dn;
53 const struct ldb_dn *builtin_dn;
54 const struct ldb_dn *system_dn;
55 const char *domain_name;
56 struct dom_sid *domain_sid;
57 struct dom_sid *builtin_sid;
62 state associated with a lsa_OpenAccount() operation
64 struct lsa_account_state {
65 struct lsa_policy_state *policy;
67 struct dom_sid *account_sid;
68 const struct ldb_dn *account_dn;
73 state associated with a lsa_OpenSecret() operation
75 struct lsa_secret_state {
76 struct lsa_policy_state *policy;
78 const struct ldb_dn *secret_dn;
79 struct ldb_context *sam_ldb;
84 state associated with a lsa_OpenTrustedDomain() operation
86 struct lsa_trusted_domain_state {
87 struct lsa_policy_state *policy;
89 const struct ldb_dn *trusted_domain_dn;
95 static NTSTATUS lsa_Close(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
98 struct dcesrv_handle *h;
100 *r->out.handle = *r->in.handle;
102 DCESRV_PULL_HANDLE(h, r->in.handle, DCESRV_HANDLE_ANY);
106 ZERO_STRUCTP(r->out.handle);
115 static NTSTATUS lsa_Delete(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
116 struct lsa_Delete *r)
118 struct dcesrv_handle *h;
121 DCESRV_PULL_HANDLE(h, r->in.handle, DCESRV_HANDLE_ANY);
122 if (h->wire_handle.handle_type == LSA_HANDLE_SECRET) {
123 struct lsa_secret_state *secret_state = h->data;
124 ret = samdb_delete(secret_state->sam_ldb, mem_ctx, secret_state->secret_dn);
127 return NT_STATUS_INVALID_HANDLE;
131 } else if (h->wire_handle.handle_type == LSA_HANDLE_TRUSTED_DOMAIN) {
132 struct lsa_trusted_domain_state *trusted_domain_state = h->data;
133 ret = samdb_delete(trusted_domain_state->policy->sam_ldb, mem_ctx,
134 trusted_domain_state->trusted_domain_dn);
137 return NT_STATUS_INVALID_HANDLE;
143 return NT_STATUS_INVALID_HANDLE;
150 static NTSTATUS lsa_EnumPrivs(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
151 struct lsa_EnumPrivs *r)
153 struct dcesrv_handle *h;
154 struct lsa_policy_state *state;
156 const char *privname;
158 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
162 i = *r->in.resume_handle;
165 while ((privname = sec_privilege_name(i)) &&
166 r->out.privs->count < r->in.max_count) {
167 struct lsa_PrivEntry *e;
169 r->out.privs->privs = talloc_realloc(r->out.privs,
171 struct lsa_PrivEntry,
172 r->out.privs->count+1);
173 if (r->out.privs->privs == NULL) {
174 return NT_STATUS_NO_MEMORY;
176 e = &r->out.privs->privs[r->out.privs->count];
179 e->name.string = privname;
180 r->out.privs->count++;
184 *r->out.resume_handle = i;
193 static NTSTATUS lsa_QuerySecurity(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
194 struct lsa_QuerySecurity *r)
196 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
203 static NTSTATUS lsa_SetSecObj(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
204 struct lsa_SetSecObj *r)
206 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
213 static NTSTATUS lsa_ChangePassword(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
214 struct lsa_ChangePassword *r)
216 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
219 static NTSTATUS lsa_get_policy_state(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
220 struct lsa_policy_state **_state)
222 struct lsa_policy_state *state;
224 state = talloc(mem_ctx, struct lsa_policy_state);
226 return NT_STATUS_NO_MEMORY;
229 /* make sure the sam database is accessible */
230 state->sam_ldb = samdb_connect(state, dce_call->conn->auth_state.session_info);
231 if (state->sam_ldb == NULL) {
232 return NT_STATUS_INVALID_SYSTEM_SERVICE;
235 state->sidmap = sidmap_open(state);
236 if (state->sidmap == NULL) {
237 return NT_STATUS_INVALID_SYSTEM_SERVICE;
240 /* work out the domain_dn - useful for so many calls its worth
242 state->domain_dn = samdb_base_dn(state);
243 if (!state->domain_dn) {
244 return NT_STATUS_NO_MEMORY;
248 = samdb_search_string(state->sam_ldb, mem_ctx, NULL, "nETBIOSName",
249 "(&(objectclass=crossRef)(ncName=%s))", ldb_dn_linearize(mem_ctx, state->domain_dn));
251 if (!state->domain_name) {
252 return NT_STATUS_NO_SUCH_DOMAIN;
254 talloc_steal(state, state->domain_name);
256 /* work out the builtin_dn - useful for so many calls its worth
258 state->builtin_dn = samdb_search_dn(state->sam_ldb, mem_ctx, state->domain_dn, "(objectClass=builtinDomain)");
259 if (!state->builtin_dn) {
260 return NT_STATUS_NO_SUCH_DOMAIN;
263 /* work out the system_dn - useful for so many calls its worth
265 state->system_dn = samdb_search_dn(state->sam_ldb, mem_ctx,
266 state->domain_dn, "(&(objectClass=container)(cn=System))");
267 if (!state->system_dn) {
268 return NT_STATUS_NO_SUCH_DOMAIN;
271 state->domain_sid = samdb_search_dom_sid(state->sam_ldb, state,
272 state->domain_dn, "objectSid", NULL);
273 if (!state->domain_sid) {
274 return NT_STATUS_NO_SUCH_DOMAIN;
277 talloc_steal(state, state->domain_sid);
279 state->builtin_sid = dom_sid_parse_talloc(state, SID_BUILTIN);
280 if (!state->builtin_sid) {
281 return NT_STATUS_NO_SUCH_DOMAIN;
292 static NTSTATUS lsa_OpenPolicy2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
293 struct lsa_OpenPolicy2 *r)
296 struct lsa_policy_state *state;
297 struct dcesrv_handle *handle;
299 ZERO_STRUCTP(r->out.handle);
301 status = lsa_get_policy_state(dce_call, mem_ctx, &state);
302 if (!NT_STATUS_IS_OK(status)) {
306 handle = dcesrv_handle_new(dce_call->context, LSA_HANDLE_POLICY);
308 return NT_STATUS_NO_MEMORY;
311 handle->data = talloc_steal(handle, state);
313 state->access_mask = r->in.access_mask;
314 state->handle = handle;
315 *r->out.handle = handle->wire_handle;
317 /* note that we have completely ignored the attr element of
318 the OpenPolicy. As far as I can tell, this is what w2k3
326 a wrapper around lsa_OpenPolicy2
328 static NTSTATUS lsa_OpenPolicy(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
329 struct lsa_OpenPolicy *r)
331 struct lsa_OpenPolicy2 r2;
333 r2.in.system_name = NULL;
334 r2.in.attr = r->in.attr;
335 r2.in.access_mask = r->in.access_mask;
336 r2.out.handle = r->out.handle;
338 return lsa_OpenPolicy2(dce_call, mem_ctx, &r2);
345 fill in the AccountDomain info
347 static NTSTATUS lsa_info_AccountDomain(struct lsa_policy_state *state, TALLOC_CTX *mem_ctx,
348 struct lsa_DomainInfo *info)
350 info->name.string = state->domain_name;
351 info->sid = state->domain_sid;
357 fill in the DNS domain info
359 static NTSTATUS lsa_info_DNS(struct lsa_policy_state *state, TALLOC_CTX *mem_ctx,
360 struct lsa_DnsDomainInfo *info)
362 const char * const attrs[] = { "dnsDomain", "objectGUID", "objectSid", NULL };
364 struct ldb_message **res;
366 ret = gendb_search_dn(state->sam_ldb, mem_ctx, state->domain_dn, &res, attrs);
368 return NT_STATUS_INTERNAL_DB_CORRUPTION;
371 info->name.string = state->domain_name;
372 info->sid = state->domain_sid;
373 info->dns_domain.string = samdb_result_string(res[0], "dnsDomain", NULL);
374 info->dns_forest.string = samdb_result_string(res[0], "dnsDomain", NULL);
375 info->domain_guid = samdb_result_guid(res[0], "objectGUID");
383 static NTSTATUS lsa_QueryInfoPolicy2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
384 struct lsa_QueryInfoPolicy2 *r)
386 struct lsa_policy_state *state;
387 struct dcesrv_handle *h;
391 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
395 r->out.info = talloc(mem_ctx, union lsa_PolicyInformation);
397 return NT_STATUS_NO_MEMORY;
400 ZERO_STRUCTP(r->out.info);
402 switch (r->in.level) {
403 case LSA_POLICY_INFO_DOMAIN:
404 case LSA_POLICY_INFO_ACCOUNT_DOMAIN:
405 return lsa_info_AccountDomain(state, mem_ctx, &r->out.info->account_domain);
407 case LSA_POLICY_INFO_DNS:
408 return lsa_info_DNS(state, mem_ctx, &r->out.info->dns);
411 return NT_STATUS_INVALID_INFO_CLASS;
417 static NTSTATUS lsa_QueryInfoPolicy(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
418 struct lsa_QueryInfoPolicy *r)
420 struct lsa_QueryInfoPolicy2 r2;
423 r2.in.handle = r->in.handle;
424 r2.in.level = r->in.level;
426 status = lsa_QueryInfoPolicy2(dce_call, mem_ctx, &r2);
428 r->out.info = r2.out.info;
436 static NTSTATUS lsa_SetInfoPolicy(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
437 struct lsa_SetInfoPolicy *r)
439 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
446 static NTSTATUS lsa_ClearAuditLog(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
447 struct lsa_ClearAuditLog *r)
449 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
456 static NTSTATUS lsa_CreateAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
457 struct lsa_CreateAccount *r)
459 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
466 static NTSTATUS lsa_EnumAccounts(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
467 struct lsa_EnumAccounts *r)
469 struct dcesrv_handle *h;
470 struct lsa_policy_state *state;
472 struct ldb_message **res;
473 const char * const attrs[] = { "objectSid", NULL};
476 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
480 ret = gendb_search(state->sam_ldb, mem_ctx, state->builtin_dn, &res, attrs,
483 return NT_STATUS_NO_SUCH_USER;
486 if (*r->in.resume_handle >= ret) {
487 return NT_STATUS_NO_MORE_ENTRIES;
490 count = ret - *r->in.resume_handle;
491 if (count > r->in.num_entries) {
492 count = r->in.num_entries;
496 return NT_STATUS_NO_MORE_ENTRIES;
499 r->out.sids->sids = talloc_array(r->out.sids, struct lsa_SidPtr, count);
500 if (r->out.sids->sids == NULL) {
501 return NT_STATUS_NO_MEMORY;
504 for (i=0;i<count;i++) {
505 r->out.sids->sids[i].sid =
506 samdb_result_dom_sid(r->out.sids->sids,
507 res[i + *r->in.resume_handle],
509 NT_STATUS_HAVE_NO_MEMORY(r->out.sids->sids[i].sid);
512 r->out.sids->num_sids = count;
513 *r->out.resume_handle = count + *r->in.resume_handle;
521 lsa_CreateTrustedDomainEx2
523 static NTSTATUS lsa_CreateTrustedDomainEx2(struct dcesrv_call_state *dce_call,
525 struct lsa_CreateTrustedDomainEx2 *r)
527 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
531 lsa_CreateTrustedDomainEx
533 static NTSTATUS lsa_CreateTrustedDomainEx(struct dcesrv_call_state *dce_call,
535 struct lsa_CreateTrustedDomainEx *r)
537 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
541 lsa_CreateTrustedDomain
543 static NTSTATUS lsa_CreateTrustedDomain(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
544 struct lsa_CreateTrustedDomain *r)
546 struct dcesrv_handle *policy_handle;
547 struct lsa_policy_state *policy_state;
548 struct lsa_trusted_domain_state *trusted_domain_state;
549 struct dcesrv_handle *handle;
550 struct ldb_message **msgs, *msg;
551 const char *attrs[] = {
557 DCESRV_PULL_HANDLE(policy_handle, r->in.handle, LSA_HANDLE_POLICY);
558 ZERO_STRUCTP(r->out.trustdom_handle);
560 policy_state = policy_handle->data;
562 if (!r->in.info->name.string) {
563 return NT_STATUS_INVALID_PARAMETER;
565 name = r->in.info->name.string;
567 trusted_domain_state = talloc(mem_ctx, struct lsa_trusted_domain_state);
568 if (!trusted_domain_state) {
569 return NT_STATUS_NO_MEMORY;
571 trusted_domain_state->policy = policy_state;
573 msg = ldb_msg_new(mem_ctx);
575 return NT_STATUS_NO_MEMORY;
578 /* search for the trusted_domain record */
579 ret = gendb_search(trusted_domain_state->policy->sam_ldb,
580 mem_ctx, policy_state->system_dn, &msgs, attrs,
581 "(&(cn=%s)(objectclass=trustedDomain))",
582 r->in.info->name.string);
584 return NT_STATUS_OBJECT_NAME_COLLISION;
587 if (ret < 0 || ret > 1) {
588 DEBUG(0,("Found %d records matching DN %s\n", ret,
589 ldb_dn_linearize(mem_ctx, policy_state->system_dn)));
590 return NT_STATUS_INTERNAL_DB_CORRUPTION;
593 msg->dn = ldb_dn_build_child(mem_ctx, "cn",
594 r->in.info->name.string,
595 policy_state->system_dn);
597 return NT_STATUS_NO_MEMORY;
600 samdb_msg_add_string(trusted_domain_state->policy->sam_ldb, mem_ctx, msg, "cn", name);
601 samdb_msg_add_string(trusted_domain_state->policy->sam_ldb, mem_ctx, msg, "flatname", name);
603 if (r->in.info->sid) {
604 const char *sid_string = dom_sid_string(mem_ctx, r->in.info->sid);
606 return NT_STATUS_NO_MEMORY;
609 samdb_msg_add_string(trusted_domain_state->policy->sam_ldb, mem_ctx, msg, "securityIdentifier", sid_string);
612 samdb_msg_add_string(trusted_domain_state->policy->sam_ldb, mem_ctx, msg, "objectClass", "trustedDomain");
614 trusted_domain_state->trusted_domain_dn = talloc_reference(trusted_domain_state, msg->dn);
616 /* create the trusted_domain */
617 ret = samdb_add(trusted_domain_state->policy->sam_ldb, mem_ctx, msg);
619 DEBUG(0,("Failed to create trusted_domain record %s\n",
620 ldb_dn_linearize(mem_ctx, msg->dn)));
621 return NT_STATUS_INTERNAL_DB_CORRUPTION;
624 handle = dcesrv_handle_new(dce_call->context, LSA_HANDLE_TRUSTED_DOMAIN);
626 return NT_STATUS_NO_MEMORY;
629 handle->data = talloc_steal(handle, trusted_domain_state);
631 trusted_domain_state->access_mask = r->in.access_mask;
632 trusted_domain_state->policy = talloc_reference(trusted_domain_state, policy_state);
634 *r->out.trustdom_handle = handle->wire_handle;
640 lsa_OpenTrustedDomain
642 static NTSTATUS lsa_OpenTrustedDomain(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
643 struct lsa_OpenTrustedDomain *r)
645 struct dcesrv_handle *policy_handle;
647 struct lsa_policy_state *policy_state;
648 struct lsa_trusted_domain_state *trusted_domain_state;
649 struct dcesrv_handle *handle;
650 struct ldb_message **msgs;
651 const char *attrs[] = {
655 const char *sid_string;
658 DCESRV_PULL_HANDLE(policy_handle, r->in.handle, LSA_HANDLE_POLICY);
659 ZERO_STRUCTP(r->out.trustdom_handle);
660 policy_state = policy_handle->data;
662 trusted_domain_state = talloc(mem_ctx, struct lsa_trusted_domain_state);
663 if (!trusted_domain_state) {
664 return NT_STATUS_NO_MEMORY;
666 trusted_domain_state->policy = policy_state;
668 sid_string = dom_sid_string(mem_ctx, r->in.sid);
670 return NT_STATUS_NO_MEMORY;
673 /* search for the trusted_domain record */
674 ret = gendb_search(trusted_domain_state->policy->sam_ldb,
675 mem_ctx, policy_state->system_dn, &msgs, attrs,
676 "(&(securityIdentifier=%s)(objectclass=trustedDomain))",
679 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
683 DEBUG(0,("Found %d records matching DN %s\n", ret,
684 ldb_dn_linearize(mem_ctx, policy_state->system_dn)));
685 return NT_STATUS_INTERNAL_DB_CORRUPTION;
688 trusted_domain_state->trusted_domain_dn = talloc_reference(trusted_domain_state, msgs[0]->dn);
690 handle = dcesrv_handle_new(dce_call->context, LSA_HANDLE_TRUSTED_DOMAIN);
692 return NT_STATUS_NO_MEMORY;
695 handle->data = talloc_steal(handle, trusted_domain_state);
697 trusted_domain_state->access_mask = r->in.access_mask;
698 trusted_domain_state->policy = talloc_reference(trusted_domain_state, policy_state);
700 *r->out.trustdom_handle = handle->wire_handle;
707 lsa_OpenTrustedDomainByName
709 static NTSTATUS lsa_OpenTrustedDomainByName(struct dcesrv_call_state *dce_call,
711 struct lsa_OpenTrustedDomainByName *r)
713 struct dcesrv_handle *policy_handle;
715 struct lsa_policy_state *policy_state;
716 struct lsa_trusted_domain_state *trusted_domain_state;
717 struct dcesrv_handle *handle;
718 struct ldb_message **msgs;
719 const char *attrs[] = {
725 DCESRV_PULL_HANDLE(policy_handle, r->in.handle, LSA_HANDLE_POLICY);
726 ZERO_STRUCTP(r->out.trustdom_handle);
727 policy_state = policy_handle->data;
729 if (!r->in.name.string) {
730 return NT_STATUS_INVALID_PARAMETER;
733 trusted_domain_state = talloc(mem_ctx, struct lsa_trusted_domain_state);
734 if (!trusted_domain_state) {
735 return NT_STATUS_NO_MEMORY;
737 trusted_domain_state->policy = policy_state;
739 /* search for the trusted_domain record */
740 ret = gendb_search(trusted_domain_state->policy->sam_ldb,
741 mem_ctx, policy_state->system_dn, &msgs, attrs,
742 "(&(flatname=%s)(objectclass=trustedDomain))",
745 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
749 DEBUG(0,("Found %d records matching DN %s\n", ret,
750 ldb_dn_linearize(mem_ctx, policy_state->system_dn)));
751 return NT_STATUS_INTERNAL_DB_CORRUPTION;
754 trusted_domain_state->trusted_domain_dn = talloc_reference(trusted_domain_state, msgs[0]->dn);
756 handle = dcesrv_handle_new(dce_call->context, LSA_HANDLE_TRUSTED_DOMAIN);
758 return NT_STATUS_NO_MEMORY;
761 handle->data = talloc_steal(handle, trusted_domain_state);
763 trusted_domain_state->access_mask = r->in.access_mask;
764 trusted_domain_state->policy = talloc_reference(trusted_domain_state, policy_state);
766 *r->out.trustdom_handle = handle->wire_handle;
773 lsa_QueryTrustedDomainInfoBySid
775 static NTSTATUS lsa_QueryTrustedDomainInfoBySid(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
776 struct lsa_QueryTrustedDomainInfoBySid *r)
778 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
783 lsa_SetTrustDomainInfo
785 static NTSTATUS lsa_SetTrustDomainInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
786 struct lsa_SetTrustDomainInfo *r)
788 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
793 lsa_DeleteTrustDomain
795 static NTSTATUS lsa_DeleteTrustDomain(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
796 struct lsa_DeleteTrustDomain *r)
798 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
803 lsa_QueryTrustedDomainInfo
805 static NTSTATUS lsa_QueryTrustedDomainInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
806 struct lsa_QueryTrustedDomainInfo *r)
808 struct dcesrv_handle *h;
809 struct lsa_trusted_domain_state *trusted_domain_state;
810 struct ldb_message *msg;
812 struct ldb_message **res;
813 const char *attrs[] = {
817 "securityIdentifier",
821 DCESRV_PULL_HANDLE(h, r->in.trustdom_handle, LSA_HANDLE_TRUSTED_DOMAIN);
823 trusted_domain_state = h->data;
825 /* pull all the user attributes */
826 ret = gendb_search_dn(trusted_domain_state->policy->sam_ldb, mem_ctx,
827 trusted_domain_state->trusted_domain_dn, &res, attrs);
829 return NT_STATUS_INTERNAL_DB_CORRUPTION;
833 r->out.info = talloc(mem_ctx, union lsa_TrustedDomainInfo);
835 return NT_STATUS_NO_MEMORY;
837 switch (r->in.level) {
838 case LSA_TRUSTED_DOMAIN_INFO_NAME:
839 r->out.info->name.netbios_name.string
840 = samdb_result_string(msg, "flatname", NULL);
842 case LSA_TRUSTED_DOMAIN_INFO_POSIX_OFFSET:
843 r->out.info->posix_offset.posix_offset
844 = samdb_result_uint(msg, "posixOffset", 0);
847 /* oops, we don't want to return the info after all */
848 talloc_free(r->out.info);
850 return NT_STATUS_INVALID_INFO_CLASS;
858 lsa_SetInformationTrustedDomain
860 static NTSTATUS lsa_SetInformationTrustedDomain(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
861 struct lsa_SetInformationTrustedDomain *r)
863 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
868 lsa_QueryTrustedDomainInfoByName
870 static NTSTATUS lsa_QueryTrustedDomainInfoByName(struct dcesrv_call_state *dce_call,
872 struct lsa_QueryTrustedDomainInfoByName *r)
874 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
878 lsa_SetTrustedDomainInfoByName
880 static NTSTATUS lsa_SetTrustedDomainInfoByName(struct dcesrv_call_state *dce_call,
882 struct lsa_SetTrustedDomainInfoByName *r)
884 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
888 lsa_EnumTrustedDomainsEx
890 static NTSTATUS lsa_EnumTrustedDomainsEx(struct dcesrv_call_state *dce_call,
892 struct lsa_EnumTrustedDomainsEx *r)
894 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
898 lsa_CloseTrustedDomainEx
900 static NTSTATUS lsa_CloseTrustedDomainEx(struct dcesrv_call_state *dce_call,
902 struct lsa_CloseTrustedDomainEx *r)
904 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
909 comparison function for sorting lsa_DomainInformation array
911 static int compare_DomainInformation(struct lsa_DomainInformation *e1, struct lsa_DomainInformation *e2)
913 return strcasecmp(e1->name.string, e2->name.string);
919 static NTSTATUS lsa_EnumTrustDom(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
920 struct lsa_EnumTrustDom *r)
922 struct dcesrv_handle *policy_handle;
923 struct lsa_DomainInformation *entries;
924 struct lsa_policy_state *policy_state;
925 struct ldb_message **domains;
926 const char *attrs[] = {
928 "securityIdentifier",
935 *r->out.resume_handle = 0;
937 r->out.domains->domains = NULL;
938 r->out.domains->count = 0;
940 DCESRV_PULL_HANDLE(policy_handle, r->in.handle, LSA_HANDLE_POLICY);
942 policy_state = policy_handle->data;
944 /* search for all users in this domain. This could possibly be cached and
945 resumed based on resume_key */
946 count = gendb_search(policy_state->sam_ldb, mem_ctx, policy_state->system_dn, &domains, attrs,
947 "objectclass=trustedDomain");
949 return NT_STATUS_INTERNAL_DB_CORRUPTION;
951 if (count == 0 || r->in.max_size == 0) {
955 /* convert to lsa_DomainInformation format */
956 entries = talloc_array(mem_ctx, struct lsa_DomainInformation, count);
958 return NT_STATUS_NO_MEMORY;
960 for (i=0;i<count;i++) {
961 entries[i].sid = samdb_result_dom_sid(mem_ctx, domains[i], "securityIdentifier");
962 entries[i].name.string = samdb_result_string(domains[i], "flatname", NULL);
965 /* sort the results by name */
966 qsort(entries, count, sizeof(struct lsa_DomainInformation),
967 (comparison_fn_t)compare_DomainInformation);
969 if (*r->in.resume_handle >= count) {
970 *r->out.resume_handle = -1;
972 return NT_STATUS_NO_MORE_ENTRIES;
975 /* return the rest, limit by max_size. Note that we
976 use the w2k3 element size value of 60 */
977 r->out.domains->count = count - *r->in.resume_handle;
978 r->out.domains->count = MIN(r->out.domains->count,
979 1+(r->in.max_size/LSA_ENUM_TRUST_DOMAIN_MULTIPLIER));
981 r->out.domains->domains = entries + *r->in.resume_handle;
982 r->out.domains->count = r->out.domains->count;
984 if (r->out.domains->count < count - *r->in.resume_handle) {
985 *r->out.resume_handle = *r->in.resume_handle + r->out.domains->count;
986 return STATUS_MORE_ENTRIES;
994 return the authority name and authority sid, given a sid
996 static NTSTATUS lsa_authority_name(struct lsa_policy_state *state,
997 TALLOC_CTX *mem_ctx, struct dom_sid *sid,
998 const char **authority_name,
999 struct dom_sid **authority_sid)
1001 if (dom_sid_in_domain(state->domain_sid, sid)) {
1002 *authority_name = state->domain_name;
1003 *authority_sid = state->domain_sid;
1004 return NT_STATUS_OK;
1007 if (dom_sid_in_domain(state->builtin_sid, sid)) {
1008 *authority_name = "BUILTIN";
1009 *authority_sid = state->builtin_sid;
1010 return NT_STATUS_OK;
1013 *authority_sid = dom_sid_dup(mem_ctx, sid);
1014 if (*authority_sid == NULL) {
1015 return NT_STATUS_NO_MEMORY;
1017 (*authority_sid)->num_auths = 0;
1018 *authority_name = dom_sid_string(mem_ctx, *authority_sid);
1019 if (*authority_name == NULL) {
1020 return NT_STATUS_NO_MEMORY;
1023 return NT_STATUS_OK;
1027 add to the lsa_RefDomainList for LookupSids and LookupNames
1029 static NTSTATUS lsa_authority_list(struct lsa_policy_state *state, TALLOC_CTX *mem_ctx,
1030 struct dom_sid *sid,
1031 struct lsa_RefDomainList *domains,
1032 uint32_t *sid_index)
1035 const char *authority_name;
1036 struct dom_sid *authority_sid;
1039 /* work out the authority name */
1040 status = lsa_authority_name(state, mem_ctx, sid,
1041 &authority_name, &authority_sid);
1042 if (!NT_STATUS_IS_OK(status)) {
1046 /* see if we've already done this authority name */
1047 for (i=0;i<domains->count;i++) {
1048 if (strcmp(authority_name, domains->domains[i].name.string) == 0) {
1050 return NT_STATUS_OK;
1054 domains->domains = talloc_realloc(domains,
1056 struct lsa_TrustInformation,
1058 if (domains->domains == NULL) {
1059 return NT_STATUS_NO_MEMORY;
1061 domains->domains[i].name.string = authority_name;
1062 domains->domains[i].sid = authority_sid;
1066 return NT_STATUS_OK;
1070 lookup a name for 1 SID
1072 static NTSTATUS lsa_lookup_sid(struct lsa_policy_state *state, TALLOC_CTX *mem_ctx,
1073 struct dom_sid *sid, const char *sid_str,
1074 const char **name, uint32_t *atype)
1077 struct ldb_message **res;
1078 const char * const attrs[] = { "sAMAccountName", "sAMAccountType", "name", NULL};
1081 ret = gendb_search(state->sam_ldb, mem_ctx, NULL, &res, attrs,
1082 "objectSid=%s", ldap_encode_ndr_dom_sid(mem_ctx, sid));
1084 *name = ldb_msg_find_string(res[0], "sAMAccountName", NULL);
1086 *name = ldb_msg_find_string(res[0], "name", NULL);
1088 *name = talloc_strdup(mem_ctx, sid_str);
1089 NTSTATUS_TALLOC_CHECK(*name);
1093 *atype = samdb_result_uint(res[0], "sAMAccountType", 0);
1095 return NT_STATUS_OK;
1098 status = sidmap_allocated_sid_lookup(state->sidmap, mem_ctx, sid, name, atype);
1107 static NTSTATUS lsa_LookupSids3(struct dcesrv_call_state *dce_call,
1108 TALLOC_CTX *mem_ctx,
1109 struct lsa_LookupSids3 *r)
1111 struct lsa_policy_state *state;
1113 NTSTATUS status = NT_STATUS_OK;
1115 r->out.domains = NULL;
1117 status = lsa_get_policy_state(dce_call, mem_ctx, &state);
1118 if (!NT_STATUS_IS_OK(status)) {
1122 r->out.domains = talloc_zero(mem_ctx, struct lsa_RefDomainList);
1123 if (r->out.domains == NULL) {
1124 return NT_STATUS_NO_MEMORY;
1127 r->out.names = talloc_zero(mem_ctx, struct lsa_TransNameArray2);
1128 if (r->out.names == NULL) {
1129 return NT_STATUS_NO_MEMORY;
1134 r->out.names->names = talloc_array(r->out.names, struct lsa_TranslatedName2,
1135 r->in.sids->num_sids);
1136 if (r->out.names->names == NULL) {
1137 return NT_STATUS_NO_MEMORY;
1140 for (i=0;i<r->in.sids->num_sids;i++) {
1141 struct dom_sid *sid = r->in.sids->sids[i].sid;
1142 char *sid_str = dom_sid_string(mem_ctx, sid);
1144 uint32_t atype, rtype, sid_index;
1147 r->out.names->count++;
1150 r->out.names->names[i].sid_type = SID_NAME_UNKNOWN;
1151 r->out.names->names[i].name.string = sid_str;
1152 r->out.names->names[i].sid_index = 0xFFFFFFFF;
1153 r->out.names->names[i].unknown = 0;
1155 if (sid_str == NULL) {
1156 r->out.names->names[i].name.string = "(SIDERROR)";
1157 status = STATUS_SOME_UNMAPPED;
1161 /* work out the authority name */
1162 status2 = lsa_authority_list(state, mem_ctx, sid, r->out.domains, &sid_index);
1163 if (!NT_STATUS_IS_OK(status2)) {
1167 status2 = lsa_lookup_sid(state, mem_ctx, sid, sid_str,
1169 if (!NT_STATUS_IS_OK(status2)) {
1170 status = STATUS_SOME_UNMAPPED;
1174 rtype = samdb_atype_map(atype);
1175 if (rtype == SID_NAME_UNKNOWN) {
1176 status = STATUS_SOME_UNMAPPED;
1180 r->out.names->names[i].sid_type = rtype;
1181 r->out.names->names[i].name.string = name;
1182 r->out.names->names[i].sid_index = sid_index;
1183 r->out.names->names[i].unknown = 0;
1193 static NTSTATUS lsa_LookupSids2(struct dcesrv_call_state *dce_call,
1194 TALLOC_CTX *mem_ctx,
1195 struct lsa_LookupSids2 *r)
1197 struct lsa_LookupSids3 r3;
1200 r3.in.sids = r->in.sids;
1201 r3.in.names = r->in.names;
1202 r3.in.level = r->in.level;
1203 r3.in.count = r->in.count;
1204 r3.in.unknown1 = r->in.unknown1;
1205 r3.in.unknown2 = r->in.unknown2;
1206 r3.out.count = r->out.count;
1207 r3.out.names = r->out.names;
1209 status = lsa_LookupSids3(dce_call, mem_ctx, &r3);
1210 if (dce_call->fault_code != 0) {
1214 r->out.domains = r3.out.domains;
1215 r->out.names = r3.out.names;
1216 r->out.count = r3.out.count;
1225 static NTSTATUS lsa_LookupSids(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1226 struct lsa_LookupSids *r)
1228 struct lsa_LookupSids3 r3;
1232 r3.in.sids = r->in.sids;
1234 r3.in.level = r->in.level;
1235 r3.in.count = r->in.count;
1238 r3.out.count = r->out.count;
1239 r3.out.names = NULL;
1241 status = lsa_LookupSids3(dce_call, mem_ctx, &r3);
1242 if (dce_call->fault_code != 0) {
1246 r->out.domains = r3.out.domains;
1247 if (!r3.out.names) {
1248 r->out.names = NULL;
1252 r->out.names = talloc(mem_ctx, struct lsa_TransNameArray);
1253 if (r->out.names == NULL) {
1254 return NT_STATUS_NO_MEMORY;
1256 r->out.names->count = r3.out.names->count;
1257 r->out.names->names = talloc_array(r->out.names, struct lsa_TranslatedName,
1258 r->out.names->count);
1259 if (r->out.names->names == NULL) {
1260 return NT_STATUS_NO_MEMORY;
1262 for (i=0;i<r->out.names->count;i++) {
1263 r->out.names->names[i].sid_type = r3.out.names->names[i].sid_type;
1264 r->out.names->names[i].name.string = r3.out.names->names[i].name.string;
1265 r->out.names->names[i].sid_index = r3.out.names->names[i].sid_index;
1275 static NTSTATUS lsa_OpenAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1276 struct lsa_OpenAccount *r)
1278 struct dcesrv_handle *h, *ah;
1279 struct lsa_policy_state *state;
1280 struct lsa_account_state *astate;
1282 ZERO_STRUCTP(r->out.acct_handle);
1284 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
1288 astate = talloc(dce_call->conn, struct lsa_account_state);
1289 if (astate == NULL) {
1290 return NT_STATUS_NO_MEMORY;
1293 astate->account_sid = dom_sid_dup(astate, r->in.sid);
1294 if (astate->account_sid == NULL) {
1295 talloc_free(astate);
1296 return NT_STATUS_NO_MEMORY;
1299 /* check it really exists */
1300 astate->account_dn = samdb_search_dn(state->sam_ldb, astate,
1301 NULL, "(&(objectSid=%s)(objectClass=group))",
1302 ldap_encode_ndr_dom_sid(mem_ctx, astate->account_sid));
1303 if (astate->account_dn == NULL) {
1304 talloc_free(astate);
1305 return NT_STATUS_NO_SUCH_USER;
1308 astate->policy = talloc_reference(astate, state);
1309 astate->access_mask = r->in.access_mask;
1311 ah = dcesrv_handle_new(dce_call->context, LSA_HANDLE_ACCOUNT);
1313 talloc_free(astate);
1314 return NT_STATUS_NO_MEMORY;
1317 ah->data = talloc_steal(ah, astate);
1319 *r->out.acct_handle = ah->wire_handle;
1321 return NT_STATUS_OK;
1326 lsa_EnumPrivsAccount
1328 static NTSTATUS lsa_EnumPrivsAccount(struct dcesrv_call_state *dce_call,
1329 TALLOC_CTX *mem_ctx,
1330 struct lsa_EnumPrivsAccount *r)
1332 struct dcesrv_handle *h;
1333 struct lsa_account_state *astate;
1335 struct ldb_message **res;
1336 const char * const attrs[] = { "privilege", NULL};
1337 struct ldb_message_element *el;
1339 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_ACCOUNT);
1343 r->out.privs = talloc(mem_ctx, struct lsa_PrivilegeSet);
1344 r->out.privs->count = 0;
1345 r->out.privs->unknown = 0;
1346 r->out.privs->set = NULL;
1348 ret = gendb_search_dn(astate->policy->sam_ldb, mem_ctx,
1349 astate->account_dn, &res, attrs);
1351 return NT_STATUS_OK;
1354 el = ldb_msg_find_element(res[0], "privilege");
1355 if (el == NULL || el->num_values == 0) {
1356 return NT_STATUS_OK;
1359 r->out.privs->set = talloc_array(r->out.privs,
1360 struct lsa_LUIDAttribute, el->num_values);
1361 if (r->out.privs->set == NULL) {
1362 return NT_STATUS_NO_MEMORY;
1365 for (i=0;i<el->num_values;i++) {
1366 int id = sec_privilege_id((const char *)el->values[i].data);
1368 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1370 r->out.privs->set[i].attribute = 0;
1371 r->out.privs->set[i].luid.low = id;
1372 r->out.privs->set[i].luid.high = 0;
1375 r->out.privs->count = el->num_values;
1377 return NT_STATUS_OK;
1381 lsa_EnumAccountRights
1383 static NTSTATUS lsa_EnumAccountRights(struct dcesrv_call_state *dce_call,
1384 TALLOC_CTX *mem_ctx,
1385 struct lsa_EnumAccountRights *r)
1387 struct dcesrv_handle *h;
1388 struct lsa_policy_state *state;
1390 struct ldb_message **res;
1391 const char * const attrs[] = { "privilege", NULL};
1393 struct ldb_message_element *el;
1395 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
1399 sidstr = ldap_encode_ndr_dom_sid(mem_ctx, r->in.sid);
1400 if (sidstr == NULL) {
1401 return NT_STATUS_NO_MEMORY;
1404 ret = gendb_search(state->sam_ldb, mem_ctx, NULL, &res, attrs,
1405 "objectSid=%s", sidstr);
1407 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1410 el = ldb_msg_find_element(res[0], "privilege");
1411 if (el == NULL || el->num_values == 0) {
1412 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1415 r->out.rights->count = el->num_values;
1416 r->out.rights->names = talloc_array(r->out.rights,
1417 struct lsa_String, r->out.rights->count);
1418 if (r->out.rights->names == NULL) {
1419 return NT_STATUS_NO_MEMORY;
1422 for (i=0;i<el->num_values;i++) {
1423 r->out.rights->names[i].string = (const char *)el->values[i].data;
1426 return NT_STATUS_OK;
1432 helper for lsa_AddAccountRights and lsa_RemoveAccountRights
1434 static NTSTATUS lsa_AddRemoveAccountRights(struct dcesrv_call_state *dce_call,
1435 TALLOC_CTX *mem_ctx,
1436 struct lsa_policy_state *state,
1438 struct dom_sid *sid,
1439 const struct lsa_RightSet *rights)
1442 struct ldb_message *msg;
1443 struct ldb_message_element el;
1445 struct lsa_EnumAccountRights r2;
1447 sidstr = ldap_encode_ndr_dom_sid(mem_ctx, sid);
1448 if (sidstr == NULL) {
1449 return NT_STATUS_NO_MEMORY;
1452 msg = ldb_msg_new(mem_ctx);
1454 return NT_STATUS_NO_MEMORY;
1457 msg->dn = samdb_search_dn(state->sam_ldb, mem_ctx, NULL, "objectSid=%s", sidstr);
1458 if (msg->dn == NULL) {
1459 return NT_STATUS_NO_SUCH_USER;
1462 if (ldb_msg_add_empty(msg, "privilege", ldb_flag)) {
1463 return NT_STATUS_NO_MEMORY;
1466 if (ldb_flag == LDB_FLAG_MOD_ADD) {
1469 r2.in.handle = &state->handle->wire_handle;
1471 r2.out.rights = talloc(mem_ctx, struct lsa_RightSet);
1473 status = lsa_EnumAccountRights(dce_call, mem_ctx, &r2);
1474 if (!NT_STATUS_IS_OK(status)) {
1475 ZERO_STRUCTP(r2.out.rights);
1480 el.values = talloc_array(mem_ctx, struct ldb_val, rights->count);
1481 if (el.values == NULL) {
1482 return NT_STATUS_NO_MEMORY;
1484 for (i=0;i<rights->count;i++) {
1485 if (sec_privilege_id(rights->names[i].string) == -1) {
1486 return NT_STATUS_NO_SUCH_PRIVILEGE;
1489 if (ldb_flag == LDB_FLAG_MOD_ADD) {
1491 for (j=0;j<r2.out.rights->count;j++) {
1492 if (strcasecmp_m(r2.out.rights->names[j].string,
1493 rights->names[i].string) == 0) {
1497 if (j != r2.out.rights->count) continue;
1501 el.values[el.num_values].length = strlen(rights->names[i].string);
1502 el.values[el.num_values].data = (uint8_t *)talloc_strdup(mem_ctx, rights->names[i].string);
1503 if (el.values[el.num_values].data == NULL) {
1504 return NT_STATUS_NO_MEMORY;
1509 if (el.num_values == 0) {
1510 return NT_STATUS_OK;
1513 ret = samdb_modify(state->sam_ldb, mem_ctx, msg);
1515 if (ldb_flag == LDB_FLAG_MOD_DELETE) {
1516 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1518 return NT_STATUS_UNEXPECTED_IO_ERROR;
1521 return NT_STATUS_OK;
1525 lsa_AddPrivilegesToAccount
1527 static NTSTATUS lsa_AddPrivilegesToAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1528 struct lsa_AddPrivilegesToAccount *r)
1530 struct lsa_RightSet rights;
1531 struct dcesrv_handle *h;
1532 struct lsa_account_state *astate;
1535 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_ACCOUNT);
1539 rights.count = r->in.privs->count;
1540 rights.names = talloc_array(mem_ctx, struct lsa_String, rights.count);
1541 if (rights.names == NULL) {
1542 return NT_STATUS_NO_MEMORY;
1544 for (i=0;i<rights.count;i++) {
1545 int id = r->in.privs->set[i].luid.low;
1546 if (r->in.privs->set[i].luid.high) {
1547 return NT_STATUS_NO_SUCH_PRIVILEGE;
1549 rights.names[i].string = sec_privilege_name(id);
1550 if (rights.names[i].string == NULL) {
1551 return NT_STATUS_NO_SUCH_PRIVILEGE;
1555 return lsa_AddRemoveAccountRights(dce_call, mem_ctx, astate->policy,
1556 LDB_FLAG_MOD_ADD, astate->account_sid,
1562 lsa_RemovePrivilegesFromAccount
1564 static NTSTATUS lsa_RemovePrivilegesFromAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1565 struct lsa_RemovePrivilegesFromAccount *r)
1567 struct lsa_RightSet *rights;
1568 struct dcesrv_handle *h;
1569 struct lsa_account_state *astate;
1572 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_ACCOUNT);
1576 rights = talloc(mem_ctx, struct lsa_RightSet);
1578 if (r->in.remove_all == 1 &&
1579 r->in.privs == NULL) {
1580 struct lsa_EnumAccountRights r2;
1583 r2.in.handle = &astate->policy->handle->wire_handle;
1584 r2.in.sid = astate->account_sid;
1585 r2.out.rights = rights;
1587 status = lsa_EnumAccountRights(dce_call, mem_ctx, &r2);
1588 if (!NT_STATUS_IS_OK(status)) {
1592 return lsa_AddRemoveAccountRights(dce_call, mem_ctx, astate->policy,
1593 LDB_FLAG_MOD_DELETE, astate->account_sid,
1597 if (r->in.remove_all != 0) {
1598 return NT_STATUS_INVALID_PARAMETER;
1601 rights->count = r->in.privs->count;
1602 rights->names = talloc_array(mem_ctx, struct lsa_String, rights->count);
1603 if (rights->names == NULL) {
1604 return NT_STATUS_NO_MEMORY;
1606 for (i=0;i<rights->count;i++) {
1607 int id = r->in.privs->set[i].luid.low;
1608 if (r->in.privs->set[i].luid.high) {
1609 return NT_STATUS_NO_SUCH_PRIVILEGE;
1611 rights->names[i].string = sec_privilege_name(id);
1612 if (rights->names[i].string == NULL) {
1613 return NT_STATUS_NO_SUCH_PRIVILEGE;
1617 return lsa_AddRemoveAccountRights(dce_call, mem_ctx, astate->policy,
1618 LDB_FLAG_MOD_DELETE, astate->account_sid,
1624 lsa_GetQuotasForAccount
1626 static NTSTATUS lsa_GetQuotasForAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1627 struct lsa_GetQuotasForAccount *r)
1629 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1634 lsa_SetQuotasForAccount
1636 static NTSTATUS lsa_SetQuotasForAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1637 struct lsa_SetQuotasForAccount *r)
1639 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1644 lsa_GetSystemAccessAccount
1646 static NTSTATUS lsa_GetSystemAccessAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1647 struct lsa_GetSystemAccessAccount *r)
1649 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1654 lsa_SetSystemAccessAccount
1656 static NTSTATUS lsa_SetSystemAccessAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1657 struct lsa_SetSystemAccessAccount *r)
1659 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1666 static NTSTATUS lsa_CreateSecret(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1667 struct lsa_CreateSecret *r)
1669 struct dcesrv_handle *policy_handle;
1670 struct lsa_policy_state *policy_state;
1671 struct lsa_secret_state *secret_state;
1672 struct dcesrv_handle *handle;
1673 struct ldb_message **msgs, *msg;
1674 const char *attrs[] = {
1682 DCESRV_PULL_HANDLE(policy_handle, r->in.handle, LSA_HANDLE_POLICY);
1683 ZERO_STRUCTP(r->out.sec_handle);
1685 policy_state = policy_handle->data;
1687 if (!r->in.name.string) {
1688 return NT_STATUS_INVALID_PARAMETER;
1691 secret_state = talloc(mem_ctx, struct lsa_secret_state);
1692 if (!secret_state) {
1693 return NT_STATUS_NO_MEMORY;
1695 secret_state->policy = policy_state;
1697 msg = ldb_msg_new(mem_ctx);
1699 return NT_STATUS_NO_MEMORY;
1702 if (strncmp("G$", r->in.name.string, 2) == 0) {
1704 name = &r->in.name.string[2];
1705 secret_state->sam_ldb = talloc_reference(secret_state, policy_state->sam_ldb);
1706 secret_state->global = True;
1708 if (strlen(name) < 1) {
1709 return NT_STATUS_INVALID_PARAMETER;
1712 name2 = talloc_asprintf(mem_ctx, "%s Secret", name);
1713 /* search for the secret record */
1714 ret = gendb_search(secret_state->sam_ldb,
1715 mem_ctx, policy_state->system_dn, &msgs, attrs,
1716 "(&(cn=%s)(objectclass=secret))",
1719 return NT_STATUS_OBJECT_NAME_COLLISION;
1722 if (ret < 0 || ret > 1) {
1723 DEBUG(0,("Found %d records matching DN %s\n", ret,
1724 ldb_dn_linearize(mem_ctx, policy_state->system_dn)));
1725 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1728 msg->dn = ldb_dn_build_child(mem_ctx, "cn", name2, policy_state->system_dn);
1729 if (!name2 || !msg->dn) {
1730 return NT_STATUS_NO_MEMORY;
1733 samdb_msg_add_string(secret_state->sam_ldb, mem_ctx, msg, "cn", name2);
1736 secret_state->global = False;
1738 name = r->in.name.string;
1739 if (strlen(name) < 1) {
1740 return NT_STATUS_INVALID_PARAMETER;
1743 secret_state->sam_ldb = talloc_reference(secret_state, secrets_db_connect(mem_ctx));
1744 /* search for the secret record */
1745 ret = gendb_search(secret_state->sam_ldb, mem_ctx,
1746 ldb_dn_explode(mem_ctx, "cn=LSA Secrets"),
1748 "(&(cn=%s)(objectclass=secret))", name);
1750 return NT_STATUS_OBJECT_NAME_COLLISION;
1753 if (ret < 0 || ret > 1) {
1754 DEBUG(0,("Found %d records matching DN %s\n", ret,
1755 ldb_dn_linearize(mem_ctx, policy_state->system_dn)));
1756 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1759 msg->dn = ldb_dn_string_compose(mem_ctx, NULL, "cn=%s,cn=LSA Secrets", name);
1760 samdb_msg_add_string(secret_state->sam_ldb, mem_ctx, msg, "cn", name);
1763 /* pull in all the template attributes. Note this is always from the global samdb */
1764 ret = samdb_copy_template(secret_state->policy->sam_ldb, mem_ctx, msg,
1765 "(&(name=TemplateSecret)(objectclass=secretTemplate))");
1767 DEBUG(0,("Failed to load TemplateSecret from samdb\n"));
1768 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1771 samdb_msg_add_string(secret_state->sam_ldb, mem_ctx, msg, "objectClass", "secret");
1773 secret_state->secret_dn = talloc_reference(secret_state, msg->dn);
1775 /* create the secret */
1776 ret = samdb_add(secret_state->sam_ldb, mem_ctx, msg);
1778 DEBUG(0,("Failed to create secret record %s\n",
1779 ldb_dn_linearize(mem_ctx, msg->dn)));
1780 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1783 handle = dcesrv_handle_new(dce_call->context, LSA_HANDLE_SECRET);
1785 return NT_STATUS_NO_MEMORY;
1788 handle->data = talloc_steal(handle, secret_state);
1790 secret_state->access_mask = r->in.access_mask;
1791 secret_state->policy = talloc_reference(secret_state, policy_state);
1793 *r->out.sec_handle = handle->wire_handle;
1795 return NT_STATUS_OK;
1802 static NTSTATUS lsa_OpenSecret(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1803 struct lsa_OpenSecret *r)
1805 struct dcesrv_handle *policy_handle;
1807 struct lsa_policy_state *policy_state;
1808 struct lsa_secret_state *secret_state;
1809 struct dcesrv_handle *handle;
1810 struct ldb_message **msgs;
1811 const char *attrs[] = {
1819 DCESRV_PULL_HANDLE(policy_handle, r->in.handle, LSA_HANDLE_POLICY);
1820 ZERO_STRUCTP(r->out.sec_handle);
1821 policy_state = policy_handle->data;
1823 if (!r->in.name.string) {
1824 return NT_STATUS_INVALID_PARAMETER;
1827 secret_state = talloc(mem_ctx, struct lsa_secret_state);
1828 if (!secret_state) {
1829 return NT_STATUS_NO_MEMORY;
1831 secret_state->policy = policy_state;
1833 if (strncmp("G$", r->in.name.string, 2) == 0) {
1834 name = &r->in.name.string[2];
1835 secret_state->sam_ldb = talloc_reference(secret_state, policy_state->sam_ldb);
1836 secret_state->global = True;
1838 if (strlen(name) < 1) {
1839 return NT_STATUS_INVALID_PARAMETER;
1842 /* search for the secret record */
1843 ret = gendb_search(secret_state->sam_ldb,
1844 mem_ctx, policy_state->system_dn, &msgs, attrs,
1845 "(&(cn=%s Secret)(objectclass=secret))",
1848 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1852 DEBUG(0,("Found %d records matching DN %s\n", ret,
1853 ldb_dn_linearize(mem_ctx, policy_state->system_dn)));
1854 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1858 secret_state->sam_ldb = talloc_reference(secret_state, secrets_db_connect(mem_ctx));
1860 secret_state->global = False;
1861 name = r->in.name.string;
1862 if (strlen(name) < 1) {
1863 return NT_STATUS_INVALID_PARAMETER;
1866 /* search for the secret record */
1867 ret = gendb_search(secret_state->sam_ldb, mem_ctx,
1868 ldb_dn_explode(mem_ctx, "cn=LSA Secrets"),
1870 "(&(cn=%s)(objectclass=secret))", name);
1872 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1876 DEBUG(0,("Found %d records matching DN %s\n", ret,
1877 ldb_dn_linearize(mem_ctx, policy_state->system_dn)));
1878 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1882 secret_state->secret_dn = talloc_reference(secret_state, msgs[0]->dn);
1884 handle = dcesrv_handle_new(dce_call->context, LSA_HANDLE_SECRET);
1886 return NT_STATUS_NO_MEMORY;
1889 handle->data = talloc_steal(handle, secret_state);
1891 secret_state->access_mask = r->in.access_mask;
1892 secret_state->policy = talloc_reference(secret_state, policy_state);
1894 *r->out.sec_handle = handle->wire_handle;
1896 return NT_STATUS_OK;
1903 static NTSTATUS lsa_SetSecret(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1904 struct lsa_SetSecret *r)
1907 struct dcesrv_handle *h;
1908 struct lsa_secret_state *secret_state;
1909 struct ldb_message *msg;
1910 DATA_BLOB session_key;
1911 DATA_BLOB crypt_secret, secret;
1914 NTSTATUS status = NT_STATUS_OK;
1916 struct timeval now = timeval_current();
1917 NTTIME nt_now = timeval_to_nttime(&now);
1919 DCESRV_PULL_HANDLE(h, r->in.sec_handle, LSA_HANDLE_SECRET);
1921 secret_state = h->data;
1923 msg = ldb_msg_new(mem_ctx);
1925 return NT_STATUS_NO_MEMORY;
1928 msg->dn = talloc_reference(mem_ctx, secret_state->secret_dn);
1930 return NT_STATUS_NO_MEMORY;
1932 status = dcesrv_fetch_session_key(dce_call->conn, &session_key);
1933 if (!NT_STATUS_IS_OK(status)) {
1937 if (r->in.old_val) {
1939 crypt_secret.data = r->in.old_val->data;
1940 crypt_secret.length = r->in.old_val->size;
1942 status = sess_decrypt_blob(mem_ctx, &crypt_secret, &session_key, &secret);
1943 if (!NT_STATUS_IS_OK(status)) {
1947 val.data = secret.data;
1948 val.length = secret.length;
1951 if (samdb_msg_add_value(secret_state->sam_ldb,
1952 mem_ctx, msg, "priorSecret", &val) != 0) {
1953 return NT_STATUS_NO_MEMORY;
1956 /* set old value mtime */
1957 if (samdb_msg_add_uint64(secret_state->sam_ldb,
1958 mem_ctx, msg, "priorSetTime", nt_now) != 0) {
1959 return NT_STATUS_NO_MEMORY;
1962 if (!r->in.new_val) {
1963 /* This behaviour varies depending of if this is a local, or a global secret... */
1964 if (secret_state->global) {
1965 /* set old value mtime */
1966 if (samdb_msg_add_uint64(secret_state->sam_ldb,
1967 mem_ctx, msg, "lastSetTime", nt_now) != 0) {
1968 return NT_STATUS_NO_MEMORY;
1971 if (samdb_msg_add_delete(secret_state->sam_ldb,
1972 mem_ctx, msg, "secret")) {
1973 return NT_STATUS_NO_MEMORY;
1975 if (samdb_msg_add_delete(secret_state->sam_ldb,
1976 mem_ctx, msg, "lastSetTime")) {
1977 return NT_STATUS_NO_MEMORY;
1983 if (r->in.new_val) {
1985 crypt_secret.data = r->in.new_val->data;
1986 crypt_secret.length = r->in.new_val->size;
1988 status = sess_decrypt_blob(mem_ctx, &crypt_secret, &session_key, &secret);
1989 if (!NT_STATUS_IS_OK(status)) {
1993 val.data = secret.data;
1994 val.length = secret.length;
1997 if (samdb_msg_add_value(secret_state->sam_ldb,
1998 mem_ctx, msg, "secret", &val) != 0) {
1999 return NT_STATUS_NO_MEMORY;
2002 /* set new value mtime */
2003 if (samdb_msg_add_uint64(secret_state->sam_ldb,
2004 mem_ctx, msg, "lastSetTime", nt_now) != 0) {
2005 return NT_STATUS_NO_MEMORY;
2008 /* If the old value is not set, then migrate the
2009 * current value to the old value */
2010 if (!r->in.old_val) {
2011 const struct ldb_val *new_val;
2012 NTTIME last_set_time;
2013 struct ldb_message **res;
2014 const char *attrs[] = {
2020 /* search for the secret record */
2021 ret = gendb_search_dn(secret_state->sam_ldb,mem_ctx,
2022 secret_state->secret_dn, &res, attrs);
2024 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2028 DEBUG(0,("Found %d records matching dn=%s\n", ret,
2029 ldb_dn_linearize(mem_ctx, secret_state->secret_dn)));
2030 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2033 new_val = ldb_msg_find_ldb_val(res[0], "secret");
2034 last_set_time = ldb_msg_find_uint64(res[0], "lastSetTime", 0);
2038 if (samdb_msg_add_value(secret_state->sam_ldb,
2039 mem_ctx, msg, "priorSecret",
2041 return NT_STATUS_NO_MEMORY;
2045 /* set new value mtime */
2046 if (ldb_msg_find_ldb_val(res[0], "lastSetTime")) {
2047 if (samdb_msg_add_uint64(secret_state->sam_ldb,
2048 mem_ctx, msg, "priorSetTime", last_set_time) != 0) {
2049 return NT_STATUS_NO_MEMORY;
2055 /* modify the samdb record */
2056 ret = samdb_replace(secret_state->sam_ldb, mem_ctx, msg);
2058 /* we really need samdb.c to return NTSTATUS */
2059 return NT_STATUS_UNSUCCESSFUL;
2062 return NT_STATUS_OK;
2069 static NTSTATUS lsa_QuerySecret(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2070 struct lsa_QuerySecret *r)
2072 struct dcesrv_handle *h;
2073 struct lsa_secret_state *secret_state;
2074 struct ldb_message *msg;
2075 DATA_BLOB session_key;
2076 DATA_BLOB crypt_secret, secret;
2078 struct ldb_message **res;
2079 const char *attrs[] = {
2089 DCESRV_PULL_HANDLE(h, r->in.sec_handle, LSA_HANDLE_SECRET);
2091 secret_state = h->data;
2093 /* pull all the user attributes */
2094 ret = gendb_search_dn(secret_state->sam_ldb, mem_ctx,
2095 secret_state->secret_dn, &res, attrs);
2097 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2101 nt_status = dcesrv_fetch_session_key(dce_call->conn, &session_key);
2102 if (!NT_STATUS_IS_OK(nt_status)) {
2106 if (r->in.old_val) {
2107 const struct ldb_val *prior_val;
2108 r->out.old_val = talloc_zero(mem_ctx, struct lsa_DATA_BUF_PTR);
2109 if (!r->out.old_val) {
2110 return NT_STATUS_NO_MEMORY;
2113 prior_val = ldb_msg_find_ldb_val(res[0], "priorSecret");
2115 if (prior_val && prior_val->length) {
2116 secret.data = prior_val->data;
2117 secret.length = prior_val->length;
2119 crypt_secret = sess_encrypt_blob(mem_ctx, &secret, &session_key);
2120 if (!crypt_secret.length) {
2121 return NT_STATUS_NO_MEMORY;
2123 r->out.old_val->buf = talloc(mem_ctx, struct lsa_DATA_BUF);
2124 if (!r->out.old_val->buf) {
2125 return NT_STATUS_NO_MEMORY;
2127 r->out.old_val->buf->size = crypt_secret.length;
2128 r->out.old_val->buf->length = crypt_secret.length;
2129 r->out.old_val->buf->data = crypt_secret.data;
2133 if (r->in.old_mtime) {
2134 r->out.old_mtime = talloc(mem_ctx, NTTIME);
2135 if (!r->out.old_mtime) {
2136 return NT_STATUS_NO_MEMORY;
2138 *r->out.old_mtime = ldb_msg_find_uint64(res[0], "priorSetTime", 0);
2141 if (r->in.new_val) {
2142 const struct ldb_val *new_val;
2143 r->out.new_val = talloc_zero(mem_ctx, struct lsa_DATA_BUF_PTR);
2144 if (!r->out.new_val) {
2145 return NT_STATUS_NO_MEMORY;
2149 new_val = ldb_msg_find_ldb_val(res[0], "secret");
2151 if (new_val && new_val->length) {
2152 secret.data = new_val->data;
2153 secret.length = new_val->length;
2155 crypt_secret = sess_encrypt_blob(mem_ctx, &secret, &session_key);
2156 if (!crypt_secret.length) {
2157 return NT_STATUS_NO_MEMORY;
2159 r->out.new_val->buf = talloc(mem_ctx, struct lsa_DATA_BUF);
2160 if (!r->out.new_val->buf) {
2161 return NT_STATUS_NO_MEMORY;
2163 r->out.new_val->buf->length = crypt_secret.length;
2164 r->out.new_val->buf->size = crypt_secret.length;
2165 r->out.new_val->buf->data = crypt_secret.data;
2169 if (r->in.new_mtime) {
2170 r->out.new_mtime = talloc(mem_ctx, NTTIME);
2171 if (!r->out.new_mtime) {
2172 return NT_STATUS_NO_MEMORY;
2174 *r->out.new_mtime = ldb_msg_find_uint64(res[0], "lastSetTime", 0);
2177 return NT_STATUS_OK;
2184 static NTSTATUS lsa_LookupPrivValue(struct dcesrv_call_state *dce_call,
2185 TALLOC_CTX *mem_ctx,
2186 struct lsa_LookupPrivValue *r)
2188 struct dcesrv_handle *h;
2189 struct lsa_policy_state *state;
2192 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
2196 id = sec_privilege_id(r->in.name->string);
2198 return NT_STATUS_NO_SUCH_PRIVILEGE;
2201 r->out.luid->low = id;
2202 r->out.luid->high = 0;
2204 return NT_STATUS_OK;
2211 static NTSTATUS lsa_LookupPrivName(struct dcesrv_call_state *dce_call,
2212 TALLOC_CTX *mem_ctx,
2213 struct lsa_LookupPrivName *r)
2215 struct dcesrv_handle *h;
2216 struct lsa_policy_state *state;
2217 const char *privname;
2219 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
2223 if (r->in.luid->high != 0) {
2224 return NT_STATUS_NO_SUCH_PRIVILEGE;
2227 privname = sec_privilege_name(r->in.luid->low);
2228 if (privname == NULL) {
2229 return NT_STATUS_NO_SUCH_PRIVILEGE;
2232 r->out.name = talloc(mem_ctx, struct lsa_String);
2233 if (r->out.name == NULL) {
2234 return NT_STATUS_NO_MEMORY;
2236 r->out.name->string = privname;
2238 return NT_STATUS_OK;
2243 lsa_LookupPrivDisplayName
2245 static NTSTATUS lsa_LookupPrivDisplayName(struct dcesrv_call_state *dce_call,
2246 TALLOC_CTX *mem_ctx,
2247 struct lsa_LookupPrivDisplayName *r)
2249 struct dcesrv_handle *h;
2250 struct lsa_policy_state *state;
2253 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
2257 id = sec_privilege_id(r->in.name->string);
2259 return NT_STATUS_NO_SUCH_PRIVILEGE;
2262 r->out.disp_name = talloc(mem_ctx, struct lsa_String);
2263 if (r->out.disp_name == NULL) {
2264 return NT_STATUS_NO_MEMORY;
2267 r->out.disp_name->string = sec_privilege_display_name(id, r->in.language_id);
2268 if (r->out.disp_name->string == NULL) {
2269 return NT_STATUS_INTERNAL_ERROR;
2272 return NT_STATUS_OK;
2279 static NTSTATUS lsa_DeleteObject(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2280 struct lsa_DeleteObject *r)
2282 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2287 lsa_EnumAccountsWithUserRight
2289 static NTSTATUS lsa_EnumAccountsWithUserRight(struct dcesrv_call_state *dce_call,
2290 TALLOC_CTX *mem_ctx,
2291 struct lsa_EnumAccountsWithUserRight *r)
2293 struct dcesrv_handle *h;
2294 struct lsa_policy_state *state;
2296 struct ldb_message **res;
2297 const char * const attrs[] = { "objectSid", NULL};
2298 const char *privname;
2300 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
2304 if (r->in.name == NULL) {
2305 return NT_STATUS_NO_SUCH_PRIVILEGE;
2308 privname = r->in.name->string;
2309 if (sec_privilege_id(privname) == -1) {
2310 return NT_STATUS_NO_SUCH_PRIVILEGE;
2313 ret = gendb_search(state->sam_ldb, mem_ctx, NULL, &res, attrs,
2314 "privilege=%s", privname);
2316 return NT_STATUS_NO_SUCH_USER;
2319 r->out.sids->sids = talloc_array(r->out.sids, struct lsa_SidPtr, ret);
2320 if (r->out.sids->sids == NULL) {
2321 return NT_STATUS_NO_MEMORY;
2323 for (i=0;i<ret;i++) {
2324 r->out.sids->sids[i].sid = samdb_result_dom_sid(r->out.sids->sids,
2325 res[i], "objectSid");
2326 NT_STATUS_HAVE_NO_MEMORY(r->out.sids->sids[i].sid);
2328 r->out.sids->num_sids = ret;
2330 return NT_STATUS_OK;
2335 lsa_AddAccountRights
2337 static NTSTATUS lsa_AddAccountRights(struct dcesrv_call_state *dce_call,
2338 TALLOC_CTX *mem_ctx,
2339 struct lsa_AddAccountRights *r)
2341 struct dcesrv_handle *h;
2342 struct lsa_policy_state *state;
2344 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
2348 return lsa_AddRemoveAccountRights(dce_call, mem_ctx, state,
2350 r->in.sid, r->in.rights);
2355 lsa_RemoveAccountRights
2357 static NTSTATUS lsa_RemoveAccountRights(struct dcesrv_call_state *dce_call,
2358 TALLOC_CTX *mem_ctx,
2359 struct lsa_RemoveAccountRights *r)
2361 struct dcesrv_handle *h;
2362 struct lsa_policy_state *state;
2364 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
2368 return lsa_AddRemoveAccountRights(dce_call, mem_ctx, state,
2369 LDB_FLAG_MOD_DELETE,
2370 r->in.sid, r->in.rights);
2375 lsa_StorePrivateData
2377 static NTSTATUS lsa_StorePrivateData(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2378 struct lsa_StorePrivateData *r)
2380 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2385 lsa_RetrievePrivateData
2387 static NTSTATUS lsa_RetrievePrivateData(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2388 struct lsa_RetrievePrivateData *r)
2390 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2397 static NTSTATUS lsa_GetUserName(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2398 struct lsa_GetUserName *r)
2400 NTSTATUS status = NT_STATUS_OK;
2401 const char *account_name;
2402 const char *authority_name;
2403 struct lsa_String *_account_name;
2404 struct lsa_StringPointer *_authority_name = NULL;
2406 /* this is what w2k3 does */
2407 r->out.account_name = r->in.account_name;
2408 r->out.authority_name = r->in.authority_name;
2410 if (r->in.account_name && r->in.account_name->string) {
2411 return NT_STATUS_INVALID_PARAMETER;
2414 if (r->in.authority_name &&
2415 r->in.authority_name->string &&
2416 r->in.authority_name->string->string) {
2417 return NT_STATUS_INVALID_PARAMETER;
2420 account_name = talloc_reference(mem_ctx, dce_call->conn->auth_state.session_info->server_info->account_name);
2421 authority_name = talloc_reference(mem_ctx, dce_call->conn->auth_state.session_info->server_info->domain_name);
2423 _account_name = talloc(mem_ctx, struct lsa_String);
2424 NTSTATUS_TALLOC_CHECK(_account_name);
2425 _account_name->string = account_name;
2427 if (r->in.authority_name) {
2428 _authority_name = talloc(mem_ctx, struct lsa_StringPointer);
2429 NTSTATUS_TALLOC_CHECK(_authority_name);
2430 _authority_name->string = talloc(mem_ctx, struct lsa_String);
2431 NTSTATUS_TALLOC_CHECK(_authority_name->string);
2432 _authority_name->string->string = authority_name;
2435 r->out.account_name = _account_name;
2436 r->out.authority_name = _authority_name;
2444 static NTSTATUS lsa_SetInfoPolicy2(struct dcesrv_call_state *dce_call,
2445 TALLOC_CTX *mem_ctx,
2446 struct lsa_SetInfoPolicy2 *r)
2448 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2452 lsa_QueryDomainInformationPolicy
2454 static NTSTATUS lsa_QueryDomainInformationPolicy(struct dcesrv_call_state *dce_call,
2455 TALLOC_CTX *mem_ctx,
2456 struct lsa_QueryDomainInformationPolicy *r)
2458 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2462 lsa_SetDomInfoPolicy
2464 static NTSTATUS lsa_SetDomainInformationPolicy(struct dcesrv_call_state *dce_call,
2465 TALLOC_CTX *mem_ctx,
2466 struct lsa_SetDomainInformationPolicy *r)
2468 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2474 static NTSTATUS lsa_TestCall(struct dcesrv_call_state *dce_call,
2475 TALLOC_CTX *mem_ctx,
2476 struct lsa_TestCall *r)
2478 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2482 lookup a SID for 1 name
2484 static NTSTATUS lsa_lookup_name(struct lsa_policy_state *state, TALLOC_CTX *mem_ctx,
2485 const char *name, struct dom_sid **sid, uint32_t *atype)
2488 struct ldb_message **res;
2489 const char * const attrs[] = { "objectSid", "sAMAccountType", NULL};
2492 p = strchr_m(name, '\\');
2494 /* TODO: properly parse the domain prefix here, and use it to
2499 ret = gendb_search(state->sam_ldb, mem_ctx, NULL, &res, attrs, "sAMAccountName=%s", name);
2501 *sid = samdb_result_dom_sid(mem_ctx, res[0], "objectSid");
2503 return NT_STATUS_INVALID_SID;
2506 *atype = samdb_result_uint(res[0], "sAMAccountType", 0);
2508 return NT_STATUS_OK;
2511 /* need to add a call into sidmap to check for a allocated sid */
2513 return NT_STATUS_INVALID_SID;
2520 static NTSTATUS lsa_LookupNames3(struct dcesrv_call_state *dce_call,
2521 TALLOC_CTX *mem_ctx,
2522 struct lsa_LookupNames3 *r)
2524 struct lsa_policy_state *state;
2525 struct dcesrv_handle *h;
2527 NTSTATUS status = NT_STATUS_OK;
2529 r->out.domains = NULL;
2531 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
2535 r->out.domains = talloc_zero(mem_ctx, struct lsa_RefDomainList);
2536 if (r->out.domains == NULL) {
2537 return NT_STATUS_NO_MEMORY;
2540 r->out.sids = talloc_zero(mem_ctx, struct lsa_TransSidArray3);
2541 if (r->out.sids == NULL) {
2542 return NT_STATUS_NO_MEMORY;
2547 r->out.sids->sids = talloc_array(r->out.sids, struct lsa_TranslatedSid3,
2549 if (r->out.sids->sids == NULL) {
2550 return NT_STATUS_NO_MEMORY;
2553 for (i=0;i<r->in.num_names;i++) {
2554 const char *name = r->in.names[i].string;
2555 struct dom_sid *sid;
2556 uint32_t atype, rtype, sid_index;
2559 r->out.sids->count++;
2562 r->out.sids->sids[i].sid_type = SID_NAME_UNKNOWN;
2563 r->out.sids->sids[i].sid = NULL;
2564 r->out.sids->sids[i].sid_index = 0xFFFFFFFF;
2565 r->out.sids->sids[i].unknown = 0;
2567 status2 = lsa_lookup_name(state, mem_ctx, name, &sid, &atype);
2568 if (!NT_STATUS_IS_OK(status2) || sid->num_auths == 0) {
2569 status = STATUS_SOME_UNMAPPED;
2573 rtype = samdb_atype_map(atype);
2574 if (rtype == SID_NAME_UNKNOWN) {
2575 status = STATUS_SOME_UNMAPPED;
2579 status2 = lsa_authority_list(state, mem_ctx, sid, r->out.domains, &sid_index);
2580 if (!NT_STATUS_IS_OK(status2)) {
2584 r->out.sids->sids[i].sid_type = rtype;
2585 r->out.sids->sids[i].sid = sid;
2586 r->out.sids->sids[i].sid_index = sid_index;
2587 r->out.sids->sids[i].unknown = 0;
2596 static NTSTATUS lsa_LookupNames2(struct dcesrv_call_state *dce_call,
2597 TALLOC_CTX *mem_ctx,
2598 struct lsa_LookupNames2 *r)
2600 struct lsa_policy_state *state;
2601 struct dcesrv_handle *h;
2603 NTSTATUS status = NT_STATUS_OK;
2605 r->out.domains = NULL;
2607 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
2611 r->out.domains = talloc_zero(mem_ctx, struct lsa_RefDomainList);
2612 if (r->out.domains == NULL) {
2613 return NT_STATUS_NO_MEMORY;
2616 r->out.sids = talloc_zero(mem_ctx, struct lsa_TransSidArray2);
2617 if (r->out.sids == NULL) {
2618 return NT_STATUS_NO_MEMORY;
2623 r->out.sids->sids = talloc_array(r->out.sids, struct lsa_TranslatedSid2,
2625 if (r->out.sids->sids == NULL) {
2626 return NT_STATUS_NO_MEMORY;
2629 for (i=0;i<r->in.num_names;i++) {
2630 const char *name = r->in.names[i].string;
2631 struct dom_sid *sid;
2632 uint32_t atype, rtype, sid_index;
2635 r->out.sids->count++;
2638 r->out.sids->sids[i].sid_type = SID_NAME_UNKNOWN;
2639 r->out.sids->sids[i].rid = 0xFFFFFFFF;
2640 r->out.sids->sids[i].sid_index = 0xFFFFFFFF;
2641 r->out.sids->sids[i].unknown = 0;
2643 status2 = lsa_lookup_name(state, mem_ctx, name, &sid, &atype);
2644 if (!NT_STATUS_IS_OK(status2) || sid->num_auths == 0) {
2645 status = STATUS_SOME_UNMAPPED;
2649 rtype = samdb_atype_map(atype);
2650 if (rtype == SID_NAME_UNKNOWN) {
2651 status = STATUS_SOME_UNMAPPED;
2655 status2 = lsa_authority_list(state, mem_ctx, sid, r->out.domains, &sid_index);
2656 if (!NT_STATUS_IS_OK(status2)) {
2660 r->out.sids->sids[i].sid_type = rtype;
2661 r->out.sids->sids[i].rid = sid->sub_auths[sid->num_auths-1];
2662 r->out.sids->sids[i].sid_index = sid_index;
2663 r->out.sids->sids[i].unknown = 0;
2672 static NTSTATUS lsa_LookupNames(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2673 struct lsa_LookupNames *r)
2675 struct lsa_LookupNames2 r2;
2679 r2.in.handle = r->in.handle;
2680 r2.in.num_names = r->in.num_names;
2681 r2.in.names = r->in.names;
2683 r2.in.level = r->in.level;
2684 r2.in.count = r->in.count;
2687 r2.out.count = r->out.count;
2689 status = lsa_LookupNames2(dce_call, mem_ctx, &r2);
2690 if (dce_call->fault_code != 0) {
2694 r->out.domains = r2.out.domains;
2695 r->out.sids = talloc(mem_ctx, struct lsa_TransSidArray);
2696 if (r->out.sids == NULL) {
2697 return NT_STATUS_NO_MEMORY;
2699 r->out.sids->count = r2.out.sids->count;
2700 r->out.sids->sids = talloc_array(r->out.sids, struct lsa_TranslatedSid,
2701 r->out.sids->count);
2702 if (r->out.sids->sids == NULL) {
2703 return NT_STATUS_NO_MEMORY;
2705 for (i=0;i<r->out.sids->count;i++) {
2706 r->out.sids->sids[i].sid_type = r2.out.sids->sids[i].sid_type;
2707 r->out.sids->sids[i].rid = r2.out.sids->sids[i].rid;
2708 r->out.sids->sids[i].sid_index = r2.out.sids->sids[i].sid_index;
2717 static NTSTATUS lsa_CREDRWRITE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2718 struct lsa_CREDRWRITE *r)
2720 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2727 static NTSTATUS lsa_CREDRREAD(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2728 struct lsa_CREDRREAD *r)
2730 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2737 static NTSTATUS lsa_CREDRENUMERATE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2738 struct lsa_CREDRENUMERATE *r)
2740 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2745 lsa_CREDRWRITEDOMAINCREDENTIALS
2747 static NTSTATUS lsa_CREDRWRITEDOMAINCREDENTIALS(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2748 struct lsa_CREDRWRITEDOMAINCREDENTIALS *r)
2750 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2755 lsa_CREDRREADDOMAINCREDENTIALS
2757 static NTSTATUS lsa_CREDRREADDOMAINCREDENTIALS(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2758 struct lsa_CREDRREADDOMAINCREDENTIALS *r)
2760 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2767 static NTSTATUS lsa_CREDRDELETE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2768 struct lsa_CREDRDELETE *r)
2770 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2775 lsa_CREDRGETTARGETINFO
2777 static NTSTATUS lsa_CREDRGETTARGETINFO(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2778 struct lsa_CREDRGETTARGETINFO *r)
2780 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2785 lsa_CREDRPROFILELOADED
2787 static NTSTATUS lsa_CREDRPROFILELOADED(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2788 struct lsa_CREDRPROFILELOADED *r)
2790 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2795 lsa_CREDRGETSESSIONTYPES
2797 static NTSTATUS lsa_CREDRGETSESSIONTYPES(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2798 struct lsa_CREDRGETSESSIONTYPES *r)
2800 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2805 lsa_LSARREGISTERAUDITEVENT
2807 static NTSTATUS lsa_LSARREGISTERAUDITEVENT(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2808 struct lsa_LSARREGISTERAUDITEVENT *r)
2810 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2815 lsa_LSARGENAUDITEVENT
2817 static NTSTATUS lsa_LSARGENAUDITEVENT(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2818 struct lsa_LSARGENAUDITEVENT *r)
2820 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2825 lsa_LSARUNREGISTERAUDITEVENT
2827 static NTSTATUS lsa_LSARUNREGISTERAUDITEVENT(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2828 struct lsa_LSARUNREGISTERAUDITEVENT *r)
2830 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2835 lsa_LSARQUERYFORESTTRUSTINFORMATION
2837 static NTSTATUS lsa_LSARQUERYFORESTTRUSTINFORMATION(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2838 struct lsa_LSARQUERYFORESTTRUSTINFORMATION *r)
2840 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2845 lsa_LSARSETFORESTTRUSTINFORMATION
2847 static NTSTATUS lsa_LSARSETFORESTTRUSTINFORMATION(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2848 struct lsa_LSARSETFORESTTRUSTINFORMATION *r)
2850 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2857 static NTSTATUS lsa_CREDRRENAME(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2858 struct lsa_CREDRRENAME *r)
2860 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2865 lsa_LSARLOOKUPNAMES4
2867 static NTSTATUS lsa_LSARLOOKUPNAMES4(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2868 struct lsa_LSARLOOKUPNAMES4 *r)
2870 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2875 lsa_LSAROPENPOLICYSCE
2877 static NTSTATUS lsa_LSAROPENPOLICYSCE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2878 struct lsa_LSAROPENPOLICYSCE *r)
2880 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2885 lsa_LSARADTREGISTERSECURITYEVENTSOURCE
2887 static NTSTATUS lsa_LSARADTREGISTERSECURITYEVENTSOURCE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2888 struct lsa_LSARADTREGISTERSECURITYEVENTSOURCE *r)
2890 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2895 lsa_LSARADTUNREGISTERSECURITYEVENTSOURCE
2897 static NTSTATUS lsa_LSARADTUNREGISTERSECURITYEVENTSOURCE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2898 struct lsa_LSARADTUNREGISTERSECURITYEVENTSOURCE *r)
2900 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2905 lsa_LSARADTREPORTSECURITYEVENT
2907 static NTSTATUS lsa_LSARADTREPORTSECURITYEVENT(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2908 struct lsa_LSARADTREPORTSECURITYEVENT *r)
2910 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2914 /* include the generated boilerplate */
2915 #include "librpc/gen_ndr/ndr_lsa_s.c"