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