s3-lsa: Fix _lsa_EnumTrustDom() and avoid infite windows client loop.
[ira/wip.git] / source3 / rpc_server / srv_lsa_nt.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Andrew Tridgell              1992-1997,
5  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
6  *  Copyright (C) Paul Ashton                       1997,
7  *  Copyright (C) Jeremy Allison                    2001, 2006.
8  *  Copyright (C) Rafal Szczesniak                  2002,
9  *  Copyright (C) Jim McDonough <jmcd@us.ibm.com>   2002,
10  *  Copyright (C) Simo Sorce                        2003.
11  *  Copyright (C) Gerald (Jerry) Carter             2005.
12  *  Copyright (C) Volker Lendecke                   2005.
13  *  Copyright (C) Guenther Deschner                 2008.
14  *
15  *  This program is free software; you can redistribute it and/or modify
16  *  it under the terms of the GNU General Public License as published by
17  *  the Free Software Foundation; either version 3 of the License, or
18  *  (at your option) any later version.
19  *
20  *  This program is distributed in the hope that it will be useful,
21  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *  GNU General Public License for more details.
24  *
25  *  You should have received a copy of the GNU General Public License
26  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
27  */
28
29 /* This is the implementation of the lsa server code. */
30
31 #include "includes.h"
32
33 #undef DBGC_CLASS
34 #define DBGC_CLASS DBGC_RPC_SRV
35
36 #define MAX_LOOKUP_SIDS 0x5000 /* 20480 */
37
38 extern PRIVS privs[];
39
40 enum lsa_handle_type { LSA_HANDLE_POLICY_TYPE = 1, LSA_HANDLE_ACCOUNT_TYPE };
41
42 struct lsa_info {
43         DOM_SID sid;
44         uint32 access;
45         enum lsa_handle_type type;
46 };
47
48 const struct generic_mapping lsa_account_mapping = {
49         LSA_ACCOUNT_READ,
50         LSA_ACCOUNT_WRITE,
51         LSA_ACCOUNT_EXECUTE,
52         LSA_ACCOUNT_ALL_ACCESS
53 };
54
55 const struct generic_mapping lsa_policy_mapping = {
56         LSA_POLICY_READ,
57         LSA_POLICY_WRITE,
58         LSA_POLICY_EXECUTE,
59         LSA_POLICY_ALL_ACCESS
60 };
61
62 /***************************************************************************
63  init_lsa_ref_domain_list - adds a domain if it's not already in, returns the index.
64 ***************************************************************************/
65
66 static int init_lsa_ref_domain_list(TALLOC_CTX *mem_ctx,
67                                     struct lsa_RefDomainList *ref,
68                                     const char *dom_name,
69                                     DOM_SID *dom_sid)
70 {
71         int num = 0;
72
73         if (dom_name != NULL) {
74                 for (num = 0; num < ref->count; num++) {
75                         if (sid_equal(dom_sid, ref->domains[num].sid)) {
76                                 return num;
77                         }
78                 }
79         } else {
80                 num = ref->count;
81         }
82
83         if (num >= LSA_REF_DOMAIN_LIST_MULTIPLIER) {
84                 /* index not found, already at maximum domain limit */
85                 return -1;
86         }
87
88         ref->count = num + 1;
89         ref->max_size = LSA_REF_DOMAIN_LIST_MULTIPLIER;
90
91         ref->domains = TALLOC_REALLOC_ARRAY(mem_ctx, ref->domains,
92                                             struct lsa_DomainInfo, ref->count);
93         if (!ref->domains) {
94                 return -1;
95         }
96
97         ZERO_STRUCT(ref->domains[num]);
98
99         init_lsa_StringLarge(&ref->domains[num].name, dom_name);
100         ref->domains[num].sid = sid_dup_talloc(mem_ctx, dom_sid);
101         if (!ref->domains[num].sid) {
102                 return -1;
103         }
104
105         return num;
106 }
107
108
109 /***************************************************************************
110  initialize a lsa_DomainInfo structure.
111  ***************************************************************************/
112
113 static void init_dom_query_3(struct lsa_DomainInfo *r,
114                              const char *name,
115                              DOM_SID *sid)
116 {
117         init_lsa_StringLarge(&r->name, name);
118         r->sid = sid;
119 }
120
121 /***************************************************************************
122  initialize a lsa_DomainInfo structure.
123  ***************************************************************************/
124
125 static void init_dom_query_5(struct lsa_DomainInfo *r,
126                              const char *name,
127                              DOM_SID *sid)
128 {
129         init_lsa_StringLarge(&r->name, name);
130         r->sid = sid;
131 }
132
133 /***************************************************************************
134  lookup_lsa_rids. Must be called as root for lookup_name to work.
135  ***************************************************************************/
136
137 static NTSTATUS lookup_lsa_rids(TALLOC_CTX *mem_ctx,
138                                 struct lsa_RefDomainList *ref,
139                                 struct lsa_TranslatedSid *prid,
140                                 uint32_t num_entries,
141                                 struct lsa_String *name,
142                                 int flags,
143                                 uint32_t *pmapped_count)
144 {
145         uint32 mapped_count, i;
146
147         SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
148
149         mapped_count = 0;
150         *pmapped_count = 0;
151
152         for (i = 0; i < num_entries; i++) {
153                 DOM_SID sid;
154                 uint32 rid;
155                 int dom_idx;
156                 const char *full_name;
157                 const char *domain;
158                 enum lsa_SidType type = SID_NAME_UNKNOWN;
159
160                 /* Split name into domain and user component */
161
162                 /* follow w2k8 behavior and return the builtin domain when no
163                  * input has been passed in */
164
165                 if (name[i].string) {
166                         full_name = name[i].string;
167                 } else {
168                         full_name = "BUILTIN";
169                 }
170
171                 DEBUG(5, ("lookup_lsa_rids: looking up name %s\n", full_name));
172
173                 /* We can ignore the result of lookup_name, it will not touch
174                    "type" if it's not successful */
175
176                 lookup_name(mem_ctx, full_name, flags, &domain, NULL,
177                             &sid, &type);
178
179                 switch (type) {
180                 case SID_NAME_USER:
181                 case SID_NAME_DOM_GRP:
182                 case SID_NAME_DOMAIN:
183                 case SID_NAME_ALIAS:
184                 case SID_NAME_WKN_GRP:
185                         DEBUG(5, ("init_lsa_rids: %s found\n", full_name));
186                         /* Leave these unchanged */
187                         break;
188                 default:
189                         /* Don't hand out anything but the list above */
190                         DEBUG(5, ("init_lsa_rids: %s not found\n", full_name));
191                         type = SID_NAME_UNKNOWN;
192                         break;
193                 }
194
195                 rid = 0;
196                 dom_idx = -1;
197
198                 if (type != SID_NAME_UNKNOWN) {
199                         if (type == SID_NAME_DOMAIN) {
200                                 rid = (uint32_t)-1;
201                         } else {
202                                 sid_split_rid(&sid, &rid);
203                         }
204                         dom_idx = init_lsa_ref_domain_list(mem_ctx, ref, domain, &sid);
205                         mapped_count++;
206                 }
207
208                 prid[i].sid_type        = type;
209                 prid[i].rid             = rid;
210                 prid[i].sid_index       = dom_idx;
211         }
212
213         *pmapped_count = mapped_count;
214         return NT_STATUS_OK;
215 }
216
217 /***************************************************************************
218  lookup_lsa_sids. Must be called as root for lookup_name to work.
219  ***************************************************************************/
220
221 static NTSTATUS lookup_lsa_sids(TALLOC_CTX *mem_ctx,
222                                 struct lsa_RefDomainList *ref,
223                                 struct lsa_TranslatedSid3 *trans_sids,
224                                 uint32_t num_entries,
225                                 struct lsa_String *name,
226                                 int flags,
227                                 uint32 *pmapped_count)
228 {
229         uint32 mapped_count, i;
230
231         SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
232
233         mapped_count = 0;
234         *pmapped_count = 0;
235
236         for (i = 0; i < num_entries; i++) {
237                 DOM_SID sid;
238                 uint32 rid;
239                 int dom_idx;
240                 const char *full_name;
241                 const char *domain;
242                 enum lsa_SidType type = SID_NAME_UNKNOWN;
243
244                 ZERO_STRUCT(sid);
245
246                 /* Split name into domain and user component */
247
248                 full_name = name[i].string;
249                 if (full_name == NULL) {
250                         return NT_STATUS_NO_MEMORY;
251                 }
252
253                 DEBUG(5, ("init_lsa_sids: looking up name %s\n", full_name));
254
255                 /* We can ignore the result of lookup_name, it will not touch
256                    "type" if it's not successful */
257
258                 lookup_name(mem_ctx, full_name, flags, &domain, NULL,
259                             &sid, &type);
260
261                 switch (type) {
262                 case SID_NAME_USER:
263                 case SID_NAME_DOM_GRP:
264                 case SID_NAME_DOMAIN:
265                 case SID_NAME_ALIAS:
266                 case SID_NAME_WKN_GRP:
267                         DEBUG(5, ("init_lsa_sids: %s found\n", full_name));
268                         /* Leave these unchanged */
269                         break;
270                 default:
271                         /* Don't hand out anything but the list above */
272                         DEBUG(5, ("init_lsa_sids: %s not found\n", full_name));
273                         type = SID_NAME_UNKNOWN;
274                         break;
275                 }
276
277                 rid = 0;
278                 dom_idx = -1;
279
280                 if (type != SID_NAME_UNKNOWN) {
281                         DOM_SID domain_sid;
282                         sid_copy(&domain_sid, &sid);
283                         sid_split_rid(&domain_sid, &rid);
284                         dom_idx = init_lsa_ref_domain_list(mem_ctx, ref, domain, &domain_sid);
285                         mapped_count++;
286                 }
287
288                 /* Initialize the lsa_TranslatedSid3 return. */
289                 trans_sids[i].sid_type = type;
290                 trans_sids[i].sid = sid_dup_talloc(mem_ctx, &sid);
291                 trans_sids[i].sid_index = dom_idx;
292         }
293
294         *pmapped_count = mapped_count;
295         return NT_STATUS_OK;
296 }
297
298 static NTSTATUS make_lsa_object_sd(TALLOC_CTX *mem_ctx, SEC_DESC **sd, size_t *sd_size,
299                                         const struct generic_mapping *map,
300                                         DOM_SID *sid, uint32_t sid_access)
301 {
302         DOM_SID adm_sid;
303         SEC_ACE ace[5];
304         size_t i = 0;
305
306         SEC_ACL *psa = NULL;
307
308         /* READ|EXECUTE access for Everyone */
309
310         init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED,
311                         map->generic_execute | map->generic_read, 0);
312
313         /* Add Full Access 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
314
315         init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
316                         SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
317         init_sec_ace(&ace[i++], &global_sid_Builtin_Account_Operators,
318                         SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
319
320         /* Add Full Access for Domain Admins */
321         sid_copy(&adm_sid, get_global_sam_sid());
322         sid_append_rid(&adm_sid, DOMAIN_GROUP_RID_ADMINS);
323         init_sec_ace(&ace[i++], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
324                         map->generic_all, 0);
325
326         /* If we have a sid, give it some special access */
327
328         if (sid) {
329                 init_sec_ace(&ace[i++], sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
330                         sid_access, 0);
331         }
332
333         if((psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, i, ace)) == NULL)
334                 return NT_STATUS_NO_MEMORY;
335
336         if((*sd = make_sec_desc(mem_ctx, SECURITY_DESCRIPTOR_REVISION_1,
337                                 SEC_DESC_SELF_RELATIVE, &adm_sid, NULL, NULL,
338                                 psa, sd_size)) == NULL)
339                 return NT_STATUS_NO_MEMORY;
340
341         return NT_STATUS_OK;
342 }
343
344
345 /***************************************************************************
346  _lsa_OpenPolicy2
347  ***************************************************************************/
348
349 NTSTATUS _lsa_OpenPolicy2(pipes_struct *p,
350                           struct lsa_OpenPolicy2 *r)
351 {
352         struct lsa_info *info;
353         SEC_DESC *psd = NULL;
354         size_t sd_size;
355         uint32 des_access = r->in.access_mask;
356         uint32 acc_granted;
357         NTSTATUS status;
358
359         /* Work out max allowed. */
360         map_max_allowed_access(p->server_info->ptok,
361                                &p->server_info->utok,
362                                &des_access);
363
364         /* map the generic bits to the lsa policy ones */
365         se_map_generic(&des_access, &lsa_policy_mapping);
366
367         /* get the generic lsa policy SD until we store it */
368         status = make_lsa_object_sd(p->mem_ctx, &psd, &sd_size, &lsa_policy_mapping,
369                         NULL, 0);
370         if (!NT_STATUS_IS_OK(status)) {
371                 return status;
372         }
373
374         status = access_check_object(psd, p->server_info->ptok,
375                 NULL, 0, des_access,
376                 &acc_granted, "_lsa_OpenPolicy2" );
377
378         if (!NT_STATUS_IS_OK(status)) {
379                 return status;
380         }
381
382         /* associate the domain SID with the (unique) handle. */
383         info = TALLOC_ZERO_P(p->mem_ctx, struct lsa_info);
384         if (info == NULL) {
385                 return NT_STATUS_NO_MEMORY;
386         }
387
388         sid_copy(&info->sid,get_global_sam_sid());
389         info->access = acc_granted;
390         info->type = LSA_HANDLE_POLICY_TYPE;
391
392         /* set up the LSA QUERY INFO response */
393         if (!create_policy_hnd(p, r->out.handle, info))
394                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
395
396         return NT_STATUS_OK;
397 }
398
399 /***************************************************************************
400  _lsa_OpenPolicy
401  ***************************************************************************/
402
403 NTSTATUS _lsa_OpenPolicy(pipes_struct *p,
404                          struct lsa_OpenPolicy *r)
405 {
406         struct lsa_OpenPolicy2 o;
407
408         o.in.system_name        = NULL; /* should be ignored */
409         o.in.attr               = r->in.attr;
410         o.in.access_mask        = r->in.access_mask;
411
412         o.out.handle            = r->out.handle;
413
414         return _lsa_OpenPolicy2(p, &o);
415 }
416
417 /***************************************************************************
418  _lsa_EnumTrustDom - this needs fixing to do more than return NULL ! JRA.
419  ufff, done :)  mimir
420  ***************************************************************************/
421
422 NTSTATUS _lsa_EnumTrustDom(pipes_struct *p,
423                            struct lsa_EnumTrustDom *r)
424 {
425         struct lsa_info *info;
426         uint32_t count;
427         struct trustdom_info **domains;
428         struct lsa_DomainInfo *entries;
429         int i;
430         NTSTATUS nt_status;
431
432         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
433                 return NT_STATUS_INVALID_HANDLE;
434
435         if (info->type != LSA_HANDLE_POLICY_TYPE) {
436                 return NT_STATUS_INVALID_HANDLE;
437         }
438
439         /* check if the user has enough rights */
440         if (!(info->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
441                 return NT_STATUS_ACCESS_DENIED;
442
443         become_root();
444         nt_status = pdb_enum_trusteddoms(p->mem_ctx, &count, &domains);
445         unbecome_root();
446
447         if (!NT_STATUS_IS_OK(nt_status)) {
448                 return nt_status;
449         }
450
451         entries = TALLOC_ZERO_ARRAY(p->mem_ctx, struct lsa_DomainInfo, count);
452         if (!entries) {
453                 return NT_STATUS_NO_MEMORY;
454         }
455
456         for (i=0; i<count; i++) {
457                 init_lsa_StringLarge(&entries[i].name, domains[i]->name);
458                 entries[i].sid = &domains[i]->sid;
459         }
460
461         if (*r->in.resume_handle >= count) {
462                 *r->out.resume_handle = -1;
463                 TALLOC_FREE(entries);
464                 return NT_STATUS_NO_MORE_ENTRIES;
465         }
466
467         /* return the rest, limit by max_size. Note that we
468            use the w2k3 element size value of 60 */
469         r->out.domains->count = count - *r->in.resume_handle;
470         r->out.domains->count = MIN(r->out.domains->count,
471                                  1+(r->in.max_size/LSA_ENUM_TRUST_DOMAIN_MULTIPLIER));
472
473         r->out.domains->domains = entries + *r->in.resume_handle;
474
475         if (r->out.domains->count < count - *r->in.resume_handle) {
476                 *r->out.resume_handle = *r->in.resume_handle + r->out.domains->count;
477                 return STATUS_MORE_ENTRIES;
478         }
479
480         /* according to MS-LSAD 3.1.4.7.8 output resume handle MUST
481          * always be larger than the previous input resume handle, in
482          * particular when hitting the last query it is vital to set the
483          * resume handle correctly to avoid infinite client loops, as
484          * seen e.g. with Windows XP SP3 when resume handle is 0 and
485          * status is NT_STATUS_OK - gd */
486
487         *r->out.resume_handle = (uint32_t)-1;
488
489         return NT_STATUS_OK;
490 }
491
492 #define LSA_AUDIT_NUM_CATEGORIES_NT4    7
493 #define LSA_AUDIT_NUM_CATEGORIES_WIN2K  9
494 #define LSA_AUDIT_NUM_CATEGORIES LSA_AUDIT_NUM_CATEGORIES_NT4
495
496 /***************************************************************************
497  _lsa_QueryInfoPolicy
498  ***************************************************************************/
499
500 NTSTATUS _lsa_QueryInfoPolicy(pipes_struct *p,
501                               struct lsa_QueryInfoPolicy *r)
502 {
503         NTSTATUS status = NT_STATUS_OK;
504         struct lsa_info *handle;
505         DOM_SID domain_sid;
506         const char *name;
507         DOM_SID *sid = NULL;
508         union lsa_PolicyInformation *info = NULL;
509         uint32_t acc_required = 0;
510
511         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle))
512                 return NT_STATUS_INVALID_HANDLE;
513
514         if (handle->type != LSA_HANDLE_POLICY_TYPE) {
515                 return NT_STATUS_INVALID_HANDLE;
516         }
517
518         switch (r->in.level) {
519         case LSA_POLICY_INFO_AUDIT_LOG:
520         case LSA_POLICY_INFO_AUDIT_EVENTS:
521                 acc_required = LSA_POLICY_VIEW_AUDIT_INFORMATION;
522                 break;
523         case LSA_POLICY_INFO_DOMAIN:
524                 acc_required = LSA_POLICY_VIEW_LOCAL_INFORMATION;
525                 break;
526         case LSA_POLICY_INFO_PD:
527                 acc_required = LSA_POLICY_GET_PRIVATE_INFORMATION;
528                 break;
529         case LSA_POLICY_INFO_ACCOUNT_DOMAIN:
530                 acc_required = LSA_POLICY_VIEW_LOCAL_INFORMATION;
531                 break;
532         case LSA_POLICY_INFO_ROLE:
533         case LSA_POLICY_INFO_REPLICA:
534                 acc_required = LSA_POLICY_VIEW_LOCAL_INFORMATION;
535                 break;
536         case LSA_POLICY_INFO_QUOTA:
537                 acc_required = LSA_POLICY_VIEW_LOCAL_INFORMATION;
538                 break;
539         case LSA_POLICY_INFO_MOD:
540         case LSA_POLICY_INFO_AUDIT_FULL_SET:
541                 /* according to MS-LSAD 3.1.4.4.3 */
542                 return NT_STATUS_INVALID_PARAMETER;
543         case LSA_POLICY_INFO_AUDIT_FULL_QUERY:
544                 acc_required = LSA_POLICY_VIEW_AUDIT_INFORMATION;
545                 break;
546         case LSA_POLICY_INFO_DNS:
547         case LSA_POLICY_INFO_DNS_INT:
548         case LSA_POLICY_INFO_L_ACCOUNT_DOMAIN:
549                 acc_required = LSA_POLICY_VIEW_LOCAL_INFORMATION;
550                 break;
551         default:
552                 break;
553         }
554
555         if (!(handle->access & acc_required)) {
556                 /* return NT_STATUS_ACCESS_DENIED; */
557         }
558
559         info = TALLOC_ZERO_P(p->mem_ctx, union lsa_PolicyInformation);
560         if (!info) {
561                 return NT_STATUS_NO_MEMORY;
562         }
563
564         switch (r->in.level) {
565         case LSA_POLICY_INFO_AUDIT_EVENTS:
566                 {
567
568                 uint32 policy_def = LSA_AUDIT_POLICY_ALL;
569
570                 /* check if the user has enough rights */
571                 if (!(handle->access & LSA_POLICY_VIEW_AUDIT_INFORMATION)) {
572                         DEBUG(10,("_lsa_QueryInfoPolicy: insufficient access rights\n"));
573                         return NT_STATUS_ACCESS_DENIED;
574                 }
575
576                 /* fake info: We audit everything. ;) */
577
578                 info->audit_events.auditing_mode = true;
579                 info->audit_events.count = LSA_AUDIT_NUM_CATEGORIES;
580                 info->audit_events.settings = TALLOC_ZERO_ARRAY(p->mem_ctx,
581                                                                 enum lsa_PolicyAuditPolicy,
582                                                                 info->audit_events.count);
583                 if (!info->audit_events.settings) {
584                         return NT_STATUS_NO_MEMORY;
585                 }
586
587                 info->audit_events.settings[LSA_AUDIT_CATEGORY_ACCOUNT_MANAGEMENT] = policy_def;
588                 info->audit_events.settings[LSA_AUDIT_CATEGORY_FILE_AND_OBJECT_ACCESS] = policy_def;
589                 info->audit_events.settings[LSA_AUDIT_CATEGORY_LOGON] = policy_def;
590                 info->audit_events.settings[LSA_AUDIT_CATEGORY_PROCCESS_TRACKING] = policy_def;
591                 info->audit_events.settings[LSA_AUDIT_CATEGORY_SECURITY_POLICY_CHANGES] = policy_def;
592                 info->audit_events.settings[LSA_AUDIT_CATEGORY_SYSTEM] = policy_def;
593                 info->audit_events.settings[LSA_AUDIT_CATEGORY_USE_OF_USER_RIGHTS] = policy_def;
594
595                 break;
596                 }
597         case LSA_POLICY_INFO_DOMAIN:
598                 /* check if the user has enough rights */
599                 if (!(handle->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
600                         return NT_STATUS_ACCESS_DENIED;
601
602                 /* Request PolicyPrimaryDomainInformation. */
603                 switch (lp_server_role()) {
604                         case ROLE_DOMAIN_PDC:
605                         case ROLE_DOMAIN_BDC:
606                                 name = get_global_sam_name();
607                                 sid = sid_dup_talloc(p->mem_ctx, get_global_sam_sid());
608                                 if (!sid) {
609                                         return NT_STATUS_NO_MEMORY;
610                                 }
611                                 break;
612                         case ROLE_DOMAIN_MEMBER:
613                                 name = lp_workgroup();
614                                 /* We need to return the Domain SID here. */
615                                 if (secrets_fetch_domain_sid(lp_workgroup(), &domain_sid)) {
616                                         sid = sid_dup_talloc(p->mem_ctx, &domain_sid);
617                                         if (!sid) {
618                                                 return NT_STATUS_NO_MEMORY;
619                                         }
620                                 } else {
621                                         return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
622                                 }
623                                 break;
624                         case ROLE_STANDALONE:
625                                 name = lp_workgroup();
626                                 sid = NULL;
627                                 break;
628                         default:
629                                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
630                 }
631                 init_dom_query_3(&info->domain, name, sid);
632                 break;
633         case LSA_POLICY_INFO_ACCOUNT_DOMAIN:
634                 /* check if the user has enough rights */
635                 if (!(handle->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
636                         return NT_STATUS_ACCESS_DENIED;
637
638                 /* Request PolicyAccountDomainInformation. */
639                 name = get_global_sam_name();
640                 sid = get_global_sam_sid();
641
642                 init_dom_query_5(&info->account_domain, name, sid);
643                 break;
644         case LSA_POLICY_INFO_ROLE:
645                 /* check if the user has enough rights */
646                 if (!(handle->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
647                         return NT_STATUS_ACCESS_DENIED;
648
649                 switch (lp_server_role()) {
650                         case ROLE_DOMAIN_BDC:
651                                 /*
652                                  * only a BDC is a backup controller
653                                  * of the domain, it controls.
654                                  */
655                                 info->role.role = LSA_ROLE_BACKUP;
656                                 break;
657                         default:
658                                 /*
659                                  * any other role is a primary
660                                  * of the domain, it controls.
661                                  */
662                                 info->role.role = LSA_ROLE_PRIMARY;
663                                 break;
664                 }
665                 break;
666         case LSA_POLICY_INFO_DNS:
667         case LSA_POLICY_INFO_DNS_INT: {
668                 struct pdb_domain_info *dominfo;
669
670                 if ((pdb_capabilities() & PDB_CAP_ADS) == 0) {
671                         DEBUG(10, ("Not replying to LSA_POLICY_INFO_DNS "
672                                    "without ADS passdb backend\n"));
673                         status = NT_STATUS_INVALID_INFO_CLASS;
674                         break;
675                 }
676
677                 dominfo = pdb_get_domain_info(info);
678                 if (dominfo == NULL) {
679                         status = NT_STATUS_NO_MEMORY;
680                         break;
681                 }
682
683                 init_lsa_StringLarge(&info->dns.name,
684                                      dominfo->name);
685                 init_lsa_StringLarge(&info->dns.dns_domain,
686                                      dominfo->dns_domain);
687                 init_lsa_StringLarge(&info->dns.dns_forest,
688                                      dominfo->dns_forest);
689                 info->dns.domain_guid = dominfo->guid;
690                 info->dns.sid = &dominfo->sid;
691                 break;
692         }
693         default:
694                 DEBUG(0,("_lsa_QueryInfoPolicy: unknown info level in Lsa Query: %d\n",
695                         r->in.level));
696                 status = NT_STATUS_INVALID_INFO_CLASS;
697                 break;
698         }
699
700         *r->out.info = info;
701
702         return status;
703 }
704
705 /***************************************************************************
706  _lsa_QueryInfoPolicy2
707  ***************************************************************************/
708
709 NTSTATUS _lsa_QueryInfoPolicy2(pipes_struct *p,
710                                struct lsa_QueryInfoPolicy2 *r2)
711 {
712         struct lsa_QueryInfoPolicy r;
713
714         if ((pdb_capabilities() & PDB_CAP_ADS) == 0) {
715                 p->rng_fault_state = True;
716                 return NT_STATUS_NOT_IMPLEMENTED;
717         }
718
719         ZERO_STRUCT(r);
720         r.in.handle = r2->in.handle;
721         r.in.level = r2->in.level;
722         r.out.info = r2->out.info;
723
724         return _lsa_QueryInfoPolicy(p, &r);
725 }
726
727 /***************************************************************************
728  _lsa_lookup_sids_internal
729  ***************************************************************************/
730
731 static NTSTATUS _lsa_lookup_sids_internal(pipes_struct *p,
732                                           TALLOC_CTX *mem_ctx,
733                                           uint16_t level,                       /* input */
734                                           int num_sids,                         /* input */
735                                           struct lsa_SidPtr *sid,               /* input */
736                                           struct lsa_RefDomainList **pp_ref,    /* input/output */
737                                           struct lsa_TranslatedName2 **pp_names,/* input/output */
738                                           uint32_t *pp_mapped_count)            /* input/output */
739 {
740         NTSTATUS status;
741         int i;
742         const DOM_SID **sids = NULL;
743         struct lsa_RefDomainList *ref = NULL;
744         uint32 mapped_count = 0;
745         struct lsa_dom_info *dom_infos = NULL;
746         struct lsa_name_info *name_infos = NULL;
747         struct lsa_TranslatedName2 *names = NULL;
748
749         *pp_mapped_count = 0;
750         *pp_names = NULL;
751         *pp_ref = NULL;
752
753         if (num_sids == 0) {
754                 return NT_STATUS_OK;
755         }
756
757         sids = TALLOC_ARRAY(p->mem_ctx, const DOM_SID *, num_sids);
758         ref = TALLOC_ZERO_P(p->mem_ctx, struct lsa_RefDomainList);
759
760         if (sids == NULL || ref == NULL) {
761                 return NT_STATUS_NO_MEMORY;
762         }
763
764         for (i=0; i<num_sids; i++) {
765                 sids[i] = sid[i].sid;
766         }
767
768         status = lookup_sids(p->mem_ctx, num_sids, sids, level,
769                                   &dom_infos, &name_infos);
770
771         if (!NT_STATUS_IS_OK(status)) {
772                 return status;
773         }
774
775         names = TALLOC_ARRAY(p->mem_ctx, struct lsa_TranslatedName2, num_sids);
776         if (names == NULL) {
777                 return NT_STATUS_NO_MEMORY;
778         }
779
780         for (i=0; i<LSA_REF_DOMAIN_LIST_MULTIPLIER; i++) {
781
782                 if (!dom_infos[i].valid) {
783                         break;
784                 }
785
786                 if (init_lsa_ref_domain_list(mem_ctx, ref,
787                                              dom_infos[i].name,
788                                              &dom_infos[i].sid) != i) {
789                         DEBUG(0, ("Domain %s mentioned twice??\n",
790                                   dom_infos[i].name));
791                         return NT_STATUS_INTERNAL_ERROR;
792                 }
793         }
794
795         for (i=0; i<num_sids; i++) {
796                 struct lsa_name_info *name = &name_infos[i];
797
798                 if (name->type == SID_NAME_UNKNOWN) {
799                         fstring tmp;
800                         name->dom_idx = -1;
801                         /* Unknown sids should return the string
802                          * representation of the SID. Windows 2003 behaves
803                          * rather erratic here, in many cases it returns the
804                          * RID as 8 bytes hex, in others it returns the full
805                          * SID. We (Jerry/VL) could not figure out which the
806                          * hard cases are, so leave it with the SID.  */
807                         name->name = talloc_asprintf(p->mem_ctx, "%s",
808                                                      sid_to_fstring(tmp,
809                                                                     sids[i]));
810                         if (name->name == NULL) {
811                                 return NT_STATUS_NO_MEMORY;
812                         }
813                 } else {
814                         mapped_count += 1;
815                 }
816
817                 names[i].sid_type       = name->type;
818                 names[i].name.string    = name->name;
819                 names[i].sid_index      = name->dom_idx;
820                 names[i].unknown        = 0;
821         }
822
823         status = NT_STATUS_NONE_MAPPED;
824         if (mapped_count > 0) {
825                 status = (mapped_count < num_sids) ?
826                         STATUS_SOME_UNMAPPED : NT_STATUS_OK;
827         }
828
829         DEBUG(10, ("num_sids %d, mapped_count %d, status %s\n",
830                    num_sids, mapped_count, nt_errstr(status)));
831
832         *pp_mapped_count = mapped_count;
833         *pp_names = names;
834         *pp_ref = ref;
835
836         return status;
837 }
838
839 /***************************************************************************
840  _lsa_LookupSids
841  ***************************************************************************/
842
843 NTSTATUS _lsa_LookupSids(pipes_struct *p,
844                          struct lsa_LookupSids *r)
845 {
846         NTSTATUS status;
847         struct lsa_info *handle;
848         int num_sids = r->in.sids->num_sids;
849         uint32 mapped_count = 0;
850         struct lsa_RefDomainList *domains = NULL;
851         struct lsa_TranslatedName *names_out = NULL;
852         struct lsa_TranslatedName2 *names = NULL;
853         int i;
854
855         if ((r->in.level < 1) || (r->in.level > 6)) {
856                 return NT_STATUS_INVALID_PARAMETER;
857         }
858
859         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle)) {
860                 return NT_STATUS_INVALID_HANDLE;
861         }
862
863         if (handle->type != LSA_HANDLE_POLICY_TYPE) {
864                 return NT_STATUS_INVALID_HANDLE;
865         }
866
867         /* check if the user has enough rights */
868         if (!(handle->access & LSA_POLICY_LOOKUP_NAMES)) {
869                 return NT_STATUS_ACCESS_DENIED;
870         }
871
872         if (num_sids >  MAX_LOOKUP_SIDS) {
873                 DEBUG(5,("_lsa_LookupSids: limit of %d exceeded, requested %d\n",
874                          MAX_LOOKUP_SIDS, num_sids));
875                 return NT_STATUS_NONE_MAPPED;
876         }
877
878         status = _lsa_lookup_sids_internal(p,
879                                            p->mem_ctx,
880                                            r->in.level,
881                                            num_sids,
882                                            r->in.sids->sids,
883                                            &domains,
884                                            &names,
885                                            &mapped_count);
886
887         /* Only return here when there is a real error.
888            NT_STATUS_NONE_MAPPED is a special case as it indicates that none of
889            the requested sids could be resolved. Older versions of XP (pre SP3)
890            rely that we return with the string representations of those SIDs in
891            that case. If we don't, XP crashes - Guenther
892            */
893
894         if (NT_STATUS_IS_ERR(status) &&
895             !NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
896                 return status;
897         }
898
899         /* Convert from lsa_TranslatedName2 to lsa_TranslatedName */
900         names_out = TALLOC_ARRAY(p->mem_ctx, struct lsa_TranslatedName,
901                                  num_sids);
902         if (!names_out) {
903                 return NT_STATUS_NO_MEMORY;
904         }
905
906         for (i=0; i<num_sids; i++) {
907                 names_out[i].sid_type = names[i].sid_type;
908                 names_out[i].name = names[i].name;
909                 names_out[i].sid_index = names[i].sid_index;
910         }
911
912         *r->out.domains = domains;
913         r->out.names->count = num_sids;
914         r->out.names->names = names_out;
915         *r->out.count = mapped_count;
916
917         return status;
918 }
919
920 /***************************************************************************
921  _lsa_LookupSids2
922  ***************************************************************************/
923
924 NTSTATUS _lsa_LookupSids2(pipes_struct *p,
925                           struct lsa_LookupSids2 *r)
926 {
927         NTSTATUS status;
928         struct lsa_info *handle;
929         int num_sids = r->in.sids->num_sids;
930         uint32 mapped_count = 0;
931         struct lsa_RefDomainList *domains = NULL;
932         struct lsa_TranslatedName2 *names = NULL;
933         bool check_policy = true;
934
935         switch (p->hdr_req.opnum) {
936                 case NDR_LSA_LOOKUPSIDS3:
937                         check_policy = false;
938                         break;
939                 case NDR_LSA_LOOKUPSIDS2:
940                 default:
941                         check_policy = true;
942         }
943
944         if ((r->in.level < 1) || (r->in.level > 6)) {
945                 return NT_STATUS_INVALID_PARAMETER;
946         }
947
948         if (check_policy) {
949                 if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle)) {
950                         return NT_STATUS_INVALID_HANDLE;
951                 }
952
953                 if (handle->type != LSA_HANDLE_POLICY_TYPE) {
954                         return NT_STATUS_INVALID_HANDLE;
955                 }
956
957                 /* check if the user has enough rights */
958                 if (!(handle->access & LSA_POLICY_LOOKUP_NAMES)) {
959                         return NT_STATUS_ACCESS_DENIED;
960                 }
961         }
962
963         if (num_sids >  MAX_LOOKUP_SIDS) {
964                 DEBUG(5,("_lsa_LookupSids2: limit of %d exceeded, requested %d\n",
965                          MAX_LOOKUP_SIDS, num_sids));
966                 return NT_STATUS_NONE_MAPPED;
967         }
968
969         status = _lsa_lookup_sids_internal(p,
970                                            p->mem_ctx,
971                                            r->in.level,
972                                            num_sids,
973                                            r->in.sids->sids,
974                                            &domains,
975                                            &names,
976                                            &mapped_count);
977
978         *r->out.domains = domains;
979         r->out.names->count = num_sids;
980         r->out.names->names = names;
981         *r->out.count = mapped_count;
982
983         return status;
984 }
985
986 /***************************************************************************
987  _lsa_LookupSids3
988  ***************************************************************************/
989
990 NTSTATUS _lsa_LookupSids3(pipes_struct *p,
991                           struct lsa_LookupSids3 *r)
992 {
993         struct lsa_LookupSids2 q;
994
995         /* No policy handle on this call. Restrict to crypto connections. */
996         if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) {
997                 DEBUG(0,("_lsa_LookupSids3: client %s not using schannel for netlogon\n",
998                         get_remote_machine_name() ));
999                 return NT_STATUS_INVALID_PARAMETER;
1000         }
1001
1002         q.in.handle             = NULL;
1003         q.in.sids               = r->in.sids;
1004         q.in.level              = r->in.level;
1005         q.in.lookup_options     = r->in.lookup_options;
1006         q.in.client_revision    = r->in.client_revision;
1007         q.in.names              = r->in.names;
1008         q.in.count              = r->in.count;
1009
1010         q.out.domains           = r->out.domains;
1011         q.out.names             = r->out.names;
1012         q.out.count             = r->out.count;
1013
1014         return _lsa_LookupSids2(p, &q);
1015 }
1016
1017 /***************************************************************************
1018  ***************************************************************************/
1019
1020 static int lsa_lookup_level_to_flags(uint16 level)
1021 {
1022         int flags;
1023
1024         switch (level) {
1025                 case 1:
1026                         flags = LOOKUP_NAME_ALL;
1027                         break;
1028                 case 2:
1029                         flags = LOOKUP_NAME_DOMAIN|LOOKUP_NAME_REMOTE|LOOKUP_NAME_ISOLATED;
1030                         break;
1031                 case 3:
1032                         flags = LOOKUP_NAME_DOMAIN|LOOKUP_NAME_ISOLATED;
1033                         break;
1034                 case 4:
1035                 case 5:
1036                 case 6:
1037                 default:
1038                         flags = LOOKUP_NAME_NONE;
1039                         break;
1040         }
1041
1042         return flags;
1043 }
1044
1045 /***************************************************************************
1046  _lsa_LookupNames
1047  ***************************************************************************/
1048
1049 NTSTATUS _lsa_LookupNames(pipes_struct *p,
1050                           struct lsa_LookupNames *r)
1051 {
1052         NTSTATUS status = NT_STATUS_NONE_MAPPED;
1053         struct lsa_info *handle;
1054         struct lsa_String *names = r->in.names;
1055         uint32 num_entries = r->in.num_names;
1056         struct lsa_RefDomainList *domains = NULL;
1057         struct lsa_TranslatedSid *rids = NULL;
1058         uint32 mapped_count = 0;
1059         int flags = 0;
1060
1061         if (num_entries >  MAX_LOOKUP_SIDS) {
1062                 num_entries = MAX_LOOKUP_SIDS;
1063                 DEBUG(5,("_lsa_LookupNames: truncating name lookup list to %d\n",
1064                         num_entries));
1065         }
1066
1067         flags = lsa_lookup_level_to_flags(r->in.level);
1068
1069         domains = TALLOC_ZERO_P(p->mem_ctx, struct lsa_RefDomainList);
1070         if (!domains) {
1071                 return NT_STATUS_NO_MEMORY;
1072         }
1073
1074         if (num_entries) {
1075                 rids = TALLOC_ZERO_ARRAY(p->mem_ctx, struct lsa_TranslatedSid,
1076                                          num_entries);
1077                 if (!rids) {
1078                         return NT_STATUS_NO_MEMORY;
1079                 }
1080         } else {
1081                 rids = NULL;
1082         }
1083
1084         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle)) {
1085                 status = NT_STATUS_INVALID_HANDLE;
1086                 goto done;
1087         }
1088
1089         if (handle->type != LSA_HANDLE_POLICY_TYPE) {
1090                 return NT_STATUS_INVALID_HANDLE;
1091         }
1092
1093         /* check if the user has enough rights */
1094         if (!(handle->access & LSA_POLICY_LOOKUP_NAMES)) {
1095                 status = NT_STATUS_ACCESS_DENIED;
1096                 goto done;
1097         }
1098
1099         /* set up the LSA Lookup RIDs response */
1100         become_root(); /* lookup_name can require root privs */
1101         status = lookup_lsa_rids(p->mem_ctx, domains, rids, num_entries,
1102                                  names, flags, &mapped_count);
1103         unbecome_root();
1104
1105 done:
1106
1107         if (NT_STATUS_IS_OK(status) && (num_entries != 0) ) {
1108                 if (mapped_count == 0) {
1109                         status = NT_STATUS_NONE_MAPPED;
1110                 } else if (mapped_count != num_entries) {
1111                         status = STATUS_SOME_UNMAPPED;
1112                 }
1113         }
1114
1115         *r->out.count = mapped_count;
1116         *r->out.domains = domains;
1117         r->out.sids->sids = rids;
1118         r->out.sids->count = num_entries;
1119
1120         return status;
1121 }
1122
1123 /***************************************************************************
1124  _lsa_LookupNames2
1125  ***************************************************************************/
1126
1127 NTSTATUS _lsa_LookupNames2(pipes_struct *p,
1128                            struct lsa_LookupNames2 *r)
1129 {
1130         NTSTATUS status;
1131         struct lsa_LookupNames q;
1132         struct lsa_TransSidArray2 *sid_array2 = r->in.sids;
1133         struct lsa_TransSidArray *sid_array = NULL;
1134         uint32_t i;
1135
1136         sid_array = TALLOC_ZERO_P(p->mem_ctx, struct lsa_TransSidArray);
1137         if (!sid_array) {
1138                 return NT_STATUS_NO_MEMORY;
1139         }
1140
1141         q.in.handle             = r->in.handle;
1142         q.in.num_names          = r->in.num_names;
1143         q.in.names              = r->in.names;
1144         q.in.level              = r->in.level;
1145         q.in.sids               = sid_array;
1146         q.in.count              = r->in.count;
1147         /* we do not know what this is for */
1148         /*                      = r->in.unknown1; */
1149         /*                      = r->in.unknown2; */
1150
1151         q.out.domains           = r->out.domains;
1152         q.out.sids              = sid_array;
1153         q.out.count             = r->out.count;
1154
1155         status = _lsa_LookupNames(p, &q);
1156
1157         sid_array2->count = sid_array->count;
1158         sid_array2->sids = TALLOC_ARRAY(p->mem_ctx, struct lsa_TranslatedSid2, sid_array->count);
1159         if (!sid_array2->sids) {
1160                 return NT_STATUS_NO_MEMORY;
1161         }
1162
1163         for (i=0; i<sid_array->count; i++) {
1164                 sid_array2->sids[i].sid_type  = sid_array->sids[i].sid_type;
1165                 sid_array2->sids[i].rid       = sid_array->sids[i].rid;
1166                 sid_array2->sids[i].sid_index = sid_array->sids[i].sid_index;
1167                 sid_array2->sids[i].unknown   = 0;
1168         }
1169
1170         r->out.sids = sid_array2;
1171
1172         return status;
1173 }
1174
1175 /***************************************************************************
1176  _lsa_LookupNames3
1177  ***************************************************************************/
1178
1179 NTSTATUS _lsa_LookupNames3(pipes_struct *p,
1180                            struct lsa_LookupNames3 *r)
1181 {
1182         NTSTATUS status;
1183         struct lsa_info *handle;
1184         struct lsa_String *names = r->in.names;
1185         uint32 num_entries = r->in.num_names;
1186         struct lsa_RefDomainList *domains = NULL;
1187         struct lsa_TranslatedSid3 *trans_sids = NULL;
1188         uint32 mapped_count = 0;
1189         int flags = 0;
1190         bool check_policy = true;
1191
1192         switch (p->hdr_req.opnum) {
1193                 case NDR_LSA_LOOKUPNAMES4:
1194                         check_policy = false;
1195                         break;
1196                 case NDR_LSA_LOOKUPNAMES3:
1197                 default:
1198                         check_policy = true;
1199         }
1200
1201         if (num_entries >  MAX_LOOKUP_SIDS) {
1202                 num_entries = MAX_LOOKUP_SIDS;
1203                 DEBUG(5,("_lsa_LookupNames3: truncating name lookup list to %d\n", num_entries));
1204         }
1205
1206         /* Probably the lookup_level is some sort of bitmask. */
1207         if (r->in.level == 1) {
1208                 flags = LOOKUP_NAME_ALL;
1209         }
1210
1211         domains = TALLOC_ZERO_P(p->mem_ctx, struct lsa_RefDomainList);
1212         if (!domains) {
1213                 return NT_STATUS_NO_MEMORY;
1214         }
1215
1216         if (num_entries) {
1217                 trans_sids = TALLOC_ZERO_ARRAY(p->mem_ctx, struct lsa_TranslatedSid3,
1218                                                num_entries);
1219                 if (!trans_sids) {
1220                         return NT_STATUS_NO_MEMORY;
1221                 }
1222         } else {
1223                 trans_sids = NULL;
1224         }
1225
1226         if (check_policy) {
1227
1228                 if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle)) {
1229                         status = NT_STATUS_INVALID_HANDLE;
1230                         goto done;
1231                 }
1232
1233                 if (handle->type != LSA_HANDLE_POLICY_TYPE) {
1234                         return NT_STATUS_INVALID_HANDLE;
1235                 }
1236
1237                 /* check if the user has enough rights */
1238                 if (!(handle->access & LSA_POLICY_LOOKUP_NAMES)) {
1239                         status = NT_STATUS_ACCESS_DENIED;
1240                         goto done;
1241                 }
1242         }
1243
1244         /* set up the LSA Lookup SIDs response */
1245         become_root(); /* lookup_name can require root privs */
1246         status = lookup_lsa_sids(p->mem_ctx, domains, trans_sids, num_entries,
1247                                  names, flags, &mapped_count);
1248         unbecome_root();
1249
1250 done:
1251
1252         if (NT_STATUS_IS_OK(status)) {
1253                 if (mapped_count == 0) {
1254                         status = NT_STATUS_NONE_MAPPED;
1255                 } else if (mapped_count != num_entries) {
1256                         status = STATUS_SOME_UNMAPPED;
1257                 }
1258         }
1259
1260         *r->out.count = mapped_count;
1261         *r->out.domains = domains;
1262         r->out.sids->sids = trans_sids;
1263         r->out.sids->count = num_entries;
1264
1265         return status;
1266 }
1267
1268 /***************************************************************************
1269  _lsa_LookupNames4
1270  ***************************************************************************/
1271
1272 NTSTATUS _lsa_LookupNames4(pipes_struct *p,
1273                            struct lsa_LookupNames4 *r)
1274 {
1275         struct lsa_LookupNames3 q;
1276
1277         /* No policy handle on this call. Restrict to crypto connections. */
1278         if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) {
1279                 DEBUG(0,("_lsa_lookup_names4: client %s not using schannel for netlogon\n",
1280                         get_remote_machine_name() ));
1281                 return NT_STATUS_INVALID_PARAMETER;
1282         }
1283
1284         q.in.handle             = NULL;
1285         q.in.num_names          = r->in.num_names;
1286         q.in.names              = r->in.names;
1287         q.in.level              = r->in.level;
1288         q.in.lookup_options     = r->in.lookup_options;
1289         q.in.client_revision    = r->in.client_revision;
1290         q.in.sids               = r->in.sids;
1291         q.in.count              = r->in.count;
1292
1293         q.out.domains           = r->out.domains;
1294         q.out.sids              = r->out.sids;
1295         q.out.count             = r->out.count;
1296
1297         return _lsa_LookupNames3(p, &q);
1298 }
1299
1300 /***************************************************************************
1301  _lsa_close. Also weird - needs to check if lsa handle is correct. JRA.
1302  ***************************************************************************/
1303
1304 NTSTATUS _lsa_Close(pipes_struct *p, struct lsa_Close *r)
1305 {
1306         if (!find_policy_by_hnd(p, r->in.handle, NULL)) {
1307                 return NT_STATUS_INVALID_HANDLE;
1308         }
1309
1310         close_policy_hnd(p, r->in.handle);
1311         ZERO_STRUCTP(r->out.handle);
1312         return NT_STATUS_OK;
1313 }
1314
1315 /***************************************************************************
1316  ***************************************************************************/
1317
1318 NTSTATUS _lsa_OpenSecret(pipes_struct *p, struct lsa_OpenSecret *r)
1319 {
1320         return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1321 }
1322
1323 /***************************************************************************
1324  ***************************************************************************/
1325
1326 NTSTATUS _lsa_OpenTrustedDomain(pipes_struct *p, struct lsa_OpenTrustedDomain *r)
1327 {
1328         return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1329 }
1330
1331 /***************************************************************************
1332  ***************************************************************************/
1333
1334 NTSTATUS _lsa_CreateTrustedDomain(pipes_struct *p, struct lsa_CreateTrustedDomain *r)
1335 {
1336         return NT_STATUS_ACCESS_DENIED;
1337 }
1338
1339 /***************************************************************************
1340  ***************************************************************************/
1341
1342 NTSTATUS _lsa_CreateSecret(pipes_struct *p, struct lsa_CreateSecret *r)
1343 {
1344         return NT_STATUS_ACCESS_DENIED;
1345 }
1346
1347 /***************************************************************************
1348  ***************************************************************************/
1349
1350 NTSTATUS _lsa_SetSecret(pipes_struct *p, struct lsa_SetSecret *r)
1351 {
1352         return NT_STATUS_ACCESS_DENIED;
1353 }
1354
1355 /***************************************************************************
1356  _lsa_DeleteObject
1357  ***************************************************************************/
1358
1359 NTSTATUS _lsa_DeleteObject(pipes_struct *p,
1360                            struct lsa_DeleteObject *r)
1361 {
1362         NTSTATUS status;
1363         struct lsa_info *info = NULL;
1364
1365         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info)) {
1366                 return NT_STATUS_INVALID_HANDLE;
1367         }
1368
1369         if (!(info->access & STD_RIGHT_DELETE_ACCESS)) {
1370                 return NT_STATUS_ACCESS_DENIED;
1371         }
1372
1373         switch (info->type) {
1374         case LSA_HANDLE_ACCOUNT_TYPE:
1375                 status = privilege_delete_account(&info->sid);
1376                 if (!NT_STATUS_IS_OK(status)) {
1377                         DEBUG(10,("_lsa_DeleteObject: privilege_delete_account gave: %s\n",
1378                                 nt_errstr(status)));
1379                         return status;
1380                 }
1381                 break;
1382         default:
1383                 return NT_STATUS_INVALID_HANDLE;
1384         }
1385
1386         close_policy_hnd(p, r->in.handle);
1387         ZERO_STRUCTP(r->out.handle);
1388
1389         return status;
1390 }
1391
1392 /***************************************************************************
1393  _lsa_EnumPrivs
1394  ***************************************************************************/
1395
1396 NTSTATUS _lsa_EnumPrivs(pipes_struct *p,
1397                         struct lsa_EnumPrivs *r)
1398 {
1399         struct lsa_info *handle;
1400         uint32 i;
1401         uint32 enum_context = *r->in.resume_handle;
1402         int num_privs = count_all_privileges();
1403         struct lsa_PrivEntry *entries = NULL;
1404         LUID_ATTR luid;
1405
1406         /* remember that the enum_context starts at 0 and not 1 */
1407
1408         if ( enum_context >= num_privs )
1409                 return NT_STATUS_NO_MORE_ENTRIES;
1410
1411         DEBUG(10,("_lsa_EnumPrivs: enum_context:%d total entries:%d\n",
1412                 enum_context, num_privs));
1413
1414         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle))
1415                 return NT_STATUS_INVALID_HANDLE;
1416
1417         if (handle->type != LSA_HANDLE_POLICY_TYPE) {
1418                 return NT_STATUS_INVALID_HANDLE;
1419         }
1420
1421         /* check if the user has enough rights
1422            I don't know if it's the right one. not documented.  */
1423
1424         if (!(handle->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
1425                 return NT_STATUS_ACCESS_DENIED;
1426
1427         if (num_privs) {
1428                 entries = TALLOC_ZERO_ARRAY(p->mem_ctx, struct lsa_PrivEntry, num_privs);
1429                 if (!entries) {
1430                         return NT_STATUS_NO_MEMORY;
1431                 }
1432         } else {
1433                 entries = NULL;
1434         }
1435
1436         for (i = 0; i < num_privs; i++) {
1437                 if( i < enum_context) {
1438
1439                         init_lsa_StringLarge(&entries[i].name, NULL);
1440
1441                         entries[i].luid.low = 0;
1442                         entries[i].luid.high = 0;
1443                 } else {
1444
1445                         init_lsa_StringLarge(&entries[i].name, privs[i].name);
1446
1447                         luid = get_privilege_luid( &privs[i].se_priv );
1448
1449                         entries[i].luid.low = luid.luid.low;
1450                         entries[i].luid.high = luid.luid.high;
1451                 }
1452         }
1453
1454         enum_context = num_privs;
1455
1456         *r->out.resume_handle = enum_context;
1457         r->out.privs->count = num_privs;
1458         r->out.privs->privs = entries;
1459
1460         return NT_STATUS_OK;
1461 }
1462
1463 /***************************************************************************
1464  _lsa_LookupPrivDisplayName
1465  ***************************************************************************/
1466
1467 NTSTATUS _lsa_LookupPrivDisplayName(pipes_struct *p,
1468                                     struct lsa_LookupPrivDisplayName *r)
1469 {
1470         struct lsa_info *handle;
1471         const char *description;
1472         struct lsa_StringLarge *lsa_name;
1473
1474         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle))
1475                 return NT_STATUS_INVALID_HANDLE;
1476
1477         if (handle->type != LSA_HANDLE_POLICY_TYPE) {
1478                 return NT_STATUS_INVALID_HANDLE;
1479         }
1480
1481         /* check if the user has enough rights */
1482
1483         /*
1484          * I don't know if it's the right one. not documented.
1485          */
1486         if (!(handle->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
1487                 return NT_STATUS_ACCESS_DENIED;
1488
1489         DEBUG(10,("_lsa_LookupPrivDisplayName: name = %s\n", r->in.name->string));
1490
1491         description = get_privilege_dispname(r->in.name->string);
1492         if (!description) {
1493                 DEBUG(10,("_lsa_LookupPrivDisplayName: doesn't exist\n"));
1494                 return NT_STATUS_NO_SUCH_PRIVILEGE;
1495         }
1496
1497         DEBUG(10,("_lsa_LookupPrivDisplayName: display name = %s\n", description));
1498
1499         lsa_name = TALLOC_ZERO_P(p->mem_ctx, struct lsa_StringLarge);
1500         if (!lsa_name) {
1501                 return NT_STATUS_NO_MEMORY;
1502         }
1503
1504         init_lsa_StringLarge(lsa_name, description);
1505
1506         *r->out.returned_language_id = r->in.language_id;
1507         *r->out.disp_name = lsa_name;
1508
1509         return NT_STATUS_OK;
1510 }
1511
1512 /***************************************************************************
1513  _lsa_EnumAccounts
1514  ***************************************************************************/
1515
1516 NTSTATUS _lsa_EnumAccounts(pipes_struct *p,
1517                            struct lsa_EnumAccounts *r)
1518 {
1519         struct lsa_info *handle;
1520         DOM_SID *sid_list;
1521         int i, j, num_entries;
1522         NTSTATUS status;
1523         struct lsa_SidPtr *sids = NULL;
1524
1525         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle))
1526                 return NT_STATUS_INVALID_HANDLE;
1527
1528         if (handle->type != LSA_HANDLE_POLICY_TYPE) {
1529                 return NT_STATUS_INVALID_HANDLE;
1530         }
1531
1532         if (!(handle->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
1533                 return NT_STATUS_ACCESS_DENIED;
1534
1535         sid_list = NULL;
1536         num_entries = 0;
1537
1538         /* The only way we can currently find out all the SIDs that have been
1539            privileged is to scan all privileges */
1540
1541         status = privilege_enumerate_accounts(&sid_list, &num_entries);
1542         if (!NT_STATUS_IS_OK(status)) {
1543                 return status;
1544         }
1545
1546         if (*r->in.resume_handle >= num_entries) {
1547                 return NT_STATUS_NO_MORE_ENTRIES;
1548         }
1549
1550         if (num_entries - *r->in.resume_handle) {
1551                 sids = TALLOC_ZERO_ARRAY(p->mem_ctx, struct lsa_SidPtr,
1552                                          num_entries - *r->in.resume_handle);
1553                 if (!sids) {
1554                         talloc_free(sid_list);
1555                         return NT_STATUS_NO_MEMORY;
1556                 }
1557
1558                 for (i = *r->in.resume_handle, j = 0; i < num_entries; i++, j++) {
1559                         sids[j].sid = sid_dup_talloc(p->mem_ctx, &sid_list[i]);
1560                         if (!sids[j].sid) {
1561                                 talloc_free(sid_list);
1562                                 return NT_STATUS_NO_MEMORY;
1563                         }
1564                 }
1565         }
1566
1567         talloc_free(sid_list);
1568
1569         *r->out.resume_handle = num_entries;
1570         r->out.sids->num_sids = num_entries;
1571         r->out.sids->sids = sids;
1572
1573         return NT_STATUS_OK;
1574 }
1575
1576 /***************************************************************************
1577  _lsa_GetUserName
1578  ***************************************************************************/
1579
1580 NTSTATUS _lsa_GetUserName(pipes_struct *p,
1581                           struct lsa_GetUserName *r)
1582 {
1583         const char *username, *domname;
1584         struct lsa_String *account_name = NULL;
1585         struct lsa_String *authority_name = NULL;
1586
1587         if (r->in.account_name &&
1588            *r->in.account_name) {
1589                 return NT_STATUS_INVALID_PARAMETER;
1590         }
1591
1592         if (r->in.authority_name &&
1593            *r->in.authority_name) {
1594                 return NT_STATUS_INVALID_PARAMETER;
1595         }
1596
1597         if (p->server_info->guest) {
1598                 /*
1599                  * I'm 99% sure this is not the right place to do this,
1600                  * global_sid_Anonymous should probably be put into the token
1601                  * instead of the guest id -- vl
1602                  */
1603                 if (!lookup_sid(p->mem_ctx, &global_sid_Anonymous,
1604                                 &domname, &username, NULL)) {
1605                         return NT_STATUS_NO_MEMORY;
1606                 }
1607         } else {
1608                 username = p->server_info->sanitized_username;
1609                 domname = pdb_get_domain(p->server_info->sam_account);
1610         }
1611
1612         account_name = TALLOC_P(p->mem_ctx, struct lsa_String);
1613         if (!account_name) {
1614                 return NT_STATUS_NO_MEMORY;
1615         }
1616         init_lsa_String(account_name, username);
1617
1618         if (r->out.authority_name) {
1619                 authority_name = TALLOC_P(p->mem_ctx, struct lsa_String);
1620                 if (!authority_name) {
1621                         return NT_STATUS_NO_MEMORY;
1622                 }
1623                 init_lsa_String(authority_name, domname);
1624         }
1625
1626         *r->out.account_name = account_name;
1627         if (r->out.authority_name) {
1628                 *r->out.authority_name = authority_name;
1629         }
1630
1631         return NT_STATUS_OK;
1632 }
1633
1634 /***************************************************************************
1635  _lsa_CreateAccount
1636  ***************************************************************************/
1637
1638 NTSTATUS _lsa_CreateAccount(pipes_struct *p,
1639                             struct lsa_CreateAccount *r)
1640 {
1641         NTSTATUS status;
1642         struct lsa_info *handle;
1643         struct lsa_info *info;
1644         uint32_t acc_granted;
1645         struct security_descriptor *psd;
1646         size_t sd_size;
1647
1648         /* find the connection policy handle. */
1649         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle))
1650                 return NT_STATUS_INVALID_HANDLE;
1651
1652         if (handle->type != LSA_HANDLE_POLICY_TYPE) {
1653                 return NT_STATUS_INVALID_HANDLE;
1654         }
1655
1656         /* check if the user has enough rights */
1657
1658         if (!(handle->access & LSA_POLICY_CREATE_ACCOUNT)) {
1659                 return NT_STATUS_ACCESS_DENIED;
1660         }
1661
1662         /* Work out max allowed. */
1663         map_max_allowed_access(p->server_info->ptok,
1664                                &p->server_info->utok,
1665                                &r->in.access_mask);
1666
1667         /* map the generic bits to the lsa policy ones */
1668         se_map_generic(&r->in.access_mask, &lsa_account_mapping);
1669
1670         status = make_lsa_object_sd(p->mem_ctx, &psd, &sd_size,
1671                                     &lsa_account_mapping,
1672                                     r->in.sid, LSA_POLICY_ALL_ACCESS);
1673         if (!NT_STATUS_IS_OK(status)) {
1674                 return status;
1675         }
1676
1677         status = access_check_object(psd, p->server_info->ptok,
1678                 NULL, 0, r->in.access_mask,
1679                 &acc_granted, "_lsa_CreateAccount");
1680         if (!NT_STATUS_IS_OK(status)) {
1681                 return status;
1682         }
1683
1684         if ( is_privileged_sid( r->in.sid ) )
1685                 return NT_STATUS_OBJECT_NAME_COLLISION;
1686
1687         /* associate the user/group SID with the (unique) handle. */
1688
1689         info = TALLOC_ZERO_P(p->mem_ctx, struct lsa_info);
1690         if (info == NULL) {
1691                 return NT_STATUS_NO_MEMORY;
1692         }
1693
1694         info->sid = *r->in.sid;
1695         info->access = acc_granted;
1696         info->type = LSA_HANDLE_ACCOUNT_TYPE;
1697
1698         /* get a (unique) handle.  open a policy on it. */
1699         if (!create_policy_hnd(p, r->out.acct_handle, info))
1700                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1701
1702         return privilege_create_account( &info->sid );
1703 }
1704
1705 /***************************************************************************
1706  _lsa_OpenAccount
1707  ***************************************************************************/
1708
1709 NTSTATUS _lsa_OpenAccount(pipes_struct *p,
1710                           struct lsa_OpenAccount *r)
1711 {
1712         struct lsa_info *handle;
1713         struct lsa_info *info;
1714         SEC_DESC *psd = NULL;
1715         size_t sd_size;
1716         uint32_t des_access = r->in.access_mask;
1717         uint32_t acc_granted;
1718         NTSTATUS status;
1719
1720         /* find the connection policy handle. */
1721         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle))
1722                 return NT_STATUS_INVALID_HANDLE;
1723
1724         if (handle->type != LSA_HANDLE_POLICY_TYPE) {
1725                 return NT_STATUS_INVALID_HANDLE;
1726         }
1727
1728         /* des_access is for the account here, not the policy
1729          * handle - so don't check against policy handle. */
1730
1731         /* Work out max allowed. */
1732         map_max_allowed_access(p->server_info->ptok,
1733                                &p->server_info->utok,
1734                                &des_access);
1735
1736         /* map the generic bits to the lsa account ones */
1737         se_map_generic(&des_access, &lsa_account_mapping);
1738
1739         /* get the generic lsa account SD until we store it */
1740         status = make_lsa_object_sd(p->mem_ctx, &psd, &sd_size,
1741                                 &lsa_account_mapping,
1742                                 r->in.sid, LSA_ACCOUNT_ALL_ACCESS);
1743         if (!NT_STATUS_IS_OK(status)) {
1744                 return status;
1745         }
1746
1747         status = access_check_object(psd, p->server_info->ptok,
1748                 NULL, 0, des_access,
1749                 &acc_granted, "_lsa_OpenAccount" );
1750
1751         if (!NT_STATUS_IS_OK(status)) {
1752                 return status;
1753         }
1754
1755         /* TODO: Fis the parsing routine before reenabling this check! */
1756         #if 0
1757         if (!lookup_sid(&handle->sid, dom_name, name, &type))
1758                 return NT_STATUS_ACCESS_DENIED;
1759         #endif
1760         /* associate the user/group SID with the (unique) handle. */
1761         info = TALLOC_ZERO_P(p->mem_ctx, struct lsa_info);
1762         if (info == NULL) {
1763                 return NT_STATUS_NO_MEMORY;
1764         }
1765
1766         info->sid = *r->in.sid;
1767         info->access = acc_granted;
1768         info->type = LSA_HANDLE_ACCOUNT_TYPE;
1769
1770         /* get a (unique) handle.  open a policy on it. */
1771         if (!create_policy_hnd(p, r->out.acct_handle, info))
1772                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1773
1774         return NT_STATUS_OK;
1775 }
1776
1777 /***************************************************************************
1778  _lsa_EnumPrivsAccount
1779  For a given SID, enumerate all the privilege this account has.
1780  ***************************************************************************/
1781
1782 NTSTATUS _lsa_EnumPrivsAccount(pipes_struct *p,
1783                                struct lsa_EnumPrivsAccount *r)
1784 {
1785         NTSTATUS status = NT_STATUS_OK;
1786         struct lsa_info *info=NULL;
1787         SE_PRIV mask;
1788         PRIVILEGE_SET privileges;
1789         struct lsa_PrivilegeSet *priv_set = NULL;
1790         struct lsa_LUIDAttribute *luid_attrs = NULL;
1791         int i;
1792
1793         /* find the connection policy handle. */
1794         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
1795                 return NT_STATUS_INVALID_HANDLE;
1796
1797         if (info->type != LSA_HANDLE_ACCOUNT_TYPE) {
1798                 return NT_STATUS_INVALID_HANDLE;
1799         }
1800
1801         if (!(info->access & LSA_ACCOUNT_VIEW))
1802                 return NT_STATUS_ACCESS_DENIED;
1803
1804         get_privileges_for_sids(&mask, &info->sid, 1);
1805
1806         privilege_set_init( &privileges );
1807
1808         priv_set = TALLOC_ZERO_P(p->mem_ctx, struct lsa_PrivilegeSet);
1809         if (!priv_set) {
1810                 status = NT_STATUS_NO_MEMORY;
1811                 goto done;
1812         }
1813
1814         if ( se_priv_to_privilege_set( &privileges, &mask ) ) {
1815
1816                 DEBUG(10,("_lsa_EnumPrivsAccount: %s has %d privileges\n",
1817                           sid_string_dbg(&info->sid),
1818                           privileges.count));
1819
1820                 luid_attrs = TALLOC_ZERO_ARRAY(p->mem_ctx,
1821                                                struct lsa_LUIDAttribute,
1822                                                privileges.count);
1823                 if (!luid_attrs) {
1824                         status = NT_STATUS_NO_MEMORY;
1825                         goto done;
1826                 }
1827
1828                 for (i=0; i<privileges.count; i++) {
1829                         luid_attrs[i].luid.low = privileges.set[i].luid.low;
1830                         luid_attrs[i].luid.high = privileges.set[i].luid.high;
1831                         luid_attrs[i].attribute = privileges.set[i].attr;
1832                 }
1833
1834                 priv_set->count = privileges.count;
1835                 priv_set->unknown = 0;
1836                 priv_set->set = luid_attrs;
1837
1838         } else {
1839                 priv_set->count = 0;
1840                 priv_set->unknown = 0;
1841                 priv_set->set = NULL;
1842         }
1843
1844         *r->out.privs = priv_set;
1845
1846  done:
1847         privilege_set_free( &privileges );
1848
1849         return status;
1850 }
1851
1852 /***************************************************************************
1853  _lsa_GetSystemAccessAccount
1854  ***************************************************************************/
1855
1856 NTSTATUS _lsa_GetSystemAccessAccount(pipes_struct *p,
1857                                      struct lsa_GetSystemAccessAccount *r)
1858 {
1859         NTSTATUS status;
1860         struct lsa_info *info = NULL;
1861         struct lsa_EnumPrivsAccount e;
1862         struct lsa_PrivilegeSet *privset;
1863
1864         /* find the connection policy handle. */
1865
1866         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
1867                 return NT_STATUS_INVALID_HANDLE;
1868
1869         if (info->type != LSA_HANDLE_ACCOUNT_TYPE) {
1870                 return NT_STATUS_INVALID_HANDLE;
1871         }
1872
1873         if (!(info->access & LSA_ACCOUNT_VIEW))
1874                 return NT_STATUS_ACCESS_DENIED;
1875
1876         privset = talloc_zero(p->mem_ctx, struct lsa_PrivilegeSet);
1877         if (!privset) {
1878                 return NT_STATUS_NO_MEMORY;
1879         }
1880
1881         e.in.handle = r->in.handle;
1882         e.out.privs = &privset;
1883
1884         status = _lsa_EnumPrivsAccount(p, &e);
1885         if (!NT_STATUS_IS_OK(status)) {
1886                 DEBUG(10,("_lsa_GetSystemAccessAccount: "
1887                         "failed to call _lsa_EnumPrivsAccount(): %s\n",
1888                         nt_errstr(status)));
1889                 return status;
1890         }
1891
1892         /* Samba4 would iterate over the privset to merge the policy mode bits,
1893          * not sure samba3 can do the same here, so just return what we did in
1894          * the past - gd */
1895
1896         /*
1897           0x01 -> Log on locally
1898           0x02 -> Access this computer from network
1899           0x04 -> Log on as a batch job
1900           0x10 -> Log on as a service
1901
1902           they can be ORed together
1903         */
1904
1905         *r->out.access_mask = LSA_POLICY_MODE_INTERACTIVE |
1906                               LSA_POLICY_MODE_NETWORK;
1907
1908         return NT_STATUS_OK;
1909 }
1910
1911 /***************************************************************************
1912   update the systemaccount information
1913  ***************************************************************************/
1914
1915 NTSTATUS _lsa_SetSystemAccessAccount(pipes_struct *p,
1916                                      struct lsa_SetSystemAccessAccount *r)
1917 {
1918         struct lsa_info *info=NULL;
1919         GROUP_MAP map;
1920
1921         /* find the connection policy handle. */
1922         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
1923                 return NT_STATUS_INVALID_HANDLE;
1924
1925         if (info->type != LSA_HANDLE_ACCOUNT_TYPE) {
1926                 return NT_STATUS_INVALID_HANDLE;
1927         }
1928
1929         if (!(info->access & LSA_ACCOUNT_ADJUST_SYSTEM_ACCESS)) {
1930                 return NT_STATUS_ACCESS_DENIED;
1931         }
1932
1933         if (!pdb_getgrsid(&map, info->sid))
1934                 return NT_STATUS_NO_SUCH_GROUP;
1935
1936         return pdb_update_group_mapping_entry(&map);
1937 }
1938
1939 /***************************************************************************
1940  _lsa_AddPrivilegesToAccount
1941  For a given SID, add some privileges.
1942  ***************************************************************************/
1943
1944 NTSTATUS _lsa_AddPrivilegesToAccount(pipes_struct *p,
1945                                      struct lsa_AddPrivilegesToAccount *r)
1946 {
1947         struct lsa_info *info = NULL;
1948         SE_PRIV mask;
1949         struct lsa_PrivilegeSet *set = NULL;
1950
1951         /* find the connection policy handle. */
1952         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
1953                 return NT_STATUS_INVALID_HANDLE;
1954
1955         if (info->type != LSA_HANDLE_ACCOUNT_TYPE) {
1956                 return NT_STATUS_INVALID_HANDLE;
1957         }
1958
1959         if (!(info->access & LSA_ACCOUNT_ADJUST_PRIVILEGES)) {
1960                 return NT_STATUS_ACCESS_DENIED;
1961         }
1962
1963         set = r->in.privs;
1964         if ( !privilege_set_to_se_priv( &mask, set ) )
1965                 return NT_STATUS_NO_SUCH_PRIVILEGE;
1966
1967         if ( !grant_privilege( &info->sid, &mask ) ) {
1968                 DEBUG(3,("_lsa_AddPrivilegesToAccount: grant_privilege(%s) failed!\n",
1969                          sid_string_dbg(&info->sid) ));
1970                 DEBUG(3,("Privilege mask:\n"));
1971                 dump_se_priv( DBGC_ALL, 3, &mask );
1972                 return NT_STATUS_NO_SUCH_PRIVILEGE;
1973         }
1974
1975         return NT_STATUS_OK;
1976 }
1977
1978 /***************************************************************************
1979  _lsa_RemovePrivilegesFromAccount
1980  For a given SID, remove some privileges.
1981  ***************************************************************************/
1982
1983 NTSTATUS _lsa_RemovePrivilegesFromAccount(pipes_struct *p,
1984                                           struct lsa_RemovePrivilegesFromAccount *r)
1985 {
1986         struct lsa_info *info = NULL;
1987         SE_PRIV mask;
1988         struct lsa_PrivilegeSet *set = NULL;
1989
1990         /* find the connection policy handle. */
1991         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
1992                 return NT_STATUS_INVALID_HANDLE;
1993
1994         if (info->type != LSA_HANDLE_ACCOUNT_TYPE) {
1995                 return NT_STATUS_INVALID_HANDLE;
1996         }
1997
1998         if (!(info->access & LSA_ACCOUNT_ADJUST_PRIVILEGES)) {
1999                 return NT_STATUS_ACCESS_DENIED;
2000         }
2001
2002         set = r->in.privs;
2003
2004         if ( !privilege_set_to_se_priv( &mask, set ) )
2005                 return NT_STATUS_NO_SUCH_PRIVILEGE;
2006
2007         if ( !revoke_privilege( &info->sid, &mask ) ) {
2008                 DEBUG(3,("_lsa_RemovePrivilegesFromAccount: revoke_privilege(%s) failed!\n",
2009                          sid_string_dbg(&info->sid) ));
2010                 DEBUG(3,("Privilege mask:\n"));
2011                 dump_se_priv( DBGC_ALL, 3, &mask );
2012                 return NT_STATUS_NO_SUCH_PRIVILEGE;
2013         }
2014
2015         return NT_STATUS_OK;
2016 }
2017
2018 /***************************************************************************
2019  _lsa_LookupPrivName
2020  ***************************************************************************/
2021
2022 NTSTATUS _lsa_LookupPrivName(pipes_struct *p,
2023                              struct lsa_LookupPrivName *r)
2024 {
2025         struct lsa_info *info = NULL;
2026         const char *name;
2027         struct lsa_StringLarge *lsa_name;
2028
2029         /* find the connection policy handle. */
2030         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info)) {
2031                 return NT_STATUS_INVALID_HANDLE;
2032         }
2033
2034         if (info->type != LSA_HANDLE_POLICY_TYPE) {
2035                 return NT_STATUS_INVALID_HANDLE;
2036         }
2037
2038         if (!(info->access & LSA_POLICY_VIEW_LOCAL_INFORMATION)) {
2039                 return NT_STATUS_ACCESS_DENIED;
2040         }
2041
2042         name = luid_to_privilege_name((LUID *)r->in.luid);
2043         if (!name) {
2044                 return NT_STATUS_NO_SUCH_PRIVILEGE;
2045         }
2046
2047         lsa_name = TALLOC_ZERO_P(p->mem_ctx, struct lsa_StringLarge);
2048         if (!lsa_name) {
2049                 return NT_STATUS_NO_MEMORY;
2050         }
2051
2052         lsa_name->string = talloc_strdup(lsa_name, name);
2053         if (!lsa_name->string) {
2054                 TALLOC_FREE(lsa_name);
2055                 return NT_STATUS_NO_MEMORY;
2056         }
2057
2058         *r->out.name = lsa_name;
2059
2060         return NT_STATUS_OK;
2061 }
2062
2063 /***************************************************************************
2064  _lsa_QuerySecurity
2065  ***************************************************************************/
2066
2067 NTSTATUS _lsa_QuerySecurity(pipes_struct *p,
2068                             struct lsa_QuerySecurity *r)
2069 {
2070         struct lsa_info *handle=NULL;
2071         SEC_DESC *psd = NULL;
2072         size_t sd_size;
2073         NTSTATUS status;
2074
2075         /* find the connection policy handle. */
2076         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle))
2077                 return NT_STATUS_INVALID_HANDLE;
2078
2079         if (handle->type == LSA_HANDLE_POLICY_TYPE) {
2080                 status = make_lsa_object_sd(p->mem_ctx, &psd, &sd_size,
2081                                 &lsa_policy_mapping, NULL, 0);
2082         } else if (handle->type == LSA_HANDLE_ACCOUNT_TYPE) {
2083                 status = make_lsa_object_sd(p->mem_ctx, &psd, &sd_size,
2084                                 &lsa_account_mapping,
2085                                 &handle->sid, LSA_ACCOUNT_ALL_ACCESS);
2086         } else {
2087                 status = NT_STATUS_INVALID_HANDLE;
2088         }
2089
2090         if (!NT_STATUS_IS_OK(status)) {
2091                 return status;
2092         }
2093
2094         *r->out.sdbuf = make_sec_desc_buf(p->mem_ctx, sd_size, psd);
2095         if (!*r->out.sdbuf) {
2096                 return NT_STATUS_NO_MEMORY;
2097         }
2098
2099         return status;
2100 }
2101
2102 /***************************************************************************
2103  _lsa_AddAccountRights
2104  ***************************************************************************/
2105
2106 NTSTATUS _lsa_AddAccountRights(pipes_struct *p,
2107                                struct lsa_AddAccountRights *r)
2108 {
2109         struct lsa_info *info = NULL;
2110         int i = 0;
2111         uint32_t acc_granted = 0;
2112         SEC_DESC *psd = NULL;
2113         size_t sd_size;
2114         DOM_SID sid;
2115         NTSTATUS status;
2116
2117         /* find the connection policy handle. */
2118         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
2119                 return NT_STATUS_INVALID_HANDLE;
2120
2121         if (info->type != LSA_HANDLE_POLICY_TYPE) {
2122                 return NT_STATUS_INVALID_HANDLE;
2123         }
2124
2125         /* get the generic lsa account SD for this SID until we store it */
2126         status = make_lsa_object_sd(p->mem_ctx, &psd, &sd_size,
2127                                 &lsa_account_mapping,
2128                                 r->in.sid, LSA_ACCOUNT_ALL_ACCESS);
2129         if (!NT_STATUS_IS_OK(status)) {
2130                 return status;
2131         }
2132
2133         /*
2134          * From the MS DOCs. If the sid doesn't exist, ask for LSA_POLICY_CREATE_ACCOUNT
2135          * on the policy handle. If it does, ask for
2136          * LSA_ACCOUNT_ADJUST_PRIVILEGES|LSA_ACCOUNT_ADJUST_SYSTEM_ACCESS|LSA_ACCOUNT_VIEW,
2137          * on the account sid. We don't check here so just use the latter. JRA.
2138          */
2139
2140         status = access_check_object(psd, p->server_info->ptok,
2141                 NULL, 0, LSA_ACCOUNT_ADJUST_PRIVILEGES|LSA_ACCOUNT_ADJUST_SYSTEM_ACCESS|LSA_ACCOUNT_VIEW,
2142                 &acc_granted, "_lsa_AddAccountRights" );
2143
2144         if (!NT_STATUS_IS_OK(status)) {
2145                 return status;
2146         }
2147
2148         /* according to an NT4 PDC, you can add privileges to SIDs even without
2149            call_lsa_create_account() first.  And you can use any arbitrary SID. */
2150
2151         sid_copy( &sid, r->in.sid );
2152
2153         for ( i=0; i < r->in.rights->count; i++ ) {
2154
2155                 const char *privname = r->in.rights->names[i].string;
2156
2157                 /* only try to add non-null strings */
2158
2159                 if ( !privname )
2160                         continue;
2161
2162                 if ( !grant_privilege_by_name( &sid, privname ) ) {
2163                         DEBUG(2,("_lsa_AddAccountRights: Failed to add privilege [%s]\n",
2164                                 privname ));
2165                         return NT_STATUS_NO_SUCH_PRIVILEGE;
2166                 }
2167         }
2168
2169         return NT_STATUS_OK;
2170 }
2171
2172 /***************************************************************************
2173  _lsa_RemoveAccountRights
2174  ***************************************************************************/
2175
2176 NTSTATUS _lsa_RemoveAccountRights(pipes_struct *p,
2177                                   struct lsa_RemoveAccountRights *r)
2178 {
2179         struct lsa_info *info = NULL;
2180         int i = 0;
2181         SEC_DESC *psd = NULL;
2182         size_t sd_size;
2183         DOM_SID sid;
2184         const char *privname = NULL;
2185         uint32_t acc_granted = 0;
2186         NTSTATUS status;
2187
2188         /* find the connection policy handle. */
2189         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
2190                 return NT_STATUS_INVALID_HANDLE;
2191
2192         if (info->type != LSA_HANDLE_POLICY_TYPE) {
2193                 return NT_STATUS_INVALID_HANDLE;
2194         }
2195
2196         /* get the generic lsa account SD for this SID until we store it */
2197         status = make_lsa_object_sd(p->mem_ctx, &psd, &sd_size,
2198                                 &lsa_account_mapping,
2199                                 r->in.sid, LSA_ACCOUNT_ALL_ACCESS);
2200         if (!NT_STATUS_IS_OK(status)) {
2201                 return status;
2202         }
2203
2204         /*
2205          * From the MS DOCs. We need
2206          * LSA_ACCOUNT_ADJUST_PRIVILEGES|LSA_ACCOUNT_ADJUST_SYSTEM_ACCESS|LSA_ACCOUNT_VIEW
2207          * and DELETE on the account sid.
2208          */
2209
2210         status = access_check_object(psd, p->server_info->ptok,
2211                 NULL, 0, LSA_ACCOUNT_ADJUST_PRIVILEGES|LSA_ACCOUNT_ADJUST_SYSTEM_ACCESS|
2212                         LSA_ACCOUNT_VIEW|STD_RIGHT_DELETE_ACCESS,
2213                 &acc_granted, "_lsa_AddAccountRights" );
2214
2215         if (!NT_STATUS_IS_OK(status)) {
2216                 return status;
2217         }
2218
2219         sid_copy( &sid, r->in.sid );
2220
2221         if ( r->in.remove_all ) {
2222                 if ( !revoke_all_privileges( &sid ) )
2223                         return NT_STATUS_ACCESS_DENIED;
2224
2225                 return NT_STATUS_OK;
2226         }
2227
2228         for ( i=0; i < r->in.rights->count; i++ ) {
2229
2230                 privname = r->in.rights->names[i].string;
2231
2232                 /* only try to add non-null strings */
2233
2234                 if ( !privname )
2235                         continue;
2236
2237                 if ( !revoke_privilege_by_name( &sid, privname ) ) {
2238                         DEBUG(2,("_lsa_RemoveAccountRights: Failed to revoke privilege [%s]\n",
2239                                 privname ));
2240                         return NT_STATUS_NO_SUCH_PRIVILEGE;
2241                 }
2242         }
2243
2244         return NT_STATUS_OK;
2245 }
2246
2247 /*******************************************************************
2248 ********************************************************************/
2249
2250 static NTSTATUS init_lsa_right_set(TALLOC_CTX *mem_ctx,
2251                                    struct lsa_RightSet *r,
2252                                    PRIVILEGE_SET *privileges)
2253 {
2254         uint32 i;
2255         const char *privname;
2256         const char **privname_array = NULL;
2257         int num_priv = 0;
2258
2259         for (i=0; i<privileges->count; i++) {
2260
2261                 privname = luid_to_privilege_name(&privileges->set[i].luid);
2262                 if (privname) {
2263                         if (!add_string_to_array(mem_ctx, privname,
2264                                                  &privname_array, &num_priv)) {
2265                                 return NT_STATUS_NO_MEMORY;
2266                         }
2267                 }
2268         }
2269
2270         if (num_priv) {
2271
2272                 r->names = TALLOC_ZERO_ARRAY(mem_ctx, struct lsa_StringLarge,
2273                                              num_priv);
2274                 if (!r->names) {
2275                         return NT_STATUS_NO_MEMORY;
2276                 }
2277
2278                 for (i=0; i<num_priv; i++) {
2279                         init_lsa_StringLarge(&r->names[i], privname_array[i]);
2280                 }
2281
2282                 r->count = num_priv;
2283         }
2284
2285         return NT_STATUS_OK;
2286 }
2287
2288 /***************************************************************************
2289  _lsa_EnumAccountRights
2290  ***************************************************************************/
2291
2292 NTSTATUS _lsa_EnumAccountRights(pipes_struct *p,
2293                                 struct lsa_EnumAccountRights *r)
2294 {
2295         NTSTATUS status;
2296         struct lsa_info *info = NULL;
2297         DOM_SID sid;
2298         PRIVILEGE_SET privileges;
2299         SE_PRIV mask;
2300
2301         /* find the connection policy handle. */
2302
2303         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
2304                 return NT_STATUS_INVALID_HANDLE;
2305
2306         if (info->type != LSA_HANDLE_POLICY_TYPE) {
2307                 return NT_STATUS_INVALID_HANDLE;
2308         }
2309
2310         if (!(info->access & LSA_ACCOUNT_VIEW)) {
2311                 return NT_STATUS_ACCESS_DENIED;
2312         }
2313
2314         /* according to an NT4 PDC, you can add privileges to SIDs even without
2315            call_lsa_create_account() first.  And you can use any arbitrary SID. */
2316
2317         sid_copy( &sid, r->in.sid );
2318
2319         /* according to MS-LSAD 3.1.4.5.10 it is required to return
2320          * NT_STATUS_OBJECT_NAME_NOT_FOUND if the account sid was not found in
2321          * the lsa database */
2322
2323         if (!get_privileges_for_sids(&mask, &sid, 1)) {
2324                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2325         }
2326
2327         status = privilege_set_init(&privileges);
2328         if (!NT_STATUS_IS_OK(status)) {
2329                 return status;
2330         }
2331
2332         se_priv_to_privilege_set(&privileges, &mask);
2333
2334         DEBUG(10,("_lsa_EnumAccountRights: %s has %d privileges\n",
2335                   sid_string_dbg(&sid), privileges.count));
2336
2337         status = init_lsa_right_set(p->mem_ctx, r->out.rights, &privileges);
2338
2339         privilege_set_free( &privileges );
2340
2341         return status;
2342 }
2343
2344 /***************************************************************************
2345  _lsa_LookupPrivValue
2346  ***************************************************************************/
2347
2348 NTSTATUS _lsa_LookupPrivValue(pipes_struct *p,
2349                               struct lsa_LookupPrivValue *r)
2350 {
2351         struct lsa_info *info = NULL;
2352         const char *name = NULL;
2353         LUID_ATTR priv_luid;
2354         SE_PRIV mask;
2355
2356         /* find the connection policy handle. */
2357
2358         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
2359                 return NT_STATUS_INVALID_HANDLE;
2360
2361         if (info->type != LSA_HANDLE_POLICY_TYPE) {
2362                 return NT_STATUS_INVALID_HANDLE;
2363         }
2364
2365         if (!(info->access & LSA_POLICY_LOOKUP_NAMES))
2366                 return NT_STATUS_ACCESS_DENIED;
2367
2368         name = r->in.name->string;
2369
2370         DEBUG(10,("_lsa_lookup_priv_value: name = %s\n", name));
2371
2372         if ( !se_priv_from_name( name, &mask ) )
2373                 return NT_STATUS_NO_SUCH_PRIVILEGE;
2374
2375         priv_luid = get_privilege_luid( &mask );
2376
2377         r->out.luid->low = priv_luid.luid.low;
2378         r->out.luid->high = priv_luid.luid.high;
2379
2380         return NT_STATUS_OK;
2381 }
2382
2383 /***************************************************************************
2384  _lsa_EnumAccountsWithUserRight
2385  ***************************************************************************/
2386
2387 NTSTATUS _lsa_EnumAccountsWithUserRight(pipes_struct *p,
2388                                         struct lsa_EnumAccountsWithUserRight *r)
2389 {
2390         NTSTATUS status;
2391         struct lsa_info *info = NULL;
2392         struct dom_sid *sids = NULL;
2393         int num_sids = 0;
2394         uint32_t i;
2395         SE_PRIV mask;
2396
2397         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info)) {
2398                 return NT_STATUS_INVALID_HANDLE;
2399         }
2400
2401         if (info->type != LSA_HANDLE_POLICY_TYPE) {
2402                 return NT_STATUS_INVALID_HANDLE;
2403         }
2404
2405         if (!(info->access & LSA_POLICY_LOOKUP_NAMES)) {
2406                 return NT_STATUS_ACCESS_DENIED;
2407         }
2408
2409         if (!r->in.name || !r->in.name->string) {
2410                 return NT_STATUS_NO_SUCH_PRIVILEGE;
2411         }
2412
2413         if (!se_priv_from_name(r->in.name->string, &mask)) {
2414                 return NT_STATUS_NO_SUCH_PRIVILEGE;
2415         }
2416
2417         status = privilege_enum_sids(&mask, p->mem_ctx,
2418                                      &sids, &num_sids);
2419         if (!NT_STATUS_IS_OK(status)) {
2420                 return status;
2421         }
2422
2423         r->out.sids->num_sids = num_sids;
2424         r->out.sids->sids = talloc_array(p->mem_ctx, struct lsa_SidPtr,
2425                                          r->out.sids->num_sids);
2426
2427         for (i=0; i < r->out.sids->num_sids; i++) {
2428                 r->out.sids->sids[i].sid = sid_dup_talloc(r->out.sids->sids,
2429                                                           &sids[i]);
2430                 if (!r->out.sids->sids[i].sid) {
2431                         TALLOC_FREE(r->out.sids->sids);
2432                         r->out.sids->num_sids = 0;
2433                         return NT_STATUS_NO_MEMORY;
2434                 }
2435         }
2436
2437         return NT_STATUS_OK;
2438 }
2439
2440 /***************************************************************************
2441  _lsa_Delete
2442  ***************************************************************************/
2443
2444 NTSTATUS _lsa_Delete(pipes_struct *p,
2445                      struct lsa_Delete *r)
2446 {
2447         return NT_STATUS_NOT_SUPPORTED;
2448 }
2449
2450 /*
2451  * From here on the server routines are just dummy ones to make smbd link with
2452  * librpc/gen_ndr/srv_lsa.c. These routines are actually never called, we are
2453  * pulling the server stubs across one by one.
2454  */
2455
2456 NTSTATUS _lsa_SetSecObj(pipes_struct *p, struct lsa_SetSecObj *r)
2457 {
2458         p->rng_fault_state = True;
2459         return NT_STATUS_NOT_IMPLEMENTED;
2460 }
2461
2462 NTSTATUS _lsa_ChangePassword(pipes_struct *p, struct lsa_ChangePassword *r)
2463 {
2464         p->rng_fault_state = True;
2465         return NT_STATUS_NOT_IMPLEMENTED;
2466 }
2467
2468 NTSTATUS _lsa_SetInfoPolicy(pipes_struct *p, struct lsa_SetInfoPolicy *r)
2469 {
2470         p->rng_fault_state = True;
2471         return NT_STATUS_NOT_IMPLEMENTED;
2472 }
2473
2474 NTSTATUS _lsa_ClearAuditLog(pipes_struct *p, struct lsa_ClearAuditLog *r)
2475 {
2476         p->rng_fault_state = True;
2477         return NT_STATUS_NOT_IMPLEMENTED;
2478 }
2479
2480 NTSTATUS _lsa_GetQuotasForAccount(pipes_struct *p, struct lsa_GetQuotasForAccount *r)
2481 {
2482         p->rng_fault_state = True;
2483         return NT_STATUS_NOT_IMPLEMENTED;
2484 }
2485
2486 NTSTATUS _lsa_SetQuotasForAccount(pipes_struct *p, struct lsa_SetQuotasForAccount *r)
2487 {
2488         p->rng_fault_state = True;
2489         return NT_STATUS_NOT_IMPLEMENTED;
2490 }
2491
2492 NTSTATUS _lsa_QueryTrustedDomainInfo(pipes_struct *p, struct lsa_QueryTrustedDomainInfo *r)
2493 {
2494         p->rng_fault_state = True;
2495         return NT_STATUS_NOT_IMPLEMENTED;
2496 }
2497
2498 NTSTATUS _lsa_SetInformationTrustedDomain(pipes_struct *p, struct lsa_SetInformationTrustedDomain *r)
2499 {
2500         p->rng_fault_state = True;
2501         return NT_STATUS_NOT_IMPLEMENTED;
2502 }
2503
2504 NTSTATUS _lsa_QuerySecret(pipes_struct *p, struct lsa_QuerySecret *r)
2505 {
2506         p->rng_fault_state = True;
2507         return NT_STATUS_NOT_IMPLEMENTED;
2508 }
2509
2510 NTSTATUS _lsa_QueryTrustedDomainInfoBySid(pipes_struct *p, struct lsa_QueryTrustedDomainInfoBySid *r)
2511 {
2512         p->rng_fault_state = True;
2513         return NT_STATUS_NOT_IMPLEMENTED;
2514 }
2515
2516 NTSTATUS _lsa_SetTrustedDomainInfo(pipes_struct *p, struct lsa_SetTrustedDomainInfo *r)
2517 {
2518         p->rng_fault_state = True;
2519         return NT_STATUS_NOT_IMPLEMENTED;
2520 }
2521
2522 NTSTATUS _lsa_DeleteTrustedDomain(pipes_struct *p, struct lsa_DeleteTrustedDomain *r)
2523 {
2524         p->rng_fault_state = True;
2525         return NT_STATUS_NOT_IMPLEMENTED;
2526 }
2527
2528 NTSTATUS _lsa_StorePrivateData(pipes_struct *p, struct lsa_StorePrivateData *r)
2529 {
2530         p->rng_fault_state = True;
2531         return NT_STATUS_NOT_IMPLEMENTED;
2532 }
2533
2534 NTSTATUS _lsa_RetrievePrivateData(pipes_struct *p, struct lsa_RetrievePrivateData *r)
2535 {
2536         p->rng_fault_state = True;
2537         return NT_STATUS_NOT_IMPLEMENTED;
2538 }
2539
2540 NTSTATUS _lsa_SetInfoPolicy2(pipes_struct *p, struct lsa_SetInfoPolicy2 *r)
2541 {
2542         p->rng_fault_state = True;
2543         return NT_STATUS_NOT_IMPLEMENTED;
2544 }
2545
2546 NTSTATUS _lsa_QueryTrustedDomainInfoByName(pipes_struct *p, struct lsa_QueryTrustedDomainInfoByName *r)
2547 {
2548         p->rng_fault_state = True;
2549         return NT_STATUS_NOT_IMPLEMENTED;
2550 }
2551
2552 NTSTATUS _lsa_SetTrustedDomainInfoByName(pipes_struct *p, struct lsa_SetTrustedDomainInfoByName *r)
2553 {
2554         p->rng_fault_state = True;
2555         return NT_STATUS_NOT_IMPLEMENTED;
2556 }
2557
2558 NTSTATUS _lsa_EnumTrustedDomainsEx(pipes_struct *p, struct lsa_EnumTrustedDomainsEx *r)
2559 {
2560         p->rng_fault_state = True;
2561         return NT_STATUS_NOT_IMPLEMENTED;
2562 }
2563
2564 NTSTATUS _lsa_CreateTrustedDomainEx(pipes_struct *p, struct lsa_CreateTrustedDomainEx *r)
2565 {
2566         p->rng_fault_state = True;
2567         return NT_STATUS_NOT_IMPLEMENTED;
2568 }
2569
2570 NTSTATUS _lsa_CloseTrustedDomainEx(pipes_struct *p, struct lsa_CloseTrustedDomainEx *r)
2571 {
2572         p->rng_fault_state = True;
2573         return NT_STATUS_NOT_IMPLEMENTED;
2574 }
2575
2576 NTSTATUS _lsa_QueryDomainInformationPolicy(pipes_struct *p, struct lsa_QueryDomainInformationPolicy *r)
2577 {
2578         p->rng_fault_state = True;
2579         return NT_STATUS_NOT_IMPLEMENTED;
2580 }
2581
2582 NTSTATUS _lsa_SetDomainInformationPolicy(pipes_struct *p, struct lsa_SetDomainInformationPolicy *r)
2583 {
2584         p->rng_fault_state = True;
2585         return NT_STATUS_NOT_IMPLEMENTED;
2586 }
2587
2588 NTSTATUS _lsa_OpenTrustedDomainByName(pipes_struct *p, struct lsa_OpenTrustedDomainByName *r)
2589 {
2590         p->rng_fault_state = True;
2591         return NT_STATUS_NOT_IMPLEMENTED;
2592 }
2593
2594 NTSTATUS _lsa_TestCall(pipes_struct *p, struct lsa_TestCall *r)
2595 {
2596         p->rng_fault_state = True;
2597         return NT_STATUS_NOT_IMPLEMENTED;
2598 }
2599
2600 NTSTATUS _lsa_CreateTrustedDomainEx2(pipes_struct *p, struct lsa_CreateTrustedDomainEx2 *r)
2601 {
2602         p->rng_fault_state = True;
2603         return NT_STATUS_NOT_IMPLEMENTED;
2604 }
2605
2606 NTSTATUS _lsa_CREDRWRITE(pipes_struct *p, struct lsa_CREDRWRITE *r)
2607 {
2608         p->rng_fault_state = True;
2609         return NT_STATUS_NOT_IMPLEMENTED;
2610 }
2611
2612 NTSTATUS _lsa_CREDRREAD(pipes_struct *p, struct lsa_CREDRREAD *r)
2613 {
2614         p->rng_fault_state = True;
2615         return NT_STATUS_NOT_IMPLEMENTED;
2616 }
2617
2618 NTSTATUS _lsa_CREDRENUMERATE(pipes_struct *p, struct lsa_CREDRENUMERATE *r)
2619 {
2620         p->rng_fault_state = True;
2621         return NT_STATUS_NOT_IMPLEMENTED;
2622 }
2623
2624 NTSTATUS _lsa_CREDRWRITEDOMAINCREDENTIALS(pipes_struct *p, struct lsa_CREDRWRITEDOMAINCREDENTIALS *r)
2625 {
2626         p->rng_fault_state = True;
2627         return NT_STATUS_NOT_IMPLEMENTED;
2628 }
2629
2630 NTSTATUS _lsa_CREDRREADDOMAINCREDENTIALS(pipes_struct *p, struct lsa_CREDRREADDOMAINCREDENTIALS *r)
2631 {
2632         p->rng_fault_state = True;
2633         return NT_STATUS_NOT_IMPLEMENTED;
2634 }
2635
2636 NTSTATUS _lsa_CREDRDELETE(pipes_struct *p, struct lsa_CREDRDELETE *r)
2637 {
2638         p->rng_fault_state = True;
2639         return NT_STATUS_NOT_IMPLEMENTED;
2640 }
2641
2642 NTSTATUS _lsa_CREDRGETTARGETINFO(pipes_struct *p, struct lsa_CREDRGETTARGETINFO *r)
2643 {
2644         p->rng_fault_state = True;
2645         return NT_STATUS_NOT_IMPLEMENTED;
2646 }
2647
2648 NTSTATUS _lsa_CREDRPROFILELOADED(pipes_struct *p, struct lsa_CREDRPROFILELOADED *r)
2649 {
2650         p->rng_fault_state = True;
2651         return NT_STATUS_NOT_IMPLEMENTED;
2652 }
2653
2654 NTSTATUS _lsa_CREDRGETSESSIONTYPES(pipes_struct *p, struct lsa_CREDRGETSESSIONTYPES *r)
2655 {
2656         p->rng_fault_state = True;
2657         return NT_STATUS_NOT_IMPLEMENTED;
2658 }
2659
2660 NTSTATUS _lsa_LSARREGISTERAUDITEVENT(pipes_struct *p, struct lsa_LSARREGISTERAUDITEVENT *r)
2661 {
2662         p->rng_fault_state = True;
2663         return NT_STATUS_NOT_IMPLEMENTED;
2664 }
2665
2666 NTSTATUS _lsa_LSARGENAUDITEVENT(pipes_struct *p, struct lsa_LSARGENAUDITEVENT *r)
2667 {
2668         p->rng_fault_state = True;
2669         return NT_STATUS_NOT_IMPLEMENTED;
2670 }
2671
2672 NTSTATUS _lsa_LSARUNREGISTERAUDITEVENT(pipes_struct *p, struct lsa_LSARUNREGISTERAUDITEVENT *r)
2673 {
2674         p->rng_fault_state = True;
2675         return NT_STATUS_NOT_IMPLEMENTED;
2676 }
2677
2678 NTSTATUS _lsa_lsaRQueryForestTrustInformation(pipes_struct *p, struct lsa_lsaRQueryForestTrustInformation *r)
2679 {
2680         p->rng_fault_state = True;
2681         return NT_STATUS_NOT_IMPLEMENTED;
2682 }
2683
2684 NTSTATUS _lsa_LSARSETFORESTTRUSTINFORMATION(pipes_struct *p, struct lsa_LSARSETFORESTTRUSTINFORMATION *r)
2685 {
2686         p->rng_fault_state = True;
2687         return NT_STATUS_NOT_IMPLEMENTED;
2688 }
2689
2690 NTSTATUS _lsa_CREDRRENAME(pipes_struct *p, struct lsa_CREDRRENAME *r)
2691 {
2692         p->rng_fault_state = True;
2693         return NT_STATUS_NOT_IMPLEMENTED;
2694 }
2695
2696 NTSTATUS _lsa_LSAROPENPOLICYSCE(pipes_struct *p, struct lsa_LSAROPENPOLICYSCE *r)
2697 {
2698         p->rng_fault_state = True;
2699         return NT_STATUS_NOT_IMPLEMENTED;
2700 }
2701
2702 NTSTATUS _lsa_LSARADTREGISTERSECURITYEVENTSOURCE(pipes_struct *p, struct lsa_LSARADTREGISTERSECURITYEVENTSOURCE *r)
2703 {
2704         p->rng_fault_state = True;
2705         return NT_STATUS_NOT_IMPLEMENTED;
2706 }
2707
2708 NTSTATUS _lsa_LSARADTUNREGISTERSECURITYEVENTSOURCE(pipes_struct *p, struct lsa_LSARADTUNREGISTERSECURITYEVENTSOURCE *r)
2709 {
2710         p->rng_fault_state = True;
2711         return NT_STATUS_NOT_IMPLEMENTED;
2712 }
2713
2714 NTSTATUS _lsa_LSARADTREPORTSECURITYEVENT(pipes_struct *p, struct lsa_LSARADTREPORTSECURITYEVENT *r)
2715 {
2716         p->rng_fault_state = True;
2717         return NT_STATUS_NOT_IMPLEMENTED;
2718 }