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 "auth/auth.h"
31 #include "system/time.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;
223 const char *domain_attrs[] = {"nETBIOSName", "nCName", NULL};
225 struct ldb_message **msgs_domain;
227 state = talloc(mem_ctx, struct lsa_policy_state);
229 return NT_STATUS_NO_MEMORY;
232 /* make sure the sam database is accessible */
233 state->sam_ldb = samdb_connect(state);
234 if (state->sam_ldb == NULL) {
235 return NT_STATUS_INVALID_SYSTEM_SERVICE;
238 state->sidmap = sidmap_open(state);
239 if (state->sidmap == NULL) {
240 return NT_STATUS_INVALID_SYSTEM_SERVICE;
243 ret_domain = gendb_search(state->sam_ldb, mem_ctx, NULL, &msgs_domain, domain_attrs,
244 "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))",
247 if (ret_domain == -1) {
248 return NT_STATUS_INTERNAL_DB_CORRUPTION;
251 if (ret_domain != 1) {
252 return NT_STATUS_NO_SUCH_DOMAIN;
255 /* work out the domain_dn - useful for so many calls its worth
257 state->domain_dn = samdb_result_dn(state, msgs_domain[0], "nCName", NULL);
258 if (!state->domain_dn) {
259 return NT_STATUS_NO_SUCH_DOMAIN;
262 /* work out the builtin_dn - useful for so many calls its worth
264 state->builtin_dn = samdb_search_dn(state->sam_ldb, mem_ctx, NULL, "objectClass=builtinDomain");
265 if (!state->builtin_dn) {
266 return NT_STATUS_NO_SUCH_DOMAIN;
269 /* work out the system_dn - useful for so many calls its worth
271 state->system_dn = samdb_search_dn(state->sam_ldb, mem_ctx,
272 state->domain_dn, "(&(objectClass=container)(cn=System))");
273 if (!state->system_dn) {
274 return NT_STATUS_NO_SUCH_DOMAIN;
277 state->domain_sid = talloc_steal(state,
278 samdb_search_dom_sid(state->sam_ldb, state,
279 state->domain_dn, "objectSid", "dn=%s",
280 ldb_dn_linearize(mem_ctx, state->domain_dn)));
281 if (!state->domain_sid) {
282 return NT_STATUS_NO_SUCH_DOMAIN;
285 state->builtin_sid = dom_sid_parse_talloc(state, SID_BUILTIN);
286 if (!state->builtin_sid) {
287 return NT_STATUS_NO_SUCH_DOMAIN;
290 state->domain_name = talloc_strdup(state,
291 samdb_result_string(msgs_domain[0], "nETBIOSName",
302 static NTSTATUS lsa_OpenPolicy2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
303 struct lsa_OpenPolicy2 *r)
306 struct lsa_policy_state *state;
307 struct dcesrv_handle *handle;
309 ZERO_STRUCTP(r->out.handle);
311 status = lsa_get_policy_state(dce_call, mem_ctx, &state);
312 if (!NT_STATUS_IS_OK(status)) {
316 handle = dcesrv_handle_new(dce_call->context, LSA_HANDLE_POLICY);
318 return NT_STATUS_NO_MEMORY;
321 handle->data = talloc_steal(handle, state);
323 state->access_mask = r->in.access_mask;
324 state->handle = handle;
325 *r->out.handle = handle->wire_handle;
327 /* note that we have completely ignored the attr element of
328 the OpenPolicy. As far as I can tell, this is what w2k3
336 a wrapper around lsa_OpenPolicy2
338 static NTSTATUS lsa_OpenPolicy(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
339 struct lsa_OpenPolicy *r)
341 struct lsa_OpenPolicy2 r2;
343 r2.in.system_name = NULL;
344 r2.in.attr = r->in.attr;
345 r2.in.access_mask = r->in.access_mask;
346 r2.out.handle = r->out.handle;
348 return lsa_OpenPolicy2(dce_call, mem_ctx, &r2);
355 fill in the AccountDomain info
357 static NTSTATUS lsa_info_AccountDomain(struct lsa_policy_state *state, TALLOC_CTX *mem_ctx,
358 struct lsa_DomainInfo *info)
360 info->name.string = state->domain_name;
361 info->sid = state->domain_sid;
367 fill in the DNS domain info
369 static NTSTATUS lsa_info_DNS(struct lsa_policy_state *state, TALLOC_CTX *mem_ctx,
370 struct lsa_DnsDomainInfo *info)
372 const char * const attrs[] = { "dnsDomain", "objectGUID", "objectSid", NULL };
374 struct ldb_message **res;
376 ret = gendb_search_dn(state->sam_ldb, mem_ctx, state->domain_dn, &res, attrs);
378 return NT_STATUS_INTERNAL_DB_CORRUPTION;
381 info->name.string = state->domain_name;
382 info->sid = state->domain_sid;
383 info->dns_domain.string = samdb_result_string(res[0], "dnsDomain", NULL);
384 info->dns_forest.string = samdb_result_string(res[0], "dnsDomain", NULL);
385 info->domain_guid = samdb_result_guid(res[0], "objectGUID");
393 static NTSTATUS lsa_QueryInfoPolicy2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
394 struct lsa_QueryInfoPolicy2 *r)
396 struct lsa_policy_state *state;
397 struct dcesrv_handle *h;
401 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
405 r->out.info = talloc(mem_ctx, union lsa_PolicyInformation);
407 return NT_STATUS_NO_MEMORY;
410 ZERO_STRUCTP(r->out.info);
412 switch (r->in.level) {
413 case LSA_POLICY_INFO_DOMAIN:
414 case LSA_POLICY_INFO_ACCOUNT_DOMAIN:
415 return lsa_info_AccountDomain(state, mem_ctx, &r->out.info->account_domain);
417 case LSA_POLICY_INFO_DNS:
418 return lsa_info_DNS(state, mem_ctx, &r->out.info->dns);
421 return NT_STATUS_INVALID_INFO_CLASS;
427 static NTSTATUS lsa_QueryInfoPolicy(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
428 struct lsa_QueryInfoPolicy *r)
430 struct lsa_QueryInfoPolicy2 r2;
433 r2.in.handle = r->in.handle;
434 r2.in.level = r->in.level;
436 status = lsa_QueryInfoPolicy2(dce_call, mem_ctx, &r2);
438 r->out.info = r2.out.info;
446 static NTSTATUS lsa_SetInfoPolicy(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
447 struct lsa_SetInfoPolicy *r)
449 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
456 static NTSTATUS lsa_ClearAuditLog(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
457 struct lsa_ClearAuditLog *r)
459 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
466 static NTSTATUS lsa_CreateAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
467 struct lsa_CreateAccount *r)
469 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
476 static NTSTATUS lsa_EnumAccounts(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
477 struct lsa_EnumAccounts *r)
479 struct dcesrv_handle *h;
480 struct lsa_policy_state *state;
482 struct ldb_message **res;
483 const char * const attrs[] = { "objectSid", NULL};
486 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
490 ret = gendb_search(state->sam_ldb, mem_ctx, state->builtin_dn, &res, attrs,
493 return NT_STATUS_NO_SUCH_USER;
496 if (*r->in.resume_handle >= ret) {
497 return NT_STATUS_NO_MORE_ENTRIES;
500 count = ret - *r->in.resume_handle;
501 if (count > r->in.num_entries) {
502 count = r->in.num_entries;
506 return NT_STATUS_NO_MORE_ENTRIES;
509 r->out.sids->sids = talloc_array(r->out.sids, struct lsa_SidPtr, count);
510 if (r->out.sids->sids == NULL) {
511 return NT_STATUS_NO_MEMORY;
514 for (i=0;i<count;i++) {
515 r->out.sids->sids[i].sid =
516 samdb_result_dom_sid(r->out.sids->sids,
517 res[i + *r->in.resume_handle],
519 NT_STATUS_HAVE_NO_MEMORY(r->out.sids->sids[i].sid);
522 r->out.sids->num_sids = count;
523 *r->out.resume_handle = count + *r->in.resume_handle;
531 lsa_CreateTrustedDomainEx2
533 static NTSTATUS lsa_CreateTrustedDomainEx2(struct dcesrv_call_state *dce_call,
535 struct lsa_CreateTrustedDomainEx2 *r)
537 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
541 lsa_CreateTrustedDomainEx
543 static NTSTATUS lsa_CreateTrustedDomainEx(struct dcesrv_call_state *dce_call,
545 struct lsa_CreateTrustedDomainEx *r)
547 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
551 lsa_CreateTrustedDomain
553 static NTSTATUS lsa_CreateTrustedDomain(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
554 struct lsa_CreateTrustedDomain *r)
556 struct dcesrv_handle *policy_handle;
557 struct lsa_policy_state *policy_state;
558 struct lsa_trusted_domain_state *trusted_domain_state;
559 struct dcesrv_handle *handle;
560 struct ldb_message **msgs, *msg;
561 const char *attrs[] = {
567 DCESRV_PULL_HANDLE(policy_handle, r->in.handle, LSA_HANDLE_POLICY);
568 ZERO_STRUCTP(r->out.trustdom_handle);
570 policy_state = policy_handle->data;
572 if (!r->in.info->name.string) {
573 return NT_STATUS_INVALID_PARAMETER;
575 name = r->in.info->name.string;
577 trusted_domain_state = talloc(mem_ctx, struct lsa_trusted_domain_state);
578 if (!trusted_domain_state) {
579 return NT_STATUS_NO_MEMORY;
581 trusted_domain_state->policy = policy_state;
583 msg = ldb_msg_new(mem_ctx);
585 return NT_STATUS_NO_MEMORY;
588 /* search for the trusted_domain record */
589 ret = gendb_search(trusted_domain_state->policy->sam_ldb,
590 mem_ctx, policy_state->system_dn, &msgs, attrs,
591 "(&(cn=%s)(objectclass=trustedDomain))",
592 r->in.info->name.string);
594 return NT_STATUS_OBJECT_NAME_COLLISION;
597 if (ret < 0 || ret > 1) {
598 DEBUG(0,("Found %d records matching DN %s\n", ret,
599 ldb_dn_linearize(mem_ctx, policy_state->system_dn)));
600 return NT_STATUS_INTERNAL_DB_CORRUPTION;
603 msg->dn = ldb_dn_build_child(mem_ctx, "cn",
604 r->in.info->name.string,
605 policy_state->system_dn);
607 return NT_STATUS_NO_MEMORY;
610 samdb_msg_add_string(trusted_domain_state->policy->sam_ldb, mem_ctx, msg, "cn", name);
611 samdb_msg_add_string(trusted_domain_state->policy->sam_ldb, mem_ctx, msg, "flatname", name);
613 if (r->in.info->sid) {
614 const char *sid_string = dom_sid_string(mem_ctx, r->in.info->sid);
616 return NT_STATUS_NO_MEMORY;
619 samdb_msg_add_string(trusted_domain_state->policy->sam_ldb, mem_ctx, msg, "securityIdentifier", sid_string);
622 samdb_msg_add_string(trusted_domain_state->policy->sam_ldb, mem_ctx, msg, "objectClass", "trustedDomain");
624 trusted_domain_state->trusted_domain_dn = talloc_reference(trusted_domain_state, msg->dn);
626 /* create the trusted_domain */
627 ret = samdb_add(trusted_domain_state->policy->sam_ldb, mem_ctx, msg);
629 DEBUG(0,("Failed to create trusted_domain record %s\n",
630 ldb_dn_linearize(mem_ctx, msg->dn)));
631 return NT_STATUS_INTERNAL_DB_CORRUPTION;
634 handle = dcesrv_handle_new(dce_call->context, LSA_HANDLE_TRUSTED_DOMAIN);
636 return NT_STATUS_NO_MEMORY;
639 handle->data = talloc_steal(handle, trusted_domain_state);
641 trusted_domain_state->access_mask = r->in.access_mask;
642 trusted_domain_state->policy = talloc_reference(trusted_domain_state, policy_state);
644 *r->out.trustdom_handle = handle->wire_handle;
650 lsa_OpenTrustedDomain
652 static NTSTATUS lsa_OpenTrustedDomain(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
653 struct lsa_OpenTrustedDomain *r)
655 struct dcesrv_handle *policy_handle;
657 struct lsa_policy_state *policy_state;
658 struct lsa_trusted_domain_state *trusted_domain_state;
659 struct dcesrv_handle *handle;
660 struct ldb_message **msgs;
661 const char *attrs[] = {
665 const char *sid_string;
668 DCESRV_PULL_HANDLE(policy_handle, r->in.handle, LSA_HANDLE_POLICY);
669 ZERO_STRUCTP(r->out.trustdom_handle);
670 policy_state = policy_handle->data;
672 trusted_domain_state = talloc(mem_ctx, struct lsa_trusted_domain_state);
673 if (!trusted_domain_state) {
674 return NT_STATUS_NO_MEMORY;
676 trusted_domain_state->policy = policy_state;
678 sid_string = dom_sid_string(mem_ctx, r->in.sid);
680 return NT_STATUS_NO_MEMORY;
683 /* search for the trusted_domain record */
684 ret = gendb_search(trusted_domain_state->policy->sam_ldb,
685 mem_ctx, policy_state->system_dn, &msgs, attrs,
686 "(&(securityIdentifier=%s)(objectclass=trustedDomain))",
689 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
693 DEBUG(0,("Found %d records matching DN %s\n", ret,
694 ldb_dn_linearize(mem_ctx, policy_state->system_dn)));
695 return NT_STATUS_INTERNAL_DB_CORRUPTION;
698 trusted_domain_state->trusted_domain_dn = talloc_reference(trusted_domain_state, msgs[0]->dn);
700 handle = dcesrv_handle_new(dce_call->context, LSA_HANDLE_TRUSTED_DOMAIN);
702 return NT_STATUS_NO_MEMORY;
705 handle->data = talloc_steal(handle, trusted_domain_state);
707 trusted_domain_state->access_mask = r->in.access_mask;
708 trusted_domain_state->policy = talloc_reference(trusted_domain_state, policy_state);
710 *r->out.trustdom_handle = handle->wire_handle;
717 lsa_OpenTrustedDomainByName
719 static NTSTATUS lsa_OpenTrustedDomainByName(struct dcesrv_call_state *dce_call,
721 struct lsa_OpenTrustedDomainByName *r)
723 struct dcesrv_handle *policy_handle;
725 struct lsa_policy_state *policy_state;
726 struct lsa_trusted_domain_state *trusted_domain_state;
727 struct dcesrv_handle *handle;
728 struct ldb_message **msgs;
729 const char *attrs[] = {
735 DCESRV_PULL_HANDLE(policy_handle, r->in.handle, LSA_HANDLE_POLICY);
736 ZERO_STRUCTP(r->out.trustdom_handle);
737 policy_state = policy_handle->data;
739 if (!r->in.name.string) {
740 return NT_STATUS_INVALID_PARAMETER;
743 trusted_domain_state = talloc(mem_ctx, struct lsa_trusted_domain_state);
744 if (!trusted_domain_state) {
745 return NT_STATUS_NO_MEMORY;
747 trusted_domain_state->policy = policy_state;
749 /* search for the trusted_domain record */
750 ret = gendb_search(trusted_domain_state->policy->sam_ldb,
751 mem_ctx, policy_state->system_dn, &msgs, attrs,
752 "(&(flatname=%s)(objectclass=trustedDomain))",
755 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
759 DEBUG(0,("Found %d records matching DN %s\n", ret,
760 ldb_dn_linearize(mem_ctx, policy_state->system_dn)));
761 return NT_STATUS_INTERNAL_DB_CORRUPTION;
764 trusted_domain_state->trusted_domain_dn = talloc_reference(trusted_domain_state, msgs[0]->dn);
766 handle = dcesrv_handle_new(dce_call->context, LSA_HANDLE_TRUSTED_DOMAIN);
768 return NT_STATUS_NO_MEMORY;
771 handle->data = talloc_steal(handle, trusted_domain_state);
773 trusted_domain_state->access_mask = r->in.access_mask;
774 trusted_domain_state->policy = talloc_reference(trusted_domain_state, policy_state);
776 *r->out.trustdom_handle = handle->wire_handle;
783 lsa_QueryTrustedDomainInfoBySid
785 static NTSTATUS lsa_QueryTrustedDomainInfoBySid(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
786 struct lsa_QueryTrustedDomainInfoBySid *r)
788 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
793 lsa_SetTrustDomainInfo
795 static NTSTATUS lsa_SetTrustDomainInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
796 struct lsa_SetTrustDomainInfo *r)
798 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
803 lsa_DeleteTrustDomain
805 static NTSTATUS lsa_DeleteTrustDomain(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
806 struct lsa_DeleteTrustDomain *r)
808 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
813 lsa_QueryTrustedDomainInfo
815 static NTSTATUS lsa_QueryTrustedDomainInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
816 struct lsa_QueryTrustedDomainInfo *r)
818 struct dcesrv_handle *h;
819 struct lsa_trusted_domain_state *trusted_domain_state;
820 struct ldb_message *msg;
822 struct ldb_message **res;
823 const char *attrs[] = {
827 "securityIdentifier",
831 DCESRV_PULL_HANDLE(h, r->in.trustdom_handle, LSA_HANDLE_TRUSTED_DOMAIN);
833 trusted_domain_state = h->data;
835 /* pull all the user attributes */
836 ret = gendb_search_dn(trusted_domain_state->policy->sam_ldb, mem_ctx,
837 trusted_domain_state->trusted_domain_dn, &res, attrs);
839 return NT_STATUS_INTERNAL_DB_CORRUPTION;
843 r->out.info = talloc(mem_ctx, union lsa_TrustedDomainInfo);
845 return NT_STATUS_NO_MEMORY;
847 switch (r->in.level) {
848 case LSA_TRUSTED_DOMAIN_INFO_NAME:
849 r->out.info->name.netbios_name.string
850 = samdb_result_string(msg, "flatname", NULL);
852 case LSA_TRUSTED_DOMAIN_INFO_POSIX_OFFSET:
853 r->out.info->posix_offset.posix_offset
854 = samdb_result_uint(msg, "posixOffset", 0);
857 /* oops, we don't want to return the info after all */
858 talloc_free(r->out.info);
860 return NT_STATUS_INVALID_INFO_CLASS;
868 lsa_SetInformationTrustedDomain
870 static NTSTATUS lsa_SetInformationTrustedDomain(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
871 struct lsa_SetInformationTrustedDomain *r)
873 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
878 lsa_QueryTrustedDomainInfoByName
880 static NTSTATUS lsa_QueryTrustedDomainInfoByName(struct dcesrv_call_state *dce_call,
882 struct lsa_QueryTrustedDomainInfoByName *r)
884 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
888 lsa_SetTrustedDomainInfoByName
890 static NTSTATUS lsa_SetTrustedDomainInfoByName(struct dcesrv_call_state *dce_call,
892 struct lsa_SetTrustedDomainInfoByName *r)
894 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
898 lsa_EnumTrustedDomainsEx
900 static NTSTATUS lsa_EnumTrustedDomainsEx(struct dcesrv_call_state *dce_call,
902 struct lsa_EnumTrustedDomainsEx *r)
904 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
908 lsa_CloseTrustedDomainEx
910 static NTSTATUS lsa_CloseTrustedDomainEx(struct dcesrv_call_state *dce_call,
912 struct lsa_CloseTrustedDomainEx *r)
914 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
919 comparison function for sorting lsa_DomainInformation array
921 static int compare_DomainInformation(struct lsa_DomainInformation *e1, struct lsa_DomainInformation *e2)
923 return strcasecmp(e1->name.string, e2->name.string);
929 static NTSTATUS lsa_EnumTrustDom(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
930 struct lsa_EnumTrustDom *r)
932 struct dcesrv_handle *policy_handle;
933 struct lsa_DomainInformation *entries;
934 struct lsa_policy_state *policy_state;
935 struct ldb_message **domains;
936 const char *attrs[] = {
938 "securityIdentifier",
945 *r->out.resume_handle = 0;
947 r->out.domains->domains = NULL;
948 r->out.domains->count = 0;
950 DCESRV_PULL_HANDLE(policy_handle, r->in.handle, LSA_HANDLE_POLICY);
952 policy_state = policy_handle->data;
954 /* search for all users in this domain. This could possibly be cached and
955 resumed based on resume_key */
956 count = gendb_search(policy_state->sam_ldb, mem_ctx, policy_state->system_dn, &domains, attrs,
957 "objectclass=trustedDomain");
959 return NT_STATUS_INTERNAL_DB_CORRUPTION;
961 if (count == 0 || r->in.max_size == 0) {
965 /* convert to lsa_DomainInformation format */
966 entries = talloc_array(mem_ctx, struct lsa_DomainInformation, count);
968 return NT_STATUS_NO_MEMORY;
970 for (i=0;i<count;i++) {
971 entries[i].sid = samdb_result_dom_sid(mem_ctx, domains[i], "securityIdentifier");
972 entries[i].name.string = samdb_result_string(domains[i], "flatname", NULL);
975 /* sort the results by name */
976 qsort(entries, count, sizeof(struct lsa_DomainInformation),
977 (comparison_fn_t)compare_DomainInformation);
979 if (*r->in.resume_handle >= count) {
980 *r->out.resume_handle = -1;
982 return NT_STATUS_NO_MORE_ENTRIES;
985 /* return the rest, limit by max_size. Note that we
986 use the w2k3 element size value of 60 */
987 r->out.domains->count = count - *r->in.resume_handle;
988 r->out.domains->count = MIN(r->out.domains->count,
989 1+(r->in.max_size/LSA_ENUM_TRUST_DOMAIN_MULTIPLIER));
991 r->out.domains->domains = entries + *r->in.resume_handle;
992 r->out.domains->count = r->out.domains->count;
994 if (r->out.domains->count < count - *r->in.resume_handle) {
995 *r->out.resume_handle = *r->in.resume_handle + r->out.domains->count;
996 return STATUS_MORE_ENTRIES;
1004 return the authority name and authority sid, given a sid
1006 static NTSTATUS lsa_authority_name(struct lsa_policy_state *state,
1007 TALLOC_CTX *mem_ctx, struct dom_sid *sid,
1008 const char **authority_name,
1009 struct dom_sid **authority_sid)
1011 if (dom_sid_in_domain(state->domain_sid, sid)) {
1012 *authority_name = state->domain_name;
1013 *authority_sid = state->domain_sid;
1014 return NT_STATUS_OK;
1017 if (dom_sid_in_domain(state->builtin_sid, sid)) {
1018 *authority_name = "BUILTIN";
1019 *authority_sid = state->builtin_sid;
1020 return NT_STATUS_OK;
1023 *authority_sid = dom_sid_dup(mem_ctx, sid);
1024 if (*authority_sid == NULL) {
1025 return NT_STATUS_NO_MEMORY;
1027 (*authority_sid)->num_auths = 0;
1028 *authority_name = dom_sid_string(mem_ctx, *authority_sid);
1029 if (*authority_name == NULL) {
1030 return NT_STATUS_NO_MEMORY;
1033 return NT_STATUS_OK;
1037 add to the lsa_RefDomainList for LookupSids and LookupNames
1039 static NTSTATUS lsa_authority_list(struct lsa_policy_state *state, TALLOC_CTX *mem_ctx,
1040 struct dom_sid *sid,
1041 struct lsa_RefDomainList *domains,
1042 uint32_t *sid_index)
1045 const char *authority_name;
1046 struct dom_sid *authority_sid;
1049 /* work out the authority name */
1050 status = lsa_authority_name(state, mem_ctx, sid,
1051 &authority_name, &authority_sid);
1052 if (!NT_STATUS_IS_OK(status)) {
1056 /* see if we've already done this authority name */
1057 for (i=0;i<domains->count;i++) {
1058 if (strcmp(authority_name, domains->domains[i].name.string) == 0) {
1060 return NT_STATUS_OK;
1064 domains->domains = talloc_realloc(domains,
1066 struct lsa_TrustInformation,
1068 if (domains->domains == NULL) {
1069 return NT_STATUS_NO_MEMORY;
1071 domains->domains[i].name.string = authority_name;
1072 domains->domains[i].sid = authority_sid;
1076 return NT_STATUS_OK;
1080 lookup a name for 1 SID
1082 static NTSTATUS lsa_lookup_sid(struct lsa_policy_state *state, TALLOC_CTX *mem_ctx,
1083 struct dom_sid *sid, const char *sid_str,
1084 const char **name, uint32_t *atype)
1087 struct ldb_message **res;
1088 const char * const attrs[] = { "sAMAccountName", "sAMAccountType", "name", NULL};
1091 ret = gendb_search(state->sam_ldb, mem_ctx, NULL, &res, attrs,
1092 "objectSid=%s", ldap_encode_ndr_dom_sid(mem_ctx, sid));
1094 *name = ldb_msg_find_string(res[0], "sAMAccountName", NULL);
1096 *name = ldb_msg_find_string(res[0], "name", NULL);
1098 *name = talloc_strdup(mem_ctx, sid_str);
1099 NTSTATUS_TALLOC_CHECK(*name);
1103 *atype = samdb_result_uint(res[0], "sAMAccountType", 0);
1105 return NT_STATUS_OK;
1108 status = sidmap_allocated_sid_lookup(state->sidmap, mem_ctx, sid, name, atype);
1117 static NTSTATUS lsa_LookupSids3(struct dcesrv_call_state *dce_call,
1118 TALLOC_CTX *mem_ctx,
1119 struct lsa_LookupSids3 *r)
1121 struct lsa_policy_state *state;
1123 NTSTATUS status = NT_STATUS_OK;
1125 r->out.domains = NULL;
1127 status = lsa_get_policy_state(dce_call, mem_ctx, &state);
1128 if (!NT_STATUS_IS_OK(status)) {
1132 r->out.domains = talloc_zero(mem_ctx, struct lsa_RefDomainList);
1133 if (r->out.domains == NULL) {
1134 return NT_STATUS_NO_MEMORY;
1137 r->out.names = talloc_zero(mem_ctx, struct lsa_TransNameArray2);
1138 if (r->out.names == NULL) {
1139 return NT_STATUS_NO_MEMORY;
1144 r->out.names->names = talloc_array(r->out.names, struct lsa_TranslatedName2,
1145 r->in.sids->num_sids);
1146 if (r->out.names->names == NULL) {
1147 return NT_STATUS_NO_MEMORY;
1150 for (i=0;i<r->in.sids->num_sids;i++) {
1151 struct dom_sid *sid = r->in.sids->sids[i].sid;
1152 char *sid_str = dom_sid_string(mem_ctx, sid);
1154 uint32_t atype, rtype, sid_index;
1157 r->out.names->count++;
1160 r->out.names->names[i].sid_type = SID_NAME_UNKNOWN;
1161 r->out.names->names[i].name.string = sid_str;
1162 r->out.names->names[i].sid_index = 0xFFFFFFFF;
1163 r->out.names->names[i].unknown = 0;
1165 if (sid_str == NULL) {
1166 r->out.names->names[i].name.string = "(SIDERROR)";
1167 status = STATUS_SOME_UNMAPPED;
1171 /* work out the authority name */
1172 status2 = lsa_authority_list(state, mem_ctx, sid, r->out.domains, &sid_index);
1173 if (!NT_STATUS_IS_OK(status2)) {
1177 status2 = lsa_lookup_sid(state, mem_ctx, sid, sid_str,
1179 if (!NT_STATUS_IS_OK(status2)) {
1180 status = STATUS_SOME_UNMAPPED;
1184 rtype = samdb_atype_map(atype);
1185 if (rtype == SID_NAME_UNKNOWN) {
1186 status = STATUS_SOME_UNMAPPED;
1190 r->out.names->names[i].sid_type = rtype;
1191 r->out.names->names[i].name.string = name;
1192 r->out.names->names[i].sid_index = sid_index;
1193 r->out.names->names[i].unknown = 0;
1203 static NTSTATUS lsa_LookupSids2(struct dcesrv_call_state *dce_call,
1204 TALLOC_CTX *mem_ctx,
1205 struct lsa_LookupSids2 *r)
1207 struct lsa_LookupSids3 r3;
1210 r3.in.sids = r->in.sids;
1211 r3.in.names = r->in.names;
1212 r3.in.level = r->in.level;
1213 r3.in.count = r->in.count;
1214 r3.in.unknown1 = r->in.unknown1;
1215 r3.in.unknown2 = r->in.unknown2;
1216 r3.out.count = r->out.count;
1217 r3.out.names = r->out.names;
1219 status = lsa_LookupSids3(dce_call, mem_ctx, &r3);
1220 if (dce_call->fault_code != 0) {
1224 r->out.domains = r3.out.domains;
1225 r->out.names = r3.out.names;
1226 r->out.count = r3.out.count;
1235 static NTSTATUS lsa_LookupSids(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1236 struct lsa_LookupSids *r)
1238 struct lsa_LookupSids3 r3;
1242 r3.in.sids = r->in.sids;
1244 r3.in.level = r->in.level;
1245 r3.in.count = r->in.count;
1248 r3.out.count = r->out.count;
1250 status = lsa_LookupSids3(dce_call, mem_ctx, &r3);
1251 if (dce_call->fault_code != 0) {
1255 r->out.domains = r3.out.domains;
1256 r->out.names = talloc(mem_ctx, struct lsa_TransNameArray);
1257 if (r->out.names == NULL) {
1258 return NT_STATUS_NO_MEMORY;
1260 r->out.names->count = r3.out.names->count;
1261 r->out.names->names = talloc_array(r->out.names, struct lsa_TranslatedName,
1262 r->out.names->count);
1263 if (r->out.names->names == NULL) {
1264 return NT_STATUS_NO_MEMORY;
1266 for (i=0;i<r->out.names->count;i++) {
1267 r->out.names->names[i].sid_type = r3.out.names->names[i].sid_type;
1268 r->out.names->names[i].name.string = r3.out.names->names[i].name.string;
1269 r->out.names->names[i].sid_index = r3.out.names->names[i].sid_index;
1279 static NTSTATUS lsa_OpenAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1280 struct lsa_OpenAccount *r)
1282 struct dcesrv_handle *h, *ah;
1283 struct lsa_policy_state *state;
1284 struct lsa_account_state *astate;
1286 ZERO_STRUCTP(r->out.acct_handle);
1288 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
1292 astate = talloc(dce_call->conn, struct lsa_account_state);
1293 if (astate == NULL) {
1294 return NT_STATUS_NO_MEMORY;
1297 astate->account_sid = dom_sid_dup(astate, r->in.sid);
1298 if (astate->account_sid == NULL) {
1299 talloc_free(astate);
1300 return NT_STATUS_NO_MEMORY;
1303 /* check it really exists */
1304 astate->account_dn = samdb_search_string(state->sam_ldb, astate,
1305 NULL, "(&(objectSid=%s)(objectClass=group))",
1306 ldap_encode_ndr_dom_sid(mem_ctx, astate->account_sid));
1307 if (astate->account_dn == NULL) {
1308 talloc_free(astate);
1309 return NT_STATUS_NO_SUCH_USER;
1312 astate->policy = talloc_reference(astate, state);
1313 astate->access_mask = r->in.access_mask;
1315 ah = dcesrv_handle_new(dce_call->context, LSA_HANDLE_ACCOUNT);
1317 talloc_free(astate);
1318 return NT_STATUS_NO_MEMORY;
1321 ah->data = talloc_steal(ah, astate);
1323 *r->out.acct_handle = ah->wire_handle;
1325 return NT_STATUS_OK;
1330 lsa_EnumPrivsAccount
1332 static NTSTATUS lsa_EnumPrivsAccount(struct dcesrv_call_state *dce_call,
1333 TALLOC_CTX *mem_ctx,
1334 struct lsa_EnumPrivsAccount *r)
1336 struct dcesrv_handle *h;
1337 struct lsa_account_state *astate;
1339 struct ldb_message **res;
1340 const char * const attrs[] = { "privilege", NULL};
1341 struct ldb_message_element *el;
1343 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_ACCOUNT);
1347 r->out.privs = talloc(mem_ctx, struct lsa_PrivilegeSet);
1348 r->out.privs->count = 0;
1349 r->out.privs->unknown = 0;
1350 r->out.privs->set = NULL;
1352 ret = gendb_search_dn(astate->policy->sam_ldb, mem_ctx,
1353 astate->account_dn, &res, attrs);
1355 return NT_STATUS_OK;
1358 el = ldb_msg_find_element(res[0], "privilege");
1359 if (el == NULL || el->num_values == 0) {
1360 return NT_STATUS_OK;
1363 r->out.privs->set = talloc_array(r->out.privs,
1364 struct lsa_LUIDAttribute, el->num_values);
1365 if (r->out.privs->set == NULL) {
1366 return NT_STATUS_NO_MEMORY;
1369 for (i=0;i<el->num_values;i++) {
1370 int id = sec_privilege_id((const char *)el->values[i].data);
1372 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1374 r->out.privs->set[i].attribute = 0;
1375 r->out.privs->set[i].luid.low = id;
1376 r->out.privs->set[i].luid.high = 0;
1379 r->out.privs->count = el->num_values;
1381 return NT_STATUS_OK;
1385 lsa_EnumAccountRights
1387 static NTSTATUS lsa_EnumAccountRights(struct dcesrv_call_state *dce_call,
1388 TALLOC_CTX *mem_ctx,
1389 struct lsa_EnumAccountRights *r)
1391 struct dcesrv_handle *h;
1392 struct lsa_policy_state *state;
1394 struct ldb_message **res;
1395 const char * const attrs[] = { "privilege", NULL};
1397 struct ldb_message_element *el;
1399 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
1403 sidstr = ldap_encode_ndr_dom_sid(mem_ctx, r->in.sid);
1404 if (sidstr == NULL) {
1405 return NT_STATUS_NO_MEMORY;
1408 ret = gendb_search(state->sam_ldb, mem_ctx, NULL, &res, attrs,
1409 "objectSid=%s", sidstr);
1411 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1414 el = ldb_msg_find_element(res[0], "privilege");
1415 if (el == NULL || el->num_values == 0) {
1416 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1419 r->out.rights->count = el->num_values;
1420 r->out.rights->names = talloc_array(r->out.rights,
1421 struct lsa_String, r->out.rights->count);
1422 if (r->out.rights->names == NULL) {
1423 return NT_STATUS_NO_MEMORY;
1426 for (i=0;i<el->num_values;i++) {
1427 r->out.rights->names[i].string = (const char *)el->values[i].data;
1430 return NT_STATUS_OK;
1436 helper for lsa_AddAccountRights and lsa_RemoveAccountRights
1438 static NTSTATUS lsa_AddRemoveAccountRights(struct dcesrv_call_state *dce_call,
1439 TALLOC_CTX *mem_ctx,
1440 struct lsa_policy_state *state,
1442 struct dom_sid *sid,
1443 const struct lsa_RightSet *rights)
1446 struct ldb_message *msg;
1447 struct ldb_message_element el;
1450 struct lsa_EnumAccountRights r2;
1452 sidstr = ldap_encode_ndr_dom_sid(mem_ctx, sid);
1453 if (sidstr == NULL) {
1454 return NT_STATUS_NO_MEMORY;
1457 msg = ldb_msg_new(mem_ctx);
1459 return NT_STATUS_NO_MEMORY;
1462 dn = samdb_search_dn(state->sam_ldb, mem_ctx, NULL, "objectSid=%s", sidstr);
1464 return NT_STATUS_NO_SUCH_USER;
1467 msg->dn = ldb_dn_explode(mem_ctx, dn);
1468 if (msg->dn == NULL) {
1469 return NT_STATUS_NO_MEMORY;
1472 if (ldb_msg_add_empty(state->sam_ldb, msg, "privilege", ldb_flag)) {
1473 return NT_STATUS_NO_MEMORY;
1476 if (ldb_flag == LDB_FLAG_MOD_ADD) {
1479 r2.in.handle = &state->handle->wire_handle;
1481 r2.out.rights = talloc(mem_ctx, struct lsa_RightSet);
1483 status = lsa_EnumAccountRights(dce_call, mem_ctx, &r2);
1484 if (!NT_STATUS_IS_OK(status)) {
1485 ZERO_STRUCTP(r2.out.rights);
1490 el.values = talloc_array(mem_ctx, struct ldb_val, rights->count);
1491 if (el.values == NULL) {
1492 return NT_STATUS_NO_MEMORY;
1494 for (i=0;i<rights->count;i++) {
1495 if (sec_privilege_id(rights->names[i].string) == -1) {
1496 return NT_STATUS_NO_SUCH_PRIVILEGE;
1499 if (ldb_flag == LDB_FLAG_MOD_ADD) {
1501 for (j=0;j<r2.out.rights->count;j++) {
1502 if (StrCaseCmp(r2.out.rights->names[j].string,
1503 rights->names[i].string) == 0) {
1507 if (j != r2.out.rights->count) continue;
1511 el.values[el.num_values].length = strlen(rights->names[i].string);
1512 el.values[el.num_values].data = (uint8_t *)talloc_strdup(mem_ctx, rights->names[i].string);
1513 if (el.values[el.num_values].data == NULL) {
1514 return NT_STATUS_NO_MEMORY;
1519 if (el.num_values == 0) {
1520 return NT_STATUS_OK;
1523 ret = samdb_modify(state->sam_ldb, mem_ctx, msg);
1525 if (ldb_flag == LDB_FLAG_MOD_DELETE) {
1526 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1528 return NT_STATUS_UNEXPECTED_IO_ERROR;
1531 return NT_STATUS_OK;
1535 lsa_AddPrivilegesToAccount
1537 static NTSTATUS lsa_AddPrivilegesToAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1538 struct lsa_AddPrivilegesToAccount *r)
1540 struct lsa_RightSet rights;
1541 struct dcesrv_handle *h;
1542 struct lsa_account_state *astate;
1545 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_ACCOUNT);
1549 rights.count = r->in.privs->count;
1550 rights.names = talloc_array(mem_ctx, struct lsa_String, rights.count);
1551 if (rights.names == NULL) {
1552 return NT_STATUS_NO_MEMORY;
1554 for (i=0;i<rights.count;i++) {
1555 int id = r->in.privs->set[i].luid.low;
1556 if (r->in.privs->set[i].luid.high) {
1557 return NT_STATUS_NO_SUCH_PRIVILEGE;
1559 rights.names[i].string = sec_privilege_name(id);
1560 if (rights.names[i].string == NULL) {
1561 return NT_STATUS_NO_SUCH_PRIVILEGE;
1565 return lsa_AddRemoveAccountRights(dce_call, mem_ctx, astate->policy,
1566 LDB_FLAG_MOD_ADD, astate->account_sid,
1572 lsa_RemovePrivilegesFromAccount
1574 static NTSTATUS lsa_RemovePrivilegesFromAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1575 struct lsa_RemovePrivilegesFromAccount *r)
1577 struct lsa_RightSet *rights;
1578 struct dcesrv_handle *h;
1579 struct lsa_account_state *astate;
1582 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_ACCOUNT);
1586 rights = talloc(mem_ctx, struct lsa_RightSet);
1588 if (r->in.remove_all == 1 &&
1589 r->in.privs == NULL) {
1590 struct lsa_EnumAccountRights r2;
1593 r2.in.handle = &astate->policy->handle->wire_handle;
1594 r2.in.sid = astate->account_sid;
1595 r2.out.rights = rights;
1597 status = lsa_EnumAccountRights(dce_call, mem_ctx, &r2);
1598 if (!NT_STATUS_IS_OK(status)) {
1602 return lsa_AddRemoveAccountRights(dce_call, mem_ctx, astate->policy,
1603 LDB_FLAG_MOD_DELETE, astate->account_sid,
1607 if (r->in.remove_all != 0) {
1608 return NT_STATUS_INVALID_PARAMETER;
1611 rights->count = r->in.privs->count;
1612 rights->names = talloc_array(mem_ctx, struct lsa_String, rights->count);
1613 if (rights->names == NULL) {
1614 return NT_STATUS_NO_MEMORY;
1616 for (i=0;i<rights->count;i++) {
1617 int id = r->in.privs->set[i].luid.low;
1618 if (r->in.privs->set[i].luid.high) {
1619 return NT_STATUS_NO_SUCH_PRIVILEGE;
1621 rights->names[i].string = sec_privilege_name(id);
1622 if (rights->names[i].string == NULL) {
1623 return NT_STATUS_NO_SUCH_PRIVILEGE;
1627 return lsa_AddRemoveAccountRights(dce_call, mem_ctx, astate->policy,
1628 LDB_FLAG_MOD_DELETE, astate->account_sid,
1634 lsa_GetQuotasForAccount
1636 static NTSTATUS lsa_GetQuotasForAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1637 struct lsa_GetQuotasForAccount *r)
1639 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1644 lsa_SetQuotasForAccount
1646 static NTSTATUS lsa_SetQuotasForAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1647 struct lsa_SetQuotasForAccount *r)
1649 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1654 lsa_GetSystemAccessAccount
1656 static NTSTATUS lsa_GetSystemAccessAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1657 struct lsa_GetSystemAccessAccount *r)
1659 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1664 lsa_SetSystemAccessAccount
1666 static NTSTATUS lsa_SetSystemAccessAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1667 struct lsa_SetSystemAccessAccount *r)
1669 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1676 static NTSTATUS lsa_CreateSecret(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1677 struct lsa_CreateSecret *r)
1679 struct dcesrv_handle *policy_handle;
1680 struct lsa_policy_state *policy_state;
1681 struct lsa_secret_state *secret_state;
1682 struct dcesrv_handle *handle;
1683 struct ldb_message **msgs, *msg;
1684 const char *attrs[] = {
1692 DCESRV_PULL_HANDLE(policy_handle, r->in.handle, LSA_HANDLE_POLICY);
1693 ZERO_STRUCTP(r->out.sec_handle);
1695 policy_state = policy_handle->data;
1697 if (!r->in.name.string) {
1698 return NT_STATUS_INVALID_PARAMETER;
1701 secret_state = talloc(mem_ctx, struct lsa_secret_state);
1702 if (!secret_state) {
1703 return NT_STATUS_NO_MEMORY;
1705 secret_state->policy = policy_state;
1707 msg = ldb_msg_new(mem_ctx);
1709 return NT_STATUS_NO_MEMORY;
1712 if (strncmp("G$", r->in.name.string, 2) == 0) {
1714 name = &r->in.name.string[2];
1715 secret_state->sam_ldb = talloc_reference(secret_state, policy_state->sam_ldb);
1716 secret_state->global = True;
1718 if (strlen(name) < 1) {
1719 return NT_STATUS_INVALID_PARAMETER;
1722 name2 = talloc_asprintf(mem_ctx, "%s Secret", name);
1723 /* search for the secret record */
1724 ret = gendb_search(secret_state->sam_ldb,
1725 mem_ctx, policy_state->system_dn, &msgs, attrs,
1726 "(&(cn=%s)(objectclass=secret))",
1729 return NT_STATUS_OBJECT_NAME_COLLISION;
1732 if (ret < 0 || ret > 1) {
1733 DEBUG(0,("Found %d records matching DN %s\n", ret,
1734 ldb_dn_linearize(mem_ctx, policy_state->system_dn)));
1735 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1738 msg->dn = ldb_dn_build_child(mem_ctx, "cn", name2, policy_state->system_dn);
1739 if (!name2 || !msg->dn) {
1740 return NT_STATUS_NO_MEMORY;
1743 samdb_msg_add_string(secret_state->sam_ldb, mem_ctx, msg, "cn", name2);
1746 secret_state->global = False;
1748 name = r->in.name.string;
1749 if (strlen(name) < 1) {
1750 return NT_STATUS_INVALID_PARAMETER;
1753 secret_state->sam_ldb = talloc_reference(secret_state, secrets_db_connect(mem_ctx));
1754 /* search for the secret record */
1755 ret = gendb_search(secret_state->sam_ldb, mem_ctx,
1756 ldb_dn_explode(mem_ctx, "cn=LSA Secrets"),
1758 "(&(cn=%s)(objectclass=secret))", name);
1760 return NT_STATUS_OBJECT_NAME_COLLISION;
1763 if (ret < 0 || ret > 1) {
1764 DEBUG(0,("Found %d records matching DN %s\n", ret,
1765 ldb_dn_linearize(mem_ctx, policy_state->system_dn)));
1766 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1769 msg->dn = ldb_dn_string_compose(mem_ctx, NULL, "cn=%s,cn=LSA Secrets", name);
1770 samdb_msg_add_string(secret_state->sam_ldb, mem_ctx, msg, "cn", name);
1773 /* pull in all the template attributes. Note this is always from the global samdb */
1774 ret = samdb_copy_template(secret_state->policy->sam_ldb, mem_ctx, msg,
1775 "(&(name=TemplateSecret)(objectclass=secretTemplate))");
1777 DEBUG(0,("Failed to load TemplateSecret from samdb\n"));
1778 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1781 samdb_msg_add_string(secret_state->sam_ldb, mem_ctx, msg, "objectClass", "secret");
1783 secret_state->secret_dn = talloc_reference(secret_state, msg->dn);
1785 /* create the secret */
1786 ret = samdb_add(secret_state->sam_ldb, mem_ctx, msg);
1788 DEBUG(0,("Failed to create secret record %s\n",
1789 ldb_dn_linearize(mem_ctx, msg->dn)));
1790 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1793 handle = dcesrv_handle_new(dce_call->context, LSA_HANDLE_SECRET);
1795 return NT_STATUS_NO_MEMORY;
1798 handle->data = talloc_steal(handle, secret_state);
1800 secret_state->access_mask = r->in.access_mask;
1801 secret_state->policy = talloc_reference(secret_state, policy_state);
1803 *r->out.sec_handle = handle->wire_handle;
1805 return NT_STATUS_OK;
1812 static NTSTATUS lsa_OpenSecret(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1813 struct lsa_OpenSecret *r)
1815 struct dcesrv_handle *policy_handle;
1817 struct lsa_policy_state *policy_state;
1818 struct lsa_secret_state *secret_state;
1819 struct dcesrv_handle *handle;
1820 struct ldb_message **msgs;
1821 const char *attrs[] = {
1829 DCESRV_PULL_HANDLE(policy_handle, r->in.handle, LSA_HANDLE_POLICY);
1830 ZERO_STRUCTP(r->out.sec_handle);
1831 policy_state = policy_handle->data;
1833 if (!r->in.name.string) {
1834 return NT_STATUS_INVALID_PARAMETER;
1837 secret_state = talloc(mem_ctx, struct lsa_secret_state);
1838 if (!secret_state) {
1839 return NT_STATUS_NO_MEMORY;
1841 secret_state->policy = policy_state;
1843 if (strncmp("G$", r->in.name.string, 2) == 0) {
1844 name = &r->in.name.string[2];
1845 secret_state->sam_ldb = talloc_reference(secret_state, policy_state->sam_ldb);
1846 secret_state->global = True;
1848 if (strlen(name) < 1) {
1849 return NT_STATUS_INVALID_PARAMETER;
1852 /* search for the secret record */
1853 ret = gendb_search(secret_state->sam_ldb,
1854 mem_ctx, policy_state->system_dn, &msgs, attrs,
1855 "(&(cn=%s Secret)(objectclass=secret))",
1858 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1862 DEBUG(0,("Found %d records matching DN %s\n", ret,
1863 ldb_dn_linearize(mem_ctx, policy_state->system_dn)));
1864 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1868 secret_state->sam_ldb = talloc_reference(secret_state, secrets_db_connect(mem_ctx));
1870 secret_state->global = False;
1871 name = r->in.name.string;
1872 if (strlen(name) < 1) {
1873 return NT_STATUS_INVALID_PARAMETER;
1876 /* search for the secret record */
1877 ret = gendb_search(secret_state->sam_ldb, mem_ctx,
1878 ldb_dn_explode(mem_ctx, "cn=LSA Secrets"),
1880 "(&(cn=%s)(objectclass=secret))", name);
1882 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1886 DEBUG(0,("Found %d records matching DN %s\n", ret,
1887 ldb_dn_linearize(mem_ctx, policy_state->system_dn)));
1888 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1892 secret_state->secret_dn = talloc_reference(secret_state, msgs[0]->dn);
1894 handle = dcesrv_handle_new(dce_call->context, LSA_HANDLE_SECRET);
1896 return NT_STATUS_NO_MEMORY;
1899 handle->data = talloc_steal(handle, secret_state);
1901 secret_state->access_mask = r->in.access_mask;
1902 secret_state->policy = talloc_reference(secret_state, policy_state);
1904 *r->out.sec_handle = handle->wire_handle;
1906 return NT_STATUS_OK;
1913 static NTSTATUS lsa_SetSecret(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1914 struct lsa_SetSecret *r)
1917 struct dcesrv_handle *h;
1918 struct lsa_secret_state *secret_state;
1919 struct ldb_message *msg;
1920 DATA_BLOB session_key;
1921 DATA_BLOB crypt_secret, secret;
1924 NTSTATUS status = NT_STATUS_OK;
1926 struct timeval now = timeval_current();
1927 NTTIME nt_now = timeval_to_nttime(&now);
1929 DCESRV_PULL_HANDLE(h, r->in.sec_handle, LSA_HANDLE_SECRET);
1931 secret_state = h->data;
1933 msg = ldb_msg_new(mem_ctx);
1935 return NT_STATUS_NO_MEMORY;
1938 msg->dn = talloc_reference(mem_ctx, secret_state->secret_dn);
1940 return NT_STATUS_NO_MEMORY;
1942 status = dcesrv_fetch_session_key(dce_call->conn, &session_key);
1943 if (!NT_STATUS_IS_OK(status)) {
1947 if (r->in.old_val) {
1949 crypt_secret.data = r->in.old_val->data;
1950 crypt_secret.length = r->in.old_val->size;
1952 status = sess_decrypt_blob(mem_ctx, &crypt_secret, &session_key, &secret);
1953 if (!NT_STATUS_IS_OK(status)) {
1957 val.data = secret.data;
1958 val.length = secret.length;
1961 if (samdb_msg_add_value(secret_state->sam_ldb,
1962 mem_ctx, msg, "priorSecret", &val) != 0) {
1963 return NT_STATUS_NO_MEMORY;
1966 /* set old value mtime */
1967 if (samdb_msg_add_uint64(secret_state->sam_ldb,
1968 mem_ctx, msg, "priorSetTime", nt_now) != 0) {
1969 return NT_STATUS_NO_MEMORY;
1972 if (!r->in.new_val) {
1973 /* This behaviour varies depending of if this is a local, or a global secret... */
1974 if (secret_state->global) {
1975 /* set old value mtime */
1976 if (samdb_msg_add_uint64(secret_state->sam_ldb,
1977 mem_ctx, msg, "lastSetTime", nt_now) != 0) {
1978 return NT_STATUS_NO_MEMORY;
1981 if (samdb_msg_add_delete(secret_state->sam_ldb,
1982 mem_ctx, msg, "secret")) {
1983 return NT_STATUS_NO_MEMORY;
1985 if (samdb_msg_add_delete(secret_state->sam_ldb,
1986 mem_ctx, msg, "lastSetTime")) {
1987 return NT_STATUS_NO_MEMORY;
1993 if (r->in.new_val) {
1995 crypt_secret.data = r->in.new_val->data;
1996 crypt_secret.length = r->in.new_val->size;
1998 status = sess_decrypt_blob(mem_ctx, &crypt_secret, &session_key, &secret);
1999 if (!NT_STATUS_IS_OK(status)) {
2003 val.data = secret.data;
2004 val.length = secret.length;
2007 if (samdb_msg_add_value(secret_state->sam_ldb,
2008 mem_ctx, msg, "secret", &val) != 0) {
2009 return NT_STATUS_NO_MEMORY;
2012 /* set new value mtime */
2013 if (samdb_msg_add_uint64(secret_state->sam_ldb,
2014 mem_ctx, msg, "lastSetTime", nt_now) != 0) {
2015 return NT_STATUS_NO_MEMORY;
2018 /* If the old value is not set, then migrate the
2019 * current value to the old value */
2020 if (!r->in.old_val) {
2021 const struct ldb_val *new_val;
2022 NTTIME last_set_time;
2023 struct ldb_message **res;
2024 const char *attrs[] = {
2030 /* search for the secret record */
2031 ret = gendb_search_dn(secret_state->sam_ldb,mem_ctx,
2032 secret_state->secret_dn, &res, attrs);
2034 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2038 DEBUG(0,("Found %d records matching dn=%s\n", ret,
2039 ldb_dn_linearize(mem_ctx, secret_state->secret_dn)));
2040 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2043 new_val = ldb_msg_find_ldb_val(res[0], "secret");
2044 last_set_time = ldb_msg_find_uint64(res[0], "lastSetTime", 0);
2048 if (samdb_msg_add_value(secret_state->sam_ldb,
2049 mem_ctx, msg, "priorSecret",
2051 return NT_STATUS_NO_MEMORY;
2055 /* set new value mtime */
2056 if (ldb_msg_find_ldb_val(res[0], "lastSetTime")) {
2057 if (samdb_msg_add_uint64(secret_state->sam_ldb,
2058 mem_ctx, msg, "priorSetTime", last_set_time) != 0) {
2059 return NT_STATUS_NO_MEMORY;
2065 /* modify the samdb record */
2066 ret = samdb_replace(secret_state->sam_ldb, mem_ctx, msg);
2068 /* we really need samdb.c to return NTSTATUS */
2069 return NT_STATUS_UNSUCCESSFUL;
2072 return NT_STATUS_OK;
2079 static NTSTATUS lsa_QuerySecret(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2080 struct lsa_QuerySecret *r)
2082 struct dcesrv_handle *h;
2083 struct lsa_secret_state *secret_state;
2084 struct ldb_message *msg;
2085 DATA_BLOB session_key;
2086 DATA_BLOB crypt_secret, secret;
2088 struct ldb_message **res;
2089 const char *attrs[] = {
2099 DCESRV_PULL_HANDLE(h, r->in.sec_handle, LSA_HANDLE_SECRET);
2101 secret_state = h->data;
2103 /* pull all the user attributes */
2104 ret = gendb_search_dn(secret_state->sam_ldb, mem_ctx,
2105 secret_state->secret_dn, &res, attrs);
2107 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2111 nt_status = dcesrv_fetch_session_key(dce_call->conn, &session_key);
2112 if (!NT_STATUS_IS_OK(nt_status)) {
2116 if (r->in.old_val) {
2117 const struct ldb_val *prior_val;
2118 r->out.old_val = talloc_zero(mem_ctx, struct lsa_DATA_BUF_PTR);
2119 if (!r->out.old_val) {
2120 return NT_STATUS_NO_MEMORY;
2123 prior_val = ldb_msg_find_ldb_val(res[0], "priorSecret");
2125 if (prior_val && prior_val->length) {
2126 secret.data = prior_val->data;
2127 secret.length = prior_val->length;
2129 crypt_secret = sess_encrypt_blob(mem_ctx, &secret, &session_key);
2130 if (!crypt_secret.length) {
2131 return NT_STATUS_NO_MEMORY;
2133 r->out.old_val->buf = talloc(mem_ctx, struct lsa_DATA_BUF);
2134 if (!r->out.old_val->buf) {
2135 return NT_STATUS_NO_MEMORY;
2137 r->out.old_val->buf->size = crypt_secret.length;
2138 r->out.old_val->buf->length = crypt_secret.length;
2139 r->out.old_val->buf->data = crypt_secret.data;
2143 if (r->in.old_mtime) {
2144 r->out.old_mtime = talloc(mem_ctx, NTTIME);
2145 if (!r->out.old_mtime) {
2146 return NT_STATUS_NO_MEMORY;
2148 *r->out.old_mtime = ldb_msg_find_uint64(res[0], "priorSetTime", 0);
2151 if (r->in.new_val) {
2152 const struct ldb_val *new_val;
2153 r->out.new_val = talloc_zero(mem_ctx, struct lsa_DATA_BUF_PTR);
2154 if (!r->out.new_val) {
2155 return NT_STATUS_NO_MEMORY;
2159 new_val = ldb_msg_find_ldb_val(res[0], "secret");
2161 if (new_val && new_val->length) {
2162 secret.data = new_val->data;
2163 secret.length = new_val->length;
2165 crypt_secret = sess_encrypt_blob(mem_ctx, &secret, &session_key);
2166 if (!crypt_secret.length) {
2167 return NT_STATUS_NO_MEMORY;
2169 r->out.new_val->buf = talloc(mem_ctx, struct lsa_DATA_BUF);
2170 if (!r->out.new_val->buf) {
2171 return NT_STATUS_NO_MEMORY;
2173 r->out.new_val->buf->length = crypt_secret.length;
2174 r->out.new_val->buf->size = crypt_secret.length;
2175 r->out.new_val->buf->data = crypt_secret.data;
2179 if (r->in.new_mtime) {
2180 r->out.new_mtime = talloc(mem_ctx, NTTIME);
2181 if (!r->out.new_mtime) {
2182 return NT_STATUS_NO_MEMORY;
2184 *r->out.new_mtime = ldb_msg_find_uint64(res[0], "lastSetTime", 0);
2187 return NT_STATUS_OK;
2194 static NTSTATUS lsa_LookupPrivValue(struct dcesrv_call_state *dce_call,
2195 TALLOC_CTX *mem_ctx,
2196 struct lsa_LookupPrivValue *r)
2198 struct dcesrv_handle *h;
2199 struct lsa_policy_state *state;
2202 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
2206 id = sec_privilege_id(r->in.name->string);
2208 return NT_STATUS_NO_SUCH_PRIVILEGE;
2211 r->out.luid->low = id;
2212 r->out.luid->high = 0;
2214 return NT_STATUS_OK;
2221 static NTSTATUS lsa_LookupPrivName(struct dcesrv_call_state *dce_call,
2222 TALLOC_CTX *mem_ctx,
2223 struct lsa_LookupPrivName *r)
2225 struct dcesrv_handle *h;
2226 struct lsa_policy_state *state;
2227 const char *privname;
2229 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
2233 if (r->in.luid->high != 0) {
2234 return NT_STATUS_NO_SUCH_PRIVILEGE;
2237 privname = sec_privilege_name(r->in.luid->low);
2238 if (privname == NULL) {
2239 return NT_STATUS_NO_SUCH_PRIVILEGE;
2242 r->out.name = talloc(mem_ctx, struct lsa_String);
2243 if (r->out.name == NULL) {
2244 return NT_STATUS_NO_MEMORY;
2246 r->out.name->string = privname;
2248 return NT_STATUS_OK;
2253 lsa_LookupPrivDisplayName
2255 static NTSTATUS lsa_LookupPrivDisplayName(struct dcesrv_call_state *dce_call,
2256 TALLOC_CTX *mem_ctx,
2257 struct lsa_LookupPrivDisplayName *r)
2259 struct dcesrv_handle *h;
2260 struct lsa_policy_state *state;
2263 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
2267 id = sec_privilege_id(r->in.name->string);
2269 return NT_STATUS_NO_SUCH_PRIVILEGE;
2272 r->out.disp_name = talloc(mem_ctx, struct lsa_String);
2273 if (r->out.disp_name == NULL) {
2274 return NT_STATUS_NO_MEMORY;
2277 r->out.disp_name->string = sec_privilege_display_name(id, r->in.language_id);
2278 if (r->out.disp_name->string == NULL) {
2279 return NT_STATUS_INTERNAL_ERROR;
2282 return NT_STATUS_OK;
2289 static NTSTATUS lsa_DeleteObject(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2290 struct lsa_DeleteObject *r)
2292 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2297 lsa_EnumAccountsWithUserRight
2299 static NTSTATUS lsa_EnumAccountsWithUserRight(struct dcesrv_call_state *dce_call,
2300 TALLOC_CTX *mem_ctx,
2301 struct lsa_EnumAccountsWithUserRight *r)
2303 struct dcesrv_handle *h;
2304 struct lsa_policy_state *state;
2306 struct ldb_message **res;
2307 const char * const attrs[] = { "objectSid", NULL};
2308 const char *privname;
2310 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
2314 if (r->in.name == NULL) {
2315 return NT_STATUS_NO_SUCH_PRIVILEGE;
2318 privname = r->in.name->string;
2319 if (sec_privilege_id(privname) == -1) {
2320 return NT_STATUS_NO_SUCH_PRIVILEGE;
2323 ret = gendb_search(state->sam_ldb, mem_ctx, NULL, &res, attrs,
2324 "privilege=%s", privname);
2326 return NT_STATUS_NO_SUCH_USER;
2329 r->out.sids->sids = talloc_array(r->out.sids, struct lsa_SidPtr, ret);
2330 if (r->out.sids->sids == NULL) {
2331 return NT_STATUS_NO_MEMORY;
2333 for (i=0;i<ret;i++) {
2334 r->out.sids->sids[i].sid = samdb_result_dom_sid(r->out.sids->sids,
2335 res[i], "objectSid");
2336 NT_STATUS_HAVE_NO_MEMORY(r->out.sids->sids[i].sid);
2338 r->out.sids->num_sids = ret;
2340 return NT_STATUS_OK;
2345 lsa_AddAccountRights
2347 static NTSTATUS lsa_AddAccountRights(struct dcesrv_call_state *dce_call,
2348 TALLOC_CTX *mem_ctx,
2349 struct lsa_AddAccountRights *r)
2351 struct dcesrv_handle *h;
2352 struct lsa_policy_state *state;
2354 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
2358 return lsa_AddRemoveAccountRights(dce_call, mem_ctx, state,
2360 r->in.sid, r->in.rights);
2365 lsa_RemoveAccountRights
2367 static NTSTATUS lsa_RemoveAccountRights(struct dcesrv_call_state *dce_call,
2368 TALLOC_CTX *mem_ctx,
2369 struct lsa_RemoveAccountRights *r)
2371 struct dcesrv_handle *h;
2372 struct lsa_policy_state *state;
2374 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
2378 return lsa_AddRemoveAccountRights(dce_call, mem_ctx, state,
2379 LDB_FLAG_MOD_DELETE,
2380 r->in.sid, r->in.rights);
2385 lsa_StorePrivateData
2387 static NTSTATUS lsa_StorePrivateData(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2388 struct lsa_StorePrivateData *r)
2390 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2395 lsa_RetrievePrivateData
2397 static NTSTATUS lsa_RetrievePrivateData(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2398 struct lsa_RetrievePrivateData *r)
2400 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2407 static NTSTATUS lsa_GetUserName(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2408 struct lsa_GetUserName *r)
2410 NTSTATUS status = NT_STATUS_OK;
2411 const char *account_name;
2412 const char *authority_name;
2413 struct lsa_String *_account_name;
2414 struct lsa_StringPointer *_authority_name = NULL;
2416 /* this is what w2k3 does */
2417 r->out.account_name = r->in.account_name;
2418 r->out.authority_name = r->in.authority_name;
2420 if (r->in.account_name && r->in.account_name->string) {
2421 return NT_STATUS_INVALID_PARAMETER;
2424 if (r->in.authority_name &&
2425 r->in.authority_name->string &&
2426 r->in.authority_name->string->string) {
2427 return NT_STATUS_INVALID_PARAMETER;
2430 /* TODO: this check should go and we should rely on the calling code that this is valid */
2431 if (!dce_call->conn->auth_state.session_info ||
2432 !dce_call->conn->auth_state.session_info->server_info ||
2433 !dce_call->conn->auth_state.session_info->server_info->account_name ||
2434 !dce_call->conn->auth_state.session_info->server_info->domain_name) {
2435 return NT_STATUS_INTERNAL_ERROR;
2438 account_name = talloc_reference(mem_ctx, dce_call->conn->auth_state.session_info->server_info->account_name);
2439 authority_name = talloc_reference(mem_ctx, dce_call->conn->auth_state.session_info->server_info->domain_name);
2441 _account_name = talloc(mem_ctx, struct lsa_String);
2442 NTSTATUS_TALLOC_CHECK(_account_name);
2443 _account_name->string = account_name;
2445 if (r->in.authority_name) {
2446 _authority_name = talloc(mem_ctx, struct lsa_StringPointer);
2447 NTSTATUS_TALLOC_CHECK(_authority_name);
2448 _authority_name->string = talloc(mem_ctx, struct lsa_String);
2449 NTSTATUS_TALLOC_CHECK(_authority_name->string);
2450 _authority_name->string->string = authority_name;
2453 r->out.account_name = _account_name;
2454 r->out.authority_name = _authority_name;
2462 static NTSTATUS lsa_SetInfoPolicy2(struct dcesrv_call_state *dce_call,
2463 TALLOC_CTX *mem_ctx,
2464 struct lsa_SetInfoPolicy2 *r)
2466 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2470 lsa_QueryDomainInformationPolicy
2472 static NTSTATUS lsa_QueryDomainInformationPolicy(struct dcesrv_call_state *dce_call,
2473 TALLOC_CTX *mem_ctx,
2474 struct lsa_QueryDomainInformationPolicy *r)
2476 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2480 lsa_SetDomInfoPolicy
2482 static NTSTATUS lsa_SetDomInfoPolicy(struct dcesrv_call_state *dce_call,
2483 TALLOC_CTX *mem_ctx,
2484 struct lsa_SetDomInfoPolicy *r)
2486 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2492 static NTSTATUS lsa_TestCall(struct dcesrv_call_state *dce_call,
2493 TALLOC_CTX *mem_ctx,
2494 struct lsa_TestCall *r)
2496 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2500 lookup a SID for 1 name
2502 static NTSTATUS lsa_lookup_name(struct lsa_policy_state *state, TALLOC_CTX *mem_ctx,
2503 const char *name, struct dom_sid **sid, uint32_t *atype)
2506 struct ldb_message **res;
2507 const char * const attrs[] = { "objectSid", "sAMAccountType", NULL};
2510 p = strchr_m(name, '\\');
2512 /* TODO: properly parse the domain prefix here, and use it to
2517 ret = gendb_search(state->sam_ldb, mem_ctx, NULL, &res, attrs, "sAMAccountName=%s", name);
2519 *sid = samdb_result_dom_sid(mem_ctx, res[0], "objectSid");
2521 return NT_STATUS_INVALID_SID;
2524 *atype = samdb_result_uint(res[0], "sAMAccountType", 0);
2526 return NT_STATUS_OK;
2529 /* need to add a call into sidmap to check for a allocated sid */
2531 return NT_STATUS_INVALID_SID;
2538 static NTSTATUS lsa_LookupNames3(struct dcesrv_call_state *dce_call,
2539 TALLOC_CTX *mem_ctx,
2540 struct lsa_LookupNames3 *r)
2542 struct lsa_policy_state *state;
2543 struct dcesrv_handle *h;
2545 NTSTATUS status = NT_STATUS_OK;
2547 r->out.domains = NULL;
2549 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
2553 r->out.domains = talloc_zero(mem_ctx, struct lsa_RefDomainList);
2554 if (r->out.domains == NULL) {
2555 return NT_STATUS_NO_MEMORY;
2558 r->out.sids = talloc_zero(mem_ctx, struct lsa_TransSidArray3);
2559 if (r->out.sids == NULL) {
2560 return NT_STATUS_NO_MEMORY;
2565 r->out.sids->sids = talloc_array(r->out.sids, struct lsa_TranslatedSid3,
2567 if (r->out.sids->sids == NULL) {
2568 return NT_STATUS_NO_MEMORY;
2571 for (i=0;i<r->in.num_names;i++) {
2572 const char *name = r->in.names[i].string;
2573 struct dom_sid *sid;
2574 uint32_t atype, rtype, sid_index;
2577 r->out.sids->count++;
2580 r->out.sids->sids[i].sid_type = SID_NAME_UNKNOWN;
2581 r->out.sids->sids[i].sid = NULL;
2582 r->out.sids->sids[i].sid_index = 0xFFFFFFFF;
2583 r->out.sids->sids[i].unknown = 0;
2585 status2 = lsa_lookup_name(state, mem_ctx, name, &sid, &atype);
2586 if (!NT_STATUS_IS_OK(status2) || sid->num_auths == 0) {
2587 status = STATUS_SOME_UNMAPPED;
2591 rtype = samdb_atype_map(atype);
2592 if (rtype == SID_NAME_UNKNOWN) {
2593 status = STATUS_SOME_UNMAPPED;
2597 status2 = lsa_authority_list(state, mem_ctx, sid, r->out.domains, &sid_index);
2598 if (!NT_STATUS_IS_OK(status2)) {
2602 r->out.sids->sids[i].sid_type = rtype;
2603 r->out.sids->sids[i].sid = sid;
2604 r->out.sids->sids[i].sid_index = sid_index;
2605 r->out.sids->sids[i].unknown = 0;
2614 static NTSTATUS lsa_LookupNames2(struct dcesrv_call_state *dce_call,
2615 TALLOC_CTX *mem_ctx,
2616 struct lsa_LookupNames2 *r)
2618 struct lsa_policy_state *state;
2619 struct dcesrv_handle *h;
2621 NTSTATUS status = NT_STATUS_OK;
2623 r->out.domains = NULL;
2625 DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
2629 r->out.domains = talloc_zero(mem_ctx, struct lsa_RefDomainList);
2630 if (r->out.domains == NULL) {
2631 return NT_STATUS_NO_MEMORY;
2634 r->out.sids = talloc_zero(mem_ctx, struct lsa_TransSidArray2);
2635 if (r->out.sids == NULL) {
2636 return NT_STATUS_NO_MEMORY;
2641 r->out.sids->sids = talloc_array(r->out.sids, struct lsa_TranslatedSid2,
2643 if (r->out.sids->sids == NULL) {
2644 return NT_STATUS_NO_MEMORY;
2647 for (i=0;i<r->in.num_names;i++) {
2648 const char *name = r->in.names[i].string;
2649 struct dom_sid *sid;
2650 uint32_t atype, rtype, sid_index;
2653 r->out.sids->count++;
2656 r->out.sids->sids[i].sid_type = SID_NAME_UNKNOWN;
2657 r->out.sids->sids[i].rid = 0xFFFFFFFF;
2658 r->out.sids->sids[i].sid_index = 0xFFFFFFFF;
2659 r->out.sids->sids[i].unknown = 0;
2661 status2 = lsa_lookup_name(state, mem_ctx, name, &sid, &atype);
2662 if (!NT_STATUS_IS_OK(status2) || sid->num_auths == 0) {
2663 status = STATUS_SOME_UNMAPPED;
2667 rtype = samdb_atype_map(atype);
2668 if (rtype == SID_NAME_UNKNOWN) {
2669 status = STATUS_SOME_UNMAPPED;
2673 status2 = lsa_authority_list(state, mem_ctx, sid, r->out.domains, &sid_index);
2674 if (!NT_STATUS_IS_OK(status2)) {
2678 r->out.sids->sids[i].sid_type = rtype;
2679 r->out.sids->sids[i].rid = sid->sub_auths[sid->num_auths-1];
2680 r->out.sids->sids[i].sid_index = sid_index;
2681 r->out.sids->sids[i].unknown = 0;
2690 static NTSTATUS lsa_LookupNames(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2691 struct lsa_LookupNames *r)
2693 struct lsa_LookupNames2 r2;
2697 r2.in.handle = r->in.handle;
2698 r2.in.num_names = r->in.num_names;
2699 r2.in.names = r->in.names;
2701 r2.in.level = r->in.level;
2702 r2.in.count = r->in.count;
2705 r2.out.count = r->out.count;
2707 status = lsa_LookupNames2(dce_call, mem_ctx, &r2);
2708 if (dce_call->fault_code != 0) {
2712 r->out.domains = r2.out.domains;
2713 r->out.sids = talloc(mem_ctx, struct lsa_TransSidArray);
2714 if (r->out.sids == NULL) {
2715 return NT_STATUS_NO_MEMORY;
2717 r->out.sids->count = r2.out.sids->count;
2718 r->out.sids->sids = talloc_array(r->out.sids, struct lsa_TranslatedSid,
2719 r->out.sids->count);
2720 if (r->out.sids->sids == NULL) {
2721 return NT_STATUS_NO_MEMORY;
2723 for (i=0;i<r->out.sids->count;i++) {
2724 r->out.sids->sids[i].sid_type = r2.out.sids->sids[i].sid_type;
2725 r->out.sids->sids[i].rid = r2.out.sids->sids[i].rid;
2726 r->out.sids->sids[i].sid_index = r2.out.sids->sids[i].sid_index;
2735 static NTSTATUS lsa_CREDRWRITE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2736 struct lsa_CREDRWRITE *r)
2738 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2745 static NTSTATUS lsa_CREDRREAD(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2746 struct lsa_CREDRREAD *r)
2748 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2755 static NTSTATUS lsa_CREDRENUMERATE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2756 struct lsa_CREDRENUMERATE *r)
2758 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2763 lsa_CREDRWRITEDOMAINCREDENTIALS
2765 static NTSTATUS lsa_CREDRWRITEDOMAINCREDENTIALS(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2766 struct lsa_CREDRWRITEDOMAINCREDENTIALS *r)
2768 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2773 lsa_CREDRREADDOMAINCREDENTIALS
2775 static NTSTATUS lsa_CREDRREADDOMAINCREDENTIALS(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2776 struct lsa_CREDRREADDOMAINCREDENTIALS *r)
2778 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2785 static NTSTATUS lsa_CREDRDELETE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2786 struct lsa_CREDRDELETE *r)
2788 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2793 lsa_CREDRGETTARGETINFO
2795 static NTSTATUS lsa_CREDRGETTARGETINFO(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2796 struct lsa_CREDRGETTARGETINFO *r)
2798 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2803 lsa_CREDRPROFILELOADED
2805 static NTSTATUS lsa_CREDRPROFILELOADED(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2806 struct lsa_CREDRPROFILELOADED *r)
2808 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2813 lsa_CREDRGETSESSIONTYPES
2815 static NTSTATUS lsa_CREDRGETSESSIONTYPES(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2816 struct lsa_CREDRGETSESSIONTYPES *r)
2818 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2823 lsa_LSARREGISTERAUDITEVENT
2825 static NTSTATUS lsa_LSARREGISTERAUDITEVENT(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2826 struct lsa_LSARREGISTERAUDITEVENT *r)
2828 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2833 lsa_LSARGENAUDITEVENT
2835 static NTSTATUS lsa_LSARGENAUDITEVENT(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2836 struct lsa_LSARGENAUDITEVENT *r)
2838 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2843 lsa_LSARUNREGISTERAUDITEVENT
2845 static NTSTATUS lsa_LSARUNREGISTERAUDITEVENT(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2846 struct lsa_LSARUNREGISTERAUDITEVENT *r)
2848 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2853 lsa_LSARQUERYFORESTTRUSTINFORMATION
2855 static NTSTATUS lsa_LSARQUERYFORESTTRUSTINFORMATION(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2856 struct lsa_LSARQUERYFORESTTRUSTINFORMATION *r)
2858 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2863 lsa_LSARSETFORESTTRUSTINFORMATION
2865 static NTSTATUS lsa_LSARSETFORESTTRUSTINFORMATION(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2866 struct lsa_LSARSETFORESTTRUSTINFORMATION *r)
2868 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2875 static NTSTATUS lsa_CREDRRENAME(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2876 struct lsa_CREDRRENAME *r)
2878 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2883 lsa_LSARLOOKUPNAMES4
2885 static NTSTATUS lsa_LSARLOOKUPNAMES4(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2886 struct lsa_LSARLOOKUPNAMES4 *r)
2888 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2893 lsa_LSAROPENPOLICYSCE
2895 static NTSTATUS lsa_LSAROPENPOLICYSCE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2896 struct lsa_LSAROPENPOLICYSCE *r)
2898 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2903 lsa_LSARADTREGISTERSECURITYEVENTSOURCE
2905 static NTSTATUS lsa_LSARADTREGISTERSECURITYEVENTSOURCE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2906 struct lsa_LSARADTREGISTERSECURITYEVENTSOURCE *r)
2908 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2913 lsa_LSARADTUNREGISTERSECURITYEVENTSOURCE
2915 static NTSTATUS lsa_LSARADTUNREGISTERSECURITYEVENTSOURCE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2916 struct lsa_LSARADTUNREGISTERSECURITYEVENTSOURCE *r)
2918 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2923 lsa_LSARADTREPORTSECURITYEVENT
2925 static NTSTATUS lsa_LSARADTREPORTSECURITYEVENT(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2926 struct lsa_LSARADTREPORTSECURITYEVENT *r)
2928 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
2932 /* include the generated boilerplate */
2933 #include "librpc/gen_ndr/ndr_lsa_s.c"