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