r11964: rename flag to password_properties in SAM_UNK_INFO_1 because that's what
[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-2005,
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  *
15  *  This program is free software; you can redistribute it and/or modify
16  *  it under the terms of the GNU General Public License as published by
17  *  the Free Software Foundation; either version 2 of the License, or
18  *  (at your option) any later version.
19  *
20  *  This program is distributed in the hope that it will be useful,
21  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *  GNU General Public License for more details.
24  *
25  *  You should have received a copy of the GNU General Public License
26  *  along with this program; if not, write to the Free Software
27  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28  */
29
30 /*
31  * This is the implementation of the SAMR code.
32  */
33
34 #include "includes.h"
35
36 #undef DBGC_CLASS
37 #define DBGC_CLASS DBGC_RPC_SRV
38
39 #define SAMR_USR_RIGHTS_WRITE_PW \
40                 ( READ_CONTROL_ACCESS           | \
41                   SA_RIGHT_USER_CHANGE_PASSWORD | \
42                   SA_RIGHT_USER_SET_LOC_COM )
43
44 #define DISP_INFO_CACHE_TIMEOUT 10
45
46 typedef struct disp_info {
47         struct disp_info *next, *prev;
48         TALLOC_CTX *mem_ctx;
49         DOM_SID sid; /* identify which domain this is. */
50         BOOL builtin_domain; /* Quick flag to check if this is the builtin domain. */
51         struct pdb_search *users; /* querydispinfo 1 and 4 */
52         struct pdb_search *machines; /* querydispinfo 2 */
53         struct pdb_search *groups; /* querydispinfo 3 and 5, enumgroups */
54         struct pdb_search *aliases; /* enumaliases */
55
56         uint16 enum_acb_mask;
57         struct pdb_search *enum_users; /* enumusers with a mask */
58
59
60         smb_event_id_t di_cache_timeout_event; /* cache idle timeout handler. */
61 } DISP_INFO;
62
63 /* We keep a static list of these by SID as modern clients close down
64    all resources between each request in a complete enumeration. */
65
66 static DISP_INFO *disp_info_list;
67
68 struct samr_info {
69         /* for use by the \PIPE\samr policy */
70         DOM_SID sid;
71         BOOL builtin_domain; /* Quick flag to check if this is the builtin domain. */
72         uint32 status; /* some sort of flag.  best to record it.  comes from opnum 0x39 */
73         uint32 acc_granted;
74         DISP_INFO *disp_info;
75         TALLOC_CTX *mem_ctx;
76 };
77
78 static struct generic_mapping sam_generic_mapping = {
79         GENERIC_RIGHTS_SAM_READ,
80         GENERIC_RIGHTS_SAM_WRITE,
81         GENERIC_RIGHTS_SAM_EXECUTE,
82         GENERIC_RIGHTS_SAM_ALL_ACCESS};
83 static struct generic_mapping dom_generic_mapping = {
84         GENERIC_RIGHTS_DOMAIN_READ,
85         GENERIC_RIGHTS_DOMAIN_WRITE,
86         GENERIC_RIGHTS_DOMAIN_EXECUTE,
87         GENERIC_RIGHTS_DOMAIN_ALL_ACCESS};
88 static struct generic_mapping usr_generic_mapping = {
89         GENERIC_RIGHTS_USER_READ,
90         GENERIC_RIGHTS_USER_WRITE,
91         GENERIC_RIGHTS_USER_EXECUTE,
92         GENERIC_RIGHTS_USER_ALL_ACCESS};
93 static struct generic_mapping grp_generic_mapping = {
94         GENERIC_RIGHTS_GROUP_READ,
95         GENERIC_RIGHTS_GROUP_WRITE,
96         GENERIC_RIGHTS_GROUP_EXECUTE,
97         GENERIC_RIGHTS_GROUP_ALL_ACCESS};
98 static struct generic_mapping ali_generic_mapping = {
99         GENERIC_RIGHTS_ALIAS_READ,
100         GENERIC_RIGHTS_ALIAS_WRITE,
101         GENERIC_RIGHTS_ALIAS_EXECUTE,
102         GENERIC_RIGHTS_ALIAS_ALL_ACCESS};
103
104 /*******************************************************************
105 *******************************************************************/
106
107 static NTSTATUS make_samr_object_sd( TALLOC_CTX *ctx, SEC_DESC **psd, size_t *sd_size,
108                                      struct generic_mapping *map,
109                                      DOM_SID *sid, uint32 sid_access )
110 {
111         DOM_SID domadmin_sid;
112         SEC_ACE ace[5];         /* at most 5 entries */
113         SEC_ACCESS mask;
114         size_t i = 0;
115
116         SEC_ACL *psa = NULL;
117
118         /* basic access for Everyone */
119
120         init_sec_access(&mask, map->generic_execute | map->generic_read );
121         init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
122
123         /* add Full Access 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
124
125         init_sec_access(&mask, map->generic_all);
126         
127         init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
128         init_sec_ace(&ace[i++], &global_sid_Builtin_Account_Operators, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
129
130         /* Add Full Access for Domain Admins if we are a DC */
131         
132         if ( IS_DC ) {
133                 sid_copy( &domadmin_sid, get_global_sam_sid() );
134                 sid_append_rid( &domadmin_sid, DOMAIN_GROUP_RID_ADMINS );
135                 init_sec_ace(&ace[i++], &domadmin_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
136         }
137
138         /* if we have a sid, give it some special access */
139
140         if ( sid ) {
141                 init_sec_access( &mask, sid_access );
142                 init_sec_ace(&ace[i++], sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
143 }
144
145         /* create the security descriptor */
146
147         if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) == NULL)
148                 return NT_STATUS_NO_MEMORY;
149
150         if ((*psd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL, psa, sd_size)) == NULL)
151                 return NT_STATUS_NO_MEMORY;
152
153         return NT_STATUS_OK;
154 }
155
156 /*******************************************************************
157  Checks if access to an object should be granted, and returns that
158  level of access for further checks.
159 ********************************************************************/
160
161 static NTSTATUS access_check_samr_object( SEC_DESC *psd, NT_USER_TOKEN *token, 
162                                           SE_PRIV *rights, uint32 rights_mask,
163                                           uint32 des_access, uint32 *acc_granted, 
164                                           const char *debug )
165 {
166         NTSTATUS status = NT_STATUS_ACCESS_DENIED;
167         uint32 saved_mask = 0;
168
169         /* check privileges; certain SAM access bits should be overridden 
170            by privileges (mostly having to do with creating/modifying/deleting 
171            users and groups) */
172         
173         if ( rights && user_has_any_privilege( token, rights ) ) {
174         
175                 saved_mask = (des_access & rights_mask);
176                 des_access &= ~saved_mask;
177                 
178                 DEBUG(4,("access_check_samr_object: user rights access mask [0x%x]\n",
179                         rights_mask));
180         }
181                 
182         
183         /* check the security descriptor first */
184         
185         if ( se_access_check(psd, token, des_access, acc_granted, &status) )
186                 goto done;
187         
188         /* give root a free pass */
189         
190         if ( geteuid() == sec_initial_uid() ) {
191         
192                 DEBUG(4,("%s: ACCESS should be DENIED  (requested: %#010x)\n", debug, des_access));
193                 DEBUGADD(4,("but overritten by euid == sec_initial_uid()\n"));
194                 
195                 *acc_granted = des_access;
196                 
197                 status = NT_STATUS_OK;
198                 goto done;
199         }
200         
201         
202 done:
203         /* add in any bits saved during the privilege check (only 
204            matters is status is ok) */
205         
206         *acc_granted |= rights_mask;
207
208         DEBUG(4,("%s: access %s (requested: 0x%08x, granted: 0x%08x)\n", 
209                 debug, NT_STATUS_IS_OK(status) ? "GRANTED" : "DENIED", 
210                 des_access, *acc_granted));
211         
212         return status;
213 }
214
215 /*******************************************************************
216  Checks if access to a function can be granted
217 ********************************************************************/
218
219 static NTSTATUS access_check_samr_function(uint32 acc_granted, uint32 acc_required, const char *debug)
220 {
221         DEBUG(5,("%s: access check ((granted: %#010x;  required: %#010x)\n",  
222                 debug, acc_granted, acc_required));
223
224         /* check the security descriptor first */
225         
226         if ( (acc_granted&acc_required) == acc_required )
227                 return NT_STATUS_OK;
228                 
229         /* give root a free pass */
230
231         if (geteuid() == sec_initial_uid()) {
232         
233                 DEBUG(4,("%s: ACCESS should be DENIED (granted: %#010x;  required: %#010x)\n",
234                         debug, acc_granted, acc_required));
235                 DEBUGADD(4,("but overwritten by euid == 0\n"));
236                 
237                 return NT_STATUS_OK;
238         }
239         
240         DEBUG(2,("%s: ACCESS DENIED (granted: %#010x;  required: %#010x)\n", 
241                 debug, acc_granted, acc_required));
242                 
243         return NT_STATUS_ACCESS_DENIED;
244 }
245
246 /*******************************************************************
247  Fetch or create a dispinfo struct.
248 ********************************************************************/
249
250 static DISP_INFO *get_samr_dispinfo_by_sid(DOM_SID *psid, const char *sid_str)
251 {
252         TALLOC_CTX *mem_ctx;
253         DISP_INFO *dpi;
254
255         for (dpi = disp_info_list; dpi; dpi = dpi->next) {
256                 if (sid_equal(psid, &dpi->sid)) {
257                         return dpi;
258                 }
259         }
260
261         /* This struct is never free'd - I'm using talloc so we
262            can get a list out of smbd using smbcontrol. There will
263            be one of these per SID we're authorative for. JRA. */
264
265         mem_ctx = talloc_init("DISP_INFO for domain sid %s", sid_str);
266
267         if ((dpi = TALLOC_ZERO_P(mem_ctx, DISP_INFO)) == NULL)
268                 return NULL;
269
270         dpi->mem_ctx = mem_ctx;
271
272         if (psid) {
273                 sid_copy( &dpi->sid, psid);
274                 dpi->builtin_domain = sid_check_is_builtin(psid);
275         } else {
276                 dpi->builtin_domain = False;
277         }
278
279         DLIST_ADD(disp_info_list, dpi);
280
281         return dpi;
282 }
283
284 /*******************************************************************
285  Create a samr_info struct.
286 ********************************************************************/
287
288 static struct samr_info *get_samr_info_by_sid(DOM_SID *psid)
289 {
290         struct samr_info *info;
291         fstring sid_str;
292         TALLOC_CTX *mem_ctx;
293         
294         if (psid) {
295                 sid_to_string(sid_str, psid);
296         } else {
297                 fstrcpy(sid_str,"(NULL)");
298         }
299
300         mem_ctx = talloc_init("samr_info for domain sid %s", sid_str);
301
302         if ((info = TALLOC_ZERO_P(mem_ctx, struct samr_info)) == NULL)
303                 return NULL;
304
305         DEBUG(10,("get_samr_info_by_sid: created new info for sid %s\n", sid_str));
306         if (psid) {
307                 sid_copy( &info->sid, psid);
308                 info->builtin_domain = sid_check_is_builtin(psid);
309         } else {
310                 DEBUG(10,("get_samr_info_by_sid: created new info for NULL sid.\n"));
311                 info->builtin_domain = False;
312         }
313         info->mem_ctx = mem_ctx;
314
315         info->disp_info = get_samr_dispinfo_by_sid(psid, sid_str);
316
317         if (!info->disp_info) {
318                 talloc_destroy(mem_ctx);
319                 return NULL;
320         }
321
322         return info;
323 }
324
325 /*******************************************************************
326  Function to free the per SID data.
327  ********************************************************************/
328
329 static void free_samr_cache(DISP_INFO *disp_info, const char *sid_str)
330 {
331         DEBUG(10,("free_samr_cache: deleting cache for SID %s\n", sid_str));
332
333         /* We need to become root here because the paged search might have to
334          * tell the LDAP server we're not interested in the rest anymore. */
335
336         become_root();
337
338         if (disp_info->users) {
339                 DEBUG(10,("free_samr_cache: deleting users cache\n"));
340                 pdb_search_destroy(disp_info->users);
341                 disp_info->users = NULL;
342         }
343         if (disp_info->machines) {
344                 DEBUG(10,("free_samr_cache: deleting machines cache\n"));
345                 pdb_search_destroy(disp_info->machines);
346                 disp_info->machines = NULL;
347         }
348         if (disp_info->groups) {
349                 DEBUG(10,("free_samr_cache: deleting groups cache\n"));
350                 pdb_search_destroy(disp_info->groups);
351                 disp_info->groups = NULL;
352         }
353         if (disp_info->aliases) {
354                 DEBUG(10,("free_samr_cache: deleting aliases cache\n"));
355                 pdb_search_destroy(disp_info->aliases);
356                 disp_info->aliases = NULL;
357         }
358         if (disp_info->enum_users) {
359                 DEBUG(10,("free_samr_cache: deleting enum_users cache\n"));
360                 pdb_search_destroy(disp_info->enum_users);
361                 disp_info->enum_users = NULL;
362         }
363         disp_info->enum_acb_mask = 0;
364
365         unbecome_root();
366 }
367
368 /*******************************************************************
369  Function to free the per handle data.
370  ********************************************************************/
371
372 static void free_samr_info(void *ptr)
373 {
374         struct samr_info *info=(struct samr_info *) ptr;
375
376         /* Only free the dispinfo cache if no one bothered to set up
377            a timeout. */
378
379         if (info->disp_info && info->disp_info->di_cache_timeout_event == (smb_event_id_t)0) {
380                 fstring sid_str;
381                 sid_to_string(sid_str, &info->disp_info->sid);
382                 free_samr_cache(info->disp_info, sid_str);
383         }
384
385         talloc_destroy(info->mem_ctx);
386 }
387
388 /*******************************************************************
389  Idle event handler. Throw away the disp info cache.
390  ********************************************************************/
391
392 static void disp_info_cache_idle_timeout_handler(void **private_data,
393                                         time_t *ev_interval,
394                                         time_t ev_now)
395 {
396         fstring sid_str;
397         DISP_INFO *disp_info = (DISP_INFO *)(*private_data);
398
399         sid_to_string(sid_str, &disp_info->sid);
400
401         free_samr_cache(disp_info, sid_str);
402
403         /* Remove the event. */
404         smb_unregister_idle_event(disp_info->di_cache_timeout_event);
405         disp_info->di_cache_timeout_event = (smb_event_id_t)0;
406
407         DEBUG(10,("disp_info_cache_idle_timeout_handler: caching timed out for SID %s at %u\n",
408                 sid_str, (unsigned int)ev_now));
409 }
410
411 /*******************************************************************
412  Setup cache removal idle event handler.
413  ********************************************************************/
414
415 static void set_disp_info_cache_timeout(DISP_INFO *disp_info, time_t secs_fromnow)
416 {
417         fstring sid_str;
418
419         sid_to_string(sid_str, &disp_info->sid);
420
421         /* Remove any pending timeout and update. */
422
423         if (disp_info->di_cache_timeout_event) {
424                 smb_unregister_idle_event(disp_info->di_cache_timeout_event);
425                 disp_info->di_cache_timeout_event = (smb_event_id_t)0;
426         }
427
428         DEBUG(10,("set_disp_info_cache_timeout: caching enumeration for SID %s for %u seconds\n",
429                 sid_str, (unsigned int)secs_fromnow ));
430
431         disp_info->di_cache_timeout_event =
432                 smb_register_idle_event(disp_info_cache_idle_timeout_handler,
433                                         disp_info,
434                                         secs_fromnow);
435 }
436
437 /*******************************************************************
438  Force flush any cache. We do this on any samr_set_xxx call.
439  We must also remove the timeout handler.
440  ********************************************************************/
441
442 static void force_flush_samr_cache(DISP_INFO *disp_info)
443 {
444         if (disp_info) {
445                 fstring sid_str;
446
447                 sid_to_string(sid_str, &disp_info->sid);
448                 if (disp_info->di_cache_timeout_event) {
449                         smb_unregister_idle_event(disp_info->di_cache_timeout_event);
450                         disp_info->di_cache_timeout_event = (smb_event_id_t)0;
451                         DEBUG(10,("force_flush_samr_cache: clearing idle event for SID %s\n",
452                                 sid_str));
453                 }
454                 free_samr_cache(disp_info, sid_str);
455         }
456 }
457
458 /*******************************************************************
459  Ensure password info is never given out. Paranioa... JRA.
460  ********************************************************************/
461
462 static void samr_clear_sam_passwd(SAM_ACCOUNT *sam_pass)
463 {
464         
465         if (!sam_pass)
466                 return;
467
468         /* These now zero out the old password */
469
470         pdb_set_lanman_passwd(sam_pass, NULL, PDB_DEFAULT);
471         pdb_set_nt_passwd(sam_pass, NULL, PDB_DEFAULT);
472 }
473
474 static uint32 count_sam_users(struct disp_info *info, uint16 acct_flags)
475 {
476         struct samr_displayentry *entry;
477
478         if (info->builtin_domain) {
479                 /* No users in builtin. */
480                 return 0;
481         }
482
483         if (info->users == NULL) {
484                 info->users = pdb_search_users(acct_flags);
485                 if (info->users == NULL) {
486                         return 0;
487                 }
488         }
489         /* Fetch the last possible entry, thus trigger an enumeration */
490         pdb_search_entries(info->users, 0xffffffff, 1, &entry);
491
492         /* Ensure we cache this enumeration. */
493         set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
494
495         return info->users->num_entries;
496 }
497
498 static uint32 count_sam_groups(struct disp_info *info)
499 {
500         struct samr_displayentry *entry;
501
502         if (info->builtin_domain) {
503                 /* No groups in builtin. */
504                 return 0;
505         }
506
507         if (info->groups == NULL) {
508                 info->groups = pdb_search_groups();
509                 if (info->groups == NULL) {
510                         return 0;
511                 }
512         }
513         /* Fetch the last possible entry, thus trigger an enumeration */
514         pdb_search_entries(info->groups, 0xffffffff, 1, &entry);
515
516         /* Ensure we cache this enumeration. */
517         set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
518
519         return info->groups->num_entries;
520 }
521
522 static uint32 count_sam_aliases(struct disp_info *info)
523 {
524         struct samr_displayentry *entry;
525
526         if (info->aliases == NULL) {
527                 info->aliases = pdb_search_aliases(&info->sid);
528                 if (info->aliases == NULL) {
529                         return 0;
530                 }
531         }
532         /* Fetch the last possible entry, thus trigger an enumeration */
533         pdb_search_entries(info->aliases, 0xffffffff, 1, &entry);
534
535         /* Ensure we cache this enumeration. */
536         set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
537
538         return info->aliases->num_entries;
539 }
540
541 /*******************************************************************
542  _samr_close_hnd
543  ********************************************************************/
544
545 NTSTATUS _samr_close_hnd(pipes_struct *p, SAMR_Q_CLOSE_HND *q_u, SAMR_R_CLOSE_HND *r_u)
546 {
547         r_u->status = NT_STATUS_OK;
548
549         /* close the policy handle */
550         if (!close_policy_hnd(p, &q_u->pol))
551                 return NT_STATUS_OBJECT_NAME_INVALID;
552
553         DEBUG(5,("samr_reply_close_hnd: %d\n", __LINE__));
554
555         return r_u->status;
556 }
557
558 /*******************************************************************
559  samr_reply_open_domain
560  ********************************************************************/
561
562 NTSTATUS _samr_open_domain(pipes_struct *p, SAMR_Q_OPEN_DOMAIN *q_u, SAMR_R_OPEN_DOMAIN *r_u)
563 {
564         struct    samr_info *info;
565         SEC_DESC *psd = NULL;
566         uint32    acc_granted;
567         uint32    des_access = q_u->flags;
568         NTSTATUS  status;
569         size_t    sd_size;
570         SE_PRIV se_rights;
571
572         r_u->status = NT_STATUS_OK;
573
574         /* find the connection policy handle. */
575         
576         if ( !find_policy_by_hnd(p, &q_u->pol, (void**)&info) )
577                 return NT_STATUS_INVALID_HANDLE;
578
579         status = access_check_samr_function( info->acc_granted, 
580                 SA_RIGHT_SAM_OPEN_DOMAIN, "_samr_open_domain" );
581                 
582         if ( !NT_STATUS_IS_OK(status) )
583                 return status;
584
585         /*check if access can be granted as requested by client. */
586         
587         make_samr_object_sd( p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0 );
588         se_map_generic( &des_access, &dom_generic_mapping );
589         
590         se_priv_copy( &se_rights, &se_machine_account );
591         se_priv_add( &se_rights, &se_add_users );
592
593         status = access_check_samr_object( psd, p->pipe_user.nt_user_token, 
594                 &se_rights, GENERIC_RIGHTS_DOMAIN_WRITE, des_access, 
595                 &acc_granted, "_samr_open_domain" );
596                 
597         if ( !NT_STATUS_IS_OK(status) )
598                 return status;
599
600         if (!sid_check_is_domain(&q_u->dom_sid.sid) &&
601             !sid_check_is_builtin(&q_u->dom_sid.sid)) {
602                 return NT_STATUS_NO_SUCH_DOMAIN;
603         }
604
605         /* associate the domain SID with the (unique) handle. */
606         if ((info = get_samr_info_by_sid(&q_u->dom_sid.sid))==NULL)
607                 return NT_STATUS_NO_MEMORY;
608         info->acc_granted = acc_granted;
609
610         /* get a (unique) handle.  open a policy on it. */
611         if (!create_policy_hnd(p, &r_u->domain_pol, free_samr_info, (void *)info))
612                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
613
614         DEBUG(5,("samr_open_domain: %d\n", __LINE__));
615
616         return r_u->status;
617 }
618
619 /*******************************************************************
620  _samr_get_usrdom_pwinfo
621  ********************************************************************/
622
623 NTSTATUS _samr_get_usrdom_pwinfo(pipes_struct *p, SAMR_Q_GET_USRDOM_PWINFO *q_u, SAMR_R_GET_USRDOM_PWINFO *r_u)
624 {
625         struct samr_info *info = NULL;
626
627         r_u->status = NT_STATUS_OK;
628
629         /* find the policy handle.  open a policy on it. */
630         if (!find_policy_by_hnd(p, &q_u->user_pol, (void **)&info))
631                 return NT_STATUS_INVALID_HANDLE;
632
633         if (!sid_check_is_in_our_domain(&info->sid))
634                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
635
636         init_samr_r_get_usrdom_pwinfo(r_u, NT_STATUS_OK);
637
638         DEBUG(5,("_samr_get_usrdom_pwinfo: %d\n", __LINE__));
639
640         /* 
641          * NT sometimes return NT_STATUS_ACCESS_DENIED
642          * I don't know yet why.
643          */
644
645         return r_u->status;
646 }
647
648 /*******************************************************************
649  _samr_set_sec_obj
650  ********************************************************************/
651
652 NTSTATUS _samr_set_sec_obj(pipes_struct *p, SAMR_Q_SET_SEC_OBJ *q_u, SAMR_R_SET_SEC_OBJ *r_u)
653 {
654         DEBUG(0,("_samr_set_sec_obj: Not yet implemented!\n"));
655         return NT_STATUS_NOT_IMPLEMENTED;
656 }
657
658 /*******************************************************************
659 ********************************************************************/
660
661 static BOOL get_lsa_policy_samr_sid( pipes_struct *p, POLICY_HND *pol, 
662                                         DOM_SID *sid, uint32 *acc_granted,
663                                         DISP_INFO **ppdisp_info)
664 {
665         struct samr_info *info = NULL;
666
667         /* find the policy handle.  open a policy on it. */
668         if (!find_policy_by_hnd(p, pol, (void **)&info))
669                 return False;
670
671         if (!info)
672                 return False;
673
674         *sid = info->sid;
675         *acc_granted = info->acc_granted;
676         if (ppdisp_info) {
677                 *ppdisp_info = info->disp_info;
678         }
679
680         return True;
681 }
682
683 /*******************************************************************
684  _samr_query_sec_obj
685  ********************************************************************/
686
687 NTSTATUS _samr_query_sec_obj(pipes_struct *p, SAMR_Q_QUERY_SEC_OBJ *q_u, SAMR_R_QUERY_SEC_OBJ *r_u)
688 {
689         DOM_SID pol_sid;
690         fstring str_sid;
691         SEC_DESC * psd = NULL;
692         uint32 acc_granted;
693         size_t sd_size;
694
695         r_u->status = NT_STATUS_OK;
696
697         /* Get the SID. */
698         if (!get_lsa_policy_samr_sid(p, &q_u->user_pol, &pol_sid, &acc_granted, NULL))
699                 return NT_STATUS_INVALID_HANDLE;
700
701         DEBUG(10,("_samr_query_sec_obj: querying security on SID: %s\n", sid_to_string(str_sid, &pol_sid)));
702
703         /* Check what typ of SID is beeing queried (e.g Domain SID, User SID, Group SID) */
704
705         /* To query the security of the SAM it self an invalid SID with S-0-0 is passed to this function */
706         if (pol_sid.sid_rev_num == 0) {
707                 DEBUG(5,("_samr_query_sec_obj: querying security on SAM\n"));
708                 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
709         } else if (sid_equal(&pol_sid,get_global_sam_sid())) { 
710                 /* check if it is our domain SID */
711                 DEBUG(5,("_samr_query_sec_obj: querying security on Domain with SID: %s\n", sid_to_string(str_sid, &pol_sid)));
712                 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
713         } else if (sid_equal(&pol_sid,&global_sid_Builtin)) {
714                 /* check if it is the Builtin  Domain */
715                 /* TODO: Builtin probably needs a different SD with restricted write access*/
716                 DEBUG(5,("_samr_query_sec_obj: querying security on Builtin Domain with SID: %s\n", sid_to_string(str_sid, &pol_sid)));
717                 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
718         } else if (sid_check_is_in_our_domain(&pol_sid) ||
719                  sid_check_is_in_builtin(&pol_sid)) {
720                 /* TODO: different SDs have to be generated for aliases groups and users.
721                          Currently all three get a default user SD  */
722                 DEBUG(10,("_samr_query_sec_obj: querying security on Object with SID: %s\n", sid_to_string(str_sid, &pol_sid)));
723                 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &pol_sid, SAMR_USR_RIGHTS_WRITE_PW);
724         } else {
725                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
726         }
727
728         if ((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
729                 return NT_STATUS_NO_MEMORY;
730
731         if (NT_STATUS_IS_OK(r_u->status))
732                 r_u->ptr = 1;
733
734         return r_u->status;
735 }
736
737 /*******************************************************************
738 makes a SAM_ENTRY / UNISTR2* structure from a user list.
739 ********************************************************************/
740
741 static NTSTATUS make_user_sam_entry_list(TALLOC_CTX *ctx, SAM_ENTRY **sam_pp,
742                                          UNISTR2 **uni_name_pp,
743                                          uint32 num_entries, uint32 start_idx,
744                                          struct samr_displayentry *entries)
745 {
746         uint32 i;
747         SAM_ENTRY *sam;
748         UNISTR2 *uni_name;
749         
750         *sam_pp = NULL;
751         *uni_name_pp = NULL;
752
753         if (num_entries == 0)
754                 return NT_STATUS_OK;
755
756         sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_entries);
757
758         uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_entries);
759
760         if (sam == NULL || uni_name == NULL) {
761                 DEBUG(0, ("make_user_sam_entry_list: talloc_zero failed!\n"));
762                 return NT_STATUS_NO_MEMORY;
763         }
764
765         for (i = 0; i < num_entries; i++) {
766                 UNISTR2 uni_temp_name;
767                 /*
768                  * usrmgr expects a non-NULL terminated string with
769                  * trust relationships
770                  */
771                 if (entries[i].acct_flags & ACB_DOMTRUST) {
772                         init_unistr2(&uni_temp_name, entries[i].account_name,
773                                      UNI_FLAGS_NONE);
774                 } else {
775                         init_unistr2(&uni_temp_name, entries[i].account_name,
776                                      UNI_STR_TERMINATE);
777                 }
778
779                 init_sam_entry(&sam[i], &uni_temp_name, entries[i].rid);
780                 copy_unistr2(&uni_name[i], &uni_temp_name);
781         }
782
783         *sam_pp = sam;
784         *uni_name_pp = uni_name;
785         return NT_STATUS_OK;
786 }
787
788 /*******************************************************************
789  samr_reply_enum_dom_users
790  ********************************************************************/
791
792 NTSTATUS _samr_enum_dom_users(pipes_struct *p, SAMR_Q_ENUM_DOM_USERS *q_u, 
793                               SAMR_R_ENUM_DOM_USERS *r_u)
794 {
795         struct samr_info *info = NULL;
796         int num_account;
797         uint32 enum_context=q_u->start_idx;
798         enum remote_arch_types ra_type = get_remote_arch();
799         int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
800         uint32 max_entries = max_sam_entries;
801         struct samr_displayentry *entries = NULL;
802         
803         r_u->status = NT_STATUS_OK;
804
805         /* find the policy handle.  open a policy on it. */
806         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
807                 return NT_STATUS_INVALID_HANDLE;
808
809         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted, 
810                                         SA_RIGHT_DOMAIN_ENUM_ACCOUNTS, 
811                                         "_samr_enum_dom_users"))) {
812                 return r_u->status;
813         }
814         
815         DEBUG(5,("_samr_enum_dom_users: %d\n", __LINE__));
816
817         if (info->builtin_domain) {
818                 /* No users in builtin. */
819                 init_samr_r_enum_dom_users(r_u, q_u->start_idx, 0);
820                 DEBUG(5,("_samr_enum_dom_users: No users in BUILTIN\n"));
821                 return r_u->status;
822         }
823
824         become_root();
825
826         /* AS ROOT !!!! */
827
828         if ((info->disp_info->enum_users != NULL) &&
829             (info->disp_info->enum_acb_mask != q_u->acb_mask)) {
830                 pdb_search_destroy(info->disp_info->enum_users);
831                 info->disp_info->enum_users = NULL;
832         }
833
834         if (info->disp_info->enum_users == NULL) {
835                 info->disp_info->enum_users = pdb_search_users(q_u->acb_mask);
836                 info->disp_info->enum_acb_mask = q_u->acb_mask;
837         }
838
839         if (info->disp_info->enum_users == NULL) {
840                 /* END AS ROOT !!!! */
841                 unbecome_root();
842                 return NT_STATUS_ACCESS_DENIED;
843         }
844
845         num_account = pdb_search_entries(info->disp_info->enum_users,
846                                          enum_context, max_entries,
847                                          &entries);
848
849         /* END AS ROOT !!!! */
850
851         unbecome_root();
852
853         if (num_account == 0) {
854                 DEBUG(5, ("_samr_enum_dom_users: enumeration handle over "
855                           "total entries\n"));
856                 return NT_STATUS_OK;
857         }
858
859         r_u->status = make_user_sam_entry_list(p->mem_ctx, &r_u->sam,
860                                                &r_u->uni_acct_name, 
861                                                num_account, enum_context,
862                                                entries);
863
864         if (!NT_STATUS_IS_OK(r_u->status))
865                 return r_u->status;
866
867         if (max_entries <= num_account) {
868                 r_u->status = STATUS_MORE_ENTRIES;
869         } else {
870                 r_u->status = NT_STATUS_OK;
871         }
872
873         /* Ensure we cache this enumeration. */
874         set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
875
876         DEBUG(5, ("_samr_enum_dom_users: %d\n", __LINE__));
877
878         init_samr_r_enum_dom_users(r_u, q_u->start_idx + num_account,
879                                    num_account);
880
881         DEBUG(5,("_samr_enum_dom_users: %d\n", __LINE__));
882
883         return r_u->status;
884 }
885
886 /*******************************************************************
887 makes a SAM_ENTRY / UNISTR2* structure from a group list.
888 ********************************************************************/
889
890 static void make_group_sam_entry_list(TALLOC_CTX *ctx, SAM_ENTRY **sam_pp,
891                                       UNISTR2 **uni_name_pp,
892                                       uint32 num_sam_entries,
893                                       struct samr_displayentry *entries)
894 {
895         uint32 i;
896         SAM_ENTRY *sam;
897         UNISTR2 *uni_name;
898
899         *sam_pp = NULL;
900         *uni_name_pp = NULL;
901
902         if (num_sam_entries == 0)
903                 return;
904
905         sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_sam_entries);
906         uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_sam_entries);
907
908         if (sam == NULL || uni_name == NULL) {
909                 DEBUG(0, ("NULL pointers in SAMR_R_QUERY_DISPINFO\n"));
910                 return;
911         }
912
913         for (i = 0; i < num_sam_entries; i++) {
914                 /*
915                  * JRA. I think this should include the null. TNG does not.
916                  */
917                 init_unistr2(&uni_name[i], entries[i].account_name,
918                              UNI_STR_TERMINATE);
919                 init_sam_entry(&sam[i], &uni_name[i], entries[i].rid);
920         }
921
922         *sam_pp = sam;
923         *uni_name_pp = uni_name;
924 }
925
926 /*******************************************************************
927  samr_reply_enum_dom_groups
928  ********************************************************************/
929
930 NTSTATUS _samr_enum_dom_groups(pipes_struct *p, SAMR_Q_ENUM_DOM_GROUPS *q_u, SAMR_R_ENUM_DOM_GROUPS *r_u)
931 {
932         struct samr_info *info = NULL;
933         struct samr_displayentry *groups;
934         uint32 num_groups;
935
936         r_u->status = NT_STATUS_OK;
937
938         /* find the policy handle.  open a policy on it. */
939         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
940                 return NT_STATUS_INVALID_HANDLE;
941
942         r_u->status = access_check_samr_function(info->acc_granted,
943                                                  SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
944                                                  "_samr_enum_dom_groups");
945         if (!NT_STATUS_IS_OK(r_u->status))
946                 return r_u->status;
947
948         DEBUG(5,("samr_reply_enum_dom_groups: %d\n", __LINE__));
949
950         if (info->builtin_domain) {
951                 /* No groups in builtin. */
952                 init_samr_r_enum_dom_groups(r_u, q_u->start_idx, 0);
953                 DEBUG(5,("_samr_enum_dom_users: No groups in BUILTIN\n"));
954                 return r_u->status;
955         }
956
957         /* the domain group array is being allocated in the function below */
958
959         become_root();
960
961         if (info->disp_info->groups == NULL) {
962                 info->disp_info->groups = pdb_search_groups();
963
964                 if (info->disp_info->groups == NULL) {
965                         unbecome_root();
966                         return NT_STATUS_ACCESS_DENIED;
967                 }
968         }
969
970         num_groups = pdb_search_entries(info->disp_info->groups, q_u->start_idx,
971                                         MAX_SAM_ENTRIES, &groups);
972         unbecome_root();
973         
974         /* Ensure we cache this enumeration. */
975         set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
976
977         make_group_sam_entry_list(p->mem_ctx, &r_u->sam, &r_u->uni_grp_name,
978                                   num_groups, groups);
979
980         init_samr_r_enum_dom_groups(r_u, q_u->start_idx, num_groups);
981
982         DEBUG(5,("samr_enum_dom_groups: %d\n", __LINE__));
983
984         return r_u->status;
985 }
986
987 /*******************************************************************
988  samr_reply_enum_dom_aliases
989  ********************************************************************/
990
991 NTSTATUS _samr_enum_dom_aliases(pipes_struct *p, SAMR_Q_ENUM_DOM_ALIASES *q_u, SAMR_R_ENUM_DOM_ALIASES *r_u)
992 {
993         struct samr_info *info;
994         struct samr_displayentry *aliases;
995         uint32 num_aliases = 0;
996
997         /* find the policy handle.  open a policy on it. */
998         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
999                 return NT_STATUS_INVALID_HANDLE;
1000
1001         r_u->status = access_check_samr_function(info->acc_granted,
1002                                                  SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
1003                                                  "_samr_enum_dom_aliases");
1004         if (!NT_STATUS_IS_OK(r_u->status))
1005                 return r_u->status;
1006
1007         DEBUG(5,("samr_reply_enum_dom_aliases: sid %s\n",
1008                  sid_string_static(&info->sid)));
1009
1010         become_root();
1011
1012         if (info->disp_info->aliases == NULL) {
1013                 info->disp_info->aliases = pdb_search_aliases(&info->sid);
1014                 if (info->disp_info->aliases == NULL) {
1015                         unbecome_root();
1016                         return NT_STATUS_ACCESS_DENIED;
1017                 }
1018         }
1019
1020         num_aliases = pdb_search_entries(info->disp_info->aliases, q_u->start_idx,
1021                                          MAX_SAM_ENTRIES, &aliases);
1022         unbecome_root();
1023         
1024         /* Ensure we cache this enumeration. */
1025         set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1026
1027         make_group_sam_entry_list(p->mem_ctx, &r_u->sam, &r_u->uni_grp_name,
1028                                   num_aliases, aliases);
1029
1030         init_samr_r_enum_dom_aliases(r_u, q_u->start_idx + num_aliases,
1031                                      num_aliases);
1032
1033         DEBUG(5,("samr_enum_dom_aliases: %d\n", __LINE__));
1034
1035         return r_u->status;
1036 }
1037
1038 /*******************************************************************
1039  samr_reply_query_dispinfo
1040  ********************************************************************/
1041
1042 NTSTATUS _samr_query_dispinfo(pipes_struct *p, SAMR_Q_QUERY_DISPINFO *q_u, 
1043                               SAMR_R_QUERY_DISPINFO *r_u)
1044 {
1045         struct samr_info *info = NULL;
1046         uint32 struct_size=0x20; /* W2K always reply that, client doesn't care */
1047         
1048         uint32 max_entries=q_u->max_entries;
1049         uint32 enum_context=q_u->start_idx;
1050         uint32 max_size=q_u->max_size;
1051
1052         SAM_DISPINFO_CTR *ctr;
1053         uint32 temp_size=0, total_data_size=0;
1054         NTSTATUS disp_ret = NT_STATUS_UNSUCCESSFUL;
1055         uint32 num_account = 0;
1056         enum remote_arch_types ra_type = get_remote_arch();
1057         int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
1058         struct samr_displayentry *entries = NULL;
1059
1060         DEBUG(5, ("samr_reply_query_dispinfo: %d\n", __LINE__));
1061         r_u->status = NT_STATUS_UNSUCCESSFUL;
1062
1063         /* find the policy handle.  open a policy on it. */
1064         if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)&info))
1065                 return NT_STATUS_INVALID_HANDLE;
1066
1067         /*
1068          * calculate how many entries we will return.
1069          * based on 
1070          * - the number of entries the client asked
1071          * - our limit on that
1072          * - the starting point (enumeration context)
1073          * - the buffer size the client will accept
1074          */
1075
1076         /*
1077          * We are a lot more like W2K. Instead of reading the SAM
1078          * each time to find the records we need to send back,
1079          * we read it once and link that copy to the sam handle.
1080          * For large user list (over the MAX_SAM_ENTRIES)
1081          * it's a definitive win.
1082          * second point to notice: between enumerations
1083          * our sam is now the same as it's a snapshoot.
1084          * third point: got rid of the static SAM_USER_21 struct
1085          * no more intermediate.
1086          * con: it uses much more memory, as a full copy is stored
1087          * in memory.
1088          *
1089          * If you want to change it, think twice and think
1090          * of the second point , that's really important.
1091          *
1092          * JFM, 12/20/2001
1093          */
1094
1095         if ((q_u->switch_level < 1) || (q_u->switch_level > 5)) {
1096                 DEBUG(0,("_samr_query_dispinfo: Unknown info level (%u)\n",
1097                          (unsigned int)q_u->switch_level ));
1098                 return NT_STATUS_INVALID_INFO_CLASS;
1099         }
1100
1101         /* first limit the number of entries we will return */
1102         if(max_entries > max_sam_entries) {
1103                 DEBUG(5, ("samr_reply_query_dispinfo: client requested %d "
1104                           "entries, limiting to %d\n", max_entries,
1105                           max_sam_entries));
1106                 max_entries = max_sam_entries;
1107         }
1108
1109         /* calculate the size and limit on the number of entries we will
1110          * return */
1111
1112         temp_size=max_entries*struct_size;
1113         
1114         if (temp_size>max_size) {
1115                 max_entries=MIN((max_size/struct_size),max_entries);;
1116                 DEBUG(5, ("samr_reply_query_dispinfo: buffer size limits to "
1117                           "only %d entries\n", max_entries));
1118         }
1119
1120         if (!(ctr = TALLOC_ZERO_P(p->mem_ctx,SAM_DISPINFO_CTR)))
1121                 return NT_STATUS_NO_MEMORY;
1122
1123         ZERO_STRUCTP(ctr);
1124
1125         become_root();
1126
1127         /* THe following done as ROOT. Don't return without unbecome_root(). */
1128
1129         switch (q_u->switch_level) {
1130         case 0x1:
1131         case 0x4:
1132                 if (info->disp_info->users == NULL) {
1133                         info->disp_info->users = pdb_search_users(ACB_NORMAL);
1134                         if (info->disp_info->users == NULL) {
1135                                 unbecome_root();
1136                                 return NT_STATUS_ACCESS_DENIED;
1137                         }
1138                         DEBUG(10,("samr_reply_query_dispinfo: starting user enumeration at index %u\n",
1139                                 (unsigned  int)enum_context ));
1140                 } else {
1141                         DEBUG(10,("samr_reply_query_dispinfo: using cached user enumeration at index %u\n",
1142                                 (unsigned  int)enum_context ));
1143                 }
1144
1145                 num_account = pdb_search_entries(info->disp_info->users,
1146                                                  enum_context, max_entries,
1147                                                  &entries);
1148                 break;
1149         case 0x2:
1150                 if (info->disp_info->machines == NULL) {
1151                         info->disp_info->machines =
1152                                 pdb_search_users(ACB_WSTRUST|ACB_SVRTRUST);
1153                         if (info->disp_info->machines == NULL) {
1154                                 unbecome_root();
1155                                 return NT_STATUS_ACCESS_DENIED;
1156                         }
1157                         DEBUG(10,("samr_reply_query_dispinfo: starting machine enumeration at index %u\n",
1158                                 (unsigned  int)enum_context ));
1159                 } else {
1160                         DEBUG(10,("samr_reply_query_dispinfo: using cached machine enumeration at index %u\n",
1161                                 (unsigned  int)enum_context ));
1162                 }
1163
1164                 num_account = pdb_search_entries(info->disp_info->machines,
1165                                                  enum_context, max_entries,
1166                                                  &entries);
1167                 break;
1168         case 0x3:
1169         case 0x5:
1170                 if (info->disp_info->groups == NULL) {
1171                         info->disp_info->groups = pdb_search_groups();
1172                         if (info->disp_info->groups == NULL) {
1173                                 unbecome_root();
1174                                 return NT_STATUS_ACCESS_DENIED;
1175                         }
1176                         DEBUG(10,("samr_reply_query_dispinfo: starting group enumeration at index %u\n",
1177                                 (unsigned  int)enum_context ));
1178                 } else {
1179                         DEBUG(10,("samr_reply_query_dispinfo: using cached group enumeration at index %u\n",
1180                                 (unsigned  int)enum_context ));
1181                 }
1182
1183                 num_account = pdb_search_entries(info->disp_info->groups,
1184                                                  enum_context, max_entries,
1185                                                  &entries);
1186                 break;
1187         default:
1188                 unbecome_root();
1189                 smb_panic("info class changed");
1190                 break;
1191         }
1192         unbecome_root();
1193
1194         /* Now create reply structure */
1195         switch (q_u->switch_level) {
1196         case 0x1:
1197                 disp_ret = init_sam_dispinfo_1(p->mem_ctx, &ctr->sam.info1,
1198                                                num_account, enum_context,
1199                                                entries);
1200                 break;
1201         case 0x2:
1202                 disp_ret = init_sam_dispinfo_2(p->mem_ctx, &ctr->sam.info2,
1203                                                num_account, enum_context,
1204                                                entries);
1205                 break;
1206         case 0x3:
1207                 disp_ret = init_sam_dispinfo_3(p->mem_ctx, &ctr->sam.info3,
1208                                                num_account, enum_context,
1209                                                entries);
1210                 break;
1211         case 0x4:
1212                 disp_ret = init_sam_dispinfo_4(p->mem_ctx, &ctr->sam.info4,
1213                                                num_account, enum_context,
1214                                                entries);
1215                 break;
1216         case 0x5:
1217                 disp_ret = init_sam_dispinfo_5(p->mem_ctx, &ctr->sam.info5,
1218                                                num_account, enum_context,
1219                                                entries);
1220                 break;
1221         default:
1222                 smb_panic("info class changed");
1223                 break;
1224         }
1225
1226         if (!NT_STATUS_IS_OK(disp_ret))
1227                 return disp_ret;
1228
1229         /* calculate the total size */
1230         total_data_size=num_account*struct_size;
1231
1232         if (num_account) {
1233                 r_u->status = STATUS_MORE_ENTRIES;
1234         } else {
1235                 r_u->status = NT_STATUS_OK;
1236         }
1237
1238         /* Ensure we cache this enumeration. */
1239         set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1240
1241         DEBUG(5, ("_samr_query_dispinfo: %d\n", __LINE__));
1242
1243         init_samr_r_query_dispinfo(r_u, num_account, total_data_size,
1244                                    temp_size, q_u->switch_level, ctr,
1245                                    r_u->status);
1246
1247         return r_u->status;
1248
1249 }
1250
1251 /*******************************************************************
1252  samr_reply_query_aliasinfo
1253  ********************************************************************/
1254
1255 NTSTATUS _samr_query_aliasinfo(pipes_struct *p, SAMR_Q_QUERY_ALIASINFO *q_u, SAMR_R_QUERY_ALIASINFO *r_u)
1256 {
1257         DOM_SID   sid;
1258         struct acct_info info;
1259         uint32    acc_granted;
1260         BOOL ret;
1261
1262         r_u->status = NT_STATUS_OK;
1263
1264         DEBUG(5,("_samr_query_aliasinfo: %d\n", __LINE__));
1265
1266         /* find the policy handle.  open a policy on it. */
1267         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &sid, &acc_granted, NULL))
1268                 return NT_STATUS_INVALID_HANDLE;
1269         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_LOOKUP_INFO, "_samr_query_aliasinfo"))) {
1270                 return r_u->status;
1271         }
1272
1273         become_root();
1274         ret = pdb_get_aliasinfo(&sid, &info);
1275         unbecome_root();
1276         
1277         if ( !ret )
1278                 return NT_STATUS_NO_SUCH_ALIAS;
1279
1280         if ( !(r_u->ctr = TALLOC_ZERO_P( p->mem_ctx, ALIAS_INFO_CTR )) ) 
1281                 return NT_STATUS_NO_MEMORY;
1282
1283
1284         switch (q_u->level ) {
1285         case 1:
1286                 r_u->ctr->level = 1;
1287                 init_samr_alias_info1(&r_u->ctr->alias.info1, info.acct_name, 1, info.acct_desc);
1288                 break;
1289         case 3:
1290                 r_u->ctr->level = 3;
1291                 init_samr_alias_info3(&r_u->ctr->alias.info3, info.acct_desc);
1292                 break;
1293         default:
1294                 return NT_STATUS_INVALID_INFO_CLASS;
1295         }
1296
1297         DEBUG(5,("_samr_query_aliasinfo: %d\n", __LINE__));
1298
1299         return r_u->status;
1300 }
1301
1302 #if 0
1303 /*******************************************************************
1304  samr_reply_lookup_ids
1305  ********************************************************************/
1306
1307  uint32 _samr_lookup_ids(pipes_struct *p, SAMR_Q_LOOKUP_IDS *q_u, SAMR_R_LOOKUP_IDS *r_u)
1308 {
1309     uint32 rid[MAX_SAM_ENTRIES];
1310     int num_rids = q_u->num_sids1;
1311
1312     r_u->status = NT_STATUS_OK;
1313
1314     DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1315
1316     if (num_rids > MAX_SAM_ENTRIES) {
1317         num_rids = MAX_SAM_ENTRIES;
1318         DEBUG(5,("_samr_lookup_ids: truncating entries to %d\n", num_rids));
1319     }
1320
1321 #if 0
1322     int i;
1323     SMB_ASSERT_ARRAY(q_u->uni_user_name, num_rids);
1324
1325     for (i = 0; i < num_rids && status == 0; i++)
1326     {
1327         struct sam_passwd *sam_pass;
1328         fstring user_name;
1329
1330
1331         fstrcpy(user_name, unistrn2(q_u->uni_user_name[i].buffer,
1332                                     q_u->uni_user_name[i].uni_str_len));
1333
1334         /* find the user account */
1335         become_root();
1336         sam_pass = get_smb21pwd_entry(user_name, 0);
1337         unbecome_root();
1338
1339         if (sam_pass == NULL)
1340         {
1341             status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
1342             rid[i] = 0;
1343         }
1344         else
1345         {
1346             rid[i] = sam_pass->user_rid;
1347         }
1348     }
1349 #endif
1350
1351     num_rids = 1;
1352     rid[0] = BUILTIN_ALIAS_RID_USERS;
1353
1354     init_samr_r_lookup_ids(&r_u, num_rids, rid, NT_STATUS_OK);
1355
1356     DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1357
1358     return r_u->status;
1359 }
1360 #endif
1361
1362 /*******************************************************************
1363  _samr_lookup_names
1364  ********************************************************************/
1365
1366 NTSTATUS _samr_lookup_names(pipes_struct *p, SAMR_Q_LOOKUP_NAMES *q_u, SAMR_R_LOOKUP_NAMES *r_u)
1367 {
1368         uint32 rid[MAX_SAM_ENTRIES];
1369         uint32 local_rid;
1370         enum SID_NAME_USE type[MAX_SAM_ENTRIES];
1371         enum SID_NAME_USE local_type;
1372         int i;
1373         int num_rids = q_u->num_names2;
1374         DOM_SID pol_sid;
1375         fstring sid_str;
1376         uint32  acc_granted;
1377
1378         r_u->status = NT_STATUS_OK;
1379
1380         DEBUG(5,("_samr_lookup_names: %d\n", __LINE__));
1381
1382         ZERO_ARRAY(rid);
1383         ZERO_ARRAY(type);
1384
1385         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &pol_sid, &acc_granted, NULL)) {
1386                 init_samr_r_lookup_names(p->mem_ctx, r_u, 0, NULL, NULL, NT_STATUS_OBJECT_TYPE_MISMATCH);
1387                 return r_u->status;
1388         }
1389         
1390         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, 0, "_samr_lookup_names"))) { /* Don't know the acc_bits yet */
1391                 return r_u->status;
1392         }
1393
1394         if (num_rids > MAX_SAM_ENTRIES) {
1395                 num_rids = MAX_SAM_ENTRIES;
1396                 DEBUG(5,("_samr_lookup_names: truncating entries to %d\n", num_rids));
1397         }
1398
1399         DEBUG(5,("_samr_lookup_names: looking name on SID %s\n", sid_to_string(sid_str, &pol_sid)));
1400         
1401         for (i = 0; i < num_rids; i++) {
1402                 fstring name;
1403                 DOM_SID sid;
1404                 int ret;
1405
1406                 r_u->status = NT_STATUS_NONE_MAPPED;
1407
1408                 rid [i] = 0xffffffff;
1409                 type[i] = SID_NAME_UNKNOWN;
1410
1411                 ret = rpcstr_pull(name, q_u->uni_name[i].buffer, sizeof(name), q_u->uni_name[i].uni_str_len*2, 0);
1412
1413                 /*
1414                  * we are only looking for a name
1415                  * the SID we get back can be outside
1416                  * the scope of the pol_sid
1417                  * 
1418                  * in clear: it prevents to reply to domain\group: yes
1419                  * when only builtin\group exists.
1420                  *
1421                  * a cleaner code is to add the sid of the domain we're looking in
1422                  * to the local_lookup_name function.
1423                  */
1424                  
1425                 if ((ret > 0) && local_lookup_name(name, &sid, &local_type)) {
1426                         sid_split_rid(&sid, &local_rid);
1427                                 
1428                         if (sid_equal(&sid, &pol_sid)) {
1429                                 rid[i]=local_rid;
1430
1431                                 /* Windows does not return WKN_GRP here, even
1432                                  * on lookups in builtin */
1433                                 type[i] = (local_type == SID_NAME_WKN_GRP) ?
1434                                         SID_NAME_ALIAS : local_type;
1435
1436                                 r_u->status = NT_STATUS_OK;
1437                         }
1438                 }
1439         }
1440
1441         init_samr_r_lookup_names(p->mem_ctx, r_u, num_rids, rid, (uint32 *)type, r_u->status);
1442
1443         DEBUG(5,("_samr_lookup_names: %d\n", __LINE__));
1444
1445         return r_u->status;
1446 }
1447
1448 /*******************************************************************
1449  _samr_chgpasswd_user
1450  ********************************************************************/
1451
1452 NTSTATUS _samr_chgpasswd_user(pipes_struct *p, SAMR_Q_CHGPASSWD_USER *q_u, SAMR_R_CHGPASSWD_USER *r_u)
1453 {
1454         fstring user_name;
1455         fstring wks;
1456
1457         DEBUG(5,("_samr_chgpasswd_user: %d\n", __LINE__));
1458
1459         r_u->status = NT_STATUS_OK;
1460
1461         rpcstr_pull(user_name, q_u->uni_user_name.buffer, sizeof(user_name), q_u->uni_user_name.uni_str_len*2, 0);
1462         rpcstr_pull(wks, q_u->uni_dest_host.buffer, sizeof(wks), q_u->uni_dest_host.uni_str_len*2,0);
1463
1464         DEBUG(5,("samr_chgpasswd_user: user: %s wks: %s\n", user_name, wks));
1465
1466         /*
1467          * Pass the user through the NT -> unix user mapping
1468          * function.
1469          */
1470  
1471         (void)map_username(user_name);
1472  
1473         /*
1474          * UNIX username case mangling not required, pass_oem_change 
1475          * is case insensitive.
1476          */
1477
1478         r_u->status = pass_oem_change(user_name, q_u->lm_newpass.pass, q_u->lm_oldhash.hash,
1479                                 q_u->nt_newpass.pass, q_u->nt_oldhash.hash);
1480
1481         init_samr_r_chgpasswd_user(r_u, r_u->status);
1482
1483         DEBUG(5,("_samr_chgpasswd_user: %d\n", __LINE__));
1484
1485         return r_u->status;
1486 }
1487
1488 /*******************************************************************
1489 makes a SAMR_R_LOOKUP_RIDS structure.
1490 ********************************************************************/
1491
1492 static BOOL make_samr_lookup_rids(TALLOC_CTX *ctx, uint32 num_names,
1493                                   const char **names, UNIHDR **pp_hdr_name,
1494                                   UNISTR2 **pp_uni_name)
1495 {
1496         uint32 i;
1497         UNIHDR *hdr_name=NULL;
1498         UNISTR2 *uni_name=NULL;
1499
1500         *pp_uni_name = NULL;
1501         *pp_hdr_name = NULL;
1502
1503         if (num_names != 0) {
1504                 hdr_name = TALLOC_ZERO_ARRAY(ctx, UNIHDR, num_names);
1505                 if (hdr_name == NULL)
1506                         return False;
1507
1508                 uni_name = TALLOC_ZERO_ARRAY(ctx,UNISTR2, num_names);
1509                 if (uni_name == NULL)
1510                         return False;
1511         }
1512
1513         for (i = 0; i < num_names; i++) {
1514                 DEBUG(10, ("names[%d]:%s\n", i, names[i] && *names[i] ? names[i] : ""));
1515                 init_unistr2(&uni_name[i], names[i], UNI_FLAGS_NONE);
1516                 init_uni_hdr(&hdr_name[i], &uni_name[i]);
1517         }
1518
1519         *pp_uni_name = uni_name;
1520         *pp_hdr_name = hdr_name;
1521
1522         return True;
1523 }
1524
1525 /*******************************************************************
1526  _samr_lookup_rids
1527  ********************************************************************/
1528
1529 NTSTATUS _samr_lookup_rids(pipes_struct *p, SAMR_Q_LOOKUP_RIDS *q_u, SAMR_R_LOOKUP_RIDS *r_u)
1530 {
1531         const char **names;
1532         uint32 *attrs = NULL;
1533         UNIHDR *hdr_name = NULL;
1534         UNISTR2 *uni_name = NULL;
1535         DOM_SID pol_sid;
1536         int num_rids = q_u->num_rids1;
1537         uint32 acc_granted;
1538         
1539         r_u->status = NT_STATUS_OK;
1540
1541         DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
1542
1543         /* find the policy handle.  open a policy on it. */
1544         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &pol_sid, &acc_granted, NULL))
1545                 return NT_STATUS_INVALID_HANDLE;
1546
1547         if (num_rids > 1000) {
1548                 DEBUG(0, ("Got asked for %d rids (more than 1000) -- according "
1549                           "to samba4 idl this is not possible\n", num_rids));
1550                 return NT_STATUS_UNSUCCESSFUL;
1551         }
1552
1553         names = TALLOC_ZERO_ARRAY(p->mem_ctx, const char *, num_rids);
1554         attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_rids);
1555
1556         if ((num_rids != 0) && ((names == NULL) || (attrs == NULL)))
1557                 return NT_STATUS_NO_MEMORY;
1558
1559         become_root();  /* lookup_sid can require root privs */
1560         r_u->status = pdb_lookup_rids(&pol_sid, num_rids, q_u->rid,
1561                                       names, attrs);
1562         unbecome_root();
1563
1564         if(!make_samr_lookup_rids(p->mem_ctx, num_rids, names,
1565                                   &hdr_name, &uni_name))
1566                 return NT_STATUS_NO_MEMORY;
1567
1568         init_samr_r_lookup_rids(r_u, num_rids, hdr_name, uni_name, attrs);
1569
1570         DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
1571
1572         return r_u->status;
1573 }
1574
1575 /*******************************************************************
1576  _samr_open_user. Safe - gives out no passwd info.
1577  ********************************************************************/
1578
1579 NTSTATUS _samr_open_user(pipes_struct *p, SAMR_Q_OPEN_USER *q_u, SAMR_R_OPEN_USER *r_u)
1580 {
1581         SAM_ACCOUNT *sampass=NULL;
1582         DOM_SID sid;
1583         POLICY_HND domain_pol = q_u->domain_pol;
1584         POLICY_HND *user_pol = &r_u->user_pol;
1585         struct samr_info *info = NULL;
1586         SEC_DESC *psd = NULL;
1587         uint32    acc_granted;
1588         uint32    des_access = q_u->access_mask;
1589         size_t    sd_size;
1590         BOOL ret;
1591         NTSTATUS nt_status;
1592         SE_PRIV se_rights;
1593
1594         r_u->status = NT_STATUS_OK;
1595
1596         /* find the domain policy handle and get domain SID / access bits in the domain policy. */
1597         
1598         if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
1599                 return NT_STATUS_INVALID_HANDLE;
1600         
1601         nt_status = access_check_samr_function( acc_granted, 
1602                 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_user" );
1603                 
1604         if ( !NT_STATUS_IS_OK(nt_status) )
1605                 return nt_status;
1606
1607         nt_status = pdb_init_sam_talloc(p->mem_ctx, &sampass);
1608         
1609         if (!NT_STATUS_IS_OK(nt_status))
1610                 return nt_status;
1611
1612         /* append the user's RID to it */
1613         
1614         if (!sid_append_rid(&sid, q_u->user_rid))
1615                 return NT_STATUS_NO_SUCH_USER;
1616         
1617         /* check if access can be granted as requested by client. */
1618         
1619         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
1620         se_map_generic(&des_access, &usr_generic_mapping);
1621         
1622         se_priv_copy( &se_rights, &se_machine_account );
1623         se_priv_add( &se_rights, &se_add_users );
1624         
1625         nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
1626                 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access, 
1627                 &acc_granted, "_samr_open_user");
1628                 
1629         if ( !NT_STATUS_IS_OK(nt_status) )
1630                 return nt_status;
1631
1632         become_root();
1633         ret=pdb_getsampwsid(sampass, &sid);
1634         unbecome_root();
1635
1636         /* check that the SID exists in our domain. */
1637         if (ret == False) {
1638                 return NT_STATUS_NO_SUCH_USER;
1639         }
1640
1641         pdb_free_sam(&sampass);
1642
1643         /* associate the user's SID and access bits with the new handle. */
1644         if ((info = get_samr_info_by_sid(&sid)) == NULL)
1645                 return NT_STATUS_NO_MEMORY;
1646         info->acc_granted = acc_granted;
1647
1648         /* get a (unique) handle.  open a policy on it. */
1649         if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info))
1650                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1651
1652         return r_u->status;
1653 }
1654
1655 /*************************************************************************
1656  get_user_info_7. Safe. Only gives out account_name.
1657  *************************************************************************/
1658
1659 static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx, SAM_USER_INFO_7 *id7, DOM_SID *user_sid)
1660 {
1661         SAM_ACCOUNT *smbpass=NULL;
1662         BOOL ret;
1663         NTSTATUS nt_status;
1664
1665         nt_status = pdb_init_sam_talloc(mem_ctx, &smbpass);
1666         
1667         if (!NT_STATUS_IS_OK(nt_status)) {
1668                 return nt_status;
1669         }
1670
1671         become_root();
1672         ret = pdb_getsampwsid(smbpass, user_sid);
1673         unbecome_root();
1674
1675         if (ret==False) {
1676                 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1677                 return NT_STATUS_NO_SUCH_USER;
1678         }
1679
1680         DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1681
1682         ZERO_STRUCTP(id7);
1683         init_sam_user_info7(id7, pdb_get_username(smbpass) );
1684
1685         pdb_free_sam(&smbpass);
1686
1687         return NT_STATUS_OK;
1688 }
1689 /*************************************************************************
1690  get_user_info_16. Safe. Only gives out acb bits.
1691  *************************************************************************/
1692
1693 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx, SAM_USER_INFO_16 *id16, DOM_SID *user_sid)
1694 {
1695         SAM_ACCOUNT *smbpass=NULL;
1696         BOOL ret;
1697         NTSTATUS nt_status;
1698
1699         nt_status = pdb_init_sam_talloc(mem_ctx, &smbpass);
1700         
1701         if (!NT_STATUS_IS_OK(nt_status)) {
1702                 return nt_status;
1703         }
1704
1705         become_root();
1706         ret = pdb_getsampwsid(smbpass, user_sid);
1707         unbecome_root();
1708
1709         if (ret==False) {
1710                 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1711                 return NT_STATUS_NO_SUCH_USER;
1712         }
1713
1714         DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1715
1716         ZERO_STRUCTP(id16);
1717         init_sam_user_info16(id16, pdb_get_acct_ctrl(smbpass) );
1718
1719         pdb_free_sam(&smbpass);
1720
1721         return NT_STATUS_OK;
1722 }
1723
1724 /*************************************************************************
1725  get_user_info_18. OK - this is the killer as it gives out password info.
1726  Ensure that this is only allowed on an encrypted connection with a root
1727  user. JRA. 
1728  *************************************************************************/
1729
1730 static NTSTATUS get_user_info_18(pipes_struct *p, TALLOC_CTX *mem_ctx, SAM_USER_INFO_18 * id18, DOM_SID *user_sid)
1731 {
1732         SAM_ACCOUNT *smbpass=NULL;
1733         BOOL ret;
1734         NTSTATUS nt_status;
1735
1736         if (p->auth.auth_type != PIPE_AUTH_TYPE_NTLMSSP || p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP) {
1737                 return NT_STATUS_ACCESS_DENIED;
1738         }
1739
1740         if (p->auth.auth_level != PIPE_AUTH_LEVEL_PRIVACY) {
1741                 return NT_STATUS_ACCESS_DENIED;
1742         }
1743
1744         /*
1745          * Do *NOT* do become_root()/unbecome_root() here ! JRA.
1746          */
1747
1748         nt_status = pdb_init_sam_talloc(mem_ctx, &smbpass);
1749         
1750         if (!NT_STATUS_IS_OK(nt_status)) {
1751                 return nt_status;
1752         }
1753
1754         ret = pdb_getsampwsid(smbpass, user_sid);
1755
1756         if (ret == False) {
1757                 DEBUG(4, ("User %s not found\n", sid_string_static(user_sid)));
1758                 pdb_free_sam(&smbpass);
1759                 return (geteuid() == (uid_t)0) ? NT_STATUS_NO_SUCH_USER : NT_STATUS_ACCESS_DENIED;
1760         }
1761
1762         DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
1763
1764         if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
1765                 pdb_free_sam(&smbpass);
1766                 return NT_STATUS_ACCOUNT_DISABLED;
1767         }
1768
1769         ZERO_STRUCTP(id18);
1770         init_sam_user_info18(id18, pdb_get_lanman_passwd(smbpass), pdb_get_nt_passwd(smbpass));
1771         
1772         pdb_free_sam(&smbpass);
1773
1774         return NT_STATUS_OK;
1775 }
1776
1777 /*************************************************************************
1778  get_user_info_20
1779  *************************************************************************/
1780
1781 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx, SAM_USER_INFO_20 *id20, DOM_SID *user_sid)
1782 {
1783         SAM_ACCOUNT *sampass=NULL;
1784         BOOL ret;
1785
1786         pdb_init_sam_talloc(mem_ctx, &sampass);
1787
1788         become_root();
1789         ret = pdb_getsampwsid(sampass, user_sid);
1790         unbecome_root();
1791
1792         if (ret == False) {
1793                 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1794                 return NT_STATUS_NO_SUCH_USER;
1795         }
1796
1797         samr_clear_sam_passwd(sampass);
1798
1799         DEBUG(3,("User:[%s]\n",  pdb_get_username(sampass) ));
1800
1801         ZERO_STRUCTP(id20);
1802         init_sam_user_info20A(id20, sampass);
1803         
1804         pdb_free_sam(&sampass);
1805
1806         return NT_STATUS_OK;
1807 }
1808
1809 /*************************************************************************
1810  get_user_info_21
1811  *************************************************************************/
1812
1813 static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx, SAM_USER_INFO_21 *id21, 
1814                                  DOM_SID *user_sid, DOM_SID *domain_sid)
1815 {
1816         SAM_ACCOUNT *sampass=NULL;
1817         BOOL ret;
1818         NTSTATUS nt_status;
1819
1820         nt_status = pdb_init_sam_talloc(mem_ctx, &sampass);
1821         if (!NT_STATUS_IS_OK(nt_status)) {
1822                 return nt_status;
1823         }
1824
1825         become_root();
1826         ret = pdb_getsampwsid(sampass, user_sid);
1827         unbecome_root();
1828
1829         if (ret == False) {
1830                 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1831                 return NT_STATUS_NO_SUCH_USER;
1832         }
1833
1834         samr_clear_sam_passwd(sampass);
1835
1836         DEBUG(3,("User:[%s]\n",  pdb_get_username(sampass) ));
1837
1838         ZERO_STRUCTP(id21);
1839         nt_status = init_sam_user_info21A(id21, sampass, domain_sid);
1840         
1841         pdb_free_sam(&sampass);
1842
1843         return NT_STATUS_OK;
1844 }
1845
1846 /*******************************************************************
1847  _samr_query_userinfo
1848  ********************************************************************/
1849
1850 NTSTATUS _samr_query_userinfo(pipes_struct *p, SAMR_Q_QUERY_USERINFO *q_u, SAMR_R_QUERY_USERINFO *r_u)
1851 {
1852         SAM_USERINFO_CTR *ctr;
1853         struct samr_info *info = NULL;
1854         DOM_SID domain_sid;
1855         uint32 rid;
1856         
1857         r_u->status=NT_STATUS_OK;
1858
1859         /* search for the handle */
1860         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1861                 return NT_STATUS_INVALID_HANDLE;
1862
1863         domain_sid = info->sid;
1864
1865         sid_split_rid(&domain_sid, &rid);
1866
1867         if (!sid_check_is_in_our_domain(&info->sid))
1868                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1869
1870         DEBUG(5,("_samr_query_userinfo: sid:%s\n", sid_string_static(&info->sid)));
1871
1872         ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_USERINFO_CTR);
1873         if (!ctr)
1874                 return NT_STATUS_NO_MEMORY;
1875
1876         ZERO_STRUCTP(ctr);
1877
1878         /* ok!  user info levels (lots: see MSDEV help), off we go... */
1879         ctr->switch_value = q_u->switch_value;
1880
1881         switch (q_u->switch_value) {
1882         case 7:
1883                 ctr->info.id7 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_7);
1884                 if (ctr->info.id7 == NULL)
1885                         return NT_STATUS_NO_MEMORY;
1886
1887                 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_7(p->mem_ctx, ctr->info.id7, &info->sid)))
1888                         return r_u->status;
1889                 break;
1890         case 16:
1891                 ctr->info.id16 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_16);
1892                 if (ctr->info.id16 == NULL)
1893                         return NT_STATUS_NO_MEMORY;
1894
1895                 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_16(p->mem_ctx, ctr->info.id16, &info->sid)))
1896                         return r_u->status;
1897                 break;
1898
1899 #if 0
1900 /* whoops - got this wrong.  i think.  or don't understand what's happening. */
1901         case 17:
1902         {
1903             NTTIME expire;
1904             info = (void *)&id11;
1905
1906             expire.low = 0xffffffff;
1907             expire.high = 0x7fffffff;
1908
1909             ctr->info.id = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_17));
1910             ZERO_STRUCTP(ctr->info.id17);
1911             init_sam_user_info17(ctr->info.id17, &expire,
1912                          "BROOKFIELDS$",    /* name */
1913                          0x03ef,    /* user rid */
1914                          0x201, /* group rid */
1915                          0x0080);   /* acb info */
1916
1917             break;
1918         }
1919 #endif
1920
1921         case 18:
1922                 ctr->info.id18 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_18);
1923                 if (ctr->info.id18 == NULL)
1924                         return NT_STATUS_NO_MEMORY;
1925
1926                 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_18(p, p->mem_ctx, ctr->info.id18, &info->sid)))
1927                         return r_u->status;
1928                 break;
1929                 
1930         case 20:
1931                 ctr->info.id20 = TALLOC_ZERO_P(p->mem_ctx,SAM_USER_INFO_20);
1932                 if (ctr->info.id20 == NULL)
1933                         return NT_STATUS_NO_MEMORY;
1934                 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_20(p->mem_ctx, ctr->info.id20, &info->sid)))
1935                         return r_u->status;
1936                 break;
1937
1938         case 21:
1939                 ctr->info.id21 = TALLOC_ZERO_P(p->mem_ctx,SAM_USER_INFO_21);
1940                 if (ctr->info.id21 == NULL)
1941                         return NT_STATUS_NO_MEMORY;
1942                 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_21(p->mem_ctx, ctr->info.id21, 
1943                                                                     &info->sid, &domain_sid)))
1944                         return r_u->status;
1945                 break;
1946
1947         default:
1948                 return NT_STATUS_INVALID_INFO_CLASS;
1949         }
1950
1951         init_samr_r_query_userinfo(r_u, ctr, r_u->status);
1952
1953         DEBUG(5,("_samr_query_userinfo: %d\n", __LINE__));
1954         
1955         return r_u->status;
1956 }
1957
1958 /*******************************************************************
1959  samr_reply_query_usergroups
1960  ********************************************************************/
1961
1962 NTSTATUS _samr_query_usergroups(pipes_struct *p, SAMR_Q_QUERY_USERGROUPS *q_u, SAMR_R_QUERY_USERGROUPS *r_u)
1963 {
1964         SAM_ACCOUNT *sam_pass=NULL;
1965         struct passwd *passwd;
1966         DOM_SID  sid;
1967         DOM_SID *sids;
1968         DOM_GID *gids = NULL;
1969         size_t num_groups = 0;
1970         gid_t *unix_gids;
1971         size_t i, num_gids;
1972         uint32 acc_granted;
1973         BOOL ret;
1974         NTSTATUS result;
1975
1976         /*
1977          * from the SID in the request:
1978          * we should send back the list of DOMAIN GROUPS
1979          * the user is a member of
1980          *
1981          * and only the DOMAIN GROUPS
1982          * no ALIASES !!! neither aliases of the domain
1983          * nor aliases of the builtin SID
1984          *
1985          * JFM, 12/2/2001
1986          */
1987
1988         r_u->status = NT_STATUS_OK;
1989
1990         DEBUG(5,("_samr_query_usergroups: %d\n", __LINE__));
1991
1992         /* find the policy handle.  open a policy on it. */
1993         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &sid, &acc_granted, NULL))
1994                 return NT_STATUS_INVALID_HANDLE;
1995         
1996         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_USER_GET_GROUPS, "_samr_query_usergroups"))) {
1997                 return r_u->status;
1998         }
1999
2000         if (!sid_check_is_in_our_domain(&sid))
2001                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2002
2003         pdb_init_sam(&sam_pass);
2004         
2005         become_root();
2006         ret = pdb_getsampwsid(sam_pass, &sid);
2007         unbecome_root();
2008
2009         if (ret == False) {
2010                 pdb_free_sam(&sam_pass);
2011                 return NT_STATUS_NO_SUCH_USER;
2012         }
2013
2014         passwd = getpwnam_alloc(pdb_get_username(sam_pass));
2015         if (passwd == NULL) {
2016                 pdb_free_sam(&sam_pass);
2017                 return NT_STATUS_NO_SUCH_USER;
2018         }
2019
2020         sids = NULL;
2021
2022         become_root();
2023         result = pdb_enum_group_memberships(pdb_get_username(sam_pass),
2024                                             passwd->pw_gid,
2025                                             &sids, &unix_gids, &num_groups);
2026         unbecome_root();
2027
2028         pdb_free_sam(&sam_pass);
2029         passwd_free(&passwd);
2030
2031         if (!NT_STATUS_IS_OK(result))
2032                 return result;
2033
2034         SAFE_FREE(unix_gids);
2035
2036         gids = NULL;
2037         num_gids = 0;
2038
2039         for (i=0; i<num_groups; i++) {
2040                 uint32 rid;
2041
2042                 if (!sid_peek_check_rid(get_global_sam_sid(),
2043                                         &(sids[i]), &rid))
2044                         continue;
2045
2046                 gids = TALLOC_REALLOC_ARRAY(p->mem_ctx, gids, DOM_GID, num_gids+1);
2047                 gids[num_gids].attr= (SE_GROUP_MANDATORY|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_ENABLED);
2048                 gids[num_gids].g_rid = rid;
2049                 num_gids += 1;
2050         }
2051         SAFE_FREE(sids);
2052         
2053         /* construct the response.  lkclXXXX: gids are not copied! */
2054         init_samr_r_query_usergroups(r_u, num_groups, gids, r_u->status);
2055         
2056         DEBUG(5,("_samr_query_usergroups: %d\n", __LINE__));
2057         
2058         return r_u->status;
2059 }
2060
2061 /*******************************************************************
2062  _samr_query_dom_info
2063  ********************************************************************/
2064
2065 NTSTATUS _samr_query_dom_info(pipes_struct *p, SAMR_Q_QUERY_DOMAIN_INFO *q_u, SAMR_R_QUERY_DOMAIN_INFO *r_u)
2066 {
2067         struct samr_info *info = NULL;
2068         SAM_UNK_CTR *ctr;
2069         uint32 min_pass_len,pass_hist,flag;
2070         time_t u_expire, u_min_age;
2071         NTTIME nt_expire, nt_min_age;
2072
2073         time_t u_lock_duration, u_reset_time;
2074         NTTIME nt_lock_duration, nt_reset_time;
2075         uint32 lockout;
2076         time_t u_logout;
2077         NTTIME nt_logout;
2078
2079         uint32 account_policy_temp;
2080
2081         time_t seq_num;
2082         uint32 server_role;
2083
2084         uint32 num_users=0, num_groups=0, num_aliases=0;
2085
2086         if ((ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_UNK_CTR)) == NULL) {
2087                 return NT_STATUS_NO_MEMORY;
2088         }
2089
2090         ZERO_STRUCTP(ctr);
2091
2092         r_u->status = NT_STATUS_OK;
2093         
2094         DEBUG(5,("_samr_query_dom_info: %d\n", __LINE__));
2095         
2096         /* find the policy handle.  open a policy on it. */
2097         if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)&info)) {
2098                 return NT_STATUS_INVALID_HANDLE;
2099         }
2100         
2101         switch (q_u->switch_value) {
2102                 case 0x01:
2103                         
2104                         become_root();
2105
2106                         /* AS ROOT !!! */
2107
2108                         pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
2109                         min_pass_len = account_policy_temp;
2110
2111                         pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
2112                         pass_hist = account_policy_temp;
2113
2114                         pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
2115                         flag = account_policy_temp;
2116
2117                         pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
2118                         u_expire = account_policy_temp;
2119
2120                         pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
2121                         u_min_age = account_policy_temp;
2122
2123                         /* !AS ROOT */
2124                         
2125                         unbecome_root();
2126
2127                         unix_to_nt_time_abs(&nt_expire, u_expire);
2128                         unix_to_nt_time_abs(&nt_min_age, u_min_age);
2129
2130                         init_unk_info1(&ctr->info.inf1, (uint16)min_pass_len, (uint16)pass_hist, 
2131                                        flag, nt_expire, nt_min_age);
2132                         break;
2133                 case 0x02:
2134
2135                         become_root();
2136
2137                         /* AS ROOT !!! */
2138
2139                         num_users = count_sam_users(info->disp_info, ACB_NORMAL);
2140                         num_groups = count_sam_groups(info->disp_info);
2141                         num_aliases = count_sam_aliases(info->disp_info);
2142
2143                         pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
2144                         u_logout = account_policy_temp;
2145
2146                         unix_to_nt_time_abs(&nt_logout, u_logout);
2147
2148                         if (!pdb_get_seq_num(&seq_num))
2149                                 seq_num = time(NULL);
2150
2151                         /* !AS ROOT */
2152                         
2153                         unbecome_root();
2154
2155                         server_role = ROLE_DOMAIN_PDC;
2156                         if (lp_server_role() == ROLE_DOMAIN_BDC)
2157                                 server_role = ROLE_DOMAIN_BDC;
2158
2159                         init_unk_info2(&ctr->info.inf2, lp_serverstring(), lp_workgroup(), global_myname(), seq_num, 
2160                                        num_users, num_groups, num_aliases, nt_logout, server_role);
2161                         break;
2162                 case 0x03:
2163
2164                         become_root();
2165
2166                         /* AS ROOT !!! */
2167
2168                         pdb_get_account_policy(AP_TIME_TO_LOGOUT, (unsigned int *)&u_logout);
2169
2170                         /* !AS ROOT */
2171                         
2172                         unbecome_root();
2173
2174                         unix_to_nt_time_abs(&nt_logout, u_logout);
2175                         
2176                         init_unk_info3(&ctr->info.inf3, nt_logout);
2177                         break;
2178                 case 0x05:
2179                         init_unk_info5(&ctr->info.inf5, global_myname());
2180                         break;
2181                 case 0x06:
2182                         init_unk_info6(&ctr->info.inf6);
2183                         break;
2184                 case 0x07:
2185                         server_role = ROLE_DOMAIN_PDC;
2186                         if (lp_server_role() == ROLE_DOMAIN_BDC)
2187                                 server_role = ROLE_DOMAIN_BDC;
2188
2189                         init_unk_info7(&ctr->info.inf7, server_role);
2190                         break;
2191                 case 0x08:
2192
2193                         become_root();
2194
2195                         /* AS ROOT !!! */
2196
2197                         if (!pdb_get_seq_num(&seq_num)) {
2198                                 seq_num = time(NULL);
2199                         }
2200
2201                         /* !AS ROOT */
2202                         
2203                         unbecome_root();
2204
2205                         init_unk_info8(&ctr->info.inf8, (uint32) seq_num);
2206                         break;
2207                 case 0x0c:
2208
2209                         become_root();
2210
2211                         /* AS ROOT !!! */
2212
2213                         pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION, &account_policy_temp);
2214                         u_lock_duration = account_policy_temp;
2215                         if (u_lock_duration != -1) {
2216                                 u_lock_duration *= 60;
2217                         }
2218
2219                         pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
2220                         u_reset_time = account_policy_temp * 60;
2221
2222                         pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
2223                         lockout = account_policy_temp;
2224
2225                         /* !AS ROOT */
2226                         
2227                         unbecome_root();
2228
2229                         unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
2230                         unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
2231         
2232                         init_unk_info12(&ctr->info.inf12, nt_lock_duration, nt_reset_time, (uint16)lockout);
2233                         break;
2234                 default:
2235                         return NT_STATUS_INVALID_INFO_CLASS;
2236                 }
2237         
2238
2239         init_samr_r_query_dom_info(r_u, q_u->switch_value, ctr, NT_STATUS_OK);
2240         
2241         DEBUG(5,("_samr_query_dom_info: %d\n", __LINE__));
2242         
2243         return r_u->status;
2244 }
2245
2246 /*******************************************************************
2247  _samr_create_user
2248  Create an account, can be either a normal user or a machine.
2249  This funcion will need to be updated for bdc/domain trusts.
2250  ********************************************************************/
2251
2252 NTSTATUS _samr_create_user(pipes_struct *p, SAMR_Q_CREATE_USER *q_u, SAMR_R_CREATE_USER *r_u)
2253 {
2254         SAM_ACCOUNT *sam_pass=NULL;
2255         fstring account;
2256         DOM_SID sid;
2257         pstring add_script;
2258         POLICY_HND dom_pol = q_u->domain_pol;
2259         UNISTR2 user_account = q_u->uni_name;
2260         uint16 acb_info = q_u->acb_info;
2261         POLICY_HND *user_pol = &r_u->user_pol;
2262         struct samr_info *info = NULL;
2263         BOOL ret;
2264         NTSTATUS nt_status;
2265         struct passwd *pw;
2266         uint32 acc_granted;
2267         SEC_DESC *psd;
2268         size_t    sd_size;
2269         uint32 new_rid = 0;
2270         /* check this, when giving away 'add computer to domain' privs */
2271         uint32    des_access = GENERIC_RIGHTS_USER_ALL_ACCESS;
2272         BOOL can_add_account = False;
2273         SE_PRIV se_rights;
2274         DISP_INFO *disp_info = NULL;
2275
2276         /* Get the domain SID stored in the domain policy */
2277         if (!get_lsa_policy_samr_sid(p, &dom_pol, &sid, &acc_granted, &disp_info))
2278                 return NT_STATUS_INVALID_HANDLE;
2279
2280         if (!NT_STATUS_IS_OK(nt_status = access_check_samr_function(acc_granted, SA_RIGHT_DOMAIN_CREATE_USER, "_samr_create_user"))) {
2281                 return nt_status;
2282         }
2283
2284         if (!(acb_info == ACB_NORMAL || acb_info == ACB_DOMTRUST || acb_info == ACB_WSTRUST || acb_info == ACB_SVRTRUST)) { 
2285                 /* Match Win2k, and return NT_STATUS_INVALID_PARAMETER if 
2286                    this parameter is not an account type */
2287                 return NT_STATUS_INVALID_PARAMETER;
2288         }
2289
2290         rpcstr_pull(account, user_account.buffer, sizeof(account), user_account.uni_str_len*2, 0);
2291         strlower_m(account);
2292
2293         pdb_init_sam(&sam_pass);
2294
2295         become_root();
2296         ret = pdb_getsampwnam(sam_pass, account);
2297         unbecome_root();
2298         if (ret == True) {
2299                 /* this account exists: say so */
2300                 pdb_free_sam(&sam_pass);
2301                 return NT_STATUS_USER_EXISTS;
2302         }
2303
2304         pdb_free_sam(&sam_pass);
2305
2306         /*********************************************************************
2307          * HEADS UP!  If we have to create a new user account, we have to get 
2308          * a new RID from somewhere.  This used to be done by the passdb 
2309          * backend. It has been moved into idmap now.  Since idmap is now 
2310          * wrapped up behind winbind, this means you have to run winbindd if you
2311          * want new accounts to get a new RID when "enable rid algorithm = no".
2312          * Tough.  We now have a uniform way of allocating RIDs regardless
2313          * of what ever passdb backend people may use.
2314          *                                             --jerry (2003-07-10)
2315          *********************************************************************/
2316
2317         pw = Get_Pwnam(account);
2318
2319         /* determine which user right we need to check based on the acb_info */
2320         
2321         if ( acb_info & ACB_WSTRUST )
2322         {
2323                 pstrcpy(add_script, lp_addmachine_script());
2324                 se_priv_copy( &se_rights, &se_machine_account );
2325                 can_add_account = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
2326         } 
2327         /* usrmgr.exe (and net rpc trustdom grant) creates a normal user 
2328            account for domain trusts and changes the ACB flags later */
2329         else if ( acb_info & ACB_NORMAL && (account[strlen(account)-1] != '$') )
2330         {
2331                 pstrcpy(add_script, lp_adduser_script());
2332                 se_priv_copy( &se_rights, &se_add_users );
2333                 can_add_account = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
2334         } 
2335         else    /* implicit assumption of a BDC or domain trust account here (we already check the flags earlier) */
2336         {
2337                 pstrcpy(add_script, lp_addmachine_script());
2338                 if ( lp_enable_privileges() ) {
2339                         /* only Domain Admins can add a BDC or domain trust */
2340                         se_priv_copy( &se_rights, &se_priv_none );
2341                         can_add_account = nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS );
2342         }
2343         }
2344                 
2345         DEBUG(5, ("_samr_create_user: %s can add this account : %s\n",
2346                 p->pipe_user_name, can_add_account ? "True":"False" ));
2347                 
2348         /********** BEGIN Admin BLOCK **********/
2349
2350         if ( can_add_account )
2351                 become_root();
2352
2353         if ( !pw ) {
2354                 if (*add_script) {
2355                         int add_ret;
2356
2357                         all_string_sub(add_script, "%u", account, sizeof(add_script));
2358                         add_ret = smbrun(add_script,NULL);
2359                         DEBUG(add_ret ? 0 : 3,("_samr_create_user: Running the command `%s' gave %d\n", add_script, add_ret));
2360                 }
2361         }
2362
2363         /* implicit call to getpwnam() next.  we have a valid SID coming out of this call */
2364
2365         flush_pwnam_cache();
2366         nt_status = pdb_init_sam_new(&sam_pass, account, new_rid);
2367
2368         /* this code is order such that we have no unnecessary retuns 
2369            out of the admin block of code */    
2370            
2371         if ( NT_STATUS_IS_OK(nt_status) ) {
2372                 pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
2373         
2374                 if ( !(ret = pdb_add_sam_account(sam_pass)) ) {
2375                         pdb_free_sam(&sam_pass);
2376                         DEBUG(0, ("could not add user/computer %s to passdb.  Check permissions?\n", 
2377                                 account));
2378                         nt_status = NT_STATUS_ACCESS_DENIED;
2379                 }
2380         }
2381                 
2382         if ( can_add_account )
2383                 unbecome_root();
2384
2385         /********** END Admin BLOCK **********/
2386         
2387         /* now check for failure */
2388         
2389         if ( !NT_STATUS_IS_OK(nt_status) )
2390                 return nt_status;
2391                         
2392         /* Get the user's SID */
2393         
2394         sid_copy(&sid, pdb_get_user_sid(sam_pass));
2395         
2396         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
2397         se_map_generic(&des_access, &usr_generic_mapping);
2398         
2399         nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
2400                 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access, 
2401                 &acc_granted, "_samr_create_user");
2402                 
2403         if ( !NT_STATUS_IS_OK(nt_status) ) {
2404                 return nt_status;
2405         }
2406
2407         /* associate the user's SID with the new handle. */
2408         if ((info = get_samr_info_by_sid(&sid)) == NULL) {
2409                 pdb_free_sam(&sam_pass);
2410                 return NT_STATUS_NO_MEMORY;
2411         }
2412
2413         ZERO_STRUCTP(info);
2414         info->sid = sid;
2415         info->acc_granted = acc_granted;
2416
2417         /* get a (unique) handle.  open a policy on it. */
2418         if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info)) {
2419                 pdb_free_sam(&sam_pass);
2420                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2421         }
2422
2423         /* After a "set" ensure we have no cached display info. */
2424         force_flush_samr_cache(info->disp_info);
2425
2426         r_u->user_rid=pdb_get_user_rid(sam_pass);
2427
2428         r_u->access_granted = acc_granted;
2429
2430         pdb_free_sam(&sam_pass);
2431
2432         return NT_STATUS_OK;
2433 }
2434
2435 /*******************************************************************
2436  samr_reply_connect_anon
2437  ********************************************************************/
2438
2439 NTSTATUS _samr_connect_anon(pipes_struct *p, SAMR_Q_CONNECT_ANON *q_u, SAMR_R_CONNECT_ANON *r_u)
2440 {
2441         struct samr_info *info = NULL;
2442         uint32    des_access = q_u->access_mask;
2443
2444         /* Access check */
2445
2446         if (!pipe_access_check(p)) {
2447                 DEBUG(3, ("access denied to samr_connect_anon\n"));
2448                 r_u->status = NT_STATUS_ACCESS_DENIED;
2449                 return r_u->status;
2450         }
2451
2452         /* set up the SAMR connect_anon response */
2453
2454         r_u->status = NT_STATUS_OK;
2455
2456         /* associate the user's SID with the new handle. */
2457         if ((info = get_samr_info_by_sid(NULL)) == NULL)
2458                 return NT_STATUS_NO_MEMORY;
2459
2460         /* don't give away the farm but this is probably ok.  The SA_RIGHT_SAM_ENUM_DOMAINS
2461            was observed from a win98 client trying to enumerate users (when configured  
2462            user level access control on shares)   --jerry */
2463            
2464         se_map_generic( &des_access, &sam_generic_mapping );
2465         info->acc_granted = des_access & (SA_RIGHT_SAM_ENUM_DOMAINS|SA_RIGHT_SAM_OPEN_DOMAIN);
2466         
2467         info->status = q_u->unknown_0;
2468
2469         /* get a (unique) handle.  open a policy on it. */
2470         if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2471                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2472
2473         return r_u->status;
2474 }
2475
2476 /*******************************************************************
2477  samr_reply_connect
2478  ********************************************************************/
2479
2480 NTSTATUS _samr_connect(pipes_struct *p, SAMR_Q_CONNECT *q_u, SAMR_R_CONNECT *r_u)
2481 {
2482         struct samr_info *info = NULL;
2483         SEC_DESC *psd = NULL;
2484         uint32    acc_granted;
2485         uint32    des_access = q_u->access_mask;
2486         NTSTATUS  nt_status;
2487         size_t    sd_size;
2488
2489
2490         DEBUG(5,("_samr_connect: %d\n", __LINE__));
2491
2492         /* Access check */
2493
2494         if (!pipe_access_check(p)) {
2495                 DEBUG(3, ("access denied to samr_connect\n"));
2496                 r_u->status = NT_STATUS_ACCESS_DENIED;
2497                 return r_u->status;
2498         }
2499
2500         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2501         se_map_generic(&des_access, &sam_generic_mapping);
2502         
2503         nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
2504                 NULL, 0, des_access, &acc_granted, "_samr_connect");
2505         
2506         if ( !NT_STATUS_IS_OK(nt_status) ) 
2507                 return nt_status;
2508
2509         r_u->status = NT_STATUS_OK;
2510
2511         /* associate the user's SID and access granted with the new handle. */
2512         if ((info = get_samr_info_by_sid(NULL)) == NULL)
2513                 return NT_STATUS_NO_MEMORY;
2514
2515         info->acc_granted = acc_granted;
2516         info->status = q_u->access_mask;
2517
2518         /* get a (unique) handle.  open a policy on it. */
2519         if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2520                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2521
2522         DEBUG(5,("_samr_connect: %d\n", __LINE__));
2523
2524         return r_u->status;
2525 }
2526
2527 /*******************************************************************
2528  samr_connect4
2529  ********************************************************************/
2530
2531 NTSTATUS _samr_connect4(pipes_struct *p, SAMR_Q_CONNECT4 *q_u, SAMR_R_CONNECT4 *r_u)
2532 {
2533         struct samr_info *info = NULL;
2534         SEC_DESC *psd = NULL;
2535         uint32    acc_granted;
2536         uint32    des_access = q_u->access_mask;
2537         NTSTATUS  nt_status;
2538         size_t    sd_size;
2539
2540
2541         DEBUG(5,("_samr_connect4: %d\n", __LINE__));
2542
2543         /* Access check */
2544
2545         if (!pipe_access_check(p)) {
2546                 DEBUG(3, ("access denied to samr_connect4\n"));
2547                 r_u->status = NT_STATUS_ACCESS_DENIED;
2548                 return r_u->status;
2549         }
2550
2551         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2552         se_map_generic(&des_access, &sam_generic_mapping);
2553         
2554         nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
2555                 NULL, 0, des_access, &acc_granted, "_samr_connect4");
2556         
2557         if ( !NT_STATUS_IS_OK(nt_status) ) 
2558                 return nt_status;
2559
2560         r_u->status = NT_STATUS_OK;
2561
2562         /* associate the user's SID and access granted with the new handle. */
2563         if ((info = get_samr_info_by_sid(NULL)) == NULL)
2564                 return NT_STATUS_NO_MEMORY;
2565
2566         info->acc_granted = acc_granted;
2567         info->status = q_u->access_mask;
2568
2569         /* get a (unique) handle.  open a policy on it. */
2570         if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2571                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2572
2573         DEBUG(5,("_samr_connect: %d\n", __LINE__));
2574
2575         return r_u->status;
2576 }
2577
2578 /*******************************************************************
2579  samr_connect5
2580  ********************************************************************/
2581
2582 NTSTATUS _samr_connect5(pipes_struct *p, SAMR_Q_CONNECT5 *q_u, SAMR_R_CONNECT5 *r_u)
2583 {
2584         struct samr_info *info = NULL;
2585         SEC_DESC *psd = NULL;
2586         uint32    acc_granted;
2587         uint32    des_access = q_u->access_mask;
2588         NTSTATUS  nt_status;
2589         POLICY_HND pol;
2590         size_t    sd_size;
2591
2592
2593         DEBUG(5,("_samr_connect5: %d\n", __LINE__));
2594
2595         ZERO_STRUCTP(r_u);
2596
2597         /* Access check */
2598
2599         if (!pipe_access_check(p)) {
2600                 DEBUG(3, ("access denied to samr_connect5\n"));
2601                 r_u->status = NT_STATUS_ACCESS_DENIED;
2602                 return r_u->status;
2603         }
2604
2605         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2606         se_map_generic(&des_access, &sam_generic_mapping);
2607         
2608         nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
2609                 NULL, 0, des_access, &acc_granted, "_samr_connect5");
2610         
2611         if ( !NT_STATUS_IS_OK(nt_status) ) 
2612                 return nt_status;
2613
2614         /* associate the user's SID and access granted with the new handle. */
2615         if ((info = get_samr_info_by_sid(NULL)) == NULL)
2616                 return NT_STATUS_NO_MEMORY;
2617
2618         info->acc_granted = acc_granted;
2619         info->status = q_u->access_mask;
2620
2621         /* get a (unique) handle.  open a policy on it. */
2622         if (!create_policy_hnd(p, &pol, free_samr_info, (void *)info))
2623                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2624
2625         DEBUG(5,("_samr_connect: %d\n", __LINE__));
2626
2627         init_samr_r_connect5(r_u, &pol, NT_STATUS_OK);
2628
2629         return r_u->status;
2630 }
2631
2632 /**********************************************************************
2633  api_samr_lookup_domain
2634  **********************************************************************/
2635
2636 NTSTATUS _samr_lookup_domain(pipes_struct *p, SAMR_Q_LOOKUP_DOMAIN *q_u, SAMR_R_LOOKUP_DOMAIN *r_u)
2637 {
2638         struct samr_info *info;
2639         fstring domain_name;
2640         DOM_SID sid;
2641
2642         r_u->status = NT_STATUS_OK;
2643
2644         if (!find_policy_by_hnd(p, &q_u->connect_pol, (void**)&info))
2645                 return NT_STATUS_INVALID_HANDLE;
2646
2647         /* win9x user manager likes to use SA_RIGHT_SAM_ENUM_DOMAINS here.  
2648            Reverted that change so we will work with RAS servers again */
2649
2650         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted, 
2651                 SA_RIGHT_SAM_OPEN_DOMAIN, "_samr_lookup_domain"))) 
2652         {
2653                 return r_u->status;
2654         }
2655
2656         rpcstr_pull(domain_name, q_u->uni_domain.buffer, sizeof(domain_name), q_u->uni_domain.uni_str_len*2, 0);
2657
2658         ZERO_STRUCT(sid);
2659
2660         if (!secrets_fetch_domain_sid(domain_name, &sid)) {
2661                 r_u->status = NT_STATUS_NO_SUCH_DOMAIN;
2662         }
2663
2664         DEBUG(2,("Returning domain sid for domain %s -> %s\n", domain_name, sid_string_static(&sid)));
2665
2666         init_samr_r_lookup_domain(r_u, &sid, r_u->status);
2667
2668         return r_u->status;
2669 }
2670
2671 /******************************************************************
2672 makes a SAMR_R_ENUM_DOMAINS structure.
2673 ********************************************************************/
2674
2675 static BOOL make_enum_domains(TALLOC_CTX *ctx, SAM_ENTRY **pp_sam,
2676                         UNISTR2 **pp_uni_name, uint32 num_sam_entries, fstring doms[])
2677 {
2678         uint32 i;
2679         SAM_ENTRY *sam;
2680         UNISTR2 *uni_name;
2681
2682         DEBUG(5, ("make_enum_domains\n"));
2683
2684         *pp_sam = NULL;
2685         *pp_uni_name = NULL;
2686
2687         if (num_sam_entries == 0)
2688                 return True;
2689
2690         sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_sam_entries);
2691         uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_sam_entries);
2692
2693         if (sam == NULL || uni_name == NULL)
2694                 return False;
2695
2696         for (i = 0; i < num_sam_entries; i++) {
2697                 init_unistr2(&uni_name[i], doms[i], UNI_FLAGS_NONE);
2698                 init_sam_entry(&sam[i], &uni_name[i], 0);
2699         }
2700
2701         *pp_sam = sam;
2702         *pp_uni_name = uni_name;
2703
2704         return True;
2705 }
2706
2707 /**********************************************************************
2708  api_samr_enum_domains
2709  **********************************************************************/
2710
2711 NTSTATUS _samr_enum_domains(pipes_struct *p, SAMR_Q_ENUM_DOMAINS *q_u, SAMR_R_ENUM_DOMAINS *r_u)
2712 {
2713         struct samr_info *info;
2714         uint32 num_entries = 2;
2715         fstring dom[2];
2716         const char *name;
2717
2718         r_u->status = NT_STATUS_OK;
2719         
2720         if (!find_policy_by_hnd(p, &q_u->pol, (void**)&info))
2721                 return NT_STATUS_INVALID_HANDLE;
2722         
2723         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted, SA_RIGHT_SAM_ENUM_DOMAINS, "_samr_enum_domains"))) {
2724                 return r_u->status;
2725         }
2726
2727         name = get_global_sam_name();
2728
2729         fstrcpy(dom[0],name);
2730         strupper_m(dom[0]);
2731         fstrcpy(dom[1],"Builtin");
2732
2733         if (!make_enum_domains(p->mem_ctx, &r_u->sam, &r_u->uni_dom_name, num_entries, dom))
2734                 return NT_STATUS_NO_MEMORY;
2735
2736         init_samr_r_enum_domains(r_u, q_u->start_idx + num_entries, num_entries);
2737
2738         return r_u->status;
2739 }
2740
2741 /*******************************************************************
2742  api_samr_open_alias
2743  ********************************************************************/
2744
2745 NTSTATUS _samr_open_alias(pipes_struct *p, SAMR_Q_OPEN_ALIAS *q_u, SAMR_R_OPEN_ALIAS *r_u)
2746 {
2747         DOM_SID sid;
2748         POLICY_HND domain_pol = q_u->dom_pol;
2749         uint32 alias_rid = q_u->rid_alias;
2750         POLICY_HND *alias_pol = &r_u->pol;
2751         struct    samr_info *info = NULL;
2752         SEC_DESC *psd = NULL;
2753         uint32    acc_granted;
2754         uint32    des_access = q_u->access_mask;
2755         size_t    sd_size;
2756         NTSTATUS  status;
2757         SE_PRIV se_rights;
2758
2759         r_u->status = NT_STATUS_OK;
2760
2761         /* find the domain policy and get the SID / access bits stored in the domain policy */
2762         
2763         if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
2764                 return NT_STATUS_INVALID_HANDLE;
2765         
2766         status = access_check_samr_function(acc_granted, 
2767                 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_alias");
2768                 
2769         if ( !NT_STATUS_IS_OK(status) ) 
2770                 return status;
2771
2772         /* append the alias' RID to it */
2773         
2774         if (!sid_append_rid(&sid, alias_rid))
2775                 return NT_STATUS_NO_SUCH_USER;
2776                 
2777         /*check if access can be granted as requested by client. */
2778         
2779         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &ali_generic_mapping, NULL, 0);
2780         se_map_generic(&des_access,&ali_generic_mapping);
2781         
2782         se_priv_copy( &se_rights, &se_add_users );
2783         
2784         
2785         status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
2786                 &se_rights, GENERIC_RIGHTS_ALIAS_WRITE, des_access, 
2787                 &acc_granted, "_samr_open_alias");
2788                 
2789         if ( !NT_STATUS_IS_OK(status) )
2790                 return status;
2791
2792         /*
2793          * we should check if the rid really exist !!!
2794          * JFM.
2795          */
2796
2797         /* associate the user's SID with the new handle. */
2798         if ((info = get_samr_info_by_sid(&sid)) == NULL)
2799                 return NT_STATUS_NO_MEMORY;
2800                 
2801         info->acc_granted = acc_granted;
2802
2803         /* get a (unique) handle.  open a policy on it. */
2804         if (!create_policy_hnd(p, alias_pol, free_samr_info, (void *)info))
2805                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2806
2807         return r_u->status;
2808 }
2809
2810 /*******************************************************************
2811  set_user_info_7
2812  ********************************************************************/
2813 static NTSTATUS set_user_info_7(const SAM_USER_INFO_7 *id7, SAM_ACCOUNT *pwd)
2814 {
2815         fstring new_name;
2816         SAM_ACCOUNT *check_acct = NULL;
2817         NTSTATUS rc;
2818         BOOL check_rc;
2819
2820         if (id7 == NULL) {
2821                 DEBUG(5, ("set_user_info_7: NULL id7\n"));
2822                 pdb_free_sam(&pwd);
2823                 return NT_STATUS_ACCESS_DENIED;
2824         }
2825
2826         if(!rpcstr_pull(new_name, id7->uni_name.buffer, sizeof(new_name), id7->uni_name.uni_str_len*2, 0)) {
2827                 DEBUG(5, ("set_user_info_7: failed to get new username\n"));
2828                 pdb_free_sam(&pwd);
2829                 return NT_STATUS_ACCESS_DENIED;
2830         }
2831
2832         /* check to see if the new username already exists.  Note: we can't
2833            reliably lock all backends, so there is potentially the 
2834            possibility that a user can be created in between this check and
2835            the rename.  The rename should fail, but may not get the
2836            exact same failure status code.  I think this is small enough
2837            of a window for this type of operation and the results are
2838            simply that the rename fails with a slightly different status
2839            code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
2840
2841         pdb_init_sam(&check_acct);
2842         check_rc = pdb_getsampwnam(check_acct, new_name);
2843         pdb_free_sam(&check_acct);
2844
2845         if (check_rc == True) {
2846                 /* this account exists: say so */
2847                 return NT_STATUS_USER_EXISTS;
2848         }
2849
2850         rc = pdb_rename_sam_account(pwd, new_name);
2851
2852         pdb_free_sam(&pwd);
2853         return rc;
2854 }
2855
2856 /*******************************************************************
2857  set_user_info_16
2858  ********************************************************************/
2859
2860 static BOOL set_user_info_16(const SAM_USER_INFO_16 *id16, SAM_ACCOUNT *pwd)
2861 {
2862         if (id16 == NULL) {
2863                 DEBUG(5, ("set_user_info_16: NULL id16\n"));
2864                 pdb_free_sam(&pwd);
2865                 return False;
2866         }
2867         
2868         /* FIX ME: check if the value is really changed --metze */
2869         if (!pdb_set_acct_ctrl(pwd, id16->acb_info, PDB_CHANGED)) {
2870                 pdb_free_sam(&pwd);
2871                 return False;
2872         }
2873
2874         if(!pdb_update_sam_account(pwd)) {
2875                 pdb_free_sam(&pwd);
2876                 return False;
2877         }
2878
2879         pdb_free_sam(&pwd);
2880
2881         return True;
2882 }
2883
2884 /*******************************************************************
2885  set_user_info_18
2886  ********************************************************************/
2887
2888 static BOOL set_user_info_18(SAM_USER_INFO_18 *id18, SAM_ACCOUNT *pwd)
2889 {
2890
2891         if (id18 == NULL) {
2892                 DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
2893                 pdb_free_sam(&pwd);
2894                 return False;
2895         }
2896  
2897         if (!pdb_set_lanman_passwd (pwd, id18->lm_pwd, PDB_CHANGED)) {
2898                 pdb_free_sam(&pwd);
2899                 return False;
2900         }
2901         if (!pdb_set_nt_passwd     (pwd, id18->nt_pwd, PDB_CHANGED)) {
2902                 pdb_free_sam(&pwd);
2903                 return False;
2904         }
2905         if (!pdb_set_pass_changed_now (pwd)) {
2906                 pdb_free_sam(&pwd);
2907                 return False; 
2908         }
2909  
2910         if(!pdb_update_sam_account(pwd)) {
2911                 pdb_free_sam(&pwd);
2912                 return False;
2913         }
2914
2915         pdb_free_sam(&pwd);
2916         return True;
2917 }
2918
2919 /*******************************************************************
2920  The GROUPSID field in the SAM_ACCOUNT changed. Try to tell unix.
2921  ********************************************************************/
2922 static BOOL set_unix_primary_group(SAM_ACCOUNT *sampass)
2923 {
2924         struct group *grp;
2925         gid_t gid;
2926
2927         if (!NT_STATUS_IS_OK(sid_to_gid(pdb_get_group_sid(sampass),
2928                                         &gid))) {
2929                 DEBUG(2,("Could not get gid for primary group of "
2930                          "user %s\n", pdb_get_username(sampass)));
2931                 return False;
2932         }
2933
2934         grp = getgrgid(gid);
2935
2936         if (grp == NULL) {
2937                 DEBUG(2,("Could not find primary group %lu for "
2938                          "user %s\n", (unsigned long)gid, 
2939                          pdb_get_username(sampass)));
2940                 return False;
2941         }
2942
2943         if (smb_set_primary_group(grp->gr_name,
2944                                   pdb_get_username(sampass)) != 0) {
2945                 DEBUG(2,("Could not set primary group for user %s to "
2946                          "%s\n",
2947                          pdb_get_username(sampass), grp->gr_name));
2948                 return False;
2949         }
2950
2951         return True;
2952 }
2953         
2954
2955 /*******************************************************************
2956  set_user_info_20
2957  ********************************************************************/
2958
2959 static BOOL set_user_info_20(SAM_USER_INFO_20 *id20, SAM_ACCOUNT *pwd)
2960 {
2961         if (id20 == NULL) {
2962                 DEBUG(5, ("set_user_info_20: NULL id20\n"));
2963                 return False;
2964         }
2965  
2966         copy_id20_to_sam_passwd(pwd, id20);
2967
2968         /* write the change out */
2969         if(!pdb_update_sam_account(pwd)) {
2970                 pdb_free_sam(&pwd);
2971                 return False;
2972         }
2973
2974         pdb_free_sam(&pwd);
2975
2976         return True;
2977 }
2978 /*******************************************************************
2979  set_user_info_21
2980  ********************************************************************/
2981
2982 static BOOL set_user_info_21(SAM_USER_INFO_21 *id21, SAM_ACCOUNT *pwd)
2983 {
2984  
2985         if (id21 == NULL) {
2986                 DEBUG(5, ("set_user_info_21: NULL id21\n"));
2987                 return False;
2988         }
2989  
2990         copy_id21_to_sam_passwd(pwd, id21);
2991  
2992         /*
2993          * The funny part about the previous two calls is
2994          * that pwd still has the password hashes from the
2995          * passdb entry.  These have not been updated from
2996          * id21.  I don't know if they need to be set.    --jerry
2997          */
2998  
2999         if (IS_SAM_CHANGED(pwd, PDB_GROUPSID))
3000                 set_unix_primary_group(pwd);
3001
3002         /* write the change out */
3003         if(!pdb_update_sam_account(pwd)) {
3004                 pdb_free_sam(&pwd);
3005                 return False;
3006         }
3007
3008         pdb_free_sam(&pwd);
3009
3010         return True;
3011 }
3012
3013 /*******************************************************************
3014  set_user_info_23
3015  ********************************************************************/
3016
3017 static BOOL set_user_info_23(SAM_USER_INFO_23 *id23, SAM_ACCOUNT *pwd)
3018 {
3019         pstring plaintext_buf;
3020         uint32 len;
3021         uint16 acct_ctrl;
3022  
3023         if (id23 == NULL) {
3024                 DEBUG(5, ("set_user_info_23: NULL id23\n"));
3025                 return False;
3026         }
3027  
3028         DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
3029                   pdb_get_username(pwd)));
3030
3031         acct_ctrl = pdb_get_acct_ctrl(pwd);
3032
3033         if (!decode_pw_buffer(id23->pass, plaintext_buf, 256, &len, STR_UNICODE)) {
3034                 pdb_free_sam(&pwd);
3035                 return False;
3036         }
3037   
3038         if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
3039                 pdb_free_sam(&pwd);
3040                 return False;
3041         }
3042  
3043         copy_id23_to_sam_passwd(pwd, id23);
3044  
3045         /* if it's a trust account, don't update /etc/passwd */
3046         if (    ( (acct_ctrl &  ACB_DOMTRUST) == ACB_DOMTRUST ) ||
3047                 ( (acct_ctrl &  ACB_WSTRUST) ==  ACB_WSTRUST) ||
3048                 ( (acct_ctrl &  ACB_SVRTRUST) ==  ACB_SVRTRUST) ) {
3049                 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
3050         } else  {
3051                 /* update the UNIX password */
3052                 if (lp_unix_password_sync() ) {
3053                         struct passwd *passwd = Get_Pwnam(pdb_get_username(pwd));
3054                         if (!passwd) {
3055                                 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
3056                         }
3057                         
3058                         if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
3059                                 pdb_free_sam(&pwd);
3060                                 return False;
3061                         }
3062                 }
3063         }
3064  
3065         ZERO_STRUCT(plaintext_buf);
3066  
3067         if (IS_SAM_CHANGED(pwd, PDB_GROUPSID))
3068                 set_unix_primary_group(pwd);
3069
3070         if(!pdb_update_sam_account(pwd)) {
3071                 pdb_free_sam(&pwd);
3072                 return False;
3073         }
3074  
3075         pdb_free_sam(&pwd);
3076
3077         return True;
3078 }
3079
3080 /*******************************************************************
3081  set_user_info_pw
3082  ********************************************************************/
3083
3084 static BOOL set_user_info_pw(uint8 *pass, SAM_ACCOUNT *pwd)
3085 {
3086         uint32 len;
3087         pstring plaintext_buf;
3088         uint16 acct_ctrl;
3089  
3090         DEBUG(5, ("Attempting administrator password change for user %s\n",
3091                   pdb_get_username(pwd)));
3092
3093         acct_ctrl = pdb_get_acct_ctrl(pwd);
3094
3095         ZERO_STRUCT(plaintext_buf);
3096  
3097         if (!decode_pw_buffer(pass, plaintext_buf, 256, &len, STR_UNICODE)) {
3098                 pdb_free_sam(&pwd);
3099                 return False;
3100         }
3101
3102         if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
3103                 pdb_free_sam(&pwd);
3104                 return False;
3105         }
3106  
3107         /* if it's a trust account, don't update /etc/passwd */
3108         if ( ( (acct_ctrl &  ACB_DOMTRUST) == ACB_DOMTRUST ) ||
3109                 ( (acct_ctrl &  ACB_WSTRUST) ==  ACB_WSTRUST) ||
3110                 ( (acct_ctrl &  ACB_SVRTRUST) ==  ACB_SVRTRUST) ) {
3111                 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
3112         } else {
3113                 /* update the UNIX password */
3114                 if (lp_unix_password_sync()) {
3115                         struct passwd *passwd = Get_Pwnam(pdb_get_username(pwd));
3116                         if (!passwd) {
3117                                 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
3118                         }
3119                         
3120                         if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
3121                                 pdb_free_sam(&pwd);
3122                                 return False;
3123                         }
3124                 }
3125         }
3126  
3127         ZERO_STRUCT(plaintext_buf);
3128  
3129         DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
3130  
3131         /* update the SAMBA password */
3132         if(!pdb_update_sam_account(pwd)) {
3133                 pdb_free_sam(&pwd);
3134                 return False;
3135         }
3136
3137         pdb_free_sam(&pwd);
3138
3139         return True;
3140 }
3141
3142 /*******************************************************************
3143  samr_reply_set_userinfo
3144  ********************************************************************/
3145
3146 NTSTATUS _samr_set_userinfo(pipes_struct *p, SAMR_Q_SET_USERINFO *q_u, SAMR_R_SET_USERINFO *r_u)
3147 {
3148         SAM_ACCOUNT *pwd = NULL;
3149         DOM_SID sid;
3150         POLICY_HND *pol = &q_u->pol;
3151         uint16 switch_value = q_u->switch_value;
3152         SAM_USERINFO_CTR *ctr = q_u->ctr;
3153         uint32 acc_granted;
3154         uint32 acc_required;
3155         BOOL ret;
3156         BOOL has_enough_rights = False;
3157         uint32 acb_info;
3158         DISP_INFO *disp_info = NULL;
3159
3160         DEBUG(5, ("_samr_set_userinfo: %d\n", __LINE__));
3161
3162         r_u->status = NT_STATUS_OK;
3163
3164         /* find the policy handle.  open a policy on it. */
3165         if (!get_lsa_policy_samr_sid(p, pol, &sid, &acc_granted, &disp_info))
3166                 return NT_STATUS_INVALID_HANDLE;
3167
3168         /* observed when joining an XP client to a Samba domain */
3169         
3170         acc_required = SA_RIGHT_USER_SET_PASSWORD | SA_RIGHT_USER_SET_ATTRIBUTES | SA_RIGHT_USER_ACCT_FLAGS_EXPIRY;     
3171
3172         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, acc_required, "_samr_set_userinfo"))) {
3173                 return r_u->status;
3174         }
3175
3176         DEBUG(5, ("_samr_set_userinfo: sid:%s, level:%d\n", sid_string_static(&sid), switch_value));
3177
3178         if (ctr == NULL) {
3179                 DEBUG(5, ("_samr_set_userinfo: NULL info level\n"));
3180                 return NT_STATUS_INVALID_INFO_CLASS;
3181         }
3182         
3183         pdb_init_sam(&pwd);     
3184         
3185         become_root();
3186         ret = pdb_getsampwsid(pwd, &sid);
3187         unbecome_root();
3188         
3189         if ( !ret ) {
3190                 pdb_free_sam(&pwd);
3191                 return NT_STATUS_NO_SUCH_USER;
3192         }
3193         
3194         /* deal with machine password changes differently from userinfo changes */
3195         /* check to see if we have the sufficient rights */
3196         
3197         acb_info = pdb_get_acct_ctrl(pwd);
3198         if ( acb_info & ACB_WSTRUST ) 
3199                 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_machine_account);
3200         else if ( acb_info & ACB_NORMAL )
3201                 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
3202         else if ( acb_info & (ACB_SVRTRUST|ACB_DOMTRUST) ) {
3203                 if ( lp_enable_privileges() )
3204                         has_enough_rights = nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS );
3205         }
3206         
3207         DEBUG(5, ("_samr_set_userinfo: %s does%s possess sufficient rights\n",
3208                 p->pipe_user_name, has_enough_rights ? "" : " not"));
3209
3210         /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
3211         
3212         if ( has_enough_rights )                                
3213                 become_root(); 
3214         
3215         /* ok!  user info levels (lots: see MSDEV help), off we go... */
3216
3217         switch (switch_value) {
3218                 case 18:
3219                         if (!set_user_info_18(ctr->info.id18, pwd))
3220                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3221                         break;
3222
3223                 case 24:
3224                         if (!p->session_key.length) {
3225                                 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
3226                         }
3227                         SamOEMhashBlob(ctr->info.id24->pass, 516, &p->session_key);
3228
3229                         dump_data(100, (char *)ctr->info.id24->pass, 516);
3230
3231                         if (!set_user_info_pw(ctr->info.id24->pass, pwd))
3232                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3233                         break;
3234
3235                 case 25:
3236 #if 0
3237                         /*
3238                          * Currently we don't really know how to unmarshall
3239                          * the level 25 struct, and the password encryption
3240                          * is different. This is a placeholder for when we
3241                          * do understand it. In the meantime just return INVALID
3242                          * info level and W2K SP2 drops down to level 23... JRA.
3243                          */
3244
3245                         if (!p->session_key.length) {
3246                                 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
3247                         }
3248                         SamOEMhashBlob(ctr->info.id25->pass, 532, &p->session_key);
3249
3250                         dump_data(100, (char *)ctr->info.id25->pass, 532);
3251
3252                         if (!set_user_info_pw(ctr->info.id25->pass, &sid))
3253                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3254                         break;
3255 #endif
3256                         r_u->status = NT_STATUS_INVALID_INFO_CLASS;
3257                         break;
3258
3259                 case 23:
3260                         if (!p->session_key.length) {
3261                                 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
3262                         }
3263                         SamOEMhashBlob(ctr->info.id23->pass, 516, &p->session_key);
3264
3265                         dump_data(100, (char *)ctr->info.id23->pass, 516);
3266
3267                         if (!set_user_info_23(ctr->info.id23, pwd))
3268                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3269                         break;
3270
3271                 default:
3272                         r_u->status = NT_STATUS_INVALID_INFO_CLASS;
3273         }
3274
3275         
3276         if ( has_enough_rights )                                
3277                 unbecome_root();
3278                 
3279         /* ================ END SeMachineAccountPrivilege BLOCK ================ */
3280
3281         if (NT_STATUS_IS_OK(r_u->status)) {
3282                 force_flush_samr_cache(disp_info);
3283         }
3284
3285         return r_u->status;
3286 }
3287
3288 /*******************************************************************
3289  samr_reply_set_userinfo2
3290  ********************************************************************/
3291
3292 NTSTATUS _samr_set_userinfo2(pipes_struct *p, SAMR_Q_SET_USERINFO2 *q_u, SAMR_R_SET_USERINFO2 *r_u)
3293 {
3294         SAM_ACCOUNT *pwd = NULL;
3295         DOM_SID sid;
3296         SAM_USERINFO_CTR *ctr = q_u->ctr;
3297         POLICY_HND *pol = &q_u->pol;
3298         uint16 switch_value = q_u->switch_value;
3299         uint32 acc_granted;
3300         uint32 acc_required;
3301         BOOL ret;
3302         BOOL has_enough_rights = False;
3303         uint32 acb_info;
3304         DISP_INFO *disp_info = NULL;
3305
3306         DEBUG(5, ("samr_reply_set_userinfo2: %d\n", __LINE__));
3307
3308         r_u->status = NT_STATUS_OK;
3309
3310         /* find the policy handle.  open a policy on it. */
3311         if (!get_lsa_policy_samr_sid(p, pol, &sid, &acc_granted, &disp_info))
3312                 return NT_STATUS_INVALID_HANDLE;
3313
3314         /* observed when joining XP client to Samba domain */
3315                 
3316         acc_required = SA_RIGHT_USER_SET_PASSWORD | SA_RIGHT_USER_SET_ATTRIBUTES | SA_RIGHT_USER_ACCT_FLAGS_EXPIRY;
3317         
3318         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, acc_required, "_samr_set_userinfo2"))) {
3319                 return r_u->status;
3320         }
3321
3322         DEBUG(5, ("samr_reply_set_userinfo2: sid:%s\n", sid_string_static(&sid)));
3323
3324         if (ctr == NULL) {
3325                 DEBUG(5, ("samr_reply_set_userinfo2: NULL info level\n"));
3326                 return NT_STATUS_INVALID_INFO_CLASS;
3327         }
3328
3329         switch_value=ctr->switch_value;
3330
3331         pdb_init_sam(&pwd);     
3332         
3333         become_root();
3334         ret = pdb_getsampwsid(pwd, &sid);
3335         unbecome_root();
3336         
3337         if ( !ret ) {
3338                 pdb_free_sam(&pwd);
3339                 return NT_STATUS_NO_SUCH_USER;
3340         }
3341         
3342         acb_info = pdb_get_acct_ctrl(pwd);
3343         if ( acb_info & ACB_WSTRUST ) 
3344                 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_machine_account);
3345         else if ( acb_info & ACB_NORMAL )
3346                 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
3347         else if ( acb_info & (ACB_SVRTRUST|ACB_DOMTRUST) ) {
3348                 if ( lp_enable_privileges() )
3349                         has_enough_rights = nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS );
3350         }
3351         
3352         DEBUG(5, ("_samr_set_userinfo: %s does%s possess sufficient rights\n",
3353                 p->pipe_user_name, has_enough_rights ? "" : " not"));
3354
3355         /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
3356         
3357         if ( has_enough_rights )                                
3358                 become_root(); 
3359         
3360         /* ok!  user info levels (lots: see MSDEV help), off we go... */
3361         
3362         switch (switch_value) {
3363                 case 7:
3364                         r_u->status = set_user_info_7(ctr->info.id7, pwd);
3365                         break;
3366                 case 16:
3367                         if (!set_user_info_16(ctr->info.id16, pwd))
3368                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3369                         break;
3370                 case 18:
3371                         /* Used by AS/U JRA. */
3372                         if (!set_user_info_18(ctr->info.id18, pwd))
3373                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3374                         break;
3375                 case 20:
3376                         if (!set_user_info_20(ctr->info.id20, pwd))
3377                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3378                         break;
3379                 case 21:
3380                         if (!set_user_info_21(ctr->info.id21, pwd))
3381                                 return NT_STATUS_ACCESS_DENIED;
3382                         break;
3383                 default:
3384                         r_u->status = NT_STATUS_INVALID_INFO_CLASS;
3385         }
3386
3387         if ( has_enough_rights )                                
3388                 unbecome_root();
3389                 
3390         /* ================ END SeMachineAccountPrivilege BLOCK ================ */
3391
3392         if (NT_STATUS_IS_OK(r_u->status)) {
3393                 force_flush_samr_cache(disp_info);
3394         }
3395
3396         return r_u->status;
3397 }
3398
3399 /*********************************************************************
3400  _samr_query_aliasmem
3401 *********************************************************************/
3402
3403 NTSTATUS _samr_query_useraliases(pipes_struct *p, SAMR_Q_QUERY_USERALIASES *q_u, SAMR_R_QUERY_USERALIASES *r_u)
3404 {
3405         size_t num_alias_rids;
3406         uint32 *alias_rids;
3407         struct samr_info *info = NULL;
3408         size_t i;
3409                 
3410         NTSTATUS ntstatus1;
3411         NTSTATUS ntstatus2;
3412
3413         DOM_SID *members;
3414         BOOL res;
3415
3416         r_u->status = NT_STATUS_OK;
3417
3418         DEBUG(5,("_samr_query_useraliases: %d\n", __LINE__));
3419
3420         /* find the policy handle.  open a policy on it. */
3421         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
3422                 return NT_STATUS_INVALID_HANDLE;
3423                 
3424         ntstatus1 = access_check_samr_function(info->acc_granted, SA_RIGHT_DOMAIN_LOOKUP_ALIAS_BY_MEM, "_samr_query_useraliases");
3425         ntstatus2 = access_check_samr_function(info->acc_granted, SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_query_useraliases");
3426         
3427         if (!NT_STATUS_IS_OK(ntstatus1) || !NT_STATUS_IS_OK(ntstatus2)) {
3428                 if (!(NT_STATUS_EQUAL(ntstatus1,NT_STATUS_ACCESS_DENIED) && NT_STATUS_IS_OK(ntstatus2)) &&
3429                     !(NT_STATUS_EQUAL(ntstatus1,NT_STATUS_ACCESS_DENIED) && NT_STATUS_IS_OK(ntstatus1))) {
3430                         return (NT_STATUS_IS_OK(ntstatus1)) ? ntstatus2 : ntstatus1;
3431                 }
3432         }               
3433
3434         if (!sid_check_is_domain(&info->sid) &&
3435             !sid_check_is_builtin(&info->sid))
3436                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
3437
3438         members = TALLOC_ARRAY(p->mem_ctx, DOM_SID, q_u->num_sids1);
3439
3440         if (members == NULL)
3441                 return NT_STATUS_NO_MEMORY;
3442
3443         for (i=0; i<q_u->num_sids1; i++)
3444                 sid_copy(&members[i], &q_u->sid[i].sid);
3445
3446         alias_rids = NULL;
3447         num_alias_rids = 0;
3448
3449         become_root();
3450         res = pdb_enum_alias_memberships(p->mem_ctx, &info->sid, members,
3451                                          q_u->num_sids1,
3452                                          &alias_rids, &num_alias_rids);
3453         unbecome_root();
3454
3455         if (!res)
3456                 return NT_STATUS_UNSUCCESSFUL;
3457
3458         init_samr_r_query_useraliases(r_u, num_alias_rids, alias_rids,
3459                                       NT_STATUS_OK);
3460         return NT_STATUS_OK;
3461 }
3462
3463 /*********************************************************************
3464  _samr_query_aliasmem
3465 *********************************************************************/
3466
3467 NTSTATUS _samr_query_aliasmem(pipes_struct *p, SAMR_Q_QUERY_ALIASMEM *q_u, SAMR_R_QUERY_ALIASMEM *r_u)
3468 {
3469         size_t i;
3470         size_t num_sids = 0;
3471         DOM_SID2 *sid;
3472         DOM_SID *sids=NULL;
3473
3474         DOM_SID alias_sid;
3475
3476         uint32 acc_granted;
3477
3478         /* find the policy handle.  open a policy on it. */
3479         if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted, NULL)) 
3480                 return NT_STATUS_INVALID_HANDLE;
3481         
3482         if (!NT_STATUS_IS_OK(r_u->status = 
3483                 access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_GET_MEMBERS, "_samr_query_aliasmem"))) {
3484                 return r_u->status;
3485         }
3486
3487         DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
3488
3489         if (!pdb_enum_aliasmem(&alias_sid, &sids, &num_sids))
3490                 return NT_STATUS_NO_SUCH_ALIAS;
3491
3492         sid = TALLOC_ZERO_ARRAY(p->mem_ctx, DOM_SID2, num_sids);        
3493         if (num_sids!=0 && sid == NULL) {
3494                 SAFE_FREE(sids);
3495                 return NT_STATUS_NO_MEMORY;
3496         }
3497
3498         for (i = 0; i < num_sids; i++) {
3499                 init_dom_sid2(&sid[i], &sids[i]);
3500         }
3501
3502         init_samr_r_query_aliasmem(r_u, num_sids, sid, NT_STATUS_OK);
3503
3504         SAFE_FREE(sids);
3505
3506         return NT_STATUS_OK;
3507 }
3508
3509 static void add_uid_to_array_unique(uid_t uid, uid_t **uids, int *num)
3510 {
3511         int i;
3512
3513         for (i=0; i<*num; i++) {
3514                 if ((*uids)[i] == uid)
3515                         return;
3516         }
3517         
3518         *uids = SMB_REALLOC_ARRAY(*uids, uid_t, *num+1);
3519
3520         if (*uids == NULL)
3521                 return;
3522
3523         (*uids)[*num] = uid;
3524         *num += 1;
3525 }
3526
3527
3528 static BOOL get_memberuids(gid_t gid, uid_t **uids, int *num)
3529 {
3530         struct group *grp;
3531         char **gr;
3532         struct sys_pwent *userlist, *user;
3533  
3534         *uids = NULL;
3535         *num = 0;
3536
3537         /* We only look at our own sam, so don't care about imported stuff */
3538
3539         winbind_off();
3540
3541         if ((grp = getgrgid(gid)) == NULL) {
3542                 winbind_on();
3543                 return False;
3544         }
3545
3546         /* Primary group members */
3547
3548         userlist = getpwent_list();
3549
3550         for (user = userlist; user != NULL; user = user->next) {
3551                 if (user->pw_gid != gid)
3552                         continue;
3553                 add_uid_to_array_unique(user->pw_uid, uids, num);
3554         }
3555
3556         pwent_free(userlist);
3557
3558         /* Secondary group members */
3559
3560         for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
3561                 struct passwd *pw = getpwnam(*gr);
3562
3563                 if (pw == NULL)
3564                         continue;
3565                 add_uid_to_array_unique(pw->pw_uid, uids, num);
3566         }
3567
3568         winbind_on();
3569
3570         return True;
3571 }       
3572
3573 /*********************************************************************
3574  _samr_query_groupmem
3575 *********************************************************************/
3576
3577 NTSTATUS _samr_query_groupmem(pipes_struct *p, SAMR_Q_QUERY_GROUPMEM *q_u, SAMR_R_QUERY_GROUPMEM *r_u)
3578 {
3579         DOM_SID group_sid;
3580         fstring group_sid_str;
3581         size_t i, num_members;
3582
3583         uint32 *rid=NULL;
3584         uint32 *attr=NULL;
3585
3586         uint32 acc_granted;
3587
3588         NTSTATUS result;
3589
3590         /* find the policy handle.  open a policy on it. */
3591         if (!get_lsa_policy_samr_sid(p, &q_u->group_pol, &group_sid, &acc_granted, NULL)) 
3592                 return NT_STATUS_INVALID_HANDLE;
3593                 
3594         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_GET_MEMBERS, "_samr_query_groupmem"))) {
3595                 return r_u->status;
3596         }
3597                 
3598         sid_to_string(group_sid_str, &group_sid);
3599         DEBUG(10, ("sid is %s\n", group_sid_str));
3600
3601         if (!sid_check_is_in_our_domain(&group_sid)) {
3602                 DEBUG(3, ("sid %s is not in our domain\n", group_sid_str));
3603                 return NT_STATUS_NO_SUCH_GROUP;
3604         }
3605
3606         DEBUG(10, ("lookup on Domain SID\n"));
3607
3608         become_root();
3609         result = pdb_enum_group_members(p->mem_ctx, &group_sid,
3610                                         &rid, &num_members);
3611         unbecome_root();
3612
3613         if (!NT_STATUS_IS_OK(result))
3614                 return result;
3615
3616         attr=TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_members);
3617         
3618         if ((num_members!=0) && (rid==NULL))
3619                 return NT_STATUS_NO_MEMORY;
3620         
3621         for (i=0; i<num_members; i++)
3622                 attr[i] = SID_NAME_USER;
3623
3624         init_samr_r_query_groupmem(r_u, num_members, rid, attr, NT_STATUS_OK);
3625
3626         return NT_STATUS_OK;
3627 }
3628
3629 /*********************************************************************
3630  _samr_add_aliasmem
3631 *********************************************************************/
3632
3633 NTSTATUS _samr_add_aliasmem(pipes_struct *p, SAMR_Q_ADD_ALIASMEM *q_u, SAMR_R_ADD_ALIASMEM *r_u)
3634 {
3635         DOM_SID alias_sid;
3636         uint32 acc_granted;
3637         SE_PRIV se_rights;
3638         BOOL can_add_accounts;
3639         BOOL ret;
3640         DISP_INFO *disp_info = NULL;
3641
3642         /* Find the policy handle. Open a policy on it. */
3643         if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted, &disp_info)) 
3644                 return NT_STATUS_INVALID_HANDLE;
3645         
3646         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_ADD_MEMBER, "_samr_add_aliasmem"))) {
3647                 return r_u->status;
3648         }
3649                 
3650         DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
3651         
3652         se_priv_copy( &se_rights, &se_add_users );
3653         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3654
3655         /******** BEGIN SeAddUsers BLOCK *********/
3656         
3657         if ( can_add_accounts )
3658                 become_root();
3659         
3660         ret = pdb_add_aliasmem(&alias_sid, &q_u->sid.sid);
3661         
3662         if ( can_add_accounts )
3663                 unbecome_root();
3664                 
3665         /******** END SeAddUsers BLOCK *********/
3666         
3667         if (ret) {
3668                 force_flush_samr_cache(disp_info);
3669         }
3670
3671         return ret ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3672 }
3673
3674 /*********************************************************************
3675  _samr_del_aliasmem
3676 *********************************************************************/
3677
3678 NTSTATUS _samr_del_aliasmem(pipes_struct *p, SAMR_Q_DEL_ALIASMEM *q_u, SAMR_R_DEL_ALIASMEM *r_u)
3679 {
3680         DOM_SID alias_sid;
3681         uint32 acc_granted;
3682         SE_PRIV se_rights;
3683         BOOL can_add_accounts;
3684         BOOL ret;
3685         DISP_INFO *disp_info = NULL;
3686
3687         /* Find the policy handle. Open a policy on it. */
3688         if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted, &disp_info)) 
3689                 return NT_STATUS_INVALID_HANDLE;
3690         
3691         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_REMOVE_MEMBER, "_samr_del_aliasmem"))) {
3692                 return r_u->status;
3693         }
3694         
3695         DEBUG(10, ("_samr_del_aliasmem:sid is %s\n",
3696                    sid_string_static(&alias_sid)));
3697
3698         se_priv_copy( &se_rights, &se_add_users );
3699         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3700
3701         /******** BEGIN SeAddUsers BLOCK *********/
3702         
3703         if ( can_add_accounts )
3704                 become_root();
3705
3706         ret = pdb_del_aliasmem(&alias_sid, &q_u->sid.sid);
3707         
3708         if ( can_add_accounts )
3709                 unbecome_root();
3710                 
3711         /******** END SeAddUsers BLOCK *********/
3712         
3713         if (ret) {
3714                 force_flush_samr_cache(disp_info);
3715         }
3716
3717         return ret ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3718 }
3719
3720 /*********************************************************************
3721  _samr_add_groupmem
3722 *********************************************************************/
3723
3724 NTSTATUS _samr_add_groupmem(pipes_struct *p, SAMR_Q_ADD_GROUPMEM *q_u, SAMR_R_ADD_GROUPMEM *r_u)
3725 {
3726         DOM_SID group_sid;
3727         DOM_SID user_sid;
3728         fstring group_sid_str;
3729         uid_t uid;
3730         struct passwd *pwd;
3731         struct group *grp;
3732         fstring grp_name;
3733         GROUP_MAP map;
3734         NTSTATUS ret;
3735         SAM_ACCOUNT *sam_user=NULL;
3736         BOOL check;
3737         uint32 acc_granted;
3738         SE_PRIV se_rights;
3739         BOOL can_add_accounts;
3740         DISP_INFO *disp_info = NULL;
3741
3742         /* Find the policy handle. Open a policy on it. */
3743         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted, &disp_info)) 
3744                 return NT_STATUS_INVALID_HANDLE;
3745         
3746         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_ADD_MEMBER, "_samr_add_groupmem"))) {
3747                 return r_u->status;
3748         }
3749
3750         sid_to_string(group_sid_str, &group_sid);
3751         DEBUG(10, ("sid is %s\n", group_sid_str));
3752
3753         if (sid_compare(&group_sid, get_global_sam_sid())<=0)
3754                 return NT_STATUS_NO_SUCH_GROUP;
3755
3756         DEBUG(10, ("lookup on Domain SID\n"));
3757
3758         if(!get_domain_group_from_sid(group_sid, &map))
3759                 return NT_STATUS_NO_SUCH_GROUP;
3760
3761         sid_copy(&user_sid, get_global_sam_sid());
3762         sid_append_rid(&user_sid, q_u->rid);
3763
3764         ret = pdb_init_sam(&sam_user);
3765         if (!NT_STATUS_IS_OK(ret))
3766                 return ret;
3767         
3768         check = pdb_getsampwsid(sam_user, &user_sid);
3769         
3770         if (check != True) {
3771                 pdb_free_sam(&sam_user);
3772                 return NT_STATUS_NO_SUCH_USER;
3773         }
3774
3775         /* check a real user exist before we run the script to add a user to a group */
3776         if (!NT_STATUS_IS_OK(sid_to_uid(pdb_get_user_sid(sam_user), &uid))) {
3777                 pdb_free_sam(&sam_user);
3778                 return NT_STATUS_NO_SUCH_USER;
3779         }
3780
3781         pdb_free_sam(&sam_user);
3782
3783         if ((pwd=getpwuid_alloc(uid)) == NULL) {
3784                 return NT_STATUS_NO_SUCH_USER;
3785         }
3786
3787         if ((grp=getgrgid(map.gid)) == NULL) {
3788                 passwd_free(&pwd);
3789                 return NT_STATUS_NO_SUCH_GROUP;
3790         }
3791
3792         /* we need to copy the name otherwise it's overloaded in user_in_unix_group_list */
3793         fstrcpy(grp_name, grp->gr_name);
3794
3795         /* if the user is already in the group */
3796         if(user_in_unix_group_list(pwd->pw_name, grp_name)) {
3797                 passwd_free(&pwd);
3798                 return NT_STATUS_MEMBER_IN_GROUP;
3799         }
3800
3801         se_priv_copy( &se_rights, &se_add_users );
3802         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3803
3804         /******** BEGIN SeAddUsers BLOCK *********/
3805         
3806         if ( can_add_accounts )
3807                 become_root();
3808                 
3809         /* 
3810          * ok, the group exist, the user exist, the user is not in the group,
3811          *
3812          * we can (finally) add it to the group !
3813          */
3814
3815         smb_add_user_group(grp_name, pwd->pw_name);
3816
3817         if ( can_add_accounts )
3818                 unbecome_root();
3819                 
3820         /******** END SeAddUsers BLOCK *********/
3821         
3822         /* check if the user has been added then ... */
3823         if(!user_in_unix_group_list(pwd->pw_name, grp_name)) {
3824                 passwd_free(&pwd);
3825                 return NT_STATUS_MEMBER_NOT_IN_GROUP;           /* don't know what to reply else */
3826         }
3827
3828         passwd_free(&pwd);
3829
3830         force_flush_samr_cache(disp_info);
3831
3832         return NT_STATUS_OK;
3833 }
3834
3835 /*********************************************************************
3836  _samr_del_groupmem
3837 *********************************************************************/
3838
3839 NTSTATUS _samr_del_groupmem(pipes_struct *p, SAMR_Q_DEL_GROUPMEM *q_u, SAMR_R_DEL_GROUPMEM *r_u)
3840 {
3841         DOM_SID group_sid;
3842         DOM_SID user_sid;
3843         SAM_ACCOUNT *sam_pass=NULL;
3844         GROUP_MAP map;
3845         fstring grp_name;
3846         struct group *grp;
3847         uint32 acc_granted;
3848         SE_PRIV se_rights;
3849         BOOL can_add_accounts;
3850         DISP_INFO *disp_info = NULL;
3851
3852         /*
3853          * delete the group member named q_u->rid
3854          * who is a member of the sid associated with the handle
3855          * the rid is a user's rid as the group is a domain group.
3856          */
3857
3858         /* Find the policy handle. Open a policy on it. */
3859         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted, &disp_info)) 
3860                 return NT_STATUS_INVALID_HANDLE;
3861         
3862         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_REMOVE_MEMBER, "_samr_del_groupmem"))) {
3863                 return r_u->status;
3864         }
3865                 
3866         if (!sid_check_is_in_our_domain(&group_sid))
3867                 return NT_STATUS_NO_SUCH_GROUP;
3868
3869         sid_copy(&user_sid, get_global_sam_sid());
3870         sid_append_rid(&user_sid, q_u->rid);
3871
3872         if (!get_domain_group_from_sid(group_sid, &map))
3873                 return NT_STATUS_NO_SUCH_GROUP;
3874
3875         if ((grp=getgrgid(map.gid)) == NULL)
3876                 return NT_STATUS_NO_SUCH_GROUP;
3877
3878         /* we need to copy the name otherwise it's overloaded in user_in_group_list */
3879         fstrcpy(grp_name, grp->gr_name);
3880
3881         /* check if the user exists before trying to remove it from the group */
3882         pdb_init_sam(&sam_pass);
3883         if (!pdb_getsampwsid(sam_pass, &user_sid)) {
3884                 DEBUG(5,("User %s doesn't exist.\n", pdb_get_username(sam_pass)));
3885                 pdb_free_sam(&sam_pass);
3886                 return NT_STATUS_NO_SUCH_USER;
3887         }
3888
3889         /* if the user is not in the group */
3890         if (!user_in_unix_group_list(pdb_get_username(sam_pass), grp_name)) {
3891                 pdb_free_sam(&sam_pass);
3892                 return NT_STATUS_MEMBER_NOT_IN_GROUP;
3893         }
3894         
3895
3896         se_priv_copy( &se_rights, &se_add_users );
3897         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3898
3899         /******** BEGIN SeAddUsers BLOCK *********/
3900         
3901         if ( can_add_accounts )
3902                 become_root();
3903                 
3904         smb_delete_user_group(grp_name, pdb_get_username(sam_pass));
3905
3906         if ( can_add_accounts )
3907                 unbecome_root();
3908                 
3909         /******** END SeAddUsers BLOCK *********/
3910         
3911         /* check if the user has been removed then ... */
3912         if (user_in_unix_group_list(pdb_get_username(sam_pass), grp_name)) {
3913                 pdb_free_sam(&sam_pass);
3914                 return NT_STATUS_ACCESS_DENIED;         /* don't know what to reply else */
3915         }
3916         
3917         pdb_free_sam(&sam_pass);
3918
3919         force_flush_samr_cache(disp_info);
3920
3921         return NT_STATUS_OK;
3922
3923 }
3924
3925 /****************************************************************************
3926  Delete a UNIX user on demand.
3927 ****************************************************************************/
3928
3929 static int smb_delete_user(const char *unix_user)
3930 {
3931         pstring del_script;
3932         int ret;
3933
3934         pstrcpy(del_script, lp_deluser_script());
3935         if (! *del_script)
3936                 return -1;
3937         all_string_sub(del_script, "%u", unix_user, sizeof(del_script));
3938         ret = smbrun(del_script,NULL);
3939         flush_pwnam_cache();
3940         DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
3941
3942         return ret;
3943 }
3944
3945 /*********************************************************************
3946  _samr_delete_dom_user
3947 *********************************************************************/
3948
3949 NTSTATUS _samr_delete_dom_user(pipes_struct *p, SAMR_Q_DELETE_DOM_USER *q_u, SAMR_R_DELETE_DOM_USER *r_u )
3950 {
3951         DOM_SID user_sid;
3952         SAM_ACCOUNT *sam_pass=NULL;
3953         uint32 acc_granted;
3954         BOOL can_add_accounts;
3955         BOOL ret;
3956         DISP_INFO *disp_info = NULL;
3957
3958         DEBUG(5, ("_samr_delete_dom_user: %d\n", __LINE__));
3959
3960         /* Find the policy handle. Open a policy on it. */
3961         if (!get_lsa_policy_samr_sid(p, &q_u->user_pol, &user_sid, &acc_granted, &disp_info)) 
3962                 return NT_STATUS_INVALID_HANDLE;
3963                 
3964         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_user"))) {
3965                 return r_u->status;
3966         }
3967                 
3968         if (!sid_check_is_in_our_domain(&user_sid))
3969                 return NT_STATUS_CANNOT_DELETE;
3970
3971         /* check if the user exists before trying to delete */
3972         pdb_init_sam(&sam_pass);
3973         if(!pdb_getsampwsid(sam_pass, &user_sid)) {
3974                 DEBUG(5,("_samr_delete_dom_user:User %s doesn't exist.\n", 
3975                         sid_string_static(&user_sid)));
3976                 pdb_free_sam(&sam_pass);
3977                 return NT_STATUS_NO_SUCH_USER;
3978         }
3979         
3980         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
3981
3982         /******** BEGIN SeAddUsers BLOCK *********/
3983         
3984         if ( can_add_accounts )
3985                 become_root();
3986
3987         /* First delete the samba side....
3988            code is order to prevent unnecessary returns out of the admin 
3989            block of code */
3990            
3991         if ( (ret = pdb_delete_sam_account(sam_pass)) == True ) {
3992                 /*
3993                  * Now delete the unix side ....
3994                  * note: we don't check if the delete really happened
3995                  * as the script is not necessary present
3996                  * and maybe the sysadmin doesn't want to delete the unix side
3997                  */
3998                 smb_delete_user( pdb_get_username(sam_pass) );
3999         }
4000         
4001         if ( can_add_accounts )
4002                 unbecome_root();
4003                 
4004         /******** END SeAddUsers BLOCK *********/
4005                 
4006         if ( !ret ) {
4007                 DEBUG(5,("_samr_delete_dom_user:Failed to delete entry for user %s.\n", pdb_get_username(sam_pass)));
4008                 pdb_free_sam(&sam_pass);
4009                 return NT_STATUS_CANNOT_DELETE;
4010         }
4011
4012
4013         pdb_free_sam(&sam_pass);
4014
4015         if (!close_policy_hnd(p, &q_u->user_pol))
4016                 return NT_STATUS_OBJECT_NAME_INVALID;
4017
4018         force_flush_samr_cache(disp_info);
4019
4020         return NT_STATUS_OK;
4021 }
4022
4023 /*********************************************************************
4024  _samr_delete_dom_group
4025 *********************************************************************/
4026
4027 NTSTATUS _samr_delete_dom_group(pipes_struct *p, SAMR_Q_DELETE_DOM_GROUP *q_u, SAMR_R_DELETE_DOM_GROUP *r_u)
4028 {
4029         DOM_SID group_sid;
4030         DOM_SID dom_sid;
4031         uint32 group_rid;
4032         fstring group_sid_str;
4033         gid_t gid;
4034         struct group *grp;
4035         GROUP_MAP map;
4036         uint32 acc_granted;
4037         SE_PRIV se_rights;
4038         BOOL can_add_accounts;
4039         BOOL ret;
4040         DISP_INFO *disp_info = NULL;
4041
4042         DEBUG(5, ("samr_delete_dom_group: %d\n", __LINE__));
4043
4044         /* Find the policy handle. Open a policy on it. */
4045         if (!get_lsa_policy_samr_sid(p, &q_u->group_pol, &group_sid, &acc_granted, &disp_info)) 
4046                 return NT_STATUS_INVALID_HANDLE;
4047                 
4048         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_group"))) {
4049                 return r_u->status;
4050         }
4051                 
4052         sid_copy(&dom_sid, &group_sid);
4053         sid_to_string(group_sid_str, &dom_sid);
4054         sid_split_rid(&dom_sid, &group_rid);
4055
4056         DEBUG(10, ("sid is %s\n", group_sid_str));
4057
4058         /* we check if it's our SID before deleting */
4059         if (!sid_equal(&dom_sid, get_global_sam_sid()))
4060                 return NT_STATUS_NO_SUCH_GROUP;
4061
4062         DEBUG(10, ("lookup on Domain SID\n"));
4063
4064         if(!get_domain_group_from_sid(group_sid, &map))
4065                 return NT_STATUS_NO_SUCH_GROUP;
4066
4067         gid=map.gid;
4068
4069         /* check if group really exists */
4070         if ( (grp=getgrgid(gid)) == NULL) 
4071                 return NT_STATUS_NO_SUCH_GROUP;
4072
4073         se_priv_copy( &se_rights, &se_add_users );
4074         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4075
4076         /******** BEGIN SeAddUsers BLOCK *********/
4077         
4078         if ( can_add_accounts )
4079                 become_root();
4080
4081         /* delete mapping first */
4082         
4083         if ( (ret = pdb_delete_group_mapping_entry(group_sid)) == True ) {
4084                 smb_delete_group( grp->gr_name );
4085         }
4086
4087         if ( can_add_accounts )
4088                 unbecome_root();
4089                 
4090         /******** END SeAddUsers BLOCK *********/
4091         
4092         if ( !ret ) {
4093                 DEBUG(5,("_samr_delete_dom_group: Failed to delete mapping entry for group %s.\n", 
4094                         group_sid_str));
4095                 return NT_STATUS_ACCESS_DENIED;
4096         }
4097         
4098         /* don't check that the unix group has been deleted.  Work like 
4099            _samr_delet_dom_user() */
4100
4101         if (!close_policy_hnd(p, &q_u->group_pol))
4102                 return NT_STATUS_OBJECT_NAME_INVALID;
4103
4104         force_flush_samr_cache(disp_info);
4105
4106         return NT_STATUS_OK;
4107 }
4108
4109 /*********************************************************************
4110  _samr_delete_dom_alias
4111 *********************************************************************/
4112
4113 NTSTATUS _samr_delete_dom_alias(pipes_struct *p, SAMR_Q_DELETE_DOM_ALIAS *q_u, SAMR_R_DELETE_DOM_ALIAS *r_u)
4114 {
4115         DOM_SID alias_sid;
4116         uint32 acc_granted;
4117         SE_PRIV se_rights;
4118         BOOL can_add_accounts;
4119         BOOL ret;
4120         DISP_INFO *disp_info = NULL;
4121
4122         DEBUG(5, ("_samr_delete_dom_alias: %d\n", __LINE__));
4123
4124         /* Find the policy handle. Open a policy on it. */
4125         if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted, &disp_info)) 
4126                 return NT_STATUS_INVALID_HANDLE;
4127         
4128         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_alias"))) {
4129                 return r_u->status;
4130         }
4131
4132         DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
4133
4134         if (!sid_check_is_in_our_domain(&alias_sid))
4135                 return NT_STATUS_NO_SUCH_ALIAS;
4136                 
4137         DEBUG(10, ("lookup on Local SID\n"));
4138
4139         se_priv_copy( &se_rights, &se_add_users );
4140         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4141
4142         /******** BEGIN SeAddUsers BLOCK *********/
4143         
4144         if ( can_add_accounts )
4145                 become_root();
4146
4147         /* Have passdb delete the alias */
4148         ret = pdb_delete_alias(&alias_sid);
4149         
4150         if ( can_add_accounts )
4151                 unbecome_root();
4152                 
4153         /******** END SeAddUsers BLOCK *********/
4154
4155         if ( !ret )
4156                 return NT_STATUS_ACCESS_DENIED;
4157
4158         if (!close_policy_hnd(p, &q_u->alias_pol))
4159                 return NT_STATUS_OBJECT_NAME_INVALID;
4160
4161         force_flush_samr_cache(disp_info);
4162
4163         return NT_STATUS_OK;
4164 }
4165
4166 /*********************************************************************
4167  _samr_create_dom_group
4168 *********************************************************************/
4169
4170 NTSTATUS _samr_create_dom_group(pipes_struct *p, SAMR_Q_CREATE_DOM_GROUP *q_u, SAMR_R_CREATE_DOM_GROUP *r_u)
4171 {
4172         DOM_SID dom_sid;
4173         DOM_SID info_sid;
4174         fstring name;
4175         fstring sid_string;
4176         struct group *grp;
4177         struct samr_info *info;
4178         uint32 acc_granted;
4179         gid_t gid;
4180         SE_PRIV se_rights;
4181         BOOL can_add_accounts;
4182         NTSTATUS result;
4183         DISP_INFO *disp_info = NULL;
4184
4185         /* Find the policy handle. Open a policy on it. */
4186         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &dom_sid, &acc_granted, &disp_info)) 
4187                 return NT_STATUS_INVALID_HANDLE;
4188         
4189         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_DOMAIN_CREATE_GROUP, "_samr_create_dom_group"))) {
4190                 return r_u->status;
4191         }
4192                 
4193         if (!sid_equal(&dom_sid, get_global_sam_sid()))
4194                 return NT_STATUS_ACCESS_DENIED;
4195
4196         unistr2_to_ascii(name, &q_u->uni_acct_desc, sizeof(name)-1);
4197
4198         /* check if group already exist */
4199         if ((grp=getgrnam(name)) != NULL)
4200                 return NT_STATUS_GROUP_EXISTS;
4201
4202         se_priv_copy( &se_rights, &se_add_users );
4203         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4204
4205         /******** BEGIN SeAddUsers BLOCK *********/
4206         
4207         if ( can_add_accounts )
4208                 become_root();
4209         
4210         /* check that we successfully create the UNIX group */
4211         
4212         result = NT_STATUS_ACCESS_DENIED;
4213         if ( (smb_create_group(name, &gid) == 0) && ((grp=getgrgid(gid)) != NULL) ) {
4214         
4215                 /* so far, so good */
4216                 
4217                 result = NT_STATUS_OK;
4218                 
4219                 r_u->rid = pdb_gid_to_group_rid( grp->gr_gid );
4220
4221                 /* add the group to the mapping table */
4222                 
4223                 sid_copy( &info_sid, get_global_sam_sid() );
4224                 sid_append_rid( &info_sid, r_u->rid );
4225                 sid_to_string( sid_string, &info_sid );
4226                 
4227                 /* reset the error code if we fail to add the mapping entry */
4228                 
4229                 if ( !add_initial_entry(grp->gr_gid, sid_string, SID_NAME_DOM_GRP, name, NULL) )
4230                         result = NT_STATUS_ACCESS_DENIED;
4231         }
4232
4233         if ( can_add_accounts )
4234                 unbecome_root();
4235                 
4236         /******** END SeAddUsers BLOCK *********/
4237         
4238         /* check if we should bail out here */
4239         
4240         if ( !NT_STATUS_IS_OK(result) )
4241                 return result;
4242         
4243         if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
4244                 return NT_STATUS_NO_MEMORY;
4245
4246
4247         /* they created it; let the user do what he wants with it */
4248
4249         info->acc_granted = GENERIC_RIGHTS_GROUP_ALL_ACCESS;
4250
4251         /* get a (unique) handle.  open a policy on it. */
4252         if (!create_policy_hnd(p, &r_u->pol, free_samr_info, (void *)info))
4253                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4254
4255         force_flush_samr_cache(disp_info);
4256
4257         return NT_STATUS_OK;
4258 }
4259
4260 /*********************************************************************
4261  _samr_create_dom_alias
4262 *********************************************************************/
4263
4264 NTSTATUS _samr_create_dom_alias(pipes_struct *p, SAMR_Q_CREATE_DOM_ALIAS *q_u, SAMR_R_CREATE_DOM_ALIAS *r_u)
4265 {
4266         DOM_SID dom_sid;
4267         DOM_SID info_sid;
4268         fstring name;
4269         struct samr_info *info;
4270         uint32 acc_granted;
4271         gid_t gid;
4272         NTSTATUS result;
4273         SE_PRIV se_rights;
4274         BOOL can_add_accounts;
4275         DISP_INFO *disp_info = NULL;
4276
4277         /* Find the policy handle. Open a policy on it. */
4278         if (!get_lsa_policy_samr_sid(p, &q_u->dom_pol, &dom_sid, &acc_granted, &disp_info)) 
4279                 return NT_STATUS_INVALID_HANDLE;
4280                 
4281         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_DOMAIN_CREATE_ALIAS, "_samr_create_alias"))) {
4282                 return r_u->status;
4283         }
4284                 
4285         if (!sid_equal(&dom_sid, get_global_sam_sid()))
4286                 return NT_STATUS_ACCESS_DENIED;
4287
4288         unistr2_to_ascii(name, &q_u->uni_acct_desc, sizeof(name)-1);
4289
4290         se_priv_copy( &se_rights, &se_add_users );
4291         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4292
4293         /******** BEGIN SeAddUsers BLOCK *********/
4294         
4295         if ( can_add_accounts )
4296                 become_root();
4297
4298         /* Have passdb create the alias */
4299         result = pdb_create_alias(name, &r_u->rid);
4300
4301         if ( can_add_accounts )
4302                 unbecome_root();
4303                 
4304         /******** END SeAddUsers BLOCK *********/
4305
4306         if (!NT_STATUS_IS_OK(result))
4307                 return result;
4308
4309         sid_copy(&info_sid, get_global_sam_sid());
4310         sid_append_rid(&info_sid, r_u->rid);
4311
4312         if (!NT_STATUS_IS_OK(sid_to_gid(&info_sid, &gid)))
4313                 return NT_STATUS_ACCESS_DENIED;
4314
4315         /* check if the group has been successfully created */
4316         if ( getgrgid(gid) == NULL )
4317                 return NT_STATUS_ACCESS_DENIED;
4318
4319         if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
4320                 return NT_STATUS_NO_MEMORY;
4321
4322         /* they created it; let the user do what he wants with it */
4323
4324         info->acc_granted = GENERIC_RIGHTS_ALIAS_ALL_ACCESS;
4325
4326         /* get a (unique) handle.  open a policy on it. */
4327         if (!create_policy_hnd(p, &r_u->alias_pol, free_samr_info, (void *)info))
4328                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4329
4330         force_flush_samr_cache(disp_info);
4331
4332         return NT_STATUS_OK;
4333 }
4334
4335 /*********************************************************************
4336  _samr_query_groupinfo
4337
4338 sends the name/comment pair of a domain group
4339 level 1 send also the number of users of that group
4340 *********************************************************************/
4341
4342 NTSTATUS _samr_query_groupinfo(pipes_struct *p, SAMR_Q_QUERY_GROUPINFO *q_u, SAMR_R_QUERY_GROUPINFO *r_u)
4343 {
4344         DOM_SID group_sid;
4345         GROUP_MAP map;
4346         DOM_SID *sids=NULL;
4347         uid_t *uids;
4348         int num=0;
4349         GROUP_INFO_CTR *ctr;
4350         uint32 acc_granted;
4351         BOOL ret;
4352
4353         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted, NULL)) 
4354                 return NT_STATUS_INVALID_HANDLE;
4355         
4356         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_LOOKUP_INFO, "_samr_query_groupinfo"))) {
4357                 return r_u->status;
4358         }
4359                 
4360         become_root();
4361         ret = get_domain_group_from_sid(group_sid, &map);
4362         unbecome_root();
4363         if (!ret)
4364                 return NT_STATUS_INVALID_HANDLE;
4365
4366         ctr=TALLOC_ZERO_P(p->mem_ctx, GROUP_INFO_CTR);
4367         if (ctr==NULL)
4368                 return NT_STATUS_NO_MEMORY;
4369
4370         switch (q_u->switch_level) {
4371                 case 1:
4372                         ctr->switch_value1 = 1;
4373                         if(!get_memberuids(map.gid, &uids, &num))
4374                                 return NT_STATUS_NO_SUCH_GROUP;
4375                         SAFE_FREE(uids);
4376                         init_samr_group_info1(&ctr->group.info1, map.nt_name, map.comment, num);
4377                         SAFE_FREE(sids);
4378                         break;
4379                 case 3:
4380                         ctr->switch_value1 = 3;
4381                         init_samr_group_info3(&ctr->group.info3);
4382                         break;
4383                 case 4:
4384                         ctr->switch_value1 = 4;
4385                         init_samr_group_info4(&ctr->group.info4, map.comment);
4386                         break;
4387                 default:
4388                         return NT_STATUS_INVALID_INFO_CLASS;
4389         }
4390
4391         init_samr_r_query_groupinfo(r_u, ctr, NT_STATUS_OK);
4392
4393         return NT_STATUS_OK;
4394 }
4395
4396 /*********************************************************************
4397  _samr_set_groupinfo
4398  
4399  update a domain group's comment.
4400 *********************************************************************/
4401
4402 NTSTATUS _samr_set_groupinfo(pipes_struct *p, SAMR_Q_SET_GROUPINFO *q_u, SAMR_R_SET_GROUPINFO *r_u)
4403 {
4404         DOM_SID group_sid;
4405         GROUP_MAP map;
4406         GROUP_INFO_CTR *ctr;
4407         uint32 acc_granted;
4408         BOOL ret;
4409         BOOL can_mod_accounts;
4410         DISP_INFO *disp_info = NULL;
4411
4412         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted, &disp_info))
4413                 return NT_STATUS_INVALID_HANDLE;
4414         
4415         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_SET_INFO, "_samr_set_groupinfo"))) {
4416                 return r_u->status;
4417         }
4418                 
4419         if (!get_domain_group_from_sid(group_sid, &map))
4420                 return NT_STATUS_NO_SUCH_GROUP;
4421         
4422         ctr=q_u->ctr;
4423
4424         switch (ctr->switch_value1) {
4425                 case 1:
4426                         unistr2_to_ascii(map.comment, &(ctr->group.info1.uni_acct_desc), sizeof(map.comment)-1);
4427                         break;
4428                 case 4:
4429                         unistr2_to_ascii(map.comment, &(ctr->group.info4.uni_acct_desc), sizeof(map.comment)-1);
4430                         break;
4431                 default:
4432                         return NT_STATUS_INVALID_INFO_CLASS;
4433         }
4434
4435         can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
4436
4437         /******** BEGIN SeAddUsers BLOCK *********/
4438
4439         if ( can_mod_accounts )
4440                 become_root();
4441           
4442         ret = pdb_update_group_mapping_entry(&map);
4443
4444         if ( can_mod_accounts )
4445                 unbecome_root();
4446
4447         /******** End SeAddUsers BLOCK *********/
4448
4449         if (ret) {
4450                 force_flush_samr_cache(disp_info);
4451         }
4452
4453         return ret ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4454 }
4455
4456 /*********************************************************************
4457  _samr_set_aliasinfo
4458  
4459  update an alias's comment.
4460 *********************************************************************/
4461
4462 NTSTATUS _samr_set_aliasinfo(pipes_struct *p, SAMR_Q_SET_ALIASINFO *q_u, SAMR_R_SET_ALIASINFO *r_u)
4463 {
4464         DOM_SID group_sid;
4465         struct acct_info info;
4466         ALIAS_INFO_CTR *ctr;
4467         uint32 acc_granted;
4468         BOOL ret;
4469         BOOL can_mod_accounts;
4470         DISP_INFO *disp_info = NULL;
4471
4472         if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &group_sid, &acc_granted, &disp_info))
4473                 return NT_STATUS_INVALID_HANDLE;
4474         
4475         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_SET_INFO, "_samr_set_aliasinfo"))) {
4476                 return r_u->status;
4477         }
4478                 
4479         ctr=&q_u->ctr;
4480
4481         switch (ctr->level) {
4482                 case 3:
4483                         if ( ctr->alias.info3.description.string ) {
4484                                 unistr2_to_ascii( info.acct_desc, 
4485                                         ctr->alias.info3.description.string, 
4486                                         sizeof(info.acct_desc)-1 );
4487                         }
4488                         break;
4489                 default:
4490                         return NT_STATUS_INVALID_INFO_CLASS;
4491         }
4492
4493         can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
4494
4495         /******** BEGIN SeAddUsers BLOCK *********/
4496
4497         if ( can_mod_accounts )
4498                 become_root();
4499
4500         ret = pdb_set_aliasinfo( &group_sid, &info );
4501
4502         if ( can_mod_accounts )
4503                 unbecome_root();
4504
4505         /******** End SeAddUsers BLOCK *********/
4506
4507         if (ret) {
4508                 force_flush_samr_cache(disp_info);
4509         }
4510
4511         return ret ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4512 }
4513
4514 /*********************************************************************
4515  _samr_get_dom_pwinfo
4516 *********************************************************************/
4517
4518 NTSTATUS _samr_get_dom_pwinfo(pipes_struct *p, SAMR_Q_GET_DOM_PWINFO *q_u, SAMR_R_GET_DOM_PWINFO *r_u)
4519 {
4520         /* Perform access check.  Since this rpc does not require a
4521            policy handle it will not be caught by the access checks on
4522            SAMR_CONNECT or SAMR_CONNECT_ANON. */
4523
4524         if (!pipe_access_check(p)) {
4525                 DEBUG(3, ("access denied to samr_get_dom_pwinfo\n"));
4526                 r_u->status = NT_STATUS_ACCESS_DENIED;
4527                 return r_u->status;
4528         }
4529
4530         /* Actually, returning zeros here works quite well :-). */
4531
4532         return NT_STATUS_OK;
4533 }
4534
4535 /*********************************************************************
4536  _samr_open_group
4537 *********************************************************************/
4538
4539 NTSTATUS _samr_open_group(pipes_struct *p, SAMR_Q_OPEN_GROUP *q_u, SAMR_R_OPEN_GROUP *r_u)
4540 {
4541         DOM_SID sid;
4542         DOM_SID info_sid;
4543         GROUP_MAP map;
4544         struct samr_info *info;
4545         SEC_DESC         *psd = NULL;
4546         uint32            acc_granted;
4547         uint32            des_access = q_u->access_mask;
4548         size_t            sd_size;
4549         NTSTATUS          status;
4550         fstring sid_string;
4551         BOOL ret;
4552         SE_PRIV se_rights;
4553
4554         if (!get_lsa_policy_samr_sid(p, &q_u->domain_pol, &sid, &acc_granted, NULL)) 
4555                 return NT_STATUS_INVALID_HANDLE;
4556         
4557         status = access_check_samr_function(acc_granted, 
4558                 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_group");
4559                 
4560         if ( !NT_STATUS_IS_OK(status) )
4561                 return status;
4562                 
4563         /*check if access can be granted as requested by client. */
4564         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &grp_generic_mapping, NULL, 0);
4565         se_map_generic(&des_access,&grp_generic_mapping);
4566
4567         se_priv_copy( &se_rights, &se_add_users );
4568
4569         status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
4570                 &se_rights, GENERIC_RIGHTS_GROUP_WRITE, des_access, 
4571                 &acc_granted, "_samr_open_group");
4572                 
4573         if ( !NT_STATUS_IS_OK(status) ) 
4574                 return status;
4575
4576         /* this should not be hard-coded like this */
4577         
4578         if (!sid_equal(&sid, get_global_sam_sid()))
4579                 return NT_STATUS_ACCESS_DENIED;
4580
4581         sid_copy(&info_sid, get_global_sam_sid());
4582         sid_append_rid(&info_sid, q_u->rid_group);
4583         sid_to_string(sid_string, &info_sid);
4584
4585         if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
4586                 return NT_STATUS_NO_MEMORY;
4587                 
4588         info->acc_granted = acc_granted;
4589
4590         DEBUG(10, ("_samr_open_group:Opening SID: %s\n", sid_string));
4591
4592         /* check if that group really exists */
4593         become_root();
4594         ret = get_domain_group_from_sid(info->sid, &map);
4595         unbecome_root();
4596         if (!ret)
4597                 return NT_STATUS_NO_SUCH_GROUP;
4598
4599         /* get a (unique) handle.  open a policy on it. */
4600         if (!create_policy_hnd(p, &r_u->pol, free_samr_info, (void *)info))
4601                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4602
4603         return NT_STATUS_OK;
4604 }
4605
4606 /*********************************************************************
4607  _samr_remove_sid_foreign_domain
4608 *********************************************************************/
4609
4610 NTSTATUS _samr_remove_sid_foreign_domain(pipes_struct *p, 
4611                                           SAMR_Q_REMOVE_SID_FOREIGN_DOMAIN *q_u, 
4612                                           SAMR_R_REMOVE_SID_FOREIGN_DOMAIN *r_u)
4613 {
4614         DOM_SID                 delete_sid, domain_sid;
4615         uint32                  acc_granted;
4616         NTSTATUS                result;
4617         DISP_INFO *disp_info = NULL;
4618
4619         sid_copy( &delete_sid, &q_u->sid.sid );
4620         
4621         DEBUG(5,("_samr_remove_sid_foreign_domain: removing SID [%s]\n",
4622                 sid_string_static(&delete_sid)));
4623                 
4624         /* Find the policy handle. Open a policy on it. */
4625         
4626         if (!get_lsa_policy_samr_sid(p, &q_u->dom_pol, &domain_sid,
4627                                      &acc_granted, &disp_info)) 
4628                 return NT_STATUS_INVALID_HANDLE;
4629         
4630         result = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, 
4631                 "_samr_remove_sid_foreign_domain");
4632                 
4633         if (!NT_STATUS_IS_OK(result)) 
4634                 return result;
4635                         
4636         DEBUG(8, ("_samr_remove_sid_foreign_domain:sid is %s\n", 
4637                 sid_string_static(&domain_sid)));
4638
4639         /* we can only delete a user from a group since we don't have 
4640            nested groups anyways.  So in the latter case, just say OK */
4641
4642         /* TODO: The above comment nowadays is bogus. Since we have nested
4643          * groups now, and aliases members are never reported out of the unix
4644          * group membership, the "just say OK" makes this call a no-op. For
4645          * us. This needs fixing however. */
4646
4647         /* I've only ever seen this in the wild when deleting a user from
4648          * usrmgr.exe. domain_sid is the builtin domain, and the sid to delete
4649          * is the user about to be deleted. I very much suspect this is the
4650          * only application of this call. To verify this, let people report
4651          * other cases. */
4652
4653         if (!sid_check_is_builtin(&domain_sid)) {
4654                 DEBUG(1,("_samr_remove_sid_foreign_domain: domain_sid = %s, "
4655                          "global_sam_sid() = %s\n",
4656                          sid_string_static(&domain_sid),
4657                          sid_string_static(get_global_sam_sid())));
4658                 DEBUGADD(1,("please report to samba-technical@samba.org!\n"));
4659                 return NT_STATUS_OK;
4660         }
4661
4662         force_flush_samr_cache(disp_info);
4663
4664         result = NT_STATUS_OK;
4665
4666         return result;
4667 }
4668
4669 /*******************************************************************
4670  _samr_query_domain_info2
4671  ********************************************************************/
4672
4673 NTSTATUS _samr_query_domain_info2(pipes_struct *p,
4674                 SAMR_Q_QUERY_DOMAIN_INFO2 *q_u,
4675                 SAMR_R_QUERY_DOMAIN_INFO2 *r_u)
4676 {
4677         struct samr_info *info = NULL;
4678         SAM_UNK_CTR *ctr;
4679         uint32 min_pass_len,pass_hist,flag;
4680         time_t u_expire, u_min_age;
4681         NTTIME nt_expire, nt_min_age;
4682
4683         time_t u_lock_duration, u_reset_time;
4684         NTTIME nt_lock_duration, nt_reset_time;
4685         uint32 lockout;
4686         
4687         time_t u_logout;
4688         NTTIME nt_logout;
4689
4690         uint32 num_users=0, num_groups=0, num_aliases=0;
4691
4692         uint32 account_policy_temp;
4693
4694         time_t seq_num;
4695         uint32 server_role;
4696
4697         if ((ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_UNK_CTR)) == NULL)
4698                 return NT_STATUS_NO_MEMORY;
4699
4700         ZERO_STRUCTP(ctr);
4701
4702         r_u->status = NT_STATUS_OK;
4703
4704         DEBUG(5,("_samr_query_domain_info2: %d\n", __LINE__));
4705
4706         /* find the policy handle.  open a policy on it. */
4707         if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)&info))
4708                 return NT_STATUS_INVALID_HANDLE;
4709
4710         switch (q_u->switch_value) {
4711                 case 0x01:
4712                         pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
4713                         min_pass_len = account_policy_temp;
4714
4715                         pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
4716                         pass_hist = account_policy_temp;
4717
4718                         pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
4719                         flag = account_policy_temp;
4720
4721                         pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
4722                         u_expire = account_policy_temp;
4723
4724                         pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
4725                         u_min_age = account_policy_temp;
4726
4727                         unix_to_nt_time_abs(&nt_expire, u_expire);
4728                         unix_to_nt_time_abs(&nt_min_age, u_min_age);
4729
4730                         init_unk_info1(&ctr->info.inf1, (uint16)min_pass_len, (uint16)pass_hist, 
4731                                        flag, nt_expire, nt_min_age);
4732                         break;
4733                 case 0x02:
4734                         become_root();          
4735                         num_users = count_sam_users(info->disp_info, ACB_NORMAL);
4736                         num_groups = count_sam_groups(info->disp_info);
4737                         num_aliases = count_sam_aliases(info->disp_info);
4738                         unbecome_root();
4739
4740                         pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
4741                         u_logout = account_policy_temp;
4742
4743                         unix_to_nt_time_abs(&nt_logout, u_logout);
4744
4745                         if (!pdb_get_seq_num(&seq_num))
4746                                 seq_num = time(NULL);
4747
4748                         server_role = ROLE_DOMAIN_PDC;
4749                         if (lp_server_role() == ROLE_DOMAIN_BDC)
4750                                 server_role = ROLE_DOMAIN_BDC;
4751
4752                         init_unk_info2(&ctr->info.inf2, lp_serverstring(), lp_workgroup(), global_myname(), seq_num, 
4753                                        num_users, num_groups, num_aliases, nt_logout, server_role);
4754                         break;
4755                 case 0x03:
4756                         pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
4757                         u_logout = account_policy_temp;
4758
4759                         unix_to_nt_time_abs(&nt_logout, u_logout);
4760                         
4761                         init_unk_info3(&ctr->info.inf3, nt_logout);
4762                         break;
4763                 case 0x05:
4764                         init_unk_info5(&ctr->info.inf5, global_myname());
4765                         break;
4766                 case 0x06:
4767                         init_unk_info6(&ctr->info.inf6);
4768                         break;
4769                 case 0x07:
4770                         server_role = ROLE_DOMAIN_PDC;
4771                         if (lp_server_role() == ROLE_DOMAIN_BDC)
4772                                 server_role = ROLE_DOMAIN_BDC;
4773
4774                         init_unk_info7(&ctr->info.inf7, server_role);
4775                         break;
4776                 case 0x08:
4777                         if (!pdb_get_seq_num(&seq_num))
4778                                 seq_num = time(NULL);
4779
4780                         init_unk_info8(&ctr->info.inf8, (uint32) seq_num);
4781                         break;
4782                 case 0x0c:
4783                         pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION, &account_policy_temp);
4784                         u_lock_duration = account_policy_temp;
4785                         if (u_lock_duration != -1)
4786                                 u_lock_duration *= 60;
4787
4788                         pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
4789                         u_reset_time = account_policy_temp * 60;
4790
4791                         pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
4792                         lockout = account_policy_temp;
4793         
4794                         unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
4795                         unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
4796         
4797                         init_unk_info12(&ctr->info.inf12, nt_lock_duration, nt_reset_time, (uint16)lockout);
4798                         break;
4799                 default:
4800                         return NT_STATUS_INVALID_INFO_CLASS;
4801         }
4802
4803         init_samr_r_samr_query_domain_info2(r_u, q_u->switch_value, ctr, NT_STATUS_OK);
4804
4805         DEBUG(5,("_samr_query_domain_info2: %d\n", __LINE__));
4806
4807         return r_u->status;
4808 }
4809
4810 /*******************************************************************
4811  _samr_
4812  ********************************************************************/
4813
4814 NTSTATUS _samr_set_dom_info(pipes_struct *p, SAMR_Q_SET_DOMAIN_INFO *q_u, SAMR_R_SET_DOMAIN_INFO *r_u)
4815 {
4816         time_t u_expire, u_min_age;
4817         time_t u_logout;
4818         time_t u_lock_duration, u_reset_time;
4819
4820         r_u->status = NT_STATUS_OK;
4821
4822         DEBUG(5,("_samr_set_dom_info: %d\n", __LINE__));
4823
4824         /* find the policy handle.  open a policy on it. */
4825         if (!find_policy_by_hnd(p, &q_u->domain_pol, NULL))
4826                 return NT_STATUS_INVALID_HANDLE;
4827
4828         DEBUG(5,("_samr_set_dom_info: switch_value: %d\n", q_u->switch_value));
4829
4830         switch (q_u->switch_value) {
4831                 case 0x01:
4832                         u_expire=nt_time_to_unix_abs(&q_u->ctr->info.inf1.expire);
4833                         u_min_age=nt_time_to_unix_abs(&q_u->ctr->info.inf1.min_passwordage);
4834                         
4835                         pdb_set_account_policy(AP_MIN_PASSWORD_LEN, (uint32)q_u->ctr->info.inf1.min_length_password);
4836                         pdb_set_account_policy(AP_PASSWORD_HISTORY, (uint32)q_u->ctr->info.inf1.password_history);
4837                         pdb_set_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, (uint32)q_u->ctr->info.inf1.password_properties);
4838                         pdb_set_account_policy(AP_MAX_PASSWORD_AGE, (int)u_expire);
4839                         pdb_set_account_policy(AP_MIN_PASSWORD_AGE, (int)u_min_age);
4840                         break;
4841                 case 0x02:
4842                         break;
4843                 case 0x03:
4844                         u_logout=nt_time_to_unix_abs(&q_u->ctr->info.inf3.logout);
4845                         pdb_set_account_policy(AP_TIME_TO_LOGOUT, (int)u_logout);
4846                         break;
4847                 case 0x05:
4848                         break;
4849                 case 0x06:
4850                         break;
4851                 case 0x07:
4852                         break;
4853                 case 0x0c:
4854                         u_lock_duration=nt_time_to_unix_abs(&q_u->ctr->info.inf12.duration);
4855                         if (u_lock_duration != -1)
4856                                 u_lock_duration /= 60;
4857
4858                         u_reset_time=nt_time_to_unix_abs(&q_u->ctr->info.inf12.reset_count)/60;
4859                         
4860                         pdb_set_account_policy(AP_LOCK_ACCOUNT_DURATION, (int)u_lock_duration);
4861                         pdb_set_account_policy(AP_RESET_COUNT_TIME, (int)u_reset_time);
4862                         pdb_set_account_policy(AP_BAD_ATTEMPT_LOCKOUT, (uint32)q_u->ctr->info.inf12.bad_attempt_lockout);
4863                         break;
4864                 default:
4865                         return NT_STATUS_INVALID_INFO_CLASS;
4866         }
4867
4868         init_samr_r_set_domain_info(r_u, NT_STATUS_OK);
4869
4870         DEBUG(5,("_samr_set_dom_info: %d\n", __LINE__));
4871
4872         return r_u->status;
4873 }