r3994: - removed the unused reference count code in lsa server
[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, TALLOC_CTX *mem_ctx,
832                        struct lsa_EnumAccountsWithUserRight *r)
833 {
834         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
835 }
836
837
838 /* 
839   lsa_EnumAccountRights 
840 */
841 static NTSTATUS lsa_EnumAccountRights(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
842                        struct lsa_EnumAccountRights *r)
843 {
844         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
845 }
846
847
848 /* 
849   lsa_AddAccountRights
850 */
851 static NTSTATUS lsa_AddAccountRights(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
852                        struct lsa_AddAccountRights *r)
853 {
854         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
855 }
856
857
858 /* 
859   lsa_RemoveAccountRights
860 */
861 static NTSTATUS lsa_RemoveAccountRights(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
862                        struct lsa_RemoveAccountRights *r)
863 {
864         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
865 }
866
867
868 /* 
869   lsa_QueryTrustedDomainInfoBySid
870 */
871 static NTSTATUS lsa_QueryTrustedDomainInfoBySid(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
872                                                 struct lsa_QueryTrustedDomainInfoBySid *r)
873 {
874         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
875 }
876
877
878 /* 
879   lsa_SetTrustDomainInfo
880 */
881 static NTSTATUS lsa_SetTrustDomainInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
882                        struct lsa_SetTrustDomainInfo *r)
883 {
884         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
885 }
886
887
888 /* 
889   lsa_DeleteTrustDomain
890 */
891 static NTSTATUS lsa_DeleteTrustDomain(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
892                        struct lsa_DeleteTrustDomain *r)
893 {
894         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
895 }
896
897
898 /* 
899   lsa_StorePrivateData
900 */
901 static NTSTATUS lsa_StorePrivateData(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
902                        struct lsa_StorePrivateData *r)
903 {
904         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
905 }
906
907
908 /* 
909   lsa_RetrievePrivateData
910 */
911 static NTSTATUS lsa_RetrievePrivateData(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
912                        struct lsa_RetrievePrivateData *r)
913 {
914         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
915 }
916
917
918 /* 
919   lsa_GetUserName
920 */
921 static NTSTATUS lsa_GetUserName(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
922                        struct lsa_GetUserName *r)
923 {
924         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
925 }
926
927 /*
928   lsa_SetInfoPolicy2
929 */
930 static NTSTATUS lsa_SetInfoPolicy2(struct dcesrv_call_state *dce_call,
931                                    TALLOC_CTX *mem_ctx,
932                                    struct lsa_SetInfoPolicy2 *r)
933 {
934         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
935 }
936
937 /*
938   lsa_QueryTrustedDomainInfoByName
939 */
940 static NTSTATUS lsa_QueryTrustedDomainInfoByName(struct dcesrv_call_state *dce_call,
941                                                  TALLOC_CTX *mem_ctx,
942                                                  struct lsa_QueryTrustedDomainInfoByName *r)
943 {
944         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
945 }
946
947 /*
948   lsa_SetTrustedDomainInfoByName
949 */
950 static NTSTATUS lsa_SetTrustedDomainInfoByName(struct dcesrv_call_state *dce_call,
951                                                TALLOC_CTX *mem_ctx,
952                                                struct lsa_SetTrustedDomainInfoByName *r)
953 {
954         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
955 }
956
957 /*
958   lsa_EnumTrustedDomainsEx
959 */
960 static NTSTATUS lsa_EnumTrustedDomainsEx(struct dcesrv_call_state *dce_call,
961                                          TALLOC_CTX *mem_ctx,
962                                          struct lsa_EnumTrustedDomainsEx *r)
963 {
964         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
965 }
966
967 /*
968   lsa_CreateTrustedDomainEx
969 */
970 static NTSTATUS lsa_CreateTrustedDomainEx(struct dcesrv_call_state *dce_call,
971                                           TALLOC_CTX *mem_ctx,
972                                           struct lsa_CreateTrustedDomainEx *r)
973 {
974         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
975 }
976
977 /*
978   lsa_CloseTrustedDomainEx
979 */
980 static NTSTATUS lsa_CloseTrustedDomainEx(struct dcesrv_call_state *dce_call,
981                                          TALLOC_CTX *mem_ctx,
982                                          struct lsa_CloseTrustedDomainEx *r)
983 {
984         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
985 }
986
987 /*
988   lsa_QueryDomainInformationPolicy
989 */
990 static NTSTATUS lsa_QueryDomainInformationPolicy(struct dcesrv_call_state *dce_call,
991                                                  TALLOC_CTX *mem_ctx,
992                                                  struct lsa_QueryDomainInformationPolicy *r)
993 {
994         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
995 }
996
997 /*
998   lsa_SetDomInfoPolicy
999 */
1000 static NTSTATUS lsa_SetDomInfoPolicy(struct dcesrv_call_state *dce_call,
1001                                      TALLOC_CTX *mem_ctx,
1002                                      struct lsa_SetDomInfoPolicy *r)
1003 {
1004         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1005 }
1006
1007 /*
1008   lsa_OpenTrustedDomainByName
1009 */
1010 static NTSTATUS lsa_OpenTrustedDomainByName(struct dcesrv_call_state *dce_call,
1011                                             TALLOC_CTX *mem_ctx,
1012                                             struct lsa_OpenTrustedDomainByName *r)
1013 {
1014         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1015 }
1016
1017 /*
1018   lsa_TestCall
1019 */
1020 static NTSTATUS lsa_TestCall(struct dcesrv_call_state *dce_call,
1021                              TALLOC_CTX *mem_ctx,
1022                              struct lsa_TestCall *r)
1023 {
1024         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1025 }
1026
1027 /*
1028   lsa_LookupNames2
1029 */
1030 static NTSTATUS lsa_LookupNames2(struct dcesrv_call_state *dce_call,
1031                                  TALLOC_CTX *mem_ctx,
1032                                  struct lsa_LookupNames2 *r)
1033 {
1034         struct lsa_policy_state *state;
1035         struct dcesrv_handle *h;
1036         int i;
1037         NTSTATUS status = NT_STATUS_OK;
1038
1039         r->out.domains = NULL;
1040
1041         DCESRV_PULL_HANDLE(h, r->in.handle, LSA_HANDLE_POLICY);
1042
1043         state = h->data;
1044
1045         r->out.domains = talloc_zero_p(mem_ctx,  struct lsa_RefDomainList);
1046         if (r->out.domains == NULL) {
1047                 return NT_STATUS_NO_MEMORY;
1048         }
1049
1050         r->out.sids = talloc_zero_p(mem_ctx,  struct lsa_TransSidArray2);
1051         if (r->out.sids == NULL) {
1052                 return NT_STATUS_NO_MEMORY;
1053         }
1054
1055         *r->out.count = 0;
1056
1057         r->out.sids->sids = talloc_array_p(r->out.sids, struct lsa_TranslatedSid2, 
1058                                            r->in.num_names);
1059         if (r->out.sids->sids == NULL) {
1060                 return NT_STATUS_NO_MEMORY;
1061         }
1062
1063         for (i=0;i<r->in.num_names;i++) {
1064                 const char * const attrs[] = { "objectSid", "sAMAccountType", NULL};
1065                 const char *name = r->in.names[i].string;
1066                 int ret;
1067                 const char *sid_str;
1068                 struct ldb_message **res;
1069                 struct dom_sid *sid;
1070                 uint32_t atype, rtype, sid_index;
1071                 NTSTATUS status2;
1072
1073                 r->out.sids->count++;
1074                 (*r->out.count)++;
1075
1076                 r->out.sids->sids[i].sid_type    = SID_NAME_UNKNOWN;
1077                 r->out.sids->sids[i].rid         = 0xFFFFFFFF;
1078                 r->out.sids->sids[i].sid_index   = 0xFFFFFFFF;
1079                 r->out.sids->sids[i].unknown     = 0;
1080
1081                 ret = samdb_search(state->sam_ctx, mem_ctx, NULL, &res, attrs, "sAMAccountName=%s", name);
1082                 if (ret != 1) {
1083                         status = STATUS_SOME_UNMAPPED;
1084                         continue;
1085                 }
1086
1087                 sid_str = ldb_msg_find_string(res[0], "objectSid", NULL);
1088                 if (sid_str == NULL) {
1089                         status = STATUS_SOME_UNMAPPED;
1090                         continue;
1091                 }
1092
1093                 sid = dom_sid_parse_talloc(mem_ctx, sid_str);
1094                 if (sid == NULL || sid->num_auths == 0) {
1095                         status = STATUS_SOME_UNMAPPED;
1096                         continue;
1097                 }
1098
1099                 atype = samdb_result_uint(res[0], "sAMAccountType", 0);
1100                 if (atype == 0) {
1101                         status = STATUS_SOME_UNMAPPED;
1102                         continue;
1103                 }
1104
1105                 rtype = samdb_atype_map(atype);
1106                 if (rtype == SID_NAME_UNKNOWN) {
1107                         status = STATUS_SOME_UNMAPPED;
1108                         continue;
1109                 }
1110
1111                 status2 = lsa_authority_list(state, mem_ctx, sid, r->out.domains, &sid_index);
1112                 if (!NT_STATUS_IS_OK(status2)) {
1113                         return status2;
1114                 }
1115
1116                 r->out.sids->sids[i].sid_type    = rtype;
1117                 r->out.sids->sids[i].rid         = sid->sub_auths[sid->num_auths-1];
1118                 r->out.sids->sids[i].sid_index   = sid_index;
1119                 r->out.sids->sids[i].unknown     = 0;
1120         }
1121         
1122         return status;
1123 }
1124
1125 /* 
1126   lsa_LookupNames 
1127 */
1128 static NTSTATUS lsa_LookupNames(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1129                        struct lsa_LookupNames *r)
1130 {
1131         struct lsa_LookupNames2 r2;
1132         NTSTATUS status;
1133         int i;
1134
1135         r2.in.handle    = r->in.handle;
1136         r2.in.num_names = r->in.num_names;
1137         r2.in.names     = r->in.names;
1138         r2.in.sids      = NULL;
1139         r2.in.level     = r->in.level;
1140         r2.in.count     = r->in.count;
1141         r2.in.unknown1  = 0;
1142         r2.in.unknown2  = 0;
1143         r2.out.count    = r->out.count;
1144
1145         status = lsa_LookupNames2(dce_call, mem_ctx, &r2);
1146         if (dce_call->fault_code != 0) {
1147                 return status;
1148         }
1149
1150         r->out.domains = r2.out.domains;
1151         r->out.sids = talloc_p(mem_ctx, struct lsa_TransSidArray);
1152         if (r->out.sids == NULL) {
1153                 return NT_STATUS_NO_MEMORY;
1154         }
1155         r->out.sids->count = r2.out.sids->count;
1156         r->out.sids->sids = talloc_array_p(r->out.sids, struct lsa_TranslatedSid, 
1157                                            r->out.sids->count);
1158         if (r->out.sids->sids == NULL) {
1159                 return NT_STATUS_NO_MEMORY;
1160         }
1161         for (i=0;i<r->out.sids->count;i++) {
1162                 r->out.sids->sids[i].sid_type    = r2.out.sids->sids[i].sid_type;
1163                 r->out.sids->sids[i].rid         = r2.out.sids->sids[i].rid;
1164                 r->out.sids->sids[i].sid_index   = r2.out.sids->sids[i].sid_index;
1165         }
1166
1167         return status;
1168 }
1169
1170
1171
1172 /*
1173   lsa_CreateTrustedDomainEx2
1174 */
1175 static NTSTATUS lsa_CreateTrustedDomainEx2(struct dcesrv_call_state *dce_call,
1176                                            TALLOC_CTX *mem_ctx,
1177                                            struct lsa_CreateTrustedDomainEx2 *r)
1178 {
1179         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1180 }
1181
1182 /* include the generated boilerplate */
1183 #include "librpc/gen_ndr/ndr_lsa_s.c"