s3-samr: Do not return users in _samr_QueryDisplayInfo() for builtin domain.
[ira/wip.git] / source3 / rpc_server / srv_samr_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) Marc Jacobsen                     1999,
8  *  Copyright (C) Jeremy Allison                    2001-2008,
9  *  Copyright (C) Jean François Micouleau           1998-2001,
10  *  Copyright (C) Jim McDonough <jmcd@us.ibm.com>   2002,
11  *  Copyright (C) Gerald (Jerry) Carter             2003-2004,
12  *  Copyright (C) Simo Sorce                        2003.
13  *  Copyright (C) Volker Lendecke                   2005.
14  *  Copyright (C) Guenther Deschner                 2008.
15  *
16  *  This program is free software; you can redistribute it and/or modify
17  *  it under the terms of the GNU General Public License as published by
18  *  the Free Software Foundation; either version 3 of the License, or
19  *  (at your option) any later version.
20  *
21  *  This program is distributed in the hope that it will be useful,
22  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *  GNU General Public License for more details.
25  *
26  *  You should have received a copy of the GNU General Public License
27  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
28  */
29
30 /*
31  * This is the implementation of the SAMR code.
32  */
33
34 #include "includes.h"
35 #include "../libcli/auth/libcli_auth.h"
36
37 #undef DBGC_CLASS
38 #define DBGC_CLASS DBGC_RPC_SRV
39
40 #define SAMR_USR_RIGHTS_WRITE_PW \
41                 ( READ_CONTROL_ACCESS           | \
42                   SAMR_USER_ACCESS_CHANGE_PASSWORD      | \
43                   SAMR_USER_ACCESS_SET_LOC_COM)
44 #define SAMR_USR_RIGHTS_CANT_WRITE_PW \
45                 ( READ_CONTROL_ACCESS | SAMR_USER_ACCESS_SET_LOC_COM )
46
47 #define DISP_INFO_CACHE_TIMEOUT 10
48
49 #define MAX_SAM_ENTRIES_W2K 0x400 /* 1024 */
50 #define MAX_SAM_ENTRIES_W95 50
51
52 struct samr_connect_info {
53         uint8_t dummy;
54 };
55
56 struct samr_domain_info {
57         struct dom_sid sid;
58         struct disp_info *disp_info;
59 };
60
61 struct samr_user_info {
62         struct dom_sid sid;
63 };
64
65 struct samr_group_info {
66         struct dom_sid sid;
67 };
68
69 struct samr_alias_info {
70         struct dom_sid sid;
71 };
72
73 typedef struct disp_info {
74         DOM_SID sid; /* identify which domain this is. */
75         struct pdb_search *users; /* querydispinfo 1 and 4 */
76         struct pdb_search *machines; /* querydispinfo 2 */
77         struct pdb_search *groups; /* querydispinfo 3 and 5, enumgroups */
78         struct pdb_search *aliases; /* enumaliases */
79
80         uint16 enum_acb_mask;
81         struct pdb_search *enum_users; /* enumusers with a mask */
82
83         struct timed_event *cache_timeout_event; /* cache idle timeout
84                                                   * handler. */
85 } DISP_INFO;
86
87 static const struct generic_mapping sam_generic_mapping = {
88         GENERIC_RIGHTS_SAM_READ,
89         GENERIC_RIGHTS_SAM_WRITE,
90         GENERIC_RIGHTS_SAM_EXECUTE,
91         GENERIC_RIGHTS_SAM_ALL_ACCESS};
92 static const struct generic_mapping dom_generic_mapping = {
93         GENERIC_RIGHTS_DOMAIN_READ,
94         GENERIC_RIGHTS_DOMAIN_WRITE,
95         GENERIC_RIGHTS_DOMAIN_EXECUTE,
96         GENERIC_RIGHTS_DOMAIN_ALL_ACCESS};
97 static const struct generic_mapping usr_generic_mapping = {
98         GENERIC_RIGHTS_USER_READ,
99         GENERIC_RIGHTS_USER_WRITE,
100         GENERIC_RIGHTS_USER_EXECUTE,
101         GENERIC_RIGHTS_USER_ALL_ACCESS};
102 static const struct generic_mapping usr_nopwchange_generic_mapping = {
103         GENERIC_RIGHTS_USER_READ,
104         GENERIC_RIGHTS_USER_WRITE,
105         GENERIC_RIGHTS_USER_EXECUTE & ~SAMR_USER_ACCESS_CHANGE_PASSWORD,
106         GENERIC_RIGHTS_USER_ALL_ACCESS};
107 static const struct generic_mapping grp_generic_mapping = {
108         GENERIC_RIGHTS_GROUP_READ,
109         GENERIC_RIGHTS_GROUP_WRITE,
110         GENERIC_RIGHTS_GROUP_EXECUTE,
111         GENERIC_RIGHTS_GROUP_ALL_ACCESS};
112 static const struct generic_mapping ali_generic_mapping = {
113         GENERIC_RIGHTS_ALIAS_READ,
114         GENERIC_RIGHTS_ALIAS_WRITE,
115         GENERIC_RIGHTS_ALIAS_EXECUTE,
116         GENERIC_RIGHTS_ALIAS_ALL_ACCESS};
117
118 /*******************************************************************
119 *******************************************************************/
120
121 static NTSTATUS make_samr_object_sd( TALLOC_CTX *ctx, SEC_DESC **psd, size_t *sd_size,
122                                      const struct generic_mapping *map,
123                                      DOM_SID *sid, uint32 sid_access )
124 {
125         DOM_SID domadmin_sid;
126         SEC_ACE ace[5];         /* at most 5 entries */
127         size_t i = 0;
128
129         SEC_ACL *psa = NULL;
130
131         /* basic access for Everyone */
132
133         init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED,
134                         map->generic_execute | map->generic_read, 0);
135
136         /* add Full Access 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
137
138         init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
139                         SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
140         init_sec_ace(&ace[i++], &global_sid_Builtin_Account_Operators,
141                         SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
142
143         /* Add Full Access for Domain Admins if we are a DC */
144
145         if ( IS_DC ) {
146                 sid_copy( &domadmin_sid, get_global_sam_sid() );
147                 sid_append_rid( &domadmin_sid, DOMAIN_GROUP_RID_ADMINS );
148                 init_sec_ace(&ace[i++], &domadmin_sid,
149                         SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
150         }
151
152         /* if we have a sid, give it some special access */
153
154         if ( sid ) {
155                 init_sec_ace(&ace[i++], sid, SEC_ACE_TYPE_ACCESS_ALLOWED, sid_access, 0);
156         }
157
158         /* create the security descriptor */
159
160         if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) == NULL)
161                 return NT_STATUS_NO_MEMORY;
162
163         if ((*psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
164                                   SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
165                                   psa, sd_size)) == NULL)
166                 return NT_STATUS_NO_MEMORY;
167
168         return NT_STATUS_OK;
169 }
170
171 /*******************************************************************
172  Checks if access to an object should be granted, and returns that
173  level of access for further checks.
174 ********************************************************************/
175
176 static NTSTATUS access_check_samr_object( SEC_DESC *psd, NT_USER_TOKEN *token,
177                                           SE_PRIV *rights, uint32 rights_mask,
178                                           uint32 des_access, uint32 *acc_granted,
179                                           const char *debug )
180 {
181         NTSTATUS status = NT_STATUS_ACCESS_DENIED;
182         uint32 saved_mask = 0;
183
184         /* check privileges; certain SAM access bits should be overridden
185            by privileges (mostly having to do with creating/modifying/deleting
186            users and groups) */
187
188         if ( rights && user_has_any_privilege( token, rights ) ) {
189
190                 saved_mask = (des_access & rights_mask);
191                 des_access &= ~saved_mask;
192
193                 DEBUG(4,("access_check_samr_object: user rights access mask [0x%x]\n",
194                         rights_mask));
195         }
196
197
198         /* check the security descriptor first */
199
200         status = se_access_check(psd, token, des_access, acc_granted);
201         if (NT_STATUS_IS_OK(status)) {
202                 goto done;
203         }
204
205         /* give root a free pass */
206
207         if ( geteuid() == sec_initial_uid() ) {
208
209                 DEBUG(4,("%s: ACCESS should be DENIED  (requested: %#010x)\n", debug, des_access));
210                 DEBUGADD(4,("but overritten by euid == sec_initial_uid()\n"));
211
212                 *acc_granted = des_access;
213
214                 status = NT_STATUS_OK;
215                 goto done;
216         }
217
218
219 done:
220         /* add in any bits saved during the privilege check (only
221            matters is status is ok) */
222
223         *acc_granted |= rights_mask;
224
225         DEBUG(4,("%s: access %s (requested: 0x%08x, granted: 0x%08x)\n",
226                 debug, NT_STATUS_IS_OK(status) ? "GRANTED" : "DENIED",
227                 des_access, *acc_granted));
228
229         return status;
230 }
231
232
233 /*******************************************************************
234  Map any MAXIMUM_ALLOWED_ACCESS request to a valid access set.
235 ********************************************************************/
236
237 static void map_max_allowed_access(const NT_USER_TOKEN *token,
238                                         uint32_t *pacc_requested)
239 {
240         if (!((*pacc_requested) & MAXIMUM_ALLOWED_ACCESS)) {
241                 return;
242         }
243         *pacc_requested &= ~MAXIMUM_ALLOWED_ACCESS;
244
245         /* At least try for generic read. */
246         *pacc_requested = GENERIC_READ_ACCESS;
247
248         /* root gets anything. */
249         if (geteuid() == sec_initial_uid()) {
250                 *pacc_requested |= GENERIC_ALL_ACCESS;
251                 return;
252         }
253
254         /* Full Access for 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
255
256         if (is_sid_in_token(token, &global_sid_Builtin_Administrators) ||
257                         is_sid_in_token(token, &global_sid_Builtin_Account_Operators)) {
258                 *pacc_requested |= GENERIC_ALL_ACCESS;
259                 return;
260         }
261
262         /* Full access for DOMAIN\Domain Admins. */
263         if ( IS_DC ) {
264                 DOM_SID domadmin_sid;
265                 sid_copy( &domadmin_sid, get_global_sam_sid() );
266                 sid_append_rid( &domadmin_sid, DOMAIN_GROUP_RID_ADMINS );
267                 if (is_sid_in_token(token, &domadmin_sid)) {
268                         *pacc_requested |= GENERIC_ALL_ACCESS;
269                         return;
270                 }
271         }
272         /* TODO ! Check privileges. */
273 }
274
275 /*******************************************************************
276  Fetch or create a dispinfo struct.
277 ********************************************************************/
278
279 static DISP_INFO *get_samr_dispinfo_by_sid(const struct dom_sid *psid)
280 {
281         /*
282          * We do a static cache for DISP_INFO's here. Explanation can be found
283          * in Jeremy's checkin message to r11793:
284          *
285          * Fix the SAMR cache so it works across completely insane
286          * client behaviour (ie.:
287          * open pipe/open SAMR handle/enumerate 0 - 1024
288          * close SAMR handle, close pipe.
289          * open pipe/open SAMR handle/enumerate 1024 - 2048...
290          * close SAMR handle, close pipe.
291          * And on ad-nausium. Amazing.... probably object-oriented
292          * client side programming in action yet again.
293          * This change should *massively* improve performance when
294          * enumerating users from an LDAP database.
295          * Jeremy.
296          *
297          * "Our" and the builtin domain are the only ones where we ever
298          * enumerate stuff, so just cache 2 entries.
299          */
300
301         static struct disp_info *builtin_dispinfo;
302         static struct disp_info *domain_dispinfo;
303
304         /* There are two cases to consider here:
305            1) The SID is a domain SID and we look for an equality match, or
306            2) This is an account SID and so we return the DISP_INFO* for our
307               domain */
308
309         if (psid == NULL) {
310                 return NULL;
311         }
312
313         if (sid_check_is_builtin(psid) || sid_check_is_in_builtin(psid)) {
314                 /*
315                  * Necessary only once, but it does not really hurt.
316                  */
317                 if (builtin_dispinfo == NULL) {
318                         builtin_dispinfo = talloc_zero(
319                                 talloc_autofree_context(), struct disp_info);
320                         if (builtin_dispinfo == NULL) {
321                                 return NULL;
322                         }
323                 }
324                 sid_copy(&builtin_dispinfo->sid, &global_sid_Builtin);
325
326                 return builtin_dispinfo;
327         }
328
329         if (sid_check_is_domain(psid) || sid_check_is_in_our_domain(psid)) {
330                 /*
331                  * Necessary only once, but it does not really hurt.
332                  */
333                 if (domain_dispinfo == NULL) {
334                         domain_dispinfo = talloc_zero(
335                                 talloc_autofree_context(), struct disp_info);
336                         if (domain_dispinfo == NULL) {
337                                 return NULL;
338                         }
339                 }
340                 sid_copy(&domain_dispinfo->sid, get_global_sam_sid());
341
342                 return domain_dispinfo;
343         }
344
345         return NULL;
346 }
347
348 /*******************************************************************
349  Function to free the per SID data.
350  ********************************************************************/
351
352 static void free_samr_cache(DISP_INFO *disp_info)
353 {
354         DEBUG(10, ("free_samr_cache: deleting cache for SID %s\n",
355                    sid_string_dbg(&disp_info->sid)));
356
357         /* We need to become root here because the paged search might have to
358          * tell the LDAP server we're not interested in the rest anymore. */
359
360         become_root();
361
362         TALLOC_FREE(disp_info->users);
363         TALLOC_FREE(disp_info->machines);
364         TALLOC_FREE(disp_info->groups);
365         TALLOC_FREE(disp_info->aliases);
366         TALLOC_FREE(disp_info->enum_users);
367
368         unbecome_root();
369 }
370
371 /*******************************************************************
372  Idle event handler. Throw away the disp info cache.
373  ********************************************************************/
374
375 static void disp_info_cache_idle_timeout_handler(struct event_context *ev_ctx,
376                                                  struct timed_event *te,
377                                                  struct timeval now,
378                                                  void *private_data)
379 {
380         DISP_INFO *disp_info = (DISP_INFO *)private_data;
381
382         TALLOC_FREE(disp_info->cache_timeout_event);
383
384         DEBUG(10, ("disp_info_cache_idle_timeout_handler: caching timed "
385                    "out\n"));
386         free_samr_cache(disp_info);
387 }
388
389 /*******************************************************************
390  Setup cache removal idle event handler.
391  ********************************************************************/
392
393 static void set_disp_info_cache_timeout(DISP_INFO *disp_info, time_t secs_fromnow)
394 {
395         /* Remove any pending timeout and update. */
396
397         TALLOC_FREE(disp_info->cache_timeout_event);
398
399         DEBUG(10,("set_disp_info_cache_timeout: caching enumeration for "
400                   "SID %s for %u seconds\n", sid_string_dbg(&disp_info->sid),
401                   (unsigned int)secs_fromnow ));
402
403         disp_info->cache_timeout_event = event_add_timed(
404                 smbd_event_context(), NULL,
405                 timeval_current_ofs(secs_fromnow, 0),
406                 disp_info_cache_idle_timeout_handler, (void *)disp_info);
407 }
408
409 /*******************************************************************
410  Force flush any cache. We do this on any samr_set_xxx call.
411  We must also remove the timeout handler.
412  ********************************************************************/
413
414 static void force_flush_samr_cache(const struct dom_sid *sid)
415 {
416         struct disp_info *disp_info = get_samr_dispinfo_by_sid(sid);
417
418         if ((disp_info == NULL) || (disp_info->cache_timeout_event == NULL)) {
419                 return;
420         }
421
422         DEBUG(10,("force_flush_samr_cache: clearing idle event\n"));
423         TALLOC_FREE(disp_info->cache_timeout_event);
424         free_samr_cache(disp_info);
425 }
426
427 /*******************************************************************
428  Ensure password info is never given out. Paranioa... JRA.
429  ********************************************************************/
430
431 static void samr_clear_sam_passwd(struct samu *sam_pass)
432 {
433
434         if (!sam_pass)
435                 return;
436
437         /* These now zero out the old password */
438
439         pdb_set_lanman_passwd(sam_pass, NULL, PDB_DEFAULT);
440         pdb_set_nt_passwd(sam_pass, NULL, PDB_DEFAULT);
441 }
442
443 static uint32 count_sam_users(struct disp_info *info, uint32 acct_flags)
444 {
445         struct samr_displayentry *entry;
446
447         if (sid_check_is_builtin(&info->sid)) {
448                 /* No users in builtin. */
449                 return 0;
450         }
451
452         if (info->users == NULL) {
453                 info->users = pdb_search_users(info, acct_flags);
454                 if (info->users == NULL) {
455                         return 0;
456                 }
457         }
458         /* Fetch the last possible entry, thus trigger an enumeration */
459         pdb_search_entries(info->users, 0xffffffff, 1, &entry);
460
461         /* Ensure we cache this enumeration. */
462         set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
463
464         return info->users->num_entries;
465 }
466
467 static uint32 count_sam_groups(struct disp_info *info)
468 {
469         struct samr_displayentry *entry;
470
471         if (sid_check_is_builtin(&info->sid)) {
472                 /* No groups in builtin. */
473                 return 0;
474         }
475
476         if (info->groups == NULL) {
477                 info->groups = pdb_search_groups(info);
478                 if (info->groups == NULL) {
479                         return 0;
480                 }
481         }
482         /* Fetch the last possible entry, thus trigger an enumeration */
483         pdb_search_entries(info->groups, 0xffffffff, 1, &entry);
484
485         /* Ensure we cache this enumeration. */
486         set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
487
488         return info->groups->num_entries;
489 }
490
491 static uint32 count_sam_aliases(struct disp_info *info)
492 {
493         struct samr_displayentry *entry;
494
495         if (info->aliases == NULL) {
496                 info->aliases = pdb_search_aliases(info, &info->sid);
497                 if (info->aliases == NULL) {
498                         return 0;
499                 }
500         }
501         /* Fetch the last possible entry, thus trigger an enumeration */
502         pdb_search_entries(info->aliases, 0xffffffff, 1, &entry);
503
504         /* Ensure we cache this enumeration. */
505         set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
506
507         return info->aliases->num_entries;
508 }
509
510 /*******************************************************************
511  _samr_Close
512  ********************************************************************/
513
514 NTSTATUS _samr_Close(pipes_struct *p, struct samr_Close *r)
515 {
516         if (!close_policy_hnd(p, r->in.handle)) {
517                 return NT_STATUS_INVALID_HANDLE;
518         }
519
520         ZERO_STRUCTP(r->out.handle);
521
522         return NT_STATUS_OK;
523 }
524
525 /*******************************************************************
526  _samr_OpenDomain
527  ********************************************************************/
528
529 NTSTATUS _samr_OpenDomain(pipes_struct *p,
530                           struct samr_OpenDomain *r)
531 {
532         struct samr_connect_info *cinfo;
533         struct samr_domain_info *dinfo;
534         SEC_DESC *psd = NULL;
535         uint32    acc_granted;
536         uint32    des_access = r->in.access_mask;
537         NTSTATUS  status;
538         size_t    sd_size;
539         SE_PRIV se_rights;
540
541         /* find the connection policy handle. */
542
543         cinfo = policy_handle_find(p, r->in.connect_handle, 0, NULL,
544                                    struct samr_connect_info, &status);
545         if (!NT_STATUS_IS_OK(status)) {
546                 return status;
547         }
548
549         /*check if access can be granted as requested by client. */
550         map_max_allowed_access(p->server_info->ptok, &des_access);
551
552         make_samr_object_sd( p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0 );
553         se_map_generic( &des_access, &dom_generic_mapping );
554
555         se_priv_copy( &se_rights, &se_machine_account );
556         se_priv_add( &se_rights, &se_add_users );
557
558         status = access_check_samr_object( psd, p->server_info->ptok,
559                 &se_rights, GENERIC_RIGHTS_DOMAIN_WRITE, des_access,
560                 &acc_granted, "_samr_OpenDomain" );
561
562         if ( !NT_STATUS_IS_OK(status) )
563                 return status;
564
565         if (!sid_check_is_domain(r->in.sid) &&
566             !sid_check_is_builtin(r->in.sid)) {
567                 return NT_STATUS_NO_SUCH_DOMAIN;
568         }
569
570         dinfo = policy_handle_create(p, r->out.domain_handle, acc_granted,
571                                      struct samr_domain_info, &status);
572         if (!NT_STATUS_IS_OK(status)) {
573                 return status;
574         }
575         dinfo->sid = *r->in.sid;
576         dinfo->disp_info = get_samr_dispinfo_by_sid(r->in.sid);
577
578         DEBUG(5,("_samr_OpenDomain: %d\n", __LINE__));
579
580         return NT_STATUS_OK;
581 }
582
583 /*******************************************************************
584  _samr_GetUserPwInfo
585  ********************************************************************/
586
587 NTSTATUS _samr_GetUserPwInfo(pipes_struct *p,
588                              struct samr_GetUserPwInfo *r)
589 {
590         struct samr_user_info *uinfo;
591         enum lsa_SidType sid_type;
592         uint32_t min_password_length = 0;
593         uint32_t password_properties = 0;
594         bool ret = false;
595         NTSTATUS status;
596
597         DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__));
598
599         uinfo = policy_handle_find(p, r->in.user_handle,
600                                    SAMR_USER_ACCESS_GET_ATTRIBUTES, NULL,
601                                    struct samr_user_info, &status);
602         if (!NT_STATUS_IS_OK(status)) {
603                 return status;
604         }
605
606         if (!sid_check_is_in_our_domain(&uinfo->sid)) {
607                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
608         }
609
610         become_root();
611         ret = lookup_sid(p->mem_ctx, &uinfo->sid, NULL, NULL, &sid_type);
612         unbecome_root();
613         if (ret == false) {
614                 return NT_STATUS_NO_SUCH_USER;
615         }
616
617         switch (sid_type) {
618                 case SID_NAME_USER:
619                         become_root();
620                         pdb_get_account_policy(AP_MIN_PASSWORD_LEN,
621                                                &min_password_length);
622                         pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS,
623                                                &password_properties);
624                         unbecome_root();
625
626                         if (lp_check_password_script() && *lp_check_password_script()) {
627                                 password_properties |= DOMAIN_PASSWORD_COMPLEX;
628                         }
629
630                         break;
631                 default:
632                         break;
633         }
634
635         r->out.info->min_password_length = min_password_length;
636         r->out.info->password_properties = password_properties;
637
638         DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__));
639
640         return NT_STATUS_OK;
641 }
642
643 /*******************************************************************
644  _samr_SetSecurity
645  ********************************************************************/
646
647 NTSTATUS _samr_SetSecurity(pipes_struct *p,
648                            struct samr_SetSecurity *r)
649 {
650         struct samr_user_info *uinfo;
651         uint32 i;
652         SEC_ACL *dacl;
653         bool ret;
654         struct samu *sampass=NULL;
655         NTSTATUS status;
656
657         uinfo = policy_handle_find(p, r->in.handle,
658                                    SAMR_USER_ACCESS_SET_ATTRIBUTES, NULL,
659                                    struct samr_user_info, &status);
660         if (!NT_STATUS_IS_OK(status)) {
661                 return status;
662         }
663
664         if (!(sampass = samu_new( p->mem_ctx))) {
665                 DEBUG(0,("No memory!\n"));
666                 return NT_STATUS_NO_MEMORY;
667         }
668
669         /* get the user record */
670         become_root();
671         ret = pdb_getsampwsid(sampass, &uinfo->sid);
672         unbecome_root();
673
674         if (!ret) {
675                 DEBUG(4, ("User %s not found\n",
676                           sid_string_dbg(&uinfo->sid)));
677                 TALLOC_FREE(sampass);
678                 return NT_STATUS_INVALID_HANDLE;
679         }
680
681         dacl = r->in.sdbuf->sd->dacl;
682         for (i=0; i < dacl->num_aces; i++) {
683                 if (sid_equal(&uinfo->sid, &dacl->aces[i].trustee)) {
684                         ret = pdb_set_pass_can_change(sampass,
685                                 (dacl->aces[i].access_mask &
686                                  SAMR_USER_ACCESS_CHANGE_PASSWORD) ?
687                                                       True: False);
688                         break;
689                 }
690         }
691
692         if (!ret) {
693                 TALLOC_FREE(sampass);
694                 return NT_STATUS_ACCESS_DENIED;
695         }
696
697         become_root();
698         status = pdb_update_sam_account(sampass);
699         unbecome_root();
700
701         TALLOC_FREE(sampass);
702
703         return status;
704 }
705
706 /*******************************************************************
707   build correct perms based on policies and password times for _samr_query_sec_obj
708 *******************************************************************/
709 static bool check_change_pw_access(TALLOC_CTX *mem_ctx, DOM_SID *user_sid)
710 {
711         struct samu *sampass=NULL;
712         bool ret;
713
714         if ( !(sampass = samu_new( mem_ctx )) ) {
715                 DEBUG(0,("No memory!\n"));
716                 return False;
717         }
718
719         become_root();
720         ret = pdb_getsampwsid(sampass, user_sid);
721         unbecome_root();
722
723         if (ret == False) {
724                 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
725                 TALLOC_FREE(sampass);
726                 return False;
727         }
728
729         DEBUG(3,("User:[%s]\n",  pdb_get_username(sampass) ));
730
731         if (pdb_get_pass_can_change(sampass)) {
732                 TALLOC_FREE(sampass);
733                 return True;
734         }
735         TALLOC_FREE(sampass);
736         return False;
737 }
738
739
740 /*******************************************************************
741  _samr_QuerySecurity
742  ********************************************************************/
743
744 NTSTATUS _samr_QuerySecurity(pipes_struct *p,
745                              struct samr_QuerySecurity *r)
746 {
747         struct samr_connect_info *cinfo;
748         struct samr_domain_info *dinfo;
749         struct samr_user_info *uinfo;
750         struct samr_group_info *ginfo;
751         struct samr_alias_info *ainfo;
752         NTSTATUS status;
753         SEC_DESC * psd = NULL;
754         size_t sd_size;
755
756         cinfo = policy_handle_find(p, r->in.handle,
757                                    STD_RIGHT_READ_CONTROL_ACCESS, NULL,
758                                    struct samr_connect_info, &status);
759         if (NT_STATUS_IS_OK(status)) {
760                 DEBUG(5,("_samr_QuerySecurity: querying security on SAM\n"));
761                 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size,
762                                              &sam_generic_mapping, NULL, 0);
763                 goto done;
764         }
765
766         dinfo = policy_handle_find(p, r->in.handle,
767                                    STD_RIGHT_READ_CONTROL_ACCESS, NULL,
768                                    struct samr_domain_info, &status);
769         if (NT_STATUS_IS_OK(status)) {
770                 DEBUG(5,("_samr_QuerySecurity: querying security on Domain "
771                          "with SID: %s\n", sid_string_dbg(&dinfo->sid)));
772                 /*
773                  * TODO: Builtin probably needs a different SD with restricted
774                  * write access
775                  */
776                 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size,
777                                              &dom_generic_mapping, NULL, 0);
778                 goto done;
779         }
780
781         uinfo = policy_handle_find(p, r->in.handle,
782                                    STD_RIGHT_READ_CONTROL_ACCESS, NULL,
783                                    struct samr_user_info, &status);
784         if (NT_STATUS_IS_OK(status)) {
785                 DEBUG(10,("_samr_QuerySecurity: querying security on user "
786                           "Object with SID: %s\n",
787                           sid_string_dbg(&uinfo->sid)));
788                 if (check_change_pw_access(p->mem_ctx, &uinfo->sid)) {
789                         status = make_samr_object_sd(
790                                 p->mem_ctx, &psd, &sd_size,
791                                 &usr_generic_mapping,
792                                 &uinfo->sid, SAMR_USR_RIGHTS_WRITE_PW);
793                 } else {
794                         status = make_samr_object_sd(
795                                 p->mem_ctx, &psd, &sd_size,
796                                 &usr_nopwchange_generic_mapping,
797                                 &uinfo->sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
798                 }
799                 goto done;
800         }
801
802         ginfo = policy_handle_find(p, r->in.handle,
803                                    STD_RIGHT_READ_CONTROL_ACCESS, NULL,
804                                    struct samr_group_info, &status);
805         if (NT_STATUS_IS_OK(status)) {
806                 /*
807                  * TODO: different SDs have to be generated for aliases groups
808                  * and users.  Currently all three get a default user SD
809                  */
810                 DEBUG(10,("_samr_QuerySecurity: querying security on group "
811                           "Object with SID: %s\n",
812                           sid_string_dbg(&ginfo->sid)));
813                 status = make_samr_object_sd(
814                         p->mem_ctx, &psd, &sd_size,
815                         &usr_nopwchange_generic_mapping,
816                         &ginfo->sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
817                 goto done;
818         }
819
820         ainfo = policy_handle_find(p, r->in.handle,
821                                    STD_RIGHT_READ_CONTROL_ACCESS, NULL,
822                                    struct samr_alias_info, &status);
823         if (NT_STATUS_IS_OK(status)) {
824                 /*
825                  * TODO: different SDs have to be generated for aliases groups
826                  * and users.  Currently all three get a default user SD
827                  */
828                 DEBUG(10,("_samr_QuerySecurity: querying security on alias "
829                           "Object with SID: %s\n",
830                           sid_string_dbg(&ainfo->sid)));
831                 status = make_samr_object_sd(
832                         p->mem_ctx, &psd, &sd_size,
833                         &usr_nopwchange_generic_mapping,
834                         &ainfo->sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
835                 goto done;
836         }
837
838         return NT_STATUS_OBJECT_TYPE_MISMATCH;
839 done:
840         if ((*r->out.sdbuf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
841                 return NT_STATUS_NO_MEMORY;
842
843         return status;
844 }
845
846 /*******************************************************************
847 makes a SAM_ENTRY / UNISTR2* structure from a user list.
848 ********************************************************************/
849
850 static NTSTATUS make_user_sam_entry_list(TALLOC_CTX *ctx,
851                                          struct samr_SamEntry **sam_pp,
852                                          uint32_t num_entries,
853                                          uint32_t start_idx,
854                                          struct samr_displayentry *entries)
855 {
856         uint32_t i;
857         struct samr_SamEntry *sam;
858
859         *sam_pp = NULL;
860
861         if (num_entries == 0) {
862                 return NT_STATUS_OK;
863         }
864
865         sam = TALLOC_ZERO_ARRAY(ctx, struct samr_SamEntry, num_entries);
866         if (sam == NULL) {
867                 DEBUG(0, ("make_user_sam_entry_list: TALLOC_ZERO failed!\n"));
868                 return NT_STATUS_NO_MEMORY;
869         }
870
871         for (i = 0; i < num_entries; i++) {
872 #if 0
873                 /*
874                  * usrmgr expects a non-NULL terminated string with
875                  * trust relationships
876                  */
877                 if (entries[i].acct_flags & ACB_DOMTRUST) {
878                         init_unistr2(&uni_temp_name, entries[i].account_name,
879                                      UNI_FLAGS_NONE);
880                 } else {
881                         init_unistr2(&uni_temp_name, entries[i].account_name,
882                                      UNI_STR_TERMINATE);
883                 }
884 #endif
885                 init_lsa_String(&sam[i].name, entries[i].account_name);
886                 sam[i].idx = entries[i].rid;
887         }
888
889         *sam_pp = sam;
890
891         return NT_STATUS_OK;
892 }
893
894 #define MAX_SAM_ENTRIES MAX_SAM_ENTRIES_W2K
895
896 /*******************************************************************
897  _samr_EnumDomainUsers
898  ********************************************************************/
899
900 NTSTATUS _samr_EnumDomainUsers(pipes_struct *p,
901                                struct samr_EnumDomainUsers *r)
902 {
903         NTSTATUS status;
904         struct samr_domain_info *dinfo;
905         int num_account;
906         uint32 enum_context = *r->in.resume_handle;
907         enum remote_arch_types ra_type = get_remote_arch();
908         int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
909         uint32 max_entries = max_sam_entries;
910         struct samr_displayentry *entries = NULL;
911         struct samr_SamArray *samr_array = NULL;
912         struct samr_SamEntry *samr_entries = NULL;
913
914         DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
915
916         dinfo = policy_handle_find(p, r->in.domain_handle,
917                                    SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
918                                    struct samr_domain_info, &status);
919         if (!NT_STATUS_IS_OK(status)) {
920                 return status;
921         }
922
923         if (sid_check_is_builtin(&dinfo->sid)) {
924                 /* No users in builtin. */
925                 *r->out.resume_handle = *r->in.resume_handle;
926                 DEBUG(5,("_samr_EnumDomainUsers: No users in BUILTIN\n"));
927                 return status;
928         }
929
930         samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
931         if (!samr_array) {
932                 return NT_STATUS_NO_MEMORY;
933         }
934         *r->out.sam = samr_array;
935
936         become_root();
937
938         /* AS ROOT !!!! */
939
940         if ((dinfo->disp_info->enum_users != NULL) &&
941             (dinfo->disp_info->enum_acb_mask != r->in.acct_flags)) {
942                 TALLOC_FREE(dinfo->disp_info->enum_users);
943         }
944
945         if (dinfo->disp_info->enum_users == NULL) {
946                 dinfo->disp_info->enum_users = pdb_search_users(
947                         dinfo->disp_info, r->in.acct_flags);
948                 dinfo->disp_info->enum_acb_mask = r->in.acct_flags;
949         }
950
951         if (dinfo->disp_info->enum_users == NULL) {
952                 /* END AS ROOT !!!! */
953                 unbecome_root();
954                 return NT_STATUS_ACCESS_DENIED;
955         }
956
957         num_account = pdb_search_entries(dinfo->disp_info->enum_users,
958                                          enum_context, max_entries,
959                                          &entries);
960
961         /* END AS ROOT !!!! */
962
963         unbecome_root();
964
965         if (num_account == 0) {
966                 DEBUG(5, ("_samr_EnumDomainUsers: enumeration handle over "
967                           "total entries\n"));
968                 *r->out.resume_handle = *r->in.resume_handle;
969                 return NT_STATUS_OK;
970         }
971
972         status = make_user_sam_entry_list(p->mem_ctx, &samr_entries,
973                                           num_account, enum_context,
974                                           entries);
975         if (!NT_STATUS_IS_OK(status)) {
976                 return status;
977         }
978
979         if (max_entries <= num_account) {
980                 status = STATUS_MORE_ENTRIES;
981         } else {
982                 status = NT_STATUS_OK;
983         }
984
985         /* Ensure we cache this enumeration. */
986         set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
987
988         DEBUG(5, ("_samr_EnumDomainUsers: %d\n", __LINE__));
989
990         samr_array->count = num_account;
991         samr_array->entries = samr_entries;
992
993         *r->out.resume_handle = *r->in.resume_handle + num_account;
994         *r->out.num_entries = num_account;
995
996         DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
997
998         return status;
999 }
1000
1001 /*******************************************************************
1002 makes a SAM_ENTRY / UNISTR2* structure from a group list.
1003 ********************************************************************/
1004
1005 static void make_group_sam_entry_list(TALLOC_CTX *ctx,
1006                                       struct samr_SamEntry **sam_pp,
1007                                       uint32_t num_sam_entries,
1008                                       struct samr_displayentry *entries)
1009 {
1010         struct samr_SamEntry *sam;
1011         uint32_t i;
1012
1013         *sam_pp = NULL;
1014
1015         if (num_sam_entries == 0) {
1016                 return;
1017         }
1018
1019         sam = TALLOC_ZERO_ARRAY(ctx, struct samr_SamEntry, num_sam_entries);
1020         if (sam == NULL) {
1021                 return;
1022         }
1023
1024         for (i = 0; i < num_sam_entries; i++) {
1025                 /*
1026                  * JRA. I think this should include the null. TNG does not.
1027                  */
1028                 init_lsa_String(&sam[i].name, entries[i].account_name);
1029                 sam[i].idx = entries[i].rid;
1030         }
1031
1032         *sam_pp = sam;
1033 }
1034
1035 /*******************************************************************
1036  _samr_EnumDomainGroups
1037  ********************************************************************/
1038
1039 NTSTATUS _samr_EnumDomainGroups(pipes_struct *p,
1040                                 struct samr_EnumDomainGroups *r)
1041 {
1042         NTSTATUS status;
1043         struct samr_domain_info *dinfo;
1044         struct samr_displayentry *groups;
1045         uint32 num_groups;
1046         struct samr_SamArray *samr_array = NULL;
1047         struct samr_SamEntry *samr_entries = NULL;
1048
1049         dinfo = policy_handle_find(p, r->in.domain_handle,
1050                                    SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
1051                                    struct samr_domain_info, &status);
1052         if (!NT_STATUS_IS_OK(status)) {
1053                 return status;
1054         }
1055
1056         DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
1057
1058         if (sid_check_is_builtin(&dinfo->sid)) {
1059                 /* No groups in builtin. */
1060                 *r->out.resume_handle = *r->in.resume_handle;
1061                 DEBUG(5,("_samr_EnumDomainGroups: No groups in BUILTIN\n"));
1062                 return status;
1063         }
1064
1065         samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1066         if (!samr_array) {
1067                 return NT_STATUS_NO_MEMORY;
1068         }
1069
1070         /* the domain group array is being allocated in the function below */
1071
1072         become_root();
1073
1074         if (dinfo->disp_info->groups == NULL) {
1075                 dinfo->disp_info->groups = pdb_search_groups(dinfo->disp_info);
1076
1077                 if (dinfo->disp_info->groups == NULL) {
1078                         unbecome_root();
1079                         return NT_STATUS_ACCESS_DENIED;
1080                 }
1081         }
1082
1083         num_groups = pdb_search_entries(dinfo->disp_info->groups,
1084                                         *r->in.resume_handle,
1085                                         MAX_SAM_ENTRIES, &groups);
1086         unbecome_root();
1087
1088         /* Ensure we cache this enumeration. */
1089         set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1090
1091         make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1092                                   num_groups, groups);
1093
1094         samr_array->count = num_groups;
1095         samr_array->entries = samr_entries;
1096
1097         *r->out.sam = samr_array;
1098         *r->out.num_entries = num_groups;
1099         *r->out.resume_handle = num_groups + *r->in.resume_handle;
1100
1101         DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
1102
1103         return status;
1104 }
1105
1106 /*******************************************************************
1107  _samr_EnumDomainAliases
1108  ********************************************************************/
1109
1110 NTSTATUS _samr_EnumDomainAliases(pipes_struct *p,
1111                                  struct samr_EnumDomainAliases *r)
1112 {
1113         NTSTATUS status;
1114         struct samr_domain_info *dinfo;
1115         struct samr_displayentry *aliases;
1116         uint32 num_aliases = 0;
1117         struct samr_SamArray *samr_array = NULL;
1118         struct samr_SamEntry *samr_entries = NULL;
1119
1120         dinfo = policy_handle_find(p, r->in.domain_handle,
1121                                    SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
1122                                    struct samr_domain_info, &status);
1123         if (!NT_STATUS_IS_OK(status)) {
1124                 return status;
1125         }
1126
1127         DEBUG(5,("_samr_EnumDomainAliases: sid %s\n",
1128                  sid_string_dbg(&dinfo->sid)));
1129
1130         samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1131         if (!samr_array) {
1132                 return NT_STATUS_NO_MEMORY;
1133         }
1134
1135         become_root();
1136
1137         if (dinfo->disp_info->aliases == NULL) {
1138                 dinfo->disp_info->aliases = pdb_search_aliases(
1139                         dinfo->disp_info, &dinfo->sid);
1140                 if (dinfo->disp_info->aliases == NULL) {
1141                         unbecome_root();
1142                         return NT_STATUS_ACCESS_DENIED;
1143                 }
1144         }
1145
1146         num_aliases = pdb_search_entries(dinfo->disp_info->aliases,
1147                                          *r->in.resume_handle,
1148                                          MAX_SAM_ENTRIES, &aliases);
1149         unbecome_root();
1150
1151         /* Ensure we cache this enumeration. */
1152         set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1153
1154         make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1155                                   num_aliases, aliases);
1156
1157         DEBUG(5,("_samr_EnumDomainAliases: %d\n", __LINE__));
1158
1159         samr_array->count = num_aliases;
1160         samr_array->entries = samr_entries;
1161
1162         *r->out.sam = samr_array;
1163         *r->out.num_entries = num_aliases;
1164         *r->out.resume_handle = num_aliases + *r->in.resume_handle;
1165
1166         return status;
1167 }
1168
1169 /*******************************************************************
1170  inits a samr_DispInfoGeneral structure.
1171 ********************************************************************/
1172
1173 static NTSTATUS init_samr_dispinfo_1(TALLOC_CTX *ctx,
1174                                      struct samr_DispInfoGeneral *r,
1175                                      uint32_t num_entries,
1176                                      uint32_t start_idx,
1177                                      struct samr_displayentry *entries)
1178 {
1179         uint32 i;
1180
1181         DEBUG(10, ("init_samr_dispinfo_1: num_entries: %d\n", num_entries));
1182
1183         if (num_entries == 0) {
1184                 return NT_STATUS_OK;
1185         }
1186
1187         r->count = num_entries;
1188
1189         r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryGeneral, num_entries);
1190         if (!r->entries) {
1191                 return NT_STATUS_NO_MEMORY;
1192         }
1193
1194         for (i = 0; i < num_entries ; i++) {
1195
1196                 init_lsa_String(&r->entries[i].account_name,
1197                                 entries[i].account_name);
1198
1199                 init_lsa_String(&r->entries[i].description,
1200                                 entries[i].description);
1201
1202                 init_lsa_String(&r->entries[i].full_name,
1203                                 entries[i].fullname);
1204
1205                 r->entries[i].rid = entries[i].rid;
1206                 r->entries[i].acct_flags = entries[i].acct_flags;
1207                 r->entries[i].idx = start_idx+i+1;
1208         }
1209
1210         return NT_STATUS_OK;
1211 }
1212
1213 /*******************************************************************
1214  inits a samr_DispInfoFull structure.
1215 ********************************************************************/
1216
1217 static NTSTATUS init_samr_dispinfo_2(TALLOC_CTX *ctx,
1218                                      struct samr_DispInfoFull *r,
1219                                      uint32_t num_entries,
1220                                      uint32_t start_idx,
1221                                      struct samr_displayentry *entries)
1222 {
1223         uint32_t i;
1224
1225         DEBUG(10, ("init_samr_dispinfo_2: num_entries: %d\n", num_entries));
1226
1227         if (num_entries == 0) {
1228                 return NT_STATUS_OK;
1229         }
1230
1231         r->count = num_entries;
1232
1233         r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryFull, num_entries);
1234         if (!r->entries) {
1235                 return NT_STATUS_NO_MEMORY;
1236         }
1237
1238         for (i = 0; i < num_entries ; i++) {
1239
1240                 init_lsa_String(&r->entries[i].account_name,
1241                                 entries[i].account_name);
1242
1243                 init_lsa_String(&r->entries[i].description,
1244                                 entries[i].description);
1245
1246                 r->entries[i].rid = entries[i].rid;
1247                 r->entries[i].acct_flags = entries[i].acct_flags;
1248                 r->entries[i].idx = start_idx+i+1;
1249         }
1250
1251         return NT_STATUS_OK;
1252 }
1253
1254 /*******************************************************************
1255  inits a samr_DispInfoFullGroups structure.
1256 ********************************************************************/
1257
1258 static NTSTATUS init_samr_dispinfo_3(TALLOC_CTX *ctx,
1259                                      struct samr_DispInfoFullGroups *r,
1260                                      uint32_t num_entries,
1261                                      uint32_t start_idx,
1262                                      struct samr_displayentry *entries)
1263 {
1264         uint32_t i;
1265
1266         DEBUG(5, ("init_samr_dispinfo_3: num_entries: %d\n", num_entries));
1267
1268         if (num_entries == 0) {
1269                 return NT_STATUS_OK;
1270         }
1271
1272         r->count = num_entries;
1273
1274         r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryFullGroup, num_entries);
1275         if (!r->entries) {
1276                 return NT_STATUS_NO_MEMORY;
1277         }
1278
1279         for (i = 0; i < num_entries ; i++) {
1280
1281                 init_lsa_String(&r->entries[i].account_name,
1282                                 entries[i].account_name);
1283
1284                 init_lsa_String(&r->entries[i].description,
1285                                 entries[i].description);
1286
1287                 r->entries[i].rid = entries[i].rid;
1288                 r->entries[i].acct_flags = entries[i].acct_flags;
1289                 r->entries[i].idx = start_idx+i+1;
1290         }
1291
1292         return NT_STATUS_OK;
1293 }
1294
1295 /*******************************************************************
1296  inits a samr_DispInfoAscii structure.
1297 ********************************************************************/
1298
1299 static NTSTATUS init_samr_dispinfo_4(TALLOC_CTX *ctx,
1300                                      struct samr_DispInfoAscii *r,
1301                                      uint32_t num_entries,
1302                                      uint32_t start_idx,
1303                                      struct samr_displayentry *entries)
1304 {
1305         uint32_t i;
1306
1307         DEBUG(5, ("init_samr_dispinfo_4: num_entries: %d\n", num_entries));
1308
1309         if (num_entries == 0) {
1310                 return NT_STATUS_OK;
1311         }
1312
1313         r->count = num_entries;
1314
1315         r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryAscii, num_entries);
1316         if (!r->entries) {
1317                 return NT_STATUS_NO_MEMORY;
1318         }
1319
1320         for (i = 0; i < num_entries ; i++) {
1321
1322                 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1323                                           entries[i].account_name);
1324
1325                 r->entries[i].idx = start_idx+i+1;
1326         }
1327
1328         return NT_STATUS_OK;
1329 }
1330
1331 /*******************************************************************
1332  inits a samr_DispInfoAscii structure.
1333 ********************************************************************/
1334
1335 static NTSTATUS init_samr_dispinfo_5(TALLOC_CTX *ctx,
1336                                      struct samr_DispInfoAscii *r,
1337                                      uint32_t num_entries,
1338                                      uint32_t start_idx,
1339                                      struct samr_displayentry *entries)
1340 {
1341         uint32_t i;
1342
1343         DEBUG(5, ("init_samr_dispinfo_5: num_entries: %d\n", num_entries));
1344
1345         if (num_entries == 0) {
1346                 return NT_STATUS_OK;
1347         }
1348
1349         r->count = num_entries;
1350
1351         r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryAscii, num_entries);
1352         if (!r->entries) {
1353                 return NT_STATUS_NO_MEMORY;
1354         }
1355
1356         for (i = 0; i < num_entries ; i++) {
1357
1358                 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1359                                           entries[i].account_name);
1360
1361                 r->entries[i].idx = start_idx+i+1;
1362         }
1363
1364         return NT_STATUS_OK;
1365 }
1366
1367 /*******************************************************************
1368  _samr_QueryDisplayInfo
1369  ********************************************************************/
1370
1371 NTSTATUS _samr_QueryDisplayInfo(pipes_struct *p,
1372                                 struct samr_QueryDisplayInfo *r)
1373 {
1374         NTSTATUS status;
1375         struct samr_domain_info *dinfo;
1376         uint32 struct_size=0x20; /* W2K always reply that, client doesn't care */
1377
1378         uint32 max_entries = r->in.max_entries;
1379         uint32 enum_context = r->in.start_idx;
1380         uint32 max_size = r->in.buf_size;
1381
1382         union samr_DispInfo *disp_info = r->out.info;
1383
1384         uint32 temp_size=0, total_data_size=0;
1385         NTSTATUS disp_ret = NT_STATUS_UNSUCCESSFUL;
1386         uint32 num_account = 0;
1387         enum remote_arch_types ra_type = get_remote_arch();
1388         int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
1389         struct samr_displayentry *entries = NULL;
1390
1391         DEBUG(5,("_samr_QueryDisplayInfo: %d\n", __LINE__));
1392
1393         dinfo = policy_handle_find(p, r->in.domain_handle,
1394                                    SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
1395                                    struct samr_domain_info, &status);
1396         if (!NT_STATUS_IS_OK(status)) {
1397                 return status;
1398         }
1399
1400         if (sid_check_is_builtin(&dinfo->sid)) {
1401                 DEBUG(5,("_samr_QueryDisplayInfo: no users in BUILTIN\n"));
1402                 return NT_STATUS_OK;
1403         }
1404
1405         /*
1406          * calculate how many entries we will return.
1407          * based on
1408          * - the number of entries the client asked
1409          * - our limit on that
1410          * - the starting point (enumeration context)
1411          * - the buffer size the client will accept
1412          */
1413
1414         /*
1415          * We are a lot more like W2K. Instead of reading the SAM
1416          * each time to find the records we need to send back,
1417          * we read it once and link that copy to the sam handle.
1418          * For large user list (over the MAX_SAM_ENTRIES)
1419          * it's a definitive win.
1420          * second point to notice: between enumerations
1421          * our sam is now the same as it's a snapshoot.
1422          * third point: got rid of the static SAM_USER_21 struct
1423          * no more intermediate.
1424          * con: it uses much more memory, as a full copy is stored
1425          * in memory.
1426          *
1427          * If you want to change it, think twice and think
1428          * of the second point , that's really important.
1429          *
1430          * JFM, 12/20/2001
1431          */
1432
1433         if ((r->in.level < 1) || (r->in.level > 5)) {
1434                 DEBUG(0,("_samr_QueryDisplayInfo: Unknown info level (%u)\n",
1435                          (unsigned int)r->in.level ));
1436                 return NT_STATUS_INVALID_INFO_CLASS;
1437         }
1438
1439         /* first limit the number of entries we will return */
1440         if(max_entries > max_sam_entries) {
1441                 DEBUG(5, ("_samr_QueryDisplayInfo: client requested %d "
1442                           "entries, limiting to %d\n", max_entries,
1443                           max_sam_entries));
1444                 max_entries = max_sam_entries;
1445         }
1446
1447         /* calculate the size and limit on the number of entries we will
1448          * return */
1449
1450         temp_size=max_entries*struct_size;
1451
1452         if (temp_size>max_size) {
1453                 max_entries=MIN((max_size/struct_size),max_entries);;
1454                 DEBUG(5, ("_samr_QueryDisplayInfo: buffer size limits to "
1455                           "only %d entries\n", max_entries));
1456         }
1457
1458         become_root();
1459
1460         /* THe following done as ROOT. Don't return without unbecome_root(). */
1461
1462         switch (r->in.level) {
1463         case 0x1:
1464         case 0x4:
1465                 if (dinfo->disp_info->users == NULL) {
1466                         dinfo->disp_info->users = pdb_search_users(
1467                                 dinfo->disp_info, ACB_NORMAL);
1468                         if (dinfo->disp_info->users == NULL) {
1469                                 unbecome_root();
1470                                 return NT_STATUS_ACCESS_DENIED;
1471                         }
1472                         DEBUG(10,("_samr_QueryDisplayInfo: starting user enumeration at index %u\n",
1473                                 (unsigned  int)enum_context ));
1474                 } else {
1475                         DEBUG(10,("_samr_QueryDisplayInfo: using cached user enumeration at index %u\n",
1476                                 (unsigned  int)enum_context ));
1477                 }
1478
1479                 num_account = pdb_search_entries(dinfo->disp_info->users,
1480                                                  enum_context, max_entries,
1481                                                  &entries);
1482                 break;
1483         case 0x2:
1484                 if (dinfo->disp_info->machines == NULL) {
1485                         dinfo->disp_info->machines = pdb_search_users(
1486                                 dinfo->disp_info, ACB_WSTRUST|ACB_SVRTRUST);
1487                         if (dinfo->disp_info->machines == NULL) {
1488                                 unbecome_root();
1489                                 return NT_STATUS_ACCESS_DENIED;
1490                         }
1491                         DEBUG(10,("_samr_QueryDisplayInfo: starting machine enumeration at index %u\n",
1492                                 (unsigned  int)enum_context ));
1493                 } else {
1494                         DEBUG(10,("_samr_QueryDisplayInfo: using cached machine enumeration at index %u\n",
1495                                 (unsigned  int)enum_context ));
1496                 }
1497
1498                 num_account = pdb_search_entries(dinfo->disp_info->machines,
1499                                                  enum_context, max_entries,
1500                                                  &entries);
1501                 break;
1502         case 0x3:
1503         case 0x5:
1504                 if (dinfo->disp_info->groups == NULL) {
1505                         dinfo->disp_info->groups = pdb_search_groups(
1506                                 dinfo->disp_info);
1507                         if (dinfo->disp_info->groups == NULL) {
1508                                 unbecome_root();
1509                                 return NT_STATUS_ACCESS_DENIED;
1510                         }
1511                         DEBUG(10,("_samr_QueryDisplayInfo: starting group enumeration at index %u\n",
1512                                 (unsigned  int)enum_context ));
1513                 } else {
1514                         DEBUG(10,("_samr_QueryDisplayInfo: using cached group enumeration at index %u\n",
1515                                 (unsigned  int)enum_context ));
1516                 }
1517
1518                 num_account = pdb_search_entries(dinfo->disp_info->groups,
1519                                                  enum_context, max_entries,
1520                                                  &entries);
1521                 break;
1522         default:
1523                 unbecome_root();
1524                 smb_panic("info class changed");
1525                 break;
1526         }
1527         unbecome_root();
1528
1529
1530         /* Now create reply structure */
1531         switch (r->in.level) {
1532         case 0x1:
1533                 disp_ret = init_samr_dispinfo_1(p->mem_ctx, &disp_info->info1,
1534                                                 num_account, enum_context,
1535                                                 entries);
1536                 break;
1537         case 0x2:
1538                 disp_ret = init_samr_dispinfo_2(p->mem_ctx, &disp_info->info2,
1539                                                 num_account, enum_context,
1540                                                 entries);
1541                 break;
1542         case 0x3:
1543                 disp_ret = init_samr_dispinfo_3(p->mem_ctx, &disp_info->info3,
1544                                                 num_account, enum_context,
1545                                                 entries);
1546                 break;
1547         case 0x4:
1548                 disp_ret = init_samr_dispinfo_4(p->mem_ctx, &disp_info->info4,
1549                                                 num_account, enum_context,
1550                                                 entries);
1551                 break;
1552         case 0x5:
1553                 disp_ret = init_samr_dispinfo_5(p->mem_ctx, &disp_info->info5,
1554                                                 num_account, enum_context,
1555                                                 entries);
1556                 break;
1557         default:
1558                 smb_panic("info class changed");
1559                 break;
1560         }
1561
1562         if (!NT_STATUS_IS_OK(disp_ret))
1563                 return disp_ret;
1564
1565         /* calculate the total size */
1566         total_data_size=num_account*struct_size;
1567
1568         if (max_entries <= num_account) {
1569                 status = STATUS_MORE_ENTRIES;
1570         } else {
1571                 status = NT_STATUS_OK;
1572         }
1573
1574         /* Ensure we cache this enumeration. */
1575         set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1576
1577         DEBUG(5, ("_samr_QueryDisplayInfo: %d\n", __LINE__));
1578
1579         *r->out.total_size = total_data_size;
1580         *r->out.returned_size = temp_size;
1581
1582         return status;
1583 }
1584
1585 /****************************************************************
1586  _samr_QueryDisplayInfo2
1587 ****************************************************************/
1588
1589 NTSTATUS _samr_QueryDisplayInfo2(pipes_struct *p,
1590                                  struct samr_QueryDisplayInfo2 *r)
1591 {
1592         struct samr_QueryDisplayInfo q;
1593
1594         q.in.domain_handle      = r->in.domain_handle;
1595         q.in.level              = r->in.level;
1596         q.in.start_idx          = r->in.start_idx;
1597         q.in.max_entries        = r->in.max_entries;
1598         q.in.buf_size           = r->in.buf_size;
1599
1600         q.out.total_size        = r->out.total_size;
1601         q.out.returned_size     = r->out.returned_size;
1602         q.out.info              = r->out.info;
1603
1604         return _samr_QueryDisplayInfo(p, &q);
1605 }
1606
1607 /****************************************************************
1608  _samr_QueryDisplayInfo3
1609 ****************************************************************/
1610
1611 NTSTATUS _samr_QueryDisplayInfo3(pipes_struct *p,
1612                                  struct samr_QueryDisplayInfo3 *r)
1613 {
1614         struct samr_QueryDisplayInfo q;
1615
1616         q.in.domain_handle      = r->in.domain_handle;
1617         q.in.level              = r->in.level;
1618         q.in.start_idx          = r->in.start_idx;
1619         q.in.max_entries        = r->in.max_entries;
1620         q.in.buf_size           = r->in.buf_size;
1621
1622         q.out.total_size        = r->out.total_size;
1623         q.out.returned_size     = r->out.returned_size;
1624         q.out.info              = r->out.info;
1625
1626         return _samr_QueryDisplayInfo(p, &q);
1627 }
1628
1629 /*******************************************************************
1630  _samr_QueryAliasInfo
1631  ********************************************************************/
1632
1633 NTSTATUS _samr_QueryAliasInfo(pipes_struct *p,
1634                               struct samr_QueryAliasInfo *r)
1635 {
1636         struct samr_alias_info *ainfo;
1637         struct acct_info info;
1638         NTSTATUS status;
1639         union samr_AliasInfo *alias_info = NULL;
1640         const char *alias_name = NULL;
1641         const char *alias_description = NULL;
1642
1643         DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1644
1645         ainfo = policy_handle_find(p, r->in.alias_handle,
1646                                    SAMR_ALIAS_ACCESS_LOOKUP_INFO, NULL,
1647                                    struct samr_alias_info, &status);
1648         if (!NT_STATUS_IS_OK(status)) {
1649                 return status;
1650         }
1651
1652         alias_info = TALLOC_ZERO_P(p->mem_ctx, union samr_AliasInfo);
1653         if (!alias_info) {
1654                 return NT_STATUS_NO_MEMORY;
1655         }
1656
1657         become_root();
1658         status = pdb_get_aliasinfo(&ainfo->sid, &info);
1659         unbecome_root();
1660
1661         if ( !NT_STATUS_IS_OK(status))
1662                 return status;
1663
1664         /* FIXME: info contains fstrings */
1665         alias_name = talloc_strdup(r, info.acct_name);
1666         alias_description = talloc_strdup(r, info.acct_desc);
1667
1668         switch (r->in.level) {
1669         case ALIASINFOALL:
1670                 alias_info->all.name.string             = alias_name;
1671                 alias_info->all.num_members             = 1; /* ??? */
1672                 alias_info->all.description.string      = alias_description;
1673                 break;
1674         case ALIASINFODESCRIPTION:
1675                 alias_info->description.string          = alias_description;
1676                 break;
1677         default:
1678                 return NT_STATUS_INVALID_INFO_CLASS;
1679         }
1680
1681         *r->out.info = alias_info;
1682
1683         DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1684
1685         return NT_STATUS_OK;
1686 }
1687
1688 /*******************************************************************
1689  _samr_LookupNames
1690  ********************************************************************/
1691
1692 NTSTATUS _samr_LookupNames(pipes_struct *p,
1693                            struct samr_LookupNames *r)
1694 {
1695         struct samr_domain_info *dinfo;
1696         NTSTATUS status;
1697         uint32 *rid;
1698         enum lsa_SidType *type;
1699         int i;
1700         int num_rids = r->in.num_names;
1701         struct samr_Ids rids, types;
1702         uint32_t num_mapped = 0;
1703
1704         DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1705
1706         dinfo = policy_handle_find(p, r->in.domain_handle,
1707                                    0 /* Don't know the acc_bits yet */, NULL,
1708                                    struct samr_domain_info, &status);
1709         if (!NT_STATUS_IS_OK(status)) {
1710                 return status;
1711         }
1712
1713         if (num_rids > MAX_SAM_ENTRIES) {
1714                 num_rids = MAX_SAM_ENTRIES;
1715                 DEBUG(5,("_samr_LookupNames: truncating entries to %d\n", num_rids));
1716         }
1717
1718         rid = talloc_array(p->mem_ctx, uint32, num_rids);
1719         NT_STATUS_HAVE_NO_MEMORY(rid);
1720
1721         type = talloc_array(p->mem_ctx, enum lsa_SidType, num_rids);
1722         NT_STATUS_HAVE_NO_MEMORY(type);
1723
1724         DEBUG(5,("_samr_LookupNames: looking name on SID %s\n",
1725                  sid_string_dbg(&dinfo->sid)));
1726
1727         for (i = 0; i < num_rids; i++) {
1728
1729                 status = NT_STATUS_NONE_MAPPED;
1730                 type[i] = SID_NAME_UNKNOWN;
1731
1732                 rid[i] = 0xffffffff;
1733
1734                 if (sid_check_is_builtin(&dinfo->sid)) {
1735                         if (lookup_builtin_name(r->in.names[i].string,
1736                                                 &rid[i]))
1737                         {
1738                                 type[i] = SID_NAME_ALIAS;
1739                         }
1740                 } else {
1741                         lookup_global_sam_name(r->in.names[i].string, 0,
1742                                                &rid[i], &type[i]);
1743                 }
1744
1745                 if (type[i] != SID_NAME_UNKNOWN) {
1746                         num_mapped++;
1747                 }
1748         }
1749
1750         if (num_mapped == num_rids) {
1751                 status = NT_STATUS_OK;
1752         } else if (num_mapped == 0) {
1753                 status = NT_STATUS_NONE_MAPPED;
1754         } else {
1755                 status = STATUS_SOME_UNMAPPED;
1756         }
1757
1758         rids.count = num_rids;
1759         rids.ids = rid;
1760
1761         types.count = num_rids;
1762         types.ids = type;
1763
1764         *r->out.rids = rids;
1765         *r->out.types = types;
1766
1767         DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1768
1769         return status;
1770 }
1771
1772 /*******************************************************************
1773  _samr_ChangePasswordUser2
1774  ********************************************************************/
1775
1776 NTSTATUS _samr_ChangePasswordUser2(pipes_struct *p,
1777                                    struct samr_ChangePasswordUser2 *r)
1778 {
1779         NTSTATUS status;
1780         fstring user_name;
1781         fstring wks;
1782
1783         DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1784
1785         fstrcpy(user_name, r->in.account->string);
1786         fstrcpy(wks, r->in.server->string);
1787
1788         DEBUG(5,("_samr_ChangePasswordUser2: user: %s wks: %s\n", user_name, wks));
1789
1790         /*
1791          * Pass the user through the NT -> unix user mapping
1792          * function.
1793          */
1794
1795         (void)map_username(user_name);
1796
1797         /*
1798          * UNIX username case mangling not required, pass_oem_change
1799          * is case insensitive.
1800          */
1801
1802         status = pass_oem_change(user_name,
1803                                  r->in.lm_password->data,
1804                                  r->in.lm_verifier->hash,
1805                                  r->in.nt_password->data,
1806                                  r->in.nt_verifier->hash,
1807                                  NULL);
1808
1809         DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1810
1811         return status;
1812 }
1813
1814 /*******************************************************************
1815  _samr_ChangePasswordUser3
1816  ********************************************************************/
1817
1818 NTSTATUS _samr_ChangePasswordUser3(pipes_struct *p,
1819                                    struct samr_ChangePasswordUser3 *r)
1820 {
1821         NTSTATUS status;
1822         fstring user_name;
1823         const char *wks = NULL;
1824         uint32 reject_reason;
1825         struct samr_DomInfo1 *dominfo = NULL;
1826         struct samr_ChangeReject *reject = NULL;
1827         uint32_t tmp;
1828
1829         DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
1830
1831         fstrcpy(user_name, r->in.account->string);
1832         if (r->in.server && r->in.server->string) {
1833                 wks = r->in.server->string;
1834         }
1835
1836         DEBUG(5,("_samr_ChangePasswordUser3: user: %s wks: %s\n", user_name, wks));
1837
1838         /*
1839          * Pass the user through the NT -> unix user mapping
1840          * function.
1841          */
1842
1843         (void)map_username(user_name);
1844
1845         /*
1846          * UNIX username case mangling not required, pass_oem_change
1847          * is case insensitive.
1848          */
1849
1850         status = pass_oem_change(user_name,
1851                                  r->in.lm_password->data,
1852                                  r->in.lm_verifier->hash,
1853                                  r->in.nt_password->data,
1854                                  r->in.nt_verifier->hash,
1855                                  &reject_reason);
1856
1857         if (NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION) ||
1858             NT_STATUS_EQUAL(status, NT_STATUS_ACCOUNT_RESTRICTION)) {
1859
1860                 time_t u_expire, u_min_age;
1861                 uint32 account_policy_temp;
1862
1863                 dominfo = TALLOC_ZERO_P(p->mem_ctx, struct samr_DomInfo1);
1864                 if (!dominfo) {
1865                         return NT_STATUS_NO_MEMORY;
1866                 }
1867
1868                 reject = TALLOC_ZERO_P(p->mem_ctx, struct samr_ChangeReject);
1869                 if (!reject) {
1870                         return NT_STATUS_NO_MEMORY;
1871                 }
1872
1873                 become_root();
1874
1875                 /* AS ROOT !!! */
1876
1877                 pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &tmp);
1878                 dominfo->min_password_length = tmp;
1879
1880                 pdb_get_account_policy(AP_PASSWORD_HISTORY, &tmp);
1881                 dominfo->password_history_length = tmp;
1882
1883                 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS,
1884                                        &dominfo->password_properties);
1885
1886                 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
1887                 u_expire = account_policy_temp;
1888
1889                 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
1890                 u_min_age = account_policy_temp;
1891
1892                 /* !AS ROOT */
1893
1894                 unbecome_root();
1895
1896                 unix_to_nt_time_abs((NTTIME *)&dominfo->max_password_age, u_expire);
1897                 unix_to_nt_time_abs((NTTIME *)&dominfo->min_password_age, u_min_age);
1898
1899                 if (lp_check_password_script() && *lp_check_password_script()) {
1900                         dominfo->password_properties |= DOMAIN_PASSWORD_COMPLEX;
1901                 }
1902
1903                 reject->reason = reject_reason;
1904
1905                 *r->out.dominfo = dominfo;
1906                 *r->out.reject = reject;
1907         }
1908
1909         DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
1910
1911         return status;
1912 }
1913
1914 /*******************************************************************
1915 makes a SAMR_R_LOOKUP_RIDS structure.
1916 ********************************************************************/
1917
1918 static bool make_samr_lookup_rids(TALLOC_CTX *ctx, uint32 num_names,
1919                                   const char **names,
1920                                   struct lsa_String **lsa_name_array_p)
1921 {
1922         struct lsa_String *lsa_name_array = NULL;
1923         uint32_t i;
1924
1925         *lsa_name_array_p = NULL;
1926
1927         if (num_names != 0) {
1928                 lsa_name_array = TALLOC_ZERO_ARRAY(ctx, struct lsa_String, num_names);
1929                 if (!lsa_name_array) {
1930                         return false;
1931                 }
1932         }
1933
1934         for (i = 0; i < num_names; i++) {
1935                 DEBUG(10, ("names[%d]:%s\n", i, names[i] && *names[i] ? names[i] : ""));
1936                 init_lsa_String(&lsa_name_array[i], names[i]);
1937         }
1938
1939         *lsa_name_array_p = lsa_name_array;
1940
1941         return true;
1942 }
1943
1944 /*******************************************************************
1945  _samr_LookupRids
1946  ********************************************************************/
1947
1948 NTSTATUS _samr_LookupRids(pipes_struct *p,
1949                           struct samr_LookupRids *r)
1950 {
1951         struct samr_domain_info *dinfo;
1952         NTSTATUS status;
1953         const char **names;
1954         enum lsa_SidType *attrs = NULL;
1955         uint32 *wire_attrs = NULL;
1956         int num_rids = (int)r->in.num_rids;
1957         int i;
1958         struct lsa_Strings names_array;
1959         struct samr_Ids types_array;
1960         struct lsa_String *lsa_names = NULL;
1961
1962         DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
1963
1964         dinfo = policy_handle_find(p, r->in.domain_handle,
1965                                    0 /* Don't know the acc_bits yet */, NULL,
1966                                    struct samr_domain_info, &status);
1967         if (!NT_STATUS_IS_OK(status)) {
1968                 return status;
1969         }
1970
1971         if (num_rids > 1000) {
1972                 DEBUG(0, ("Got asked for %d rids (more than 1000) -- according "
1973                           "to samba4 idl this is not possible\n", num_rids));
1974                 return NT_STATUS_UNSUCCESSFUL;
1975         }
1976
1977         if (num_rids) {
1978                 names = TALLOC_ZERO_ARRAY(p->mem_ctx, const char *, num_rids);
1979                 attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, enum lsa_SidType, num_rids);
1980                 wire_attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_rids);
1981
1982                 if ((names == NULL) || (attrs == NULL) || (wire_attrs==NULL))
1983                         return NT_STATUS_NO_MEMORY;
1984         } else {
1985                 names = NULL;
1986                 attrs = NULL;
1987                 wire_attrs = NULL;
1988         }
1989
1990         become_root();  /* lookup_sid can require root privs */
1991         status = pdb_lookup_rids(&dinfo->sid, num_rids, r->in.rids,
1992                                  names, attrs);
1993         unbecome_root();
1994
1995         if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED) && (num_rids == 0)) {
1996                 status = NT_STATUS_OK;
1997         }
1998
1999         if (!make_samr_lookup_rids(p->mem_ctx, num_rids, names,
2000                                    &lsa_names)) {
2001                 return NT_STATUS_NO_MEMORY;
2002         }
2003
2004         /* Convert from enum lsa_SidType to uint32 for wire format. */
2005         for (i = 0; i < num_rids; i++) {
2006                 wire_attrs[i] = (uint32)attrs[i];
2007         }
2008
2009         names_array.count = num_rids;
2010         names_array.names = lsa_names;
2011
2012         types_array.count = num_rids;
2013         types_array.ids = wire_attrs;
2014
2015         *r->out.names = names_array;
2016         *r->out.types = types_array;
2017
2018         DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
2019
2020         return status;
2021 }
2022
2023 /*******************************************************************
2024  _samr_OpenUser
2025 ********************************************************************/
2026
2027 NTSTATUS _samr_OpenUser(pipes_struct *p,
2028                         struct samr_OpenUser *r)
2029 {
2030         struct samu *sampass=NULL;
2031         DOM_SID sid;
2032         struct samr_domain_info *dinfo;
2033         struct samr_user_info *uinfo;
2034         SEC_DESC *psd = NULL;
2035         uint32    acc_granted;
2036         uint32    des_access = r->in.access_mask;
2037         size_t    sd_size;
2038         bool ret;
2039         NTSTATUS nt_status;
2040         SE_PRIV se_rights;
2041         NTSTATUS status;
2042
2043         dinfo = policy_handle_find(p, r->in.domain_handle,
2044                                    SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
2045                                    struct samr_domain_info, &status);
2046         if (!NT_STATUS_IS_OK(status)) {
2047                 return status;
2048         }
2049
2050         if ( !(sampass = samu_new( p->mem_ctx )) ) {
2051                 return NT_STATUS_NO_MEMORY;
2052         }
2053
2054         /* append the user's RID to it */
2055
2056         if (!sid_compose(&sid, &dinfo->sid, r->in.rid))
2057                 return NT_STATUS_NO_SUCH_USER;
2058
2059         /* check if access can be granted as requested by client. */
2060
2061         map_max_allowed_access(p->server_info->ptok, &des_access);
2062
2063         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
2064         se_map_generic(&des_access, &usr_generic_mapping);
2065
2066         se_priv_copy( &se_rights, &se_machine_account );
2067         se_priv_add( &se_rights, &se_add_users );
2068
2069         nt_status = access_check_samr_object(psd, p->server_info->ptok,
2070                 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
2071                 &acc_granted, "_samr_OpenUser");
2072
2073         if ( !NT_STATUS_IS_OK(nt_status) )
2074                 return nt_status;
2075
2076         become_root();
2077         ret=pdb_getsampwsid(sampass, &sid);
2078         unbecome_root();
2079
2080         /* check that the SID exists in our domain. */
2081         if (ret == False) {
2082                 return NT_STATUS_NO_SUCH_USER;
2083         }
2084
2085         TALLOC_FREE(sampass);
2086
2087         uinfo = policy_handle_create(p, r->out.user_handle, acc_granted,
2088                                      struct samr_user_info, &nt_status);
2089         if (!NT_STATUS_IS_OK(nt_status)) {
2090                 return nt_status;
2091         }
2092         uinfo->sid = sid;
2093
2094         return NT_STATUS_OK;
2095 }
2096
2097 /*************************************************************************
2098  *************************************************************************/
2099
2100 static NTSTATUS init_samr_parameters_string(TALLOC_CTX *mem_ctx,
2101                                             DATA_BLOB *blob,
2102                                             struct lsa_BinaryString **_r)
2103 {
2104         struct lsa_BinaryString *r;
2105
2106         if (!blob || !_r) {
2107                 return NT_STATUS_INVALID_PARAMETER;
2108         }
2109
2110         r = TALLOC_ZERO_P(mem_ctx, struct lsa_BinaryString);
2111         if (!r) {
2112                 return NT_STATUS_NO_MEMORY;
2113         }
2114
2115         r->array = TALLOC_ZERO_ARRAY(mem_ctx, uint16_t, blob->length/2);
2116         if (!r->array) {
2117                 return NT_STATUS_NO_MEMORY;
2118         }
2119         memcpy(r->array, blob->data, blob->length);
2120         r->size = blob->length;
2121         r->length = blob->length;
2122
2123         if (!r->array) {
2124                 return NT_STATUS_NO_MEMORY;
2125         }
2126
2127         *_r = r;
2128
2129         return NT_STATUS_OK;
2130 }
2131
2132 /*************************************************************************
2133  get_user_info_1.
2134  *************************************************************************/
2135
2136 static NTSTATUS get_user_info_1(TALLOC_CTX *mem_ctx,
2137                                 struct samr_UserInfo1 *r,
2138                                 struct samu *pw,
2139                                 DOM_SID *domain_sid)
2140 {
2141         const DOM_SID *sid_group;
2142         uint32_t primary_gid;
2143
2144         become_root();
2145         sid_group = pdb_get_group_sid(pw);
2146         unbecome_root();
2147
2148         if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2149                 DEBUG(0, ("get_user_info_1: User %s has Primary Group SID %s, \n"
2150                           "which conflicts with the domain sid %s.  Failing operation.\n",
2151                           pdb_get_username(pw), sid_string_dbg(sid_group),
2152                           sid_string_dbg(domain_sid)));
2153                 return NT_STATUS_UNSUCCESSFUL;
2154         }
2155
2156         r->account_name.string          = talloc_strdup(mem_ctx, pdb_get_username(pw));
2157         r->full_name.string             = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2158         r->primary_gid                  = primary_gid;
2159         r->description.string           = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2160         r->comment.string               = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2161
2162         return NT_STATUS_OK;
2163 }
2164
2165 /*************************************************************************
2166  get_user_info_2.
2167  *************************************************************************/
2168
2169 static NTSTATUS get_user_info_2(TALLOC_CTX *mem_ctx,
2170                                 struct samr_UserInfo2 *r,
2171                                 struct samu *pw)
2172 {
2173         r->comment.string               = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2174         r->unknown.string               = NULL;
2175         r->country_code                 = 0;
2176         r->code_page                    = 0;
2177
2178         return NT_STATUS_OK;
2179 }
2180
2181 /*************************************************************************
2182  get_user_info_3.
2183  *************************************************************************/
2184
2185 static NTSTATUS get_user_info_3(TALLOC_CTX *mem_ctx,
2186                                 struct samr_UserInfo3 *r,
2187                                 struct samu *pw,
2188                                 DOM_SID *domain_sid)
2189 {
2190         const DOM_SID *sid_user, *sid_group;
2191         uint32_t rid, primary_gid;
2192
2193         sid_user = pdb_get_user_sid(pw);
2194
2195         if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2196                 DEBUG(0, ("get_user_info_3: User %s has SID %s, \nwhich conflicts with "
2197                           "the domain sid %s.  Failing operation.\n",
2198                           pdb_get_username(pw), sid_string_dbg(sid_user),
2199                           sid_string_dbg(domain_sid)));
2200                 return NT_STATUS_UNSUCCESSFUL;
2201         }
2202
2203         become_root();
2204         sid_group = pdb_get_group_sid(pw);
2205         unbecome_root();
2206
2207         if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2208                 DEBUG(0, ("get_user_info_3: User %s has Primary Group SID %s, \n"
2209                           "which conflicts with the domain sid %s.  Failing operation.\n",
2210                           pdb_get_username(pw), sid_string_dbg(sid_group),
2211                           sid_string_dbg(domain_sid)));
2212                 return NT_STATUS_UNSUCCESSFUL;
2213         }
2214
2215         unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
2216         unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
2217         unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
2218         unix_to_nt_time(&r->allow_password_change, pdb_get_pass_can_change_time(pw));
2219         unix_to_nt_time(&r->force_password_change, pdb_get_pass_must_change_time(pw));
2220
2221         r->account_name.string  = talloc_strdup(mem_ctx, pdb_get_username(pw));
2222         r->full_name.string     = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2223         r->home_directory.string= talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2224         r->home_drive.string    = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2225         r->logon_script.string  = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2226         r->profile_path.string  = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2227         r->workstations.string  = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2228
2229         r->logon_hours          = get_logon_hours_from_pdb(mem_ctx, pw);
2230         r->rid                  = rid;
2231         r->primary_gid          = primary_gid;
2232         r->acct_flags           = pdb_get_acct_ctrl(pw);
2233         r->bad_password_count   = pdb_get_bad_password_count(pw);
2234         r->logon_count          = pdb_get_logon_count(pw);
2235
2236         return NT_STATUS_OK;
2237 }
2238
2239 /*************************************************************************
2240  get_user_info_4.
2241  *************************************************************************/
2242
2243 static NTSTATUS get_user_info_4(TALLOC_CTX *mem_ctx,
2244                                 struct samr_UserInfo4 *r,
2245                                 struct samu *pw)
2246 {
2247         r->logon_hours          = get_logon_hours_from_pdb(mem_ctx, pw);
2248
2249         return NT_STATUS_OK;
2250 }
2251
2252 /*************************************************************************
2253  get_user_info_5.
2254  *************************************************************************/
2255
2256 static NTSTATUS get_user_info_5(TALLOC_CTX *mem_ctx,
2257                                 struct samr_UserInfo5 *r,
2258                                 struct samu *pw,
2259                                 DOM_SID *domain_sid)
2260 {
2261         const DOM_SID *sid_user, *sid_group;
2262         uint32_t rid, primary_gid;
2263
2264         sid_user = pdb_get_user_sid(pw);
2265
2266         if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2267                 DEBUG(0, ("get_user_info_5: User %s has SID %s, \nwhich conflicts with "
2268                           "the domain sid %s.  Failing operation.\n",
2269                           pdb_get_username(pw), sid_string_dbg(sid_user),
2270                           sid_string_dbg(domain_sid)));
2271                 return NT_STATUS_UNSUCCESSFUL;
2272         }
2273
2274         become_root();
2275         sid_group = pdb_get_group_sid(pw);
2276         unbecome_root();
2277
2278         if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2279                 DEBUG(0, ("get_user_info_5: User %s has Primary Group SID %s, \n"
2280                           "which conflicts with the domain sid %s.  Failing operation.\n",
2281                           pdb_get_username(pw), sid_string_dbg(sid_group),
2282                           sid_string_dbg(domain_sid)));
2283                 return NT_STATUS_UNSUCCESSFUL;
2284         }
2285
2286         unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
2287         unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
2288         unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
2289         unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
2290
2291         r->account_name.string  = talloc_strdup(mem_ctx, pdb_get_username(pw));
2292         r->full_name.string     = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2293         r->home_directory.string= talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2294         r->home_drive.string    = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2295         r->logon_script.string  = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2296         r->profile_path.string  = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2297         r->description.string   = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2298         r->workstations.string  = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2299
2300         r->logon_hours          = get_logon_hours_from_pdb(mem_ctx, pw);
2301         r->rid                  = rid;
2302         r->primary_gid          = primary_gid;
2303         r->acct_flags           = pdb_get_acct_ctrl(pw);
2304         r->bad_password_count   = pdb_get_bad_password_count(pw);
2305         r->logon_count          = pdb_get_logon_count(pw);
2306
2307         return NT_STATUS_OK;
2308 }
2309
2310 /*************************************************************************
2311  get_user_info_6.
2312  *************************************************************************/
2313
2314 static NTSTATUS get_user_info_6(TALLOC_CTX *mem_ctx,
2315                                 struct samr_UserInfo6 *r,
2316                                 struct samu *pw)
2317 {
2318         r->account_name.string  = talloc_strdup(mem_ctx, pdb_get_username(pw));
2319         r->full_name.string     = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2320
2321         return NT_STATUS_OK;
2322 }
2323
2324 /*************************************************************************
2325  get_user_info_7. Safe. Only gives out account_name.
2326  *************************************************************************/
2327
2328 static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx,
2329                                 struct samr_UserInfo7 *r,
2330                                 struct samu *smbpass)
2331 {
2332         r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(smbpass));
2333         if (!r->account_name.string) {
2334                 return NT_STATUS_NO_MEMORY;
2335         }
2336
2337         return NT_STATUS_OK;
2338 }
2339
2340 /*************************************************************************
2341  get_user_info_8.
2342  *************************************************************************/
2343
2344 static NTSTATUS get_user_info_8(TALLOC_CTX *mem_ctx,
2345                                 struct samr_UserInfo8 *r,
2346                                 struct samu *pw)
2347 {
2348         r->full_name.string     = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2349
2350         return NT_STATUS_OK;
2351 }
2352
2353 /*************************************************************************
2354  get_user_info_9. Only gives out primary group SID.
2355  *************************************************************************/
2356
2357 static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx,
2358                                 struct samr_UserInfo9 *r,
2359                                 struct samu *smbpass)
2360 {
2361         r->primary_gid = pdb_get_group_rid(smbpass);
2362
2363         return NT_STATUS_OK;
2364 }
2365
2366 /*************************************************************************
2367  get_user_info_10.
2368  *************************************************************************/
2369
2370 static NTSTATUS get_user_info_10(TALLOC_CTX *mem_ctx,
2371                                  struct samr_UserInfo10 *r,
2372                                  struct samu *pw)
2373 {
2374         r->home_directory.string= talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2375         r->home_drive.string    = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2376
2377         return NT_STATUS_OK;
2378 }
2379
2380 /*************************************************************************
2381  get_user_info_11.
2382  *************************************************************************/
2383
2384 static NTSTATUS get_user_info_11(TALLOC_CTX *mem_ctx,
2385                                  struct samr_UserInfo11 *r,
2386                                  struct samu *pw)
2387 {
2388         r->logon_script.string  = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2389
2390         return NT_STATUS_OK;
2391 }
2392
2393 /*************************************************************************
2394  get_user_info_12.
2395  *************************************************************************/
2396
2397 static NTSTATUS get_user_info_12(TALLOC_CTX *mem_ctx,
2398                                  struct samr_UserInfo12 *r,
2399                                  struct samu *pw)
2400 {
2401         r->profile_path.string  = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2402
2403         return NT_STATUS_OK;
2404 }
2405
2406 /*************************************************************************
2407  get_user_info_13.
2408  *************************************************************************/
2409
2410 static NTSTATUS get_user_info_13(TALLOC_CTX *mem_ctx,
2411                                  struct samr_UserInfo13 *r,
2412                                  struct samu *pw)
2413 {
2414         r->description.string   = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2415
2416         return NT_STATUS_OK;
2417 }
2418
2419 /*************************************************************************
2420  get_user_info_14.
2421  *************************************************************************/
2422
2423 static NTSTATUS get_user_info_14(TALLOC_CTX *mem_ctx,
2424                                  struct samr_UserInfo14 *r,
2425                                  struct samu *pw)
2426 {
2427         r->workstations.string  = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2428
2429         return NT_STATUS_OK;
2430 }
2431
2432 /*************************************************************************
2433  get_user_info_16. Safe. Only gives out acb bits.
2434  *************************************************************************/
2435
2436 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx,
2437                                  struct samr_UserInfo16 *r,
2438                                  struct samu *smbpass)
2439 {
2440         r->acct_flags = pdb_get_acct_ctrl(smbpass);
2441
2442         return NT_STATUS_OK;
2443 }
2444
2445 /*************************************************************************
2446  get_user_info_17.
2447  *************************************************************************/
2448
2449 static NTSTATUS get_user_info_17(TALLOC_CTX *mem_ctx,
2450                                  struct samr_UserInfo17 *r,
2451                                  struct samu *pw)
2452 {
2453         unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
2454
2455         return NT_STATUS_OK;
2456 }
2457
2458 /*************************************************************************
2459  get_user_info_18. OK - this is the killer as it gives out password info.
2460  Ensure that this is only allowed on an encrypted connection with a root
2461  user. JRA.
2462  *************************************************************************/
2463
2464 static NTSTATUS get_user_info_18(pipes_struct *p,
2465                                  TALLOC_CTX *mem_ctx,
2466                                  struct samr_UserInfo18 *r,
2467                                  DOM_SID *user_sid)
2468 {
2469         struct samu *smbpass=NULL;
2470         bool ret;
2471
2472         ZERO_STRUCTP(r);
2473
2474         if (p->auth.auth_type != PIPE_AUTH_TYPE_NTLMSSP || p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP) {
2475                 return NT_STATUS_ACCESS_DENIED;
2476         }
2477
2478         if (p->auth.auth_level != PIPE_AUTH_LEVEL_PRIVACY) {
2479                 return NT_STATUS_ACCESS_DENIED;
2480         }
2481
2482         /*
2483          * Do *NOT* do become_root()/unbecome_root() here ! JRA.
2484          */
2485
2486         if ( !(smbpass = samu_new( mem_ctx )) ) {
2487                 return NT_STATUS_NO_MEMORY;
2488         }
2489
2490         ret = pdb_getsampwsid(smbpass, user_sid);
2491
2492         if (ret == False) {
2493                 DEBUG(4, ("User %s not found\n", sid_string_dbg(user_sid)));
2494                 TALLOC_FREE(smbpass);
2495                 return (geteuid() == (uid_t)0) ? NT_STATUS_NO_SUCH_USER : NT_STATUS_ACCESS_DENIED;
2496         }
2497
2498         DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
2499
2500         if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
2501                 TALLOC_FREE(smbpass);
2502                 return NT_STATUS_ACCOUNT_DISABLED;
2503         }
2504
2505         r->lm_pwd_active = true;
2506         r->nt_pwd_active = true;
2507         memcpy(r->lm_pwd.hash, pdb_get_lanman_passwd(smbpass), 16);
2508         memcpy(r->nt_pwd.hash, pdb_get_nt_passwd(smbpass), 16);
2509         r->password_expired = 0; /* FIXME */
2510
2511         TALLOC_FREE(smbpass);
2512
2513         return NT_STATUS_OK;
2514 }
2515
2516 /*************************************************************************
2517  get_user_info_20
2518  *************************************************************************/
2519
2520 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx,
2521                                  struct samr_UserInfo20 *r,
2522                                  struct samu *sampass)
2523 {
2524         const char *munged_dial = NULL;
2525         DATA_BLOB blob;
2526         NTSTATUS status;
2527         struct lsa_BinaryString *parameters = NULL;
2528
2529         ZERO_STRUCTP(r);
2530
2531         munged_dial = pdb_get_munged_dial(sampass);
2532
2533         DEBUG(3,("User:[%s] has [%s] (length: %d)\n", pdb_get_username(sampass),
2534                 munged_dial, (int)strlen(munged_dial)));
2535
2536         if (munged_dial) {
2537                 blob = base64_decode_data_blob(munged_dial);
2538         } else {
2539                 blob = data_blob_string_const_null("");
2540         }
2541
2542         status = init_samr_parameters_string(mem_ctx, &blob, &parameters);
2543         data_blob_free(&blob);
2544         if (!NT_STATUS_IS_OK(status)) {
2545                 return status;
2546         }
2547
2548         r->parameters = *parameters;
2549
2550         return NT_STATUS_OK;
2551 }
2552
2553
2554 /*************************************************************************
2555  get_user_info_21
2556  *************************************************************************/
2557
2558 static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
2559                                  struct samr_UserInfo21 *r,
2560                                  struct samu *pw,
2561                                  DOM_SID *domain_sid)
2562 {
2563         NTSTATUS status;
2564         const DOM_SID *sid_user, *sid_group;
2565         uint32_t rid, primary_gid;
2566         NTTIME force_password_change;
2567         time_t must_change_time;
2568         struct lsa_BinaryString *parameters = NULL;
2569         const char *munged_dial = NULL;
2570         DATA_BLOB blob;
2571
2572         ZERO_STRUCTP(r);
2573
2574         sid_user = pdb_get_user_sid(pw);
2575
2576         if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2577                 DEBUG(0, ("get_user_info_21: User %s has SID %s, \nwhich conflicts with "
2578                           "the domain sid %s.  Failing operation.\n",
2579                           pdb_get_username(pw), sid_string_dbg(sid_user),
2580                           sid_string_dbg(domain_sid)));
2581                 return NT_STATUS_UNSUCCESSFUL;
2582         }
2583
2584         become_root();
2585         sid_group = pdb_get_group_sid(pw);
2586         unbecome_root();
2587
2588         if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2589                 DEBUG(0, ("get_user_info_21: User %s has Primary Group SID %s, \n"
2590                           "which conflicts with the domain sid %s.  Failing operation.\n",
2591                           pdb_get_username(pw), sid_string_dbg(sid_group),
2592                           sid_string_dbg(domain_sid)));
2593                 return NT_STATUS_UNSUCCESSFUL;
2594         }
2595
2596         unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
2597         unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
2598         unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
2599         unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
2600         unix_to_nt_time(&r->allow_password_change, pdb_get_pass_can_change_time(pw));
2601
2602         must_change_time = pdb_get_pass_must_change_time(pw);
2603         if (must_change_time == get_time_t_max()) {
2604                 unix_to_nt_time_abs(&force_password_change, must_change_time);
2605         } else {
2606                 unix_to_nt_time(&force_password_change, must_change_time);
2607         }
2608
2609         munged_dial = pdb_get_munged_dial(pw);
2610         if (munged_dial) {
2611                 blob = base64_decode_data_blob(munged_dial);
2612         } else {
2613                 blob = data_blob_string_const_null("");
2614         }
2615
2616         status = init_samr_parameters_string(mem_ctx, &blob, &parameters);
2617         data_blob_free(&blob);
2618         if (!NT_STATUS_IS_OK(status)) {
2619                 return status;
2620         }
2621
2622         r->force_password_change        = force_password_change;
2623
2624         r->account_name.string          = talloc_strdup(mem_ctx, pdb_get_username(pw));
2625         r->full_name.string             = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2626         r->home_directory.string        = talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2627         r->home_drive.string            = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2628         r->logon_script.string          = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2629         r->profile_path.string          = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2630         r->description.string           = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2631         r->workstations.string          = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2632         r->comment.string               = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2633
2634         r->logon_hours                  = get_logon_hours_from_pdb(mem_ctx, pw);
2635         r->parameters                   = *parameters;
2636         r->rid                          = rid;
2637         r->primary_gid                  = primary_gid;
2638         r->acct_flags                   = pdb_get_acct_ctrl(pw);
2639         r->bad_password_count           = pdb_get_bad_password_count(pw);
2640         r->logon_count                  = pdb_get_logon_count(pw);
2641         r->fields_present               = pdb_build_fields_present(pw);
2642         r->password_expired             = (pdb_get_pass_must_change_time(pw) == 0) ?
2643                                                 PASS_MUST_CHANGE_AT_NEXT_LOGON : 0;
2644         r->country_code                 = 0;
2645         r->code_page                    = 0;
2646         r->lm_password_set              = 0;
2647         r->nt_password_set              = 0;
2648
2649 #if 0
2650
2651         /*
2652           Look at a user on a real NT4 PDC with usrmgr, press
2653           'ok'. Then you will see that fields_present is set to
2654           0x08f827fa. Look at the user immediately after that again,
2655           and you will see that 0x00fffff is returned. This solves
2656           the problem that you get access denied after having looked
2657           at the user.
2658           -- Volker
2659         */
2660
2661 #endif
2662
2663
2664         return NT_STATUS_OK;
2665 }
2666
2667 /*******************************************************************
2668  _samr_QueryUserInfo
2669  ********************************************************************/
2670
2671 NTSTATUS _samr_QueryUserInfo(pipes_struct *p,
2672                              struct samr_QueryUserInfo *r)
2673 {
2674         NTSTATUS status;
2675         union samr_UserInfo *user_info = NULL;
2676         struct samr_user_info *uinfo;
2677         DOM_SID domain_sid;
2678         uint32 rid;
2679         bool ret = false;
2680         struct samu *pwd = NULL;
2681
2682         uinfo = policy_handle_find(p, r->in.user_handle,
2683                                    SAMR_USER_ACCESS_GET_ATTRIBUTES, NULL,
2684                                    struct samr_user_info, &status);
2685         if (!NT_STATUS_IS_OK(status)) {
2686                 return status;
2687         }
2688
2689         domain_sid = uinfo->sid;
2690
2691         sid_split_rid(&domain_sid, &rid);
2692
2693         if (!sid_check_is_in_our_domain(&uinfo->sid))
2694                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2695
2696         DEBUG(5,("_samr_QueryUserInfo: sid:%s\n",
2697                  sid_string_dbg(&uinfo->sid)));
2698
2699         user_info = TALLOC_ZERO_P(p->mem_ctx, union samr_UserInfo);
2700         if (!user_info) {
2701                 return NT_STATUS_NO_MEMORY;
2702         }
2703
2704         DEBUG(5,("_samr_QueryUserInfo: user info level: %d\n", r->in.level));
2705
2706         if (!(pwd = samu_new(p->mem_ctx))) {
2707                 return NT_STATUS_NO_MEMORY;
2708         }
2709
2710         become_root();
2711         ret = pdb_getsampwsid(pwd, &uinfo->sid);
2712         unbecome_root();
2713
2714         if (ret == false) {
2715                 DEBUG(4,("User %s not found\n", sid_string_dbg(&uinfo->sid)));
2716                 TALLOC_FREE(pwd);
2717                 return NT_STATUS_NO_SUCH_USER;
2718         }
2719
2720         DEBUG(3,("User:[%s]\n", pdb_get_username(pwd)));
2721
2722         samr_clear_sam_passwd(pwd);
2723
2724         switch (r->in.level) {
2725         case 1:
2726                 status = get_user_info_1(p->mem_ctx, &user_info->info1, pwd, &domain_sid);
2727                 break;
2728         case 2:
2729                 status = get_user_info_2(p->mem_ctx, &user_info->info2, pwd);
2730                 break;
2731         case 3:
2732                 status = get_user_info_3(p->mem_ctx, &user_info->info3, pwd, &domain_sid);
2733                 break;
2734         case 4:
2735                 status = get_user_info_4(p->mem_ctx, &user_info->info4, pwd);
2736                 break;
2737         case 5:
2738                 status = get_user_info_5(p->mem_ctx, &user_info->info5, pwd, &domain_sid);
2739                 break;
2740         case 6:
2741                 status = get_user_info_6(p->mem_ctx, &user_info->info6, pwd);
2742                 break;
2743         case 7:
2744                 status = get_user_info_7(p->mem_ctx, &user_info->info7, pwd);
2745                 break;
2746         case 8:
2747                 status = get_user_info_8(p->mem_ctx, &user_info->info8, pwd);
2748                 break;
2749         case 9:
2750                 status = get_user_info_9(p->mem_ctx, &user_info->info9, pwd);
2751                 break;
2752         case 10:
2753                 status = get_user_info_10(p->mem_ctx, &user_info->info10, pwd);
2754                 break;
2755         case 11:
2756                 status = get_user_info_11(p->mem_ctx, &user_info->info11, pwd);
2757                 break;
2758         case 12:
2759                 status = get_user_info_12(p->mem_ctx, &user_info->info12, pwd);
2760                 break;
2761         case 13:
2762                 status = get_user_info_13(p->mem_ctx, &user_info->info13, pwd);
2763                 break;
2764         case 14:
2765                 status = get_user_info_14(p->mem_ctx, &user_info->info14, pwd);
2766                 break;
2767         case 16:
2768                 status = get_user_info_16(p->mem_ctx, &user_info->info16, pwd);
2769                 break;
2770         case 17:
2771                 status = get_user_info_17(p->mem_ctx, &user_info->info17, pwd);
2772                 break;
2773         case 18:
2774                 /* level 18 is special */
2775                 status = get_user_info_18(p, p->mem_ctx, &user_info->info18,
2776                                           &uinfo->sid);
2777                 break;
2778         case 20:
2779                 status = get_user_info_20(p->mem_ctx, &user_info->info20, pwd);
2780                 break;
2781         case 21:
2782                 status = get_user_info_21(p->mem_ctx, &user_info->info21, pwd, &domain_sid);
2783                 break;
2784         default:
2785                 status = NT_STATUS_INVALID_INFO_CLASS;
2786                 break;
2787         }
2788
2789         TALLOC_FREE(pwd);
2790
2791         *r->out.info = user_info;
2792
2793         DEBUG(5,("_samr_QueryUserInfo: %d\n", __LINE__));
2794
2795         return status;
2796 }
2797
2798 /****************************************************************
2799 ****************************************************************/
2800
2801 NTSTATUS _samr_QueryUserInfo2(pipes_struct *p,
2802                               struct samr_QueryUserInfo2 *r)
2803 {
2804         struct samr_QueryUserInfo u;
2805
2806         u.in.user_handle        = r->in.user_handle;
2807         u.in.level              = r->in.level;
2808         u.out.info              = r->out.info;
2809
2810         return _samr_QueryUserInfo(p, &u);
2811 }
2812
2813 /*******************************************************************
2814  _samr_GetGroupsForUser
2815  ********************************************************************/
2816
2817 NTSTATUS _samr_GetGroupsForUser(pipes_struct *p,
2818                                 struct samr_GetGroupsForUser *r)
2819 {
2820         struct samr_user_info *uinfo;
2821         struct samu *sam_pass=NULL;
2822         DOM_SID *sids;
2823         struct samr_RidWithAttribute dom_gid;
2824         struct samr_RidWithAttribute *gids = NULL;
2825         uint32 primary_group_rid;
2826         size_t num_groups = 0;
2827         gid_t *unix_gids;
2828         size_t i, num_gids;
2829         bool ret;
2830         NTSTATUS result;
2831         bool success = False;
2832
2833         struct samr_RidWithAttributeArray *rids = NULL;
2834
2835         /*
2836          * from the SID in the request:
2837          * we should send back the list of DOMAIN GROUPS
2838          * the user is a member of
2839          *
2840          * and only the DOMAIN GROUPS
2841          * no ALIASES !!! neither aliases of the domain
2842          * nor aliases of the builtin SID
2843          *
2844          * JFM, 12/2/2001
2845          */
2846
2847         DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
2848
2849         uinfo = policy_handle_find(p, r->in.user_handle,
2850                                    SAMR_USER_ACCESS_GET_GROUPS, NULL,
2851                                    struct samr_user_info, &result);
2852         if (!NT_STATUS_IS_OK(result)) {
2853                 return result;
2854         }
2855
2856         rids = TALLOC_ZERO_P(p->mem_ctx, struct samr_RidWithAttributeArray);
2857         if (!rids) {
2858                 return NT_STATUS_NO_MEMORY;
2859         }
2860
2861         if (!sid_check_is_in_our_domain(&uinfo->sid))
2862                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2863
2864         if ( !(sam_pass = samu_new( p->mem_ctx )) ) {
2865                 return NT_STATUS_NO_MEMORY;
2866         }
2867
2868         become_root();
2869         ret = pdb_getsampwsid(sam_pass, &uinfo->sid);
2870         unbecome_root();
2871
2872         if (!ret) {
2873                 DEBUG(10, ("pdb_getsampwsid failed for %s\n",
2874                            sid_string_dbg(&uinfo->sid)));
2875                 return NT_STATUS_NO_SUCH_USER;
2876         }
2877
2878         sids = NULL;
2879
2880         /* make both calls inside the root block */
2881         become_root();
2882         result = pdb_enum_group_memberships(p->mem_ctx, sam_pass,
2883                                             &sids, &unix_gids, &num_groups);
2884         if ( NT_STATUS_IS_OK(result) ) {
2885                 success = sid_peek_check_rid(get_global_sam_sid(),
2886                                              pdb_get_group_sid(sam_pass),
2887                                              &primary_group_rid);
2888         }
2889         unbecome_root();
2890
2891         if (!NT_STATUS_IS_OK(result)) {
2892                 DEBUG(10, ("pdb_enum_group_memberships failed for %s\n",
2893                            sid_string_dbg(&uinfo->sid)));
2894                 return result;
2895         }
2896
2897         if ( !success ) {
2898                 DEBUG(5, ("Group sid %s for user %s not in our domain\n",
2899                           sid_string_dbg(pdb_get_group_sid(sam_pass)),
2900                           pdb_get_username(sam_pass)));
2901                 TALLOC_FREE(sam_pass);
2902                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2903         }
2904
2905         gids = NULL;
2906         num_gids = 0;
2907
2908         dom_gid.attributes = (SE_GROUP_MANDATORY|SE_GROUP_ENABLED_BY_DEFAULT|
2909                               SE_GROUP_ENABLED);
2910         dom_gid.rid = primary_group_rid;
2911         ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
2912
2913         for (i=0; i<num_groups; i++) {
2914
2915                 if (!sid_peek_check_rid(get_global_sam_sid(),
2916                                         &(sids[i]), &dom_gid.rid)) {
2917                         DEBUG(10, ("Found sid %s not in our domain\n",
2918                                    sid_string_dbg(&sids[i])));
2919                         continue;
2920                 }
2921
2922                 if (dom_gid.rid == primary_group_rid) {
2923                         /* We added the primary group directly from the
2924                          * sam_account. The other SIDs are unique from
2925                          * enum_group_memberships */
2926                         continue;
2927                 }
2928
2929                 ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
2930         }
2931
2932         rids->count = num_gids;
2933         rids->rids = gids;
2934
2935         *r->out.rids = rids;
2936
2937         DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
2938
2939         return result;
2940 }
2941
2942 /*******************************************************************
2943  _samr_QueryDomainInfo
2944  ********************************************************************/
2945
2946 NTSTATUS _samr_QueryDomainInfo(pipes_struct *p,
2947                                struct samr_QueryDomainInfo *r)
2948 {
2949         NTSTATUS status = NT_STATUS_OK;
2950         struct samr_domain_info *dinfo;
2951         union samr_DomainInfo *dom_info;
2952         time_t u_expire, u_min_age;
2953
2954         time_t u_lock_duration, u_reset_time;
2955         uint32_t u_logout;
2956
2957         uint32 account_policy_temp;
2958
2959         time_t seq_num;
2960         uint32 server_role;
2961
2962         DEBUG(5,("_samr_QueryDomainInfo: %d\n", __LINE__));
2963
2964         dinfo = policy_handle_find(p, r->in.domain_handle,
2965                                    SAMR_ACCESS_LOOKUP_DOMAIN, NULL,
2966                                    struct samr_domain_info, &status);
2967         if (!NT_STATUS_IS_OK(status)) {
2968                 return status;
2969         }
2970
2971         dom_info = TALLOC_ZERO_P(p->mem_ctx, union samr_DomainInfo);
2972         if (!dom_info) {
2973                 return NT_STATUS_NO_MEMORY;
2974         }
2975
2976         switch (r->in.level) {
2977                 case 0x01:
2978
2979                         become_root();
2980
2981                         /* AS ROOT !!! */
2982
2983                         pdb_get_account_policy(AP_MIN_PASSWORD_LEN,
2984                                                &account_policy_temp);
2985                         dom_info->info1.min_password_length = account_policy_temp;
2986
2987                         pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
2988                         dom_info->info1.password_history_length = account_policy_temp;
2989
2990                         pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS,
2991                                 &dom_info->info1.password_properties);
2992
2993                         pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
2994                         u_expire = account_policy_temp;
2995
2996                         pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
2997                         u_min_age = account_policy_temp;
2998
2999                         /* !AS ROOT */
3000
3001                         unbecome_root();
3002
3003                         unix_to_nt_time_abs((NTTIME *)&dom_info->info1.max_password_age, u_expire);
3004                         unix_to_nt_time_abs((NTTIME *)&dom_info->info1.min_password_age, u_min_age);
3005
3006                         if (lp_check_password_script() && *lp_check_password_script()) {
3007                                 dom_info->info1.password_properties |= DOMAIN_PASSWORD_COMPLEX;
3008                         }
3009
3010                         break;
3011                 case 0x02:
3012
3013                         become_root();
3014
3015                         /* AS ROOT !!! */
3016
3017                         dom_info->general.num_users     = count_sam_users(
3018                                 dinfo->disp_info, ACB_NORMAL);
3019                         dom_info->general.num_groups    = count_sam_groups(
3020                                 dinfo->disp_info);
3021                         dom_info->general.num_aliases   = count_sam_aliases(
3022                                 dinfo->disp_info);
3023
3024                         pdb_get_account_policy(AP_TIME_TO_LOGOUT, &u_logout);
3025
3026                         unix_to_nt_time_abs(&dom_info->general.force_logoff_time, u_logout);
3027
3028                         if (!pdb_get_seq_num(&seq_num))
3029                                 seq_num = time(NULL);
3030
3031                         /* !AS ROOT */
3032
3033                         unbecome_root();
3034
3035                         server_role = ROLE_DOMAIN_PDC;
3036                         if (lp_server_role() == ROLE_DOMAIN_BDC)
3037                                 server_role = ROLE_DOMAIN_BDC;
3038
3039                         dom_info->general.oem_information.string        = lp_serverstring();
3040                         dom_info->general.domain_name.string            = lp_workgroup();
3041                         dom_info->general.primary.string                = global_myname();
3042                         dom_info->general.sequence_num                  = seq_num;
3043                         dom_info->general.domain_server_state           = DOMAIN_SERVER_ENABLED;
3044                         dom_info->general.role                          = server_role;
3045                         dom_info->general.unknown3                      = 1;
3046
3047                         break;
3048                 case 0x03:
3049
3050                         become_root();
3051
3052                         /* AS ROOT !!! */
3053
3054                         {
3055                                 uint32 ul;
3056                                 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &ul);
3057                                 u_logout = (time_t)ul;
3058                         }
3059
3060                         /* !AS ROOT */
3061
3062                         unbecome_root();
3063
3064                         unix_to_nt_time_abs(&dom_info->info3.force_logoff_time, u_logout);
3065
3066                         break;
3067                 case 0x04:
3068                         dom_info->oem.oem_information.string = lp_serverstring();
3069                         break;
3070                 case 0x05:
3071                         dom_info->info5.domain_name.string = get_global_sam_name();
3072                         break;
3073                 case 0x06:
3074                         /* NT returns its own name when a PDC. win2k and later
3075                          * only the name of the PDC if itself is a BDC (samba4
3076                          * idl) */
3077                         dom_info->info6.primary.string = global_myname();
3078                         break;
3079                 case 0x07:
3080                         server_role = ROLE_DOMAIN_PDC;
3081                         if (lp_server_role() == ROLE_DOMAIN_BDC)
3082                                 server_role = ROLE_DOMAIN_BDC;
3083
3084                         dom_info->info7.role = server_role;
3085                         break;
3086                 case 0x08:
3087
3088                         become_root();
3089
3090                         /* AS ROOT !!! */
3091
3092                         if (!pdb_get_seq_num(&seq_num)) {
3093                                 seq_num = time(NULL);
3094                         }
3095
3096                         /* !AS ROOT */
3097
3098                         unbecome_root();
3099
3100                         dom_info->info8.sequence_num = seq_num;
3101                         dom_info->info8.domain_create_time = 0;
3102
3103                         break;
3104                 case 0x0c:
3105
3106                         become_root();
3107
3108                         /* AS ROOT !!! */
3109
3110                         pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION, &account_policy_temp);
3111                         u_lock_duration = account_policy_temp;
3112                         if (u_lock_duration != -1) {
3113                                 u_lock_duration *= 60;
3114                         }
3115
3116                         pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
3117                         u_reset_time = account_policy_temp * 60;
3118
3119                         pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT,
3120                                                &account_policy_temp);
3121                         dom_info->info12.lockout_threshold = account_policy_temp;
3122
3123                         /* !AS ROOT */
3124
3125                         unbecome_root();
3126
3127                         unix_to_nt_time_abs(&dom_info->info12.lockout_duration,
3128                                             u_lock_duration);
3129                         unix_to_nt_time_abs(&dom_info->info12.lockout_window,
3130                                             u_reset_time);
3131
3132                         break;
3133                 default:
3134                         return NT_STATUS_INVALID_INFO_CLASS;
3135         }
3136
3137         *r->out.info = dom_info;
3138
3139         DEBUG(5,("_samr_QueryDomainInfo: %d\n", __LINE__));
3140
3141         return status;
3142 }
3143
3144 /* W2k3 seems to use the same check for all 3 objects that can be created via
3145  * SAMR, if you try to create for example "Dialup" as an alias it says
3146  * "NT_STATUS_USER_EXISTS". This is racy, but we can't really lock the user
3147  * database. */
3148
3149 static NTSTATUS can_create(TALLOC_CTX *mem_ctx, const char *new_name)
3150 {
3151         enum lsa_SidType type;
3152         bool result;
3153
3154         DEBUG(10, ("Checking whether [%s] can be created\n", new_name));
3155
3156         become_root();
3157         /* Lookup in our local databases (LOOKUP_NAME_REMOTE not set)
3158          * whether the name already exists */
3159         result = lookup_name(mem_ctx, new_name, LOOKUP_NAME_LOCAL,
3160                              NULL, NULL, NULL, &type);
3161         unbecome_root();
3162
3163         if (!result) {
3164                 DEBUG(10, ("%s does not exist, can create it\n", new_name));
3165                 return NT_STATUS_OK;
3166         }
3167
3168         DEBUG(5, ("trying to create %s, exists as %s\n",
3169                   new_name, sid_type_lookup(type)));
3170
3171         if (type == SID_NAME_DOM_GRP) {
3172                 return NT_STATUS_GROUP_EXISTS;
3173         }
3174         if (type == SID_NAME_ALIAS) {
3175                 return NT_STATUS_ALIAS_EXISTS;
3176         }
3177
3178         /* Yes, the default is NT_STATUS_USER_EXISTS */
3179         return NT_STATUS_USER_EXISTS;
3180 }
3181
3182 /*******************************************************************
3183  _samr_CreateUser2
3184  ********************************************************************/
3185
3186 NTSTATUS _samr_CreateUser2(pipes_struct *p,
3187                            struct samr_CreateUser2 *r)
3188 {
3189         const char *account = NULL;
3190         DOM_SID sid;
3191         uint32_t acb_info = r->in.acct_flags;
3192         struct samr_domain_info *dinfo;
3193         struct samr_user_info *uinfo;
3194         NTSTATUS nt_status;
3195         uint32 acc_granted;
3196         SEC_DESC *psd;
3197         size_t    sd_size;
3198         /* check this, when giving away 'add computer to domain' privs */
3199         uint32    des_access = GENERIC_RIGHTS_USER_ALL_ACCESS;
3200         bool can_add_account = False;
3201         SE_PRIV se_rights;
3202
3203         dinfo = policy_handle_find(p, r->in.domain_handle,
3204                                    SAMR_DOMAIN_ACCESS_CREATE_USER, NULL,
3205                                    struct samr_domain_info, &nt_status);
3206         if (!NT_STATUS_IS_OK(nt_status)) {
3207                 return nt_status;
3208         }
3209
3210         if (sid_check_is_builtin(&dinfo->sid)) {
3211                 DEBUG(5,("_samr_CreateUser2: Refusing user create in BUILTIN\n"));
3212                 return NT_STATUS_ACCESS_DENIED;
3213         }
3214
3215         if (!(acb_info == ACB_NORMAL || acb_info == ACB_DOMTRUST ||
3216               acb_info == ACB_WSTRUST || acb_info == ACB_SVRTRUST)) {
3217                 /* Match Win2k, and return NT_STATUS_INVALID_PARAMETER if
3218                    this parameter is not an account type */
3219