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