r4193: added server side implementation of lsa_EnumAccountsWithUserRight
[kai/samba-autobuild/.git] / source4 / rpc_server / lsa / dcesrv_lsa.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    endpoint server for the lsarpc pipe
5
6    Copyright (C) Andrew Tridgell 2004
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/ndr_lsa.h"
25 #include "librpc/gen_ndr/ndr_samr.h"
26 #include "rpc_server/dcerpc_server.h"
27 #include "rpc_server/common/common.h"
28 #include "lib/ldb/include/ldb.h"
29
30 /*
31   this type allows us to distinguish handle types
32 */
33 enum lsa_handle {
34         LSA_HANDLE_POLICY,
35         LSA_HANDLE_ACCOUNT,
36         LSA_HANDLE_SECRET
37 };
38
39 /*
40   state associated with a lsa_OpenPolicy() operation
41 */
42 struct lsa_policy_state {
43         void *sam_ctx;
44         struct sidmap_context *sidmap;
45         uint32_t access_mask;
46         const char *domain_dn;
47         const char *domain_name;
48         struct dom_sid *domain_sid;
49         struct dom_sid *builtin_sid;
50 };
51
52
53 /*
54   destroy an open policy. This closes the database connection
55 */
56 static void lsa_Policy_destroy(struct dcesrv_connection *conn, struct dcesrv_handle *h)
57 {
58         struct lsa_policy_state *state = h->data;
59         talloc_free(state);
60 }
61
62 /* 
63   lsa_Close 
64 */
65 static NTSTATUS lsa_Close(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
66                           struct lsa_Close *r)
67 {
68         struct dcesrv_handle *h;
69
70         *r->out.handle = *r->in.handle;
71
72         DCESRV_PULL_HANDLE(h, r->in.handle, DCESRV_HANDLE_ANY);
73
74         /* this causes the callback samr_XXX_destroy() to be called by
75            the handle destroy code which destroys the state associated
76            with the handle */
77         dcesrv_handle_destroy(dce_call->conn, h);
78
79         ZERO_STRUCTP(r->out.handle);
80
81         return NT_STATUS_OK;
82 }
83
84
85 /* 
86   lsa_Delete 
87 */
88 static NTSTATUS lsa_Delete(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
89                            struct lsa_Delete *r)
90 {
91         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
92 }
93
94
95 /* 
96   lsa_EnumPrivs 
97 */
98 static NTSTATUS lsa_EnumPrivs(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
99                               struct lsa_EnumPrivs *r)
100 {
101         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
102 }
103
104
105 /* 
106   lsa_QuerySecObj 
107 */
108 static NTSTATUS lsa_QuerySecurity(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
109                                   struct lsa_QuerySecurity *r)
110 {
111         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
112 }
113
114
115 /* 
116   lsa_SetSecObj 
117 */
118 static NTSTATUS lsa_SetSecObj(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
119                               struct lsa_SetSecObj *r)
120 {
121         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
122 }
123
124
125 /* 
126   lsa_ChangePassword 
127 */
128 static NTSTATUS lsa_ChangePassword(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
129                                    struct lsa_ChangePassword *r)
130 {
131         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
132 }
133
134
135 /* 
136   lsa_OpenPolicy2
137 */
138 static NTSTATUS lsa_OpenPolicy2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
139                                struct lsa_OpenPolicy2 *r)
140 {
141         struct lsa_policy_state *state;
142         struct dcesrv_handle *handle;
143         const char *sid_str;
144
145         ZERO_STRUCTP(r->out.handle);
146
147         state = talloc_p(dce_call->conn, struct lsa_policy_state);
148         if (!state) {
149                 return NT_STATUS_NO_MEMORY;
150         }
151
152         /* make sure the sam database is accessible */
153         state->sam_ctx = samdb_connect(state);
154         if (state->sam_ctx == NULL) {
155                 talloc_free(state);
156                 return NT_STATUS_INVALID_SYSTEM_SERVICE;
157         }
158
159         state->sidmap = sidmap_open(state);
160         if (state->sidmap == NULL) {
161                 talloc_free(state);
162                 return NT_STATUS_INVALID_SYSTEM_SERVICE;
163         }
164
165         /* work out the domain_dn - useful for so many calls its worth
166            fetching here */
167         state->domain_dn = samdb_search_string(state->sam_ctx, state, NULL,
168                                                "dn", "(&(objectClass=domain)(!(objectclass=builtinDomain)))");
169         if (!state->domain_dn) {
170                 talloc_free(state);
171                 return NT_STATUS_NO_SUCH_DOMAIN;                
172         }
173
174         sid_str = samdb_search_string(state->sam_ctx, state, NULL,
175                                       "objectSid", "dn=%s", state->domain_dn);
176         if (!sid_str) {
177                 talloc_free(state);
178                 return NT_STATUS_NO_SUCH_DOMAIN;                
179         }
180
181         state->domain_sid = dom_sid_parse_talloc(state, sid_str);
182         if (!state->domain_sid) {
183                 talloc_free(state);
184                 return NT_STATUS_NO_SUCH_DOMAIN;                
185         }
186
187         state->builtin_sid = dom_sid_parse_talloc(state, SID_BUILTIN);
188         if (!state->builtin_sid) {
189                 talloc_free(state);
190                 return NT_STATUS_NO_SUCH_DOMAIN;                
191         }
192
193         state->domain_name = samdb_search_string(state->sam_ctx, state, NULL,
194                                                  "name", "dn=%s", state->domain_dn);
195         if (!state->domain_name) {
196                 talloc_free(state);
197                 return NT_STATUS_NO_SUCH_DOMAIN;                
198         }
199         
200
201         handle = dcesrv_handle_new(dce_call->conn, LSA_HANDLE_POLICY);
202         if (!handle) {
203                 talloc_free(state);
204                 return NT_STATUS_NO_MEMORY;
205         }
206
207         handle->data = state;
208         handle->destroy = lsa_Policy_destroy;
209
210         state->access_mask = r->in.access_mask;
211         *r->out.handle = handle->wire_handle;
212
213         /* note that we have completely ignored the attr element of
214            the OpenPolicy. As far as I can tell, this is what w2k3
215            does */
216
217         return NT_STATUS_OK;
218 }
219
220 /* 
221   lsa_OpenPolicy
222   a wrapper around lsa_OpenPolicy2
223 */
224 static NTSTATUS lsa_OpenPolicy(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
225                                 struct lsa_OpenPolicy *r)
226 {
227         struct lsa_OpenPolicy2 r2;
228
229         r2.in.system_name = NULL;
230         r2.in.attr = r->in.attr;
231         r2.in.access_mask = r->in.access_mask;
232         r2.out.handle = r->out.handle;
233
234         return lsa_OpenPolicy2(dce_call, mem_ctx, &r2);
235 }
236
237
238
239
240 /*
241   fill in the AccountDomain info
242 */
243 static NTSTATUS lsa_info_AccountDomain(struct lsa_policy_state *state, TALLOC_CTX *mem_ctx,
244                                        struct lsa_DomainInfo *info)
245 {
246         const char * const attrs[] = { "objectSid", "name", NULL};
247         int ret;
248         struct ldb_message **res;
249
250         ret = samdb_search(state->sam_ctx, mem_ctx, NULL, &res, attrs, 
251                            "dn=%s", state->domain_dn);
252         if (ret != 1) {
253                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
254         }
255
256         info->name.string = samdb_result_string(res[0], "name", NULL);
257         info->sid         = samdb_result_dom_sid(mem_ctx, res[0], "objectSid");
258
259         return NT_STATUS_OK;
260 }
261
262 /*
263   fill in the DNS domain info
264 */
265 static NTSTATUS lsa_info_DNS(struct lsa_policy_state *state, TALLOC_CTX *mem_ctx,
266                              struct lsa_DnsDomainInfo *info)
267 {
268         const char * const attrs[] = { "name", "dnsDomain", "objectGUID", "objectSid", NULL };
269         int ret;
270         struct ldb_message **res;
271
272         ret = samdb_search(state->sam_ctx, mem_ctx, NULL, &res, attrs, 
273                            "dn=%s", state->domain_dn);
274         if (ret != 1) {
275                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
276         }
277
278         info->name.string       = samdb_result_string(res[0],           "name", NULL);
279         info->dns_domain.string = samdb_result_string(res[0],           "dnsDomain", NULL);
280         info->dns_forest.string = samdb_result_string(res[0],           "dnsDomain", NULL);
281         info->domain_guid       = samdb_result_guid(res[0],             "objectGUID");
282         info->sid               = samdb_result_dom_sid(mem_ctx, res[0], "objectSid");
283
284         return NT_STATUS_OK;
285 }
286
287 /* 
288   lsa_QueryInfoPolicy2
289 */
290 static NTSTATUS lsa_QueryInfoPolicy2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
291                                      struct lsa_QueryInfoPolicy2 *r)
292 {
293         struct lsa_policy_state *state;
294         struct dcesrv_handle *h;
295
296         r->out.info = NULL;
297
298         DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
299
300         state = h->data;
301
302         r->out.info = talloc_p(mem_ctx, union lsa_PolicyInformation);
303         if (!r->out.info) {
304                 return NT_STATUS_NO_MEMORY;
305         }
306
307         ZERO_STRUCTP(r->out.info);
308
309         switch (r->in.level) {
310         case LSA_POLICY_INFO_DOMAIN:
311         case LSA_POLICY_INFO_ACCOUNT_DOMAIN:
312                 return lsa_info_AccountDomain(state, mem_ctx, &r->out.info->account_domain);
313
314         case LSA_POLICY_INFO_DNS:
315                 return lsa_info_DNS(state, mem_ctx, &r->out.info->dns);
316         }
317
318         return NT_STATUS_INVALID_INFO_CLASS;
319 }
320
321 /* 
322   lsa_QueryInfoPolicy 
323 */
324 static NTSTATUS lsa_QueryInfoPolicy(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
325                                     struct lsa_QueryInfoPolicy *r)
326 {
327         struct lsa_QueryInfoPolicy2 r2;
328         NTSTATUS status;
329
330         r2.in.handle = r->in.handle;
331         r2.in.level = r->in.level;
332         
333         status = lsa_QueryInfoPolicy2(dce_call, mem_ctx, &r2);
334
335         r->out.info = r2.out.info;
336
337         return status;
338 }
339
340 /* 
341   lsa_SetInfoPolicy 
342 */
343 static NTSTATUS lsa_SetInfoPolicy(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
344                                   struct lsa_SetInfoPolicy *r)
345 {
346         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
347 }
348
349
350 /* 
351   lsa_ClearAuditLog 
352 */
353 static NTSTATUS lsa_ClearAuditLog(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
354                                   struct lsa_ClearAuditLog *r)
355 {
356         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
357 }
358
359
360 /* 
361   lsa_CreateAccount 
362 */
363 static NTSTATUS lsa_CreateAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
364                                   struct lsa_CreateAccount *r)
365 {
366         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
367 }
368
369
370 /* 
371   lsa_EnumAccounts 
372 */
373 static NTSTATUS lsa_EnumAccounts(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
374                                  struct lsa_EnumAccounts *r)
375 {
376         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
377 }
378
379
380 /* 
381   lsa_CreateTrustedDomain 
382 */
383 static NTSTATUS lsa_CreateTrustedDomain(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
384                                         struct lsa_CreateTrustedDomain *r)
385 {
386         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
387 }
388
389
390 /* 
391   lsa_EnumTrustDom 
392 */
393 static NTSTATUS lsa_EnumTrustDom(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
394                        struct lsa_EnumTrustDom *r)
395 {
396         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
397 }
398
399
400 /*
401   return the authority name and authority sid, given a sid
402 */
403 static NTSTATUS lsa_authority_name(struct lsa_policy_state *state,
404                                    TALLOC_CTX *mem_ctx, struct dom_sid *sid,
405                                    const char **authority_name,
406                                    struct dom_sid **authority_sid)
407 {
408         if (dom_sid_in_domain(state->domain_sid, sid)) {
409                 *authority_name = state->domain_name;
410                 *authority_sid = state->domain_sid;
411                 return NT_STATUS_OK;
412         }
413
414         if (dom_sid_in_domain(state->builtin_sid, sid)) {
415                 *authority_name = "BUILTIN";
416                 *authority_sid = state->builtin_sid;
417                 return NT_STATUS_OK;
418         }
419
420         *authority_sid = dom_sid_dup(mem_ctx, sid);
421         if (*authority_sid == NULL) {
422                 return NT_STATUS_NO_MEMORY;
423         }
424         (*authority_sid)->num_auths = 0;
425         *authority_name = dom_sid_string(mem_ctx, *authority_sid);
426         if (*authority_name == NULL) {
427                 return NT_STATUS_NO_MEMORY;
428         }
429
430         return NT_STATUS_OK;
431 }
432
433 /*
434   add to the lsa_RefDomainList for LookupSids and LookupNames
435 */
436 static NTSTATUS lsa_authority_list(struct lsa_policy_state *state, TALLOC_CTX *mem_ctx, 
437                                    struct dom_sid *sid, 
438                                    struct lsa_RefDomainList *domains,
439                                    uint32_t *sid_index)
440 {
441         NTSTATUS status;
442         const char *authority_name;
443         struct dom_sid *authority_sid;
444         int i;
445
446         /* work out the authority name */
447         status = lsa_authority_name(state, mem_ctx, sid, 
448                                     &authority_name, &authority_sid);
449         if (!NT_STATUS_IS_OK(status)) {
450                 return status;
451         }
452         
453         /* see if we've already done this authority name */
454         for (i=0;i<domains->count;i++) {
455                 if (strcmp(authority_name, domains->domains[i].name.string) == 0) {
456                         *sid_index = i;
457                         return NT_STATUS_OK;
458                 }
459         }
460
461         domains->domains = talloc_realloc_p(domains, 
462                                             domains->domains,
463                                             struct lsa_TrustInformation,
464                                             domains->count+1);
465         if (domains->domains == NULL) {
466                 return NT_STATUS_NO_MEMORY;
467         }
468         domains->domains[i].name.string = authority_name;
469         domains->domains[i].sid         = authority_sid;
470         domains->count++;
471         *sid_index = i;
472         
473         return NT_STATUS_OK;
474 }
475
476 /*
477   lookup a name for 1 SID
478 */
479 static NTSTATUS lsa_lookup_sid(struct lsa_policy_state *state, TALLOC_CTX *mem_ctx,
480                                struct dom_sid *sid, const char *sid_str,
481                                const char **name, uint32_t *atype)
482 {
483         int ret;
484         struct ldb_message **res;
485         const char * const attrs[] = { "sAMAccountName", "sAMAccountType", NULL};
486         NTSTATUS status;
487
488         ret = samdb_search(state->sam_ctx, mem_ctx, NULL, &res, attrs, 
489                            "objectSid=%s", sid_str);
490         if (ret == 1) {
491                 *name = ldb_msg_find_string(res[0], "sAMAccountName", NULL);
492                 if (*name == NULL) {
493                         return NT_STATUS_NO_MEMORY;
494                 }
495         
496                 *atype = samdb_result_uint(res[0], "sAMAccountType", 0);
497
498                 return NT_STATUS_OK;
499         }
500
501         status = sidmap_allocated_sid_lookup(state->sidmap, mem_ctx, sid, name, atype);
502
503         return status;
504 }
505
506
507 /*
508   lsa_LookupSids2
509 */
510 static NTSTATUS lsa_LookupSids2(struct dcesrv_call_state *dce_call,
511                                 TALLOC_CTX *mem_ctx,
512                                 struct lsa_LookupSids2 *r)
513 {
514         struct lsa_policy_state *state;
515         struct dcesrv_handle *h;
516         int i;
517         NTSTATUS status = NT_STATUS_OK;
518
519         r->out.domains = NULL;
520
521         DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
522
523         state = h->data;
524
525         r->out.domains = talloc_zero_p(mem_ctx,  struct lsa_RefDomainList);
526         if (r->out.domains == NULL) {
527                 return NT_STATUS_NO_MEMORY;
528         }
529
530         r->out.names = talloc_zero_p(mem_ctx,  struct lsa_TransNameArray2);
531         if (r->out.names == NULL) {
532                 return NT_STATUS_NO_MEMORY;
533         }
534
535         *r->out.count = 0;
536
537         r->out.names->names = talloc_array_p(r->out.names, struct lsa_TranslatedName2, 
538                                              r->in.sids->num_sids);
539         if (r->out.names->names == NULL) {
540                 return NT_STATUS_NO_MEMORY;
541         }
542
543         for (i=0;i<r->in.sids->num_sids;i++) {
544                 struct dom_sid *sid = r->in.sids->sids[i].sid;
545                 char *sid_str = dom_sid_string(mem_ctx, sid);
546                 const char *name;
547                 uint32_t atype, rtype, sid_index;
548                 NTSTATUS status2;
549
550                 r->out.names->count++;
551                 (*r->out.count)++;
552
553                 r->out.names->names[i].sid_type    = SID_NAME_UNKNOWN;
554                 r->out.names->names[i].name.string = sid_str;
555                 r->out.names->names[i].sid_index   = 0xFFFFFFFF;
556                 r->out.names->names[i].unknown     = 0;
557
558                 if (sid_str == NULL) {
559                         r->out.names->names[i].name.string = "(SIDERROR)";
560                         status = STATUS_SOME_UNMAPPED;
561                         continue;
562                 }
563
564                 /* work out the authority name */
565                 status2 = lsa_authority_list(state, mem_ctx, sid, r->out.domains, &sid_index);
566                 if (!NT_STATUS_IS_OK(status2)) {
567                         return status2;
568                 }
569
570                 status2 = lsa_lookup_sid(state, mem_ctx, sid, sid_str, 
571                                          &name, &atype);
572                 if (!NT_STATUS_IS_OK(status2)) {
573                         status = STATUS_SOME_UNMAPPED;
574                         continue;
575                 }
576
577                 rtype = samdb_atype_map(atype);
578                 if (rtype == SID_NAME_UNKNOWN) {
579                         status = STATUS_SOME_UNMAPPED;
580                         continue;
581                 }
582
583                 r->out.names->names[i].sid_type    = rtype;
584                 r->out.names->names[i].name.string = name;
585                 r->out.names->names[i].sid_index   = sid_index;
586                 r->out.names->names[i].unknown     = 0;
587         }
588         
589         return status;
590 }
591
592
593 /* 
594   lsa_LookupSids 
595 */
596 static NTSTATUS lsa_LookupSids(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
597                                struct lsa_LookupSids *r)
598 {
599         struct lsa_LookupSids2 r2;
600         NTSTATUS status;
601         int i;
602
603         r2.in.handle   = r->in.handle;
604         r2.in.sids     = r->in.sids;
605         r2.in.names    = NULL;
606         r2.in.level    = r->in.level;
607         r2.in.count    = r->in.count;
608         r2.in.unknown1 = 0;
609         r2.in.unknown2 = 0;
610         r2.out.count   = r->out.count;
611
612         status = lsa_LookupSids2(dce_call, mem_ctx, &r2);
613         if (dce_call->fault_code != 0) {
614                 return status;
615         }
616
617         r->out.domains = r2.out.domains;
618         r->out.names = talloc_p(mem_ctx, struct lsa_TransNameArray);
619         if (r->out.names == NULL) {
620                 return NT_STATUS_NO_MEMORY;
621         }
622         r->out.names->count = r2.out.names->count;
623         r->out.names->names = talloc_array_p(r->out.names, struct lsa_TranslatedName, 
624                                              r->out.names->count);
625         if (r->out.names->names == NULL) {
626                 return NT_STATUS_NO_MEMORY;
627         }
628         for (i=0;i<r->out.names->count;i++) {
629                 r->out.names->names[i].sid_type    = r2.out.names->names[i].sid_type;
630                 r->out.names->names[i].name.string = r2.out.names->names[i].name.string;
631                 r->out.names->names[i].sid_index   = r2.out.names->names[i].sid_index;
632         }
633
634         return status;
635 }
636
637
638 /* 
639   lsa_CreateSecret 
640 */
641 static NTSTATUS lsa_CreateSecret(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
642                        struct lsa_CreateSecret *r)
643 {
644         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
645 }
646
647
648 /* 
649   lsa_OpenAccount 
650 */
651 static NTSTATUS lsa_OpenAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
652                        struct lsa_OpenAccount *r)
653 {
654         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
655 }
656
657
658 /* 
659   lsa_EnumPrivsAccount 
660 */
661 static NTSTATUS lsa_EnumPrivsAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
662                        struct lsa_EnumPrivsAccount *r)
663 {
664         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
665 }
666
667
668 /* 
669   lsa_AddPrivilegesToAccount
670 */
671 static NTSTATUS lsa_AddPrivilegesToAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
672                        struct lsa_AddPrivilegesToAccount *r)
673 {
674         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
675 }
676
677
678 /* 
679   lsa_RemovePrivilegesFromAccount
680 */
681 static NTSTATUS lsa_RemovePrivilegesFromAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
682                        struct lsa_RemovePrivilegesFromAccount *r)
683 {
684         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
685 }
686
687
688 /* 
689   lsa_GetQuotasForAccount
690 */
691 static NTSTATUS lsa_GetQuotasForAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
692                        struct lsa_GetQuotasForAccount *r)
693 {
694         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
695 }
696
697
698 /* 
699   lsa_SetQuotasForAccount
700 */
701 static NTSTATUS lsa_SetQuotasForAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
702                        struct lsa_SetQuotasForAccount *r)
703 {
704         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
705 }
706
707
708 /* 
709   lsa_GetSystemAccessAccount
710 */
711 static NTSTATUS lsa_GetSystemAccessAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
712                        struct lsa_GetSystemAccessAccount *r)
713 {
714         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
715 }
716
717
718 /* 
719   lsa_SetSystemAccessAccount
720 */
721 static NTSTATUS lsa_SetSystemAccessAccount(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
722                        struct lsa_SetSystemAccessAccount *r)
723 {
724         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
725 }
726
727
728 /* 
729   lsa_OpenTrustedDomain
730 */
731 static NTSTATUS lsa_OpenTrustedDomain(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
732                        struct lsa_OpenTrustedDomain *r)
733 {
734         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
735 }
736
737
738 /* 
739   lsa_QueryTrustedDomainInfo
740 */
741 static NTSTATUS lsa_QueryTrustedDomainInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
742                        struct lsa_QueryTrustedDomainInfo *r)
743 {
744         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
745 }
746
747
748 /* 
749   lsa_SetInformationTrustedDomain
750 */
751 static NTSTATUS lsa_SetInformationTrustedDomain(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
752                        struct lsa_SetInformationTrustedDomain *r)
753 {
754         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
755 }
756
757
758 /* 
759   lsa_OpenSecret 
760 */
761 static NTSTATUS lsa_OpenSecret(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
762                        struct lsa_OpenSecret *r)
763 {
764         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
765 }
766
767
768 /* 
769   lsa_SetSecret 
770 */
771 static NTSTATUS lsa_SetSecret(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
772                        struct lsa_SetSecret *r)
773 {
774         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
775 }
776
777
778 /* 
779   lsa_QuerySecret 
780 */
781 static NTSTATUS lsa_QuerySecret(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
782                        struct lsa_QuerySecret *r)
783 {
784         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
785 }
786
787
788 /* 
789   lsa_LookupPrivValue
790 */
791 static NTSTATUS lsa_LookupPrivValue(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
792                        struct lsa_LookupPrivValue *r)
793 {
794         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
795 }
796
797
798 /* 
799   lsa_LookupPrivName 
800 */
801 static NTSTATUS lsa_LookupPrivName(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
802                        struct lsa_LookupPrivName *r)
803 {
804         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
805 }
806
807
808 /* 
809   lsa_LookupPrivDisplayName
810 */
811 static NTSTATUS lsa_LookupPrivDisplayName(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
812                        struct lsa_LookupPrivDisplayName *r)
813 {
814         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
815 }
816
817
818 /* 
819   lsa_DeleteObject
820 */
821 static NTSTATUS lsa_DeleteObject(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
822                        struct lsa_DeleteObject *r)
823 {
824         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
825 }
826
827
828 /* 
829   lsa_EnumAccountsWithUserRight
830 */
831 static NTSTATUS lsa_EnumAccountsWithUserRight(struct dcesrv_call_state *dce_call, 
832                                               TALLOC_CTX *mem_ctx,
833                                               struct lsa_EnumAccountsWithUserRight *r)
834 {
835         struct dcesrv_handle *h;
836         struct lsa_policy_state *state;
837         int ret, i;
838         struct ldb_message **res;
839         const char * const attrs[] = { "objectSid", NULL};
840         const char *privname;
841
842         DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
843
844         state = h->data;
845
846         if (r->in.name == NULL) {
847                 return NT_STATUS_NO_SUCH_PRIVILEGE;
848         } 
849
850         privname = r->in.name->string;
851         if (sec_privilege_id(privname) == -1) {
852                 return NT_STATUS_NO_SUCH_PRIVILEGE;
853         }
854
855         ret = samdb_search(state->sam_ctx, mem_ctx, NULL, &res, attrs, 
856                            "privilege=%s", privname);
857         if (ret <= 0) {
858                 return NT_STATUS_NO_SUCH_USER;
859         }
860
861         r->out.sids->sids = talloc_array_p(r->out.sids, struct lsa_SidPtr, ret);
862         if (r->out.sids->sids == NULL) {
863                 return NT_STATUS_NO_MEMORY;
864         }
865         for (i=0;i<ret;i++) {
866                 const char *sidstr;
867                 sidstr = samdb_result_string(res[i], "objectSid", NULL);
868                 if (sidstr == NULL) {
869                         return NT_STATUS_NO_MEMORY;
870                 }
871                 r->out.sids->sids[i].sid = dom_sid_parse_talloc(r->out.sids->sids,
872                                                                 sidstr);
873                 if (r->out.sids->sids[i].sid == NULL) {
874                         return NT_STATUS_NO_MEMORY;
875                 }
876         }
877         r->out.sids->num_sids = ret;
878
879         return NT_STATUS_OK;
880 }
881
882
883 /* 
884   lsa_EnumAccountRights 
885 */
886 static NTSTATUS lsa_EnumAccountRights(struct dcesrv_call_state *dce_call, 
887                                       TALLOC_CTX *mem_ctx,
888                                       struct lsa_EnumAccountRights *r)
889 {
890         struct dcesrv_handle *h;
891         struct lsa_policy_state *state;
892         int ret, i;
893         struct ldb_message **res;
894         const char * const attrs[] = { "privilege", NULL};
895         const char *sidstr;
896         struct ldb_message_element *el;
897
898         DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
899
900         state = h->data;
901
902         sidstr = dom_sid_string(mem_ctx, r->in.sid);
903         if (sidstr == NULL) {
904                 return NT_STATUS_NO_MEMORY;
905         }
906
907         ret = samdb_search(state->sam_ctx, mem_ctx, NULL, &res, attrs, 
908                            "objectSid=%s", sidstr);
909         if (ret != 1) {
910                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
911         }
912
913         el = ldb_msg_find_element(res[0], "privilege");
914         if (el == NULL || el->num_values == 0) {
915                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
916         }
917
918         r->out.rights->count = el->num_values;
919         r->out.rights->names = talloc_array_p(r->out.rights, 
920                                               struct lsa_String, r->out.rights->count);
921         if (r->out.rights->names == NULL) {
922                 return NT_STATUS_NO_MEMORY;
923         }
924
925         for (i=0;i<el->num_values;i++) {
926                 r->out.rights->names[i].string = el->values[i].data;
927         }
928
929         return NT_STATUS_OK;
930 }
931
932
933 /* 
934   lsa_AddAccountRights
935 */
936 static NTSTATUS lsa_AddAccountRights(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
937                        struct lsa_AddAccountRights *r)
938 {
939         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
940 }
941
942
943 /* 
944   lsa_RemoveAccountRights
945 */
946 static NTSTATUS lsa_RemoveAccountRights(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
947                        struct lsa_RemoveAccountRights *r)
948 {
949         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
950 }
951
952
953 /* 
954   lsa_QueryTrustedDomainInfoBySid
955 */
956 static NTSTATUS lsa_QueryTrustedDomainInfoBySid(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
957                                                 struct lsa_QueryTrustedDomainInfoBySid *r)
958 {
959         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
960 }
961
962
963 /* 
964   lsa_SetTrustDomainInfo
965 */
966 static NTSTATUS lsa_SetTrustDomainInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
967                        struct lsa_SetTrustDomainInfo *r)
968 {
969         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
970 }
971
972
973 /* 
974   lsa_DeleteTrustDomain
975 */
976 static NTSTATUS lsa_DeleteTrustDomain(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
977                        struct lsa_DeleteTrustDomain *r)
978 {
979         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
980 }
981
982
983 /* 
984   lsa_StorePrivateData
985 */
986 static NTSTATUS lsa_StorePrivateData(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
987                        struct lsa_StorePrivateData *r)
988 {
989         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
990 }
991
992
993 /* 
994   lsa_RetrievePrivateData
995 */
996 static NTSTATUS lsa_RetrievePrivateData(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
997                        struct lsa_RetrievePrivateData *r)
998 {
999         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1000 }
1001
1002
1003 /* 
1004   lsa_GetUserName
1005 */
1006 static NTSTATUS lsa_GetUserName(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1007                        struct lsa_GetUserName *r)
1008 {
1009         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1010 }
1011
1012 /*
1013   lsa_SetInfoPolicy2
1014 */
1015 static NTSTATUS lsa_SetInfoPolicy2(struct dcesrv_call_state *dce_call,
1016                                    TALLOC_CTX *mem_ctx,
1017                                    struct lsa_SetInfoPolicy2 *r)
1018 {
1019         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1020 }
1021
1022 /*
1023   lsa_QueryTrustedDomainInfoByName
1024 */
1025 static NTSTATUS lsa_QueryTrustedDomainInfoByName(struct dcesrv_call_state *dce_call,
1026                                                  TALLOC_CTX *mem_ctx,
1027                                                  struct lsa_QueryTrustedDomainInfoByName *r)
1028 {
1029         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1030 }
1031
1032 /*
1033   lsa_SetTrustedDomainInfoByName
1034 */
1035 static NTSTATUS lsa_SetTrustedDomainInfoByName(struct dcesrv_call_state *dce_call,
1036                                                TALLOC_CTX *mem_ctx,
1037                                                struct lsa_SetTrustedDomainInfoByName *r)
1038 {
1039         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1040 }
1041
1042 /*
1043   lsa_EnumTrustedDomainsEx
1044 */
1045 static NTSTATUS lsa_EnumTrustedDomainsEx(struct dcesrv_call_state *dce_call,
1046                                          TALLOC_CTX *mem_ctx,
1047                                          struct lsa_EnumTrustedDomainsEx *r)
1048 {
1049         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1050 }
1051
1052 /*
1053   lsa_CreateTrustedDomainEx
1054 */
1055 static NTSTATUS lsa_CreateTrustedDomainEx(struct dcesrv_call_state *dce_call,
1056                                           TALLOC_CTX *mem_ctx,
1057                                           struct lsa_CreateTrustedDomainEx *r)
1058 {
1059         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1060 }
1061
1062 /*
1063   lsa_CloseTrustedDomainEx
1064 */
1065 static NTSTATUS lsa_CloseTrustedDomainEx(struct dcesrv_call_state *dce_call,
1066                                          TALLOC_CTX *mem_ctx,
1067                                          struct lsa_CloseTrustedDomainEx *r)
1068 {
1069         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1070 }
1071
1072 /*
1073   lsa_QueryDomainInformationPolicy
1074 */
1075 static NTSTATUS lsa_QueryDomainInformationPolicy(struct dcesrv_call_state *dce_call,
1076                                                  TALLOC_CTX *mem_ctx,
1077                                                  struct lsa_QueryDomainInformationPolicy *r)
1078 {
1079         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1080 }
1081
1082 /*
1083   lsa_SetDomInfoPolicy
1084 */
1085 static NTSTATUS lsa_SetDomInfoPolicy(struct dcesrv_call_state *dce_call,
1086                                      TALLOC_CTX *mem_ctx,
1087                                      struct lsa_SetDomInfoPolicy *r)
1088 {
1089         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1090 }
1091
1092 /*
1093   lsa_OpenTrustedDomainByName
1094 */
1095 static NTSTATUS lsa_OpenTrustedDomainByName(struct dcesrv_call_state *dce_call,
1096                                             TALLOC_CTX *mem_ctx,
1097                                             struct lsa_OpenTrustedDomainByName *r)
1098 {
1099         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1100 }
1101
1102 /*
1103   lsa_TestCall
1104 */
1105 static NTSTATUS lsa_TestCall(struct dcesrv_call_state *dce_call,
1106                              TALLOC_CTX *mem_ctx,
1107                              struct lsa_TestCall *r)
1108 {
1109         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1110 }
1111
1112 /*
1113   lookup a SID for 1 name
1114 */
1115 static NTSTATUS lsa_lookup_name(struct lsa_policy_state *state, TALLOC_CTX *mem_ctx,
1116                                 const char *name, struct dom_sid **sid, uint32_t *atype)
1117 {
1118         int ret;
1119         struct ldb_message **res;
1120         const char * const attrs[] = { "objectSid", "sAMAccountType", NULL};
1121
1122         ret = samdb_search(state->sam_ctx, mem_ctx, NULL, &res, attrs, "sAMAccountName=%s", name);
1123         if (ret == 1) {
1124                 const char *sid_str = ldb_msg_find_string(res[0], "objectSid", NULL);
1125                 if (sid_str == NULL) {
1126                         return NT_STATUS_INVALID_SID;
1127                 }
1128
1129                 *sid = dom_sid_parse_talloc(mem_ctx, sid_str);
1130                 if (*sid == NULL) {
1131                         return NT_STATUS_INVALID_SID;
1132                 }
1133
1134                 *atype = samdb_result_uint(res[0], "sAMAccountType", 0);
1135
1136                 return NT_STATUS_OK;
1137         }
1138
1139         /* need to add a call into sidmap to check for a allocated sid */
1140
1141         return NT_STATUS_INVALID_SID;
1142 }
1143
1144 /*
1145   lsa_LookupNames2
1146 */
1147 static NTSTATUS lsa_LookupNames2(struct dcesrv_call_state *dce_call,
1148                                  TALLOC_CTX *mem_ctx,
1149                                  struct lsa_LookupNames2 *r)
1150 {
1151         struct lsa_policy_state *state;
1152         struct dcesrv_handle *h;
1153         int i;
1154         NTSTATUS status = NT_STATUS_OK;
1155
1156         r->out.domains = NULL;
1157
1158         DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
1159
1160         state = h->data;
1161
1162         r->out.domains = talloc_zero_p(mem_ctx,  struct lsa_RefDomainList);
1163         if (r->out.domains == NULL) {
1164                 return NT_STATUS_NO_MEMORY;
1165         }
1166
1167         r->out.sids = talloc_zero_p(mem_ctx,  struct lsa_TransSidArray2);
1168         if (r->out.sids == NULL) {
1169                 return NT_STATUS_NO_MEMORY;
1170         }
1171
1172         *r->out.count = 0;
1173
1174         r->out.sids->sids = talloc_array_p(r->out.sids, struct lsa_TranslatedSid2, 
1175                                            r->in.num_names);
1176         if (r->out.sids->sids == NULL) {
1177                 return NT_STATUS_NO_MEMORY;
1178         }
1179
1180         for (i=0;i<r->in.num_names;i++) {
1181                 const char *name = r->in.names[i].string;
1182                 struct dom_sid *sid;
1183                 uint32_t atype, rtype, sid_index;
1184                 NTSTATUS status2;
1185
1186                 r->out.sids->count++;
1187                 (*r->out.count)++;
1188
1189                 r->out.sids->sids[i].sid_type    = SID_NAME_UNKNOWN;
1190                 r->out.sids->sids[i].rid         = 0xFFFFFFFF;
1191                 r->out.sids->sids[i].sid_index   = 0xFFFFFFFF;
1192                 r->out.sids->sids[i].unknown     = 0;
1193
1194                 status2 = lsa_lookup_name(state, mem_ctx, name, &sid, &atype);
1195                 if (!NT_STATUS_IS_OK(status) || sid->num_auths == 0) {
1196                         status = STATUS_SOME_UNMAPPED;
1197                         continue;
1198                 }
1199
1200                 rtype = samdb_atype_map(atype);
1201                 if (rtype == SID_NAME_UNKNOWN) {
1202                         status = STATUS_SOME_UNMAPPED;
1203                         continue;
1204                 }
1205
1206                 status2 = lsa_authority_list(state, mem_ctx, sid, r->out.domains, &sid_index);
1207                 if (!NT_STATUS_IS_OK(status2)) {
1208                         return status2;
1209                 }
1210
1211                 r->out.sids->sids[i].sid_type    = rtype;
1212                 r->out.sids->sids[i].rid         = sid->sub_auths[sid->num_auths-1];
1213                 r->out.sids->sids[i].sid_index   = sid_index;
1214                 r->out.sids->sids[i].unknown     = 0;
1215         }
1216         
1217         return status;
1218 }
1219
1220 /* 
1221   lsa_LookupNames 
1222 */
1223 static NTSTATUS lsa_LookupNames(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1224                        struct lsa_LookupNames *r)
1225 {
1226         struct lsa_LookupNames2 r2;
1227         NTSTATUS status;
1228         int i;
1229
1230         r2.in.handle    = r->in.handle;
1231         r2.in.num_names = r->in.num_names;
1232         r2.in.names     = r->in.names;
1233         r2.in.sids      = NULL;
1234         r2.in.level     = r->in.level;
1235         r2.in.count     = r->in.count;
1236         r2.in.unknown1  = 0;
1237         r2.in.unknown2  = 0;
1238         r2.out.count    = r->out.count;
1239
1240         status = lsa_LookupNames2(dce_call, mem_ctx, &r2);
1241         if (dce_call->fault_code != 0) {
1242                 return status;
1243         }
1244
1245         r->out.domains = r2.out.domains;
1246         r->out.sids = talloc_p(mem_ctx, struct lsa_TransSidArray);
1247         if (r->out.sids == NULL) {
1248                 return NT_STATUS_NO_MEMORY;
1249         }
1250         r->out.sids->count = r2.out.sids->count;
1251         r->out.sids->sids = talloc_array_p(r->out.sids, struct lsa_TranslatedSid, 
1252                                            r->out.sids->count);
1253         if (r->out.sids->sids == NULL) {
1254                 return NT_STATUS_NO_MEMORY;
1255         }
1256         for (i=0;i<r->out.sids->count;i++) {
1257                 r->out.sids->sids[i].sid_type    = r2.out.sids->sids[i].sid_type;
1258                 r->out.sids->sids[i].rid         = r2.out.sids->sids[i].rid;
1259                 r->out.sids->sids[i].sid_index   = r2.out.sids->sids[i].sid_index;
1260         }
1261
1262         return status;
1263 }
1264
1265
1266
1267 /*
1268   lsa_CreateTrustedDomainEx2
1269 */
1270 static NTSTATUS lsa_CreateTrustedDomainEx2(struct dcesrv_call_state *dce_call,
1271                                            TALLOC_CTX *mem_ctx,
1272                                            struct lsa_CreateTrustedDomainEx2 *r)
1273 {
1274         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1275 }
1276
1277 /* include the generated boilerplate */
1278 #include "librpc/gen_ndr/ndr_lsa_s.c"