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