r13138: old fix I forgot to commit
[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**)(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 **)(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 **)(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 **)(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 **)(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 **)(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 **)(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         enum SID_NAME_USE type[MAX_SAM_ENTRIES];
1370         int i;
1371         int num_rids = q_u->num_names2;
1372         DOM_SID pol_sid;
1373         fstring sid_str;
1374         uint32  acc_granted;
1375
1376         r_u->status = NT_STATUS_OK;
1377
1378         DEBUG(5,("_samr_lookup_names: %d\n", __LINE__));
1379
1380         ZERO_ARRAY(rid);
1381         ZERO_ARRAY(type);
1382
1383         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &pol_sid, &acc_granted, NULL)) {
1384                 init_samr_r_lookup_names(p->mem_ctx, r_u, 0, NULL, NULL, NT_STATUS_OBJECT_TYPE_MISMATCH);
1385                 return r_u->status;
1386         }
1387         
1388         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 */
1389                 return r_u->status;
1390         }
1391
1392         if (num_rids > MAX_SAM_ENTRIES) {
1393                 num_rids = MAX_SAM_ENTRIES;
1394                 DEBUG(5,("_samr_lookup_names: truncating entries to %d\n", num_rids));
1395         }
1396
1397         DEBUG(5,("_samr_lookup_names: looking name on SID %s\n", sid_to_string(sid_str, &pol_sid)));
1398         
1399         for (i = 0; i < num_rids; i++) {
1400                 fstring name;
1401                 int ret;
1402
1403                 r_u->status = NT_STATUS_NONE_MAPPED;
1404                 type[i] = SID_NAME_UNKNOWN;
1405
1406                 rid [i] = 0xffffffff;
1407
1408                 ret = rpcstr_pull(name, q_u->uni_name[i].buffer, sizeof(name), q_u->uni_name[i].uni_str_len*2, 0);
1409
1410                 if (ret <= 0) {
1411                         continue;
1412                 }
1413
1414                 if (sid_check_is_builtin(&pol_sid)) {
1415                         if (lookup_builtin_name(name, &rid[i])) {
1416                                 type[i] = SID_NAME_ALIAS;
1417                         }
1418                 } else {
1419                         lookup_global_sam_name(name, &rid[i], &type[i]);
1420                 }
1421
1422                 if (type[i] != SID_NAME_UNKNOWN) {
1423                         r_u->status = NT_STATUS_OK;
1424                 }
1425         }
1426
1427         init_samr_r_lookup_names(p->mem_ctx, r_u, num_rids, rid, (uint32 *)type, r_u->status);
1428
1429         DEBUG(5,("_samr_lookup_names: %d\n", __LINE__));
1430
1431         return r_u->status;
1432 }
1433
1434 /*******************************************************************
1435  _samr_chgpasswd_user
1436  ********************************************************************/
1437
1438 NTSTATUS _samr_chgpasswd_user(pipes_struct *p, SAMR_Q_CHGPASSWD_USER *q_u, SAMR_R_CHGPASSWD_USER *r_u)
1439 {
1440         fstring user_name;
1441         fstring wks;
1442
1443         DEBUG(5,("_samr_chgpasswd_user: %d\n", __LINE__));
1444
1445         r_u->status = NT_STATUS_OK;
1446
1447         rpcstr_pull(user_name, q_u->uni_user_name.buffer, sizeof(user_name), q_u->uni_user_name.uni_str_len*2, 0);
1448         rpcstr_pull(wks, q_u->uni_dest_host.buffer, sizeof(wks), q_u->uni_dest_host.uni_str_len*2,0);
1449
1450         DEBUG(5,("samr_chgpasswd_user: user: %s wks: %s\n", user_name, wks));
1451
1452         /*
1453          * Pass the user through the NT -> unix user mapping
1454          * function.
1455          */
1456  
1457         (void)map_username(user_name);
1458  
1459         /*
1460          * UNIX username case mangling not required, pass_oem_change 
1461          * is case insensitive.
1462          */
1463
1464         r_u->status = pass_oem_change(user_name, q_u->lm_newpass.pass, q_u->lm_oldhash.hash,
1465                                 q_u->nt_newpass.pass, q_u->nt_oldhash.hash);
1466
1467         init_samr_r_chgpasswd_user(r_u, r_u->status);
1468
1469         DEBUG(5,("_samr_chgpasswd_user: %d\n", __LINE__));
1470
1471         return r_u->status;
1472 }
1473
1474 /*******************************************************************
1475 makes a SAMR_R_LOOKUP_RIDS structure.
1476 ********************************************************************/
1477
1478 static BOOL make_samr_lookup_rids(TALLOC_CTX *ctx, uint32 num_names,
1479                                   const char **names, UNIHDR **pp_hdr_name,
1480                                   UNISTR2 **pp_uni_name)
1481 {
1482         uint32 i;
1483         UNIHDR *hdr_name=NULL;
1484         UNISTR2 *uni_name=NULL;
1485
1486         *pp_uni_name = NULL;
1487         *pp_hdr_name = NULL;
1488
1489         if (num_names != 0) {
1490                 hdr_name = TALLOC_ZERO_ARRAY(ctx, UNIHDR, num_names);
1491                 if (hdr_name == NULL)
1492                         return False;
1493
1494                 uni_name = TALLOC_ZERO_ARRAY(ctx,UNISTR2, num_names);
1495                 if (uni_name == NULL)
1496                         return False;
1497         }
1498
1499         for (i = 0; i < num_names; i++) {
1500                 DEBUG(10, ("names[%d]:%s\n", i, names[i] && *names[i] ? names[i] : ""));
1501                 init_unistr2(&uni_name[i], names[i], UNI_FLAGS_NONE);
1502                 init_uni_hdr(&hdr_name[i], &uni_name[i]);
1503         }
1504
1505         *pp_uni_name = uni_name;
1506         *pp_hdr_name = hdr_name;
1507
1508         return True;
1509 }
1510
1511 /*******************************************************************
1512  _samr_lookup_rids
1513  ********************************************************************/
1514
1515 NTSTATUS _samr_lookup_rids(pipes_struct *p, SAMR_Q_LOOKUP_RIDS *q_u, SAMR_R_LOOKUP_RIDS *r_u)
1516 {
1517         const char **names;
1518         uint32 *attrs = NULL;
1519         UNIHDR *hdr_name = NULL;
1520         UNISTR2 *uni_name = NULL;
1521         DOM_SID pol_sid;
1522         int num_rids = q_u->num_rids1;
1523         uint32 acc_granted;
1524         
1525         r_u->status = NT_STATUS_OK;
1526
1527         DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
1528
1529         /* find the policy handle.  open a policy on it. */
1530         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &pol_sid, &acc_granted, NULL))
1531                 return NT_STATUS_INVALID_HANDLE;
1532
1533         if (num_rids > 1000) {
1534                 DEBUG(0, ("Got asked for %d rids (more than 1000) -- according "
1535                           "to samba4 idl this is not possible\n", num_rids));
1536                 return NT_STATUS_UNSUCCESSFUL;
1537         }
1538
1539         names = TALLOC_ZERO_ARRAY(p->mem_ctx, const char *, num_rids);
1540         attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_rids);
1541
1542         if ((num_rids != 0) && ((names == NULL) || (attrs == NULL)))
1543                 return NT_STATUS_NO_MEMORY;
1544
1545         become_root();  /* lookup_sid can require root privs */
1546         r_u->status = pdb_lookup_rids(&pol_sid, num_rids, q_u->rid,
1547                                       names, attrs);
1548         unbecome_root();
1549
1550         if(!make_samr_lookup_rids(p->mem_ctx, num_rids, names,
1551                                   &hdr_name, &uni_name))
1552                 return NT_STATUS_NO_MEMORY;
1553
1554         init_samr_r_lookup_rids(r_u, num_rids, hdr_name, uni_name, attrs);
1555
1556         DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
1557
1558         return r_u->status;
1559 }
1560
1561 /*******************************************************************
1562  _samr_open_user. Safe - gives out no passwd info.
1563  ********************************************************************/
1564
1565 NTSTATUS _samr_open_user(pipes_struct *p, SAMR_Q_OPEN_USER *q_u, SAMR_R_OPEN_USER *r_u)
1566 {
1567         SAM_ACCOUNT *sampass=NULL;
1568         DOM_SID sid;
1569         POLICY_HND domain_pol = q_u->domain_pol;
1570         POLICY_HND *user_pol = &r_u->user_pol;
1571         struct samr_info *info = NULL;
1572         SEC_DESC *psd = NULL;
1573         uint32    acc_granted;
1574         uint32    des_access = q_u->access_mask;
1575         size_t    sd_size;
1576         BOOL ret;
1577         NTSTATUS nt_status;
1578         SE_PRIV se_rights;
1579
1580         r_u->status = NT_STATUS_OK;
1581
1582         /* find the domain policy handle and get domain SID / access bits in the domain policy. */
1583         
1584         if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
1585                 return NT_STATUS_INVALID_HANDLE;
1586         
1587         nt_status = access_check_samr_function( acc_granted, 
1588                 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_user" );
1589                 
1590         if ( !NT_STATUS_IS_OK(nt_status) )
1591                 return nt_status;
1592
1593         nt_status = pdb_init_sam_talloc(p->mem_ctx, &sampass);
1594         
1595         if (!NT_STATUS_IS_OK(nt_status))
1596                 return nt_status;
1597
1598         /* append the user's RID to it */
1599         
1600         if (!sid_append_rid(&sid, q_u->user_rid))
1601                 return NT_STATUS_NO_SUCH_USER;
1602         
1603         /* check if access can be granted as requested by client. */
1604         
1605         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
1606         se_map_generic(&des_access, &usr_generic_mapping);
1607         
1608         se_priv_copy( &se_rights, &se_machine_account );
1609         se_priv_add( &se_rights, &se_add_users );
1610         
1611         nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
1612                 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access, 
1613                 &acc_granted, "_samr_open_user");
1614                 
1615         if ( !NT_STATUS_IS_OK(nt_status) )
1616                 return nt_status;
1617
1618         become_root();
1619         ret=pdb_getsampwsid(sampass, &sid);
1620         unbecome_root();
1621
1622         /* check that the SID exists in our domain. */
1623         if (ret == False) {
1624                 return NT_STATUS_NO_SUCH_USER;
1625         }
1626
1627         pdb_free_sam(&sampass);
1628
1629         /* associate the user's SID and access bits with the new handle. */
1630         if ((info = get_samr_info_by_sid(&sid)) == NULL)
1631                 return NT_STATUS_NO_MEMORY;
1632         info->acc_granted = acc_granted;
1633
1634         /* get a (unique) handle.  open a policy on it. */
1635         if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info))
1636                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1637
1638         return r_u->status;
1639 }
1640
1641 /*************************************************************************
1642  get_user_info_7. Safe. Only gives out account_name.
1643  *************************************************************************/
1644
1645 static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx, SAM_USER_INFO_7 *id7, DOM_SID *user_sid)
1646 {
1647         SAM_ACCOUNT *smbpass=NULL;
1648         BOOL ret;
1649         NTSTATUS nt_status;
1650
1651         nt_status = pdb_init_sam_talloc(mem_ctx, &smbpass);
1652         
1653         if (!NT_STATUS_IS_OK(nt_status)) {
1654                 return nt_status;
1655         }
1656
1657         become_root();
1658         ret = pdb_getsampwsid(smbpass, user_sid);
1659         unbecome_root();
1660
1661         if (ret==False) {
1662                 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1663                 return NT_STATUS_NO_SUCH_USER;
1664         }
1665
1666         DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1667
1668         ZERO_STRUCTP(id7);
1669         init_sam_user_info7(id7, pdb_get_username(smbpass) );
1670
1671         pdb_free_sam(&smbpass);
1672
1673         return NT_STATUS_OK;
1674 }
1675
1676 /*************************************************************************
1677  get_user_info_9. Only gives out primary group SID.
1678  *************************************************************************/
1679 static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx, SAM_USER_INFO_9 * id9, DOM_SID *user_sid)
1680 {
1681         SAM_ACCOUNT *smbpass=NULL;
1682         BOOL ret;
1683         NTSTATUS nt_status;
1684
1685         nt_status = pdb_init_sam_talloc(mem_ctx, &smbpass);
1686
1687         if (!NT_STATUS_IS_OK(nt_status)) {
1688                 return nt_status;
1689         }
1690
1691         become_root();
1692         ret = pdb_getsampwsid(smbpass, user_sid);
1693         unbecome_root();
1694
1695         if (ret==False) {
1696                 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1697                 return NT_STATUS_NO_SUCH_USER;
1698         }
1699
1700         DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1701
1702         ZERO_STRUCTP(id9);
1703         init_sam_user_info9(id9, pdb_get_group_rid(smbpass) );
1704
1705         pdb_free_sam(&smbpass);
1706
1707         return NT_STATUS_OK;
1708 }
1709
1710 /*************************************************************************
1711  get_user_info_16. Safe. Only gives out acb bits.
1712  *************************************************************************/
1713
1714 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx, SAM_USER_INFO_16 *id16, DOM_SID *user_sid)
1715 {
1716         SAM_ACCOUNT *smbpass=NULL;
1717         BOOL ret;
1718         NTSTATUS nt_status;
1719
1720         nt_status = pdb_init_sam_talloc(mem_ctx, &smbpass);
1721         
1722         if (!NT_STATUS_IS_OK(nt_status)) {
1723                 return nt_status;
1724         }
1725
1726         become_root();
1727         ret = pdb_getsampwsid(smbpass, user_sid);
1728         unbecome_root();
1729
1730         if (ret==False) {
1731                 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1732                 return NT_STATUS_NO_SUCH_USER;
1733         }
1734
1735         DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1736
1737         ZERO_STRUCTP(id16);
1738         init_sam_user_info16(id16, pdb_get_acct_ctrl(smbpass) );
1739
1740         pdb_free_sam(&smbpass);
1741
1742         return NT_STATUS_OK;
1743 }
1744
1745 /*************************************************************************
1746  get_user_info_18. OK - this is the killer as it gives out password info.
1747  Ensure that this is only allowed on an encrypted connection with a root
1748  user. JRA. 
1749  *************************************************************************/
1750
1751 static NTSTATUS get_user_info_18(pipes_struct *p, TALLOC_CTX *mem_ctx, SAM_USER_INFO_18 * id18, DOM_SID *user_sid)
1752 {
1753         SAM_ACCOUNT *smbpass=NULL;
1754         BOOL ret;
1755         NTSTATUS nt_status;
1756
1757         if (p->auth.auth_type != PIPE_AUTH_TYPE_NTLMSSP || p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP) {
1758                 return NT_STATUS_ACCESS_DENIED;
1759         }
1760
1761         if (p->auth.auth_level != PIPE_AUTH_LEVEL_PRIVACY) {
1762                 return NT_STATUS_ACCESS_DENIED;
1763         }
1764
1765         /*
1766          * Do *NOT* do become_root()/unbecome_root() here ! JRA.
1767          */
1768
1769         nt_status = pdb_init_sam_talloc(mem_ctx, &smbpass);
1770         
1771         if (!NT_STATUS_IS_OK(nt_status)) {
1772                 return nt_status;
1773         }
1774
1775         ret = pdb_getsampwsid(smbpass, user_sid);
1776
1777         if (ret == False) {
1778                 DEBUG(4, ("User %s not found\n", sid_string_static(user_sid)));
1779                 pdb_free_sam(&smbpass);
1780                 return (geteuid() == (uid_t)0) ? NT_STATUS_NO_SUCH_USER : NT_STATUS_ACCESS_DENIED;
1781         }
1782
1783         DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
1784
1785         if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
1786                 pdb_free_sam(&smbpass);
1787                 return NT_STATUS_ACCOUNT_DISABLED;
1788         }
1789
1790         ZERO_STRUCTP(id18);
1791         init_sam_user_info18(id18, pdb_get_lanman_passwd(smbpass), pdb_get_nt_passwd(smbpass));
1792         
1793         pdb_free_sam(&smbpass);
1794
1795         return NT_STATUS_OK;
1796 }
1797
1798 /*************************************************************************
1799  get_user_info_20
1800  *************************************************************************/
1801
1802 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx, SAM_USER_INFO_20 *id20, DOM_SID *user_sid)
1803 {
1804         SAM_ACCOUNT *sampass=NULL;
1805         BOOL ret;
1806
1807         pdb_init_sam_talloc(mem_ctx, &sampass);
1808
1809         become_root();
1810         ret = pdb_getsampwsid(sampass, user_sid);
1811         unbecome_root();
1812
1813         if (ret == False) {
1814                 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1815                 return NT_STATUS_NO_SUCH_USER;
1816         }
1817
1818         samr_clear_sam_passwd(sampass);
1819
1820         DEBUG(3,("User:[%s]\n",  pdb_get_username(sampass) ));
1821
1822         ZERO_STRUCTP(id20);
1823         init_sam_user_info20A(id20, sampass);
1824         
1825         pdb_free_sam(&sampass);
1826
1827         return NT_STATUS_OK;
1828 }
1829
1830 /*************************************************************************
1831  get_user_info_21
1832  *************************************************************************/
1833
1834 static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx, SAM_USER_INFO_21 *id21, 
1835                                  DOM_SID *user_sid, DOM_SID *domain_sid)
1836 {
1837         SAM_ACCOUNT *sampass=NULL;
1838         BOOL ret;
1839         NTSTATUS nt_status;
1840
1841         nt_status = pdb_init_sam_talloc(mem_ctx, &sampass);
1842         if (!NT_STATUS_IS_OK(nt_status)) {
1843                 return nt_status;
1844         }
1845
1846         become_root();
1847         ret = pdb_getsampwsid(sampass, user_sid);
1848         unbecome_root();
1849
1850         if (ret == False) {
1851                 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1852                 return NT_STATUS_NO_SUCH_USER;
1853         }
1854
1855         samr_clear_sam_passwd(sampass);
1856
1857         DEBUG(3,("User:[%s]\n",  pdb_get_username(sampass) ));
1858
1859         ZERO_STRUCTP(id21);
1860         nt_status = init_sam_user_info21A(id21, sampass, domain_sid);
1861         
1862         pdb_free_sam(&sampass);
1863
1864         return NT_STATUS_OK;
1865 }
1866
1867 /*******************************************************************
1868  _samr_query_userinfo
1869  ********************************************************************/
1870
1871 NTSTATUS _samr_query_userinfo(pipes_struct *p, SAMR_Q_QUERY_USERINFO *q_u, SAMR_R_QUERY_USERINFO *r_u)
1872 {
1873         SAM_USERINFO_CTR *ctr;
1874         struct samr_info *info = NULL;
1875         DOM_SID domain_sid;
1876         uint32 rid;
1877         
1878         r_u->status=NT_STATUS_OK;
1879
1880         /* search for the handle */
1881         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
1882                 return NT_STATUS_INVALID_HANDLE;
1883
1884         domain_sid = info->sid;
1885
1886         sid_split_rid(&domain_sid, &rid);
1887
1888         if (!sid_check_is_in_our_domain(&info->sid))
1889                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1890
1891         DEBUG(5,("_samr_query_userinfo: sid:%s\n", sid_string_static(&info->sid)));
1892
1893         ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_USERINFO_CTR);
1894         if (!ctr)
1895                 return NT_STATUS_NO_MEMORY;
1896
1897         ZERO_STRUCTP(ctr);
1898
1899         /* ok!  user info levels (lots: see MSDEV help), off we go... */
1900         ctr->switch_value = q_u->switch_value;
1901
1902         DEBUG(5,("_samr_query_userinfo: user info level: %d\n", q_u->switch_value));
1903
1904         switch (q_u->switch_value) {
1905         case 7:
1906                 ctr->info.id7 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_7);
1907                 if (ctr->info.id7 == NULL)
1908                         return NT_STATUS_NO_MEMORY;
1909
1910                 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_7(p->mem_ctx, ctr->info.id7, &info->sid)))
1911                         return r_u->status;
1912                 break;
1913         case 9:
1914                 ctr->info.id9 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_9);
1915                 if (ctr->info.id9 == NULL)
1916                         return NT_STATUS_NO_MEMORY;
1917
1918                 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_9(p->mem_ctx, ctr->info.id9, &info->sid)))
1919                         return r_u->status;
1920                 break;
1921         case 16:
1922                 ctr->info.id16 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_16);
1923                 if (ctr->info.id16 == NULL)
1924                         return NT_STATUS_NO_MEMORY;
1925
1926                 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_16(p->mem_ctx, ctr->info.id16, &info->sid)))
1927                         return r_u->status;
1928                 break;
1929
1930 #if 0
1931 /* whoops - got this wrong.  i think.  or don't understand what's happening. */
1932         case 17:
1933         {
1934             NTTIME expire;
1935             info = (void *)&id11;
1936
1937             expire.low = 0xffffffff;
1938             expire.high = 0x7fffffff;
1939
1940             ctr->info.id = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_17));
1941             ZERO_STRUCTP(ctr->info.id17);
1942             init_sam_user_info17(ctr->info.id17, &expire,
1943                          "BROOKFIELDS$",    /* name */
1944                          0x03ef,    /* user rid */
1945                          0x201, /* group rid */
1946                          0x0080);   /* acb info */
1947
1948             break;
1949         }
1950 #endif
1951
1952         case 18:
1953                 ctr->info.id18 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_18);
1954                 if (ctr->info.id18 == NULL)
1955                         return NT_STATUS_NO_MEMORY;
1956
1957                 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_18(p, p->mem_ctx, ctr->info.id18, &info->sid)))
1958                         return r_u->status;
1959                 break;
1960                 
1961         case 20:
1962                 ctr->info.id20 = TALLOC_ZERO_P(p->mem_ctx,SAM_USER_INFO_20);
1963                 if (ctr->info.id20 == NULL)
1964                         return NT_STATUS_NO_MEMORY;
1965                 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_20(p->mem_ctx, ctr->info.id20, &info->sid)))
1966                         return r_u->status;
1967                 break;
1968
1969         case 21:
1970                 ctr->info.id21 = TALLOC_ZERO_P(p->mem_ctx,SAM_USER_INFO_21);
1971                 if (ctr->info.id21 == NULL)
1972                         return NT_STATUS_NO_MEMORY;
1973                 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_21(p->mem_ctx, ctr->info.id21, 
1974                                                                     &info->sid, &domain_sid)))
1975                         return r_u->status;
1976                 break;
1977
1978         default:
1979                 return NT_STATUS_INVALID_INFO_CLASS;
1980         }
1981
1982         init_samr_r_query_userinfo(r_u, ctr, r_u->status);
1983
1984         DEBUG(5,("_samr_query_userinfo: %d\n", __LINE__));
1985         
1986         return r_u->status;
1987 }
1988
1989 /*******************************************************************
1990  samr_reply_query_usergroups
1991  ********************************************************************/
1992
1993 NTSTATUS _samr_query_usergroups(pipes_struct *p, SAMR_Q_QUERY_USERGROUPS *q_u, SAMR_R_QUERY_USERGROUPS *r_u)
1994 {
1995         SAM_ACCOUNT *sam_pass=NULL;
1996         struct passwd *passwd;
1997         DOM_SID  sid;
1998         DOM_SID *sids;
1999         DOM_GID *gids = NULL;
2000         size_t num_groups = 0;
2001         gid_t *unix_gids;
2002         size_t i, num_gids;
2003         uint32 acc_granted;
2004         BOOL ret;
2005         NTSTATUS result;
2006
2007         /*
2008          * from the SID in the request:
2009          * we should send back the list of DOMAIN GROUPS
2010          * the user is a member of
2011          *
2012          * and only the DOMAIN GROUPS
2013          * no ALIASES !!! neither aliases of the domain
2014          * nor aliases of the builtin SID
2015          *
2016          * JFM, 12/2/2001
2017          */
2018
2019         r_u->status = NT_STATUS_OK;
2020
2021         DEBUG(5,("_samr_query_usergroups: %d\n", __LINE__));
2022
2023         /* find the policy handle.  open a policy on it. */
2024         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &sid, &acc_granted, NULL))
2025                 return NT_STATUS_INVALID_HANDLE;
2026         
2027         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_USER_GET_GROUPS, "_samr_query_usergroups"))) {
2028                 return r_u->status;
2029         }
2030
2031         if (!sid_check_is_in_our_domain(&sid))
2032                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2033
2034         pdb_init_sam(&sam_pass);
2035         
2036         become_root();
2037         ret = pdb_getsampwsid(sam_pass, &sid);
2038         unbecome_root();
2039
2040         if (ret == False) {
2041                 pdb_free_sam(&sam_pass);
2042                 return NT_STATUS_NO_SUCH_USER;
2043         }
2044
2045         passwd = getpwnam_alloc(pdb_get_username(sam_pass));
2046         if (passwd == NULL) {
2047                 pdb_free_sam(&sam_pass);
2048                 return NT_STATUS_NO_SUCH_USER;
2049         }
2050
2051         sids = NULL;
2052
2053         become_root();
2054         result = pdb_enum_group_memberships(pdb_get_username(sam_pass),
2055                                             passwd->pw_gid,
2056                                             &sids, &unix_gids, &num_groups);
2057         unbecome_root();
2058
2059         pdb_free_sam(&sam_pass);
2060         passwd_free(&passwd);
2061
2062         if (!NT_STATUS_IS_OK(result))
2063                 return result;
2064
2065         SAFE_FREE(unix_gids);
2066
2067         gids = NULL;
2068         num_gids = 0;
2069
2070         for (i=0; i<num_groups; i++) {
2071                 uint32 rid;
2072
2073                 if (!sid_peek_check_rid(get_global_sam_sid(),
2074                                         &(sids[i]), &rid))
2075                         continue;
2076
2077                 gids = TALLOC_REALLOC_ARRAY(p->mem_ctx, gids, DOM_GID, num_gids+1);
2078                 gids[num_gids].attr= (SE_GROUP_MANDATORY|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_ENABLED);
2079                 gids[num_gids].g_rid = rid;
2080                 num_gids += 1;
2081         }
2082         SAFE_FREE(sids);
2083         
2084         /* construct the response.  lkclXXXX: gids are not copied! */
2085         init_samr_r_query_usergroups(r_u, num_groups, gids, r_u->status);
2086         
2087         DEBUG(5,("_samr_query_usergroups: %d\n", __LINE__));
2088         
2089         return r_u->status;
2090 }
2091
2092 /*******************************************************************
2093  _samr_query_dom_info
2094  ********************************************************************/
2095
2096 NTSTATUS _samr_query_dom_info(pipes_struct *p, SAMR_Q_QUERY_DOMAIN_INFO *q_u, SAMR_R_QUERY_DOMAIN_INFO *r_u)
2097 {
2098         struct samr_info *info = NULL;
2099         SAM_UNK_CTR *ctr;
2100         uint32 min_pass_len,pass_hist,flag;
2101         time_t u_expire, u_min_age;
2102         NTTIME nt_expire, nt_min_age;
2103
2104         time_t u_lock_duration, u_reset_time;
2105         NTTIME nt_lock_duration, nt_reset_time;
2106         uint32 lockout;
2107         time_t u_logout;
2108         NTTIME nt_logout;
2109
2110         uint32 account_policy_temp;
2111
2112         time_t seq_num;
2113         uint32 server_role;
2114
2115         uint32 num_users=0, num_groups=0, num_aliases=0;
2116
2117         if ((ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_UNK_CTR)) == NULL) {
2118                 return NT_STATUS_NO_MEMORY;
2119         }
2120
2121         ZERO_STRUCTP(ctr);
2122
2123         r_u->status = NT_STATUS_OK;
2124         
2125         DEBUG(5,("_samr_query_dom_info: %d\n", __LINE__));
2126         
2127         /* find the policy handle.  open a policy on it. */
2128         if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)(void *)&info)) {
2129                 return NT_STATUS_INVALID_HANDLE;
2130         }
2131         
2132         switch (q_u->switch_value) {
2133                 case 0x01:
2134                         
2135                         become_root();
2136
2137                         /* AS ROOT !!! */
2138
2139                         pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
2140                         min_pass_len = account_policy_temp;
2141
2142                         pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
2143                         pass_hist = account_policy_temp;
2144
2145                         pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
2146                         flag = account_policy_temp;
2147
2148                         pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
2149                         u_expire = account_policy_temp;
2150
2151                         pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
2152                         u_min_age = account_policy_temp;
2153
2154                         /* !AS ROOT */
2155                         
2156                         unbecome_root();
2157
2158                         unix_to_nt_time_abs(&nt_expire, u_expire);
2159                         unix_to_nt_time_abs(&nt_min_age, u_min_age);
2160
2161                         init_unk_info1(&ctr->info.inf1, (uint16)min_pass_len, (uint16)pass_hist, 
2162                                        flag, nt_expire, nt_min_age);
2163                         break;
2164                 case 0x02:
2165
2166                         become_root();
2167
2168                         /* AS ROOT !!! */
2169
2170                         num_users = count_sam_users(info->disp_info, ACB_NORMAL);
2171                         num_groups = count_sam_groups(info->disp_info);
2172                         num_aliases = count_sam_aliases(info->disp_info);
2173
2174                         pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
2175                         u_logout = account_policy_temp;
2176
2177                         unix_to_nt_time_abs(&nt_logout, u_logout);
2178
2179                         if (!pdb_get_seq_num(&seq_num))
2180                                 seq_num = time(NULL);
2181
2182                         /* !AS ROOT */
2183                         
2184                         unbecome_root();
2185
2186                         server_role = ROLE_DOMAIN_PDC;
2187                         if (lp_server_role() == ROLE_DOMAIN_BDC)
2188                                 server_role = ROLE_DOMAIN_BDC;
2189
2190                         init_unk_info2(&ctr->info.inf2, lp_serverstring(), lp_workgroup(), global_myname(), seq_num, 
2191                                        num_users, num_groups, num_aliases, nt_logout, server_role);
2192                         break;
2193                 case 0x03:
2194
2195                         become_root();
2196
2197                         /* AS ROOT !!! */
2198
2199                         {
2200                                 uint32 ul;
2201                                 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &ul);
2202                                 u_logout = (time_t)ul;
2203                         }
2204
2205                         /* !AS ROOT */
2206                         
2207                         unbecome_root();
2208
2209                         unix_to_nt_time_abs(&nt_logout, u_logout);
2210                         
2211                         init_unk_info3(&ctr->info.inf3, nt_logout);
2212                         break;
2213                 case 0x05:
2214                         init_unk_info5(&ctr->info.inf5, global_myname());
2215                         break;
2216                 case 0x06:
2217                         init_unk_info6(&ctr->info.inf6);
2218                         break;
2219                 case 0x07:
2220                         server_role = ROLE_DOMAIN_PDC;
2221                         if (lp_server_role() == ROLE_DOMAIN_BDC)
2222                                 server_role = ROLE_DOMAIN_BDC;
2223
2224                         init_unk_info7(&ctr->info.inf7, server_role);
2225                         break;
2226                 case 0x08:
2227
2228                         become_root();
2229
2230                         /* AS ROOT !!! */
2231
2232                         if (!pdb_get_seq_num(&seq_num)) {
2233                                 seq_num = time(NULL);
2234                         }
2235
2236                         /* !AS ROOT */
2237                         
2238                         unbecome_root();
2239
2240                         init_unk_info8(&ctr->info.inf8, (uint32) seq_num);
2241                         break;
2242                 case 0x0c:
2243
2244                         become_root();
2245
2246                         /* AS ROOT !!! */
2247
2248                         pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION, &account_policy_temp);
2249                         u_lock_duration = account_policy_temp;
2250                         if (u_lock_duration != -1) {
2251                                 u_lock_duration *= 60;
2252                         }
2253
2254                         pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
2255                         u_reset_time = account_policy_temp * 60;
2256
2257                         pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
2258                         lockout = account_policy_temp;
2259
2260                         /* !AS ROOT */
2261                         
2262                         unbecome_root();
2263
2264                         unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
2265                         unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
2266         
2267                         init_unk_info12(&ctr->info.inf12, nt_lock_duration, nt_reset_time, (uint16)lockout);
2268                         break;
2269                 default:
2270                         return NT_STATUS_INVALID_INFO_CLASS;
2271                 }
2272         
2273
2274         init_samr_r_query_dom_info(r_u, q_u->switch_value, ctr, NT_STATUS_OK);
2275         
2276         DEBUG(5,("_samr_query_dom_info: %d\n", __LINE__));
2277         
2278         return r_u->status;
2279 }
2280
2281 /* W2k3 seems to use the same check for all 3 objects that can be created via
2282  * SAMR, if you try to create for example "Dialup" as an alias it says
2283  * "NT_STATUS_USER_EXISTS". This is racy, but we can't really lock the user
2284  * database. */
2285
2286 static NTSTATUS can_create(TALLOC_CTX *mem_ctx, const char *new_name)
2287 {
2288         enum SID_NAME_USE type;
2289         BOOL result;
2290
2291         DEBUG(10, ("Checking whether [%s] can be created\n", new_name));
2292
2293         become_root();
2294         /* Lookup in our local databases (only LOOKUP_NAME_ISOLATED set)
2295          * whether the name already exists */
2296         result = lookup_name(mem_ctx, new_name, LOOKUP_NAME_ISOLATED,
2297                              NULL, NULL, NULL, &type);
2298         unbecome_root();
2299
2300         if (!result) {
2301                 DEBUG(10, ("%s does not exist, can create it\n", new_name));
2302                 return NT_STATUS_OK;
2303         }
2304
2305         DEBUG(5, ("trying to create %s, exists as %s\n",
2306                   new_name, sid_type_lookup(type)));
2307
2308         if (type == SID_NAME_DOM_GRP) {
2309                 return NT_STATUS_GROUP_EXISTS;
2310         }
2311         if (type == SID_NAME_ALIAS) {
2312                 return NT_STATUS_ALIAS_EXISTS;
2313         }
2314
2315         /* Yes, the default is NT_STATUS_USER_EXISTS */
2316         return NT_STATUS_USER_EXISTS;
2317 }
2318
2319 /*******************************************************************
2320  _samr_create_user
2321  Create an account, can be either a normal user or a machine.
2322  This funcion will need to be updated for bdc/domain trusts.
2323  ********************************************************************/
2324
2325 NTSTATUS _samr_create_user(pipes_struct *p, SAMR_Q_CREATE_USER *q_u, SAMR_R_CREATE_USER *r_u)
2326 {
2327         SAM_ACCOUNT *sam_pass=NULL;
2328         fstring account;
2329         DOM_SID sid;
2330         pstring add_script;
2331         POLICY_HND dom_pol = q_u->domain_pol;
2332         UNISTR2 user_account = q_u->uni_name;
2333         uint16 acb_info = q_u->acb_info;
2334         POLICY_HND *user_pol = &r_u->user_pol;
2335         struct samr_info *info = NULL;
2336         BOOL ret;
2337         NTSTATUS nt_status;
2338         struct passwd *pw;
2339         uint32 acc_granted;
2340         SEC_DESC *psd;
2341         size_t    sd_size;
2342         uint32 new_rid = 0;
2343         /* check this, when giving away 'add computer to domain' privs */
2344         uint32    des_access = GENERIC_RIGHTS_USER_ALL_ACCESS;
2345         BOOL can_add_account = False;
2346         SE_PRIV se_rights;
2347         DISP_INFO *disp_info = NULL;
2348
2349         /* Get the domain SID stored in the domain policy */
2350         if (!get_lsa_policy_samr_sid(p, &dom_pol, &sid, &acc_granted, &disp_info))
2351                 return NT_STATUS_INVALID_HANDLE;
2352
2353         if (!NT_STATUS_IS_OK(nt_status = access_check_samr_function(acc_granted, SA_RIGHT_DOMAIN_CREATE_USER, "_samr_create_user"))) {
2354                 return nt_status;
2355         }
2356
2357         if (!(acb_info == ACB_NORMAL || acb_info == ACB_DOMTRUST || acb_info == ACB_WSTRUST || acb_info == ACB_SVRTRUST)) { 
2358                 /* Match Win2k, and return NT_STATUS_INVALID_PARAMETER if 
2359                    this parameter is not an account type */
2360                 return NT_STATUS_INVALID_PARAMETER;
2361         }
2362
2363         rpcstr_pull(account, user_account.buffer, sizeof(account), user_account.uni_str_len*2, 0);
2364         strlower_m(account);
2365
2366         nt_status = can_create(p->mem_ctx, account);
2367         if (!NT_STATUS_IS_OK(nt_status)) {
2368                 return nt_status;
2369         }
2370
2371         /*********************************************************************
2372          * HEADS UP!  If we have to create a new user account, we have to get 
2373          * a new RID from somewhere.  This used to be done by the passdb 
2374          * backend. It has been moved into idmap now.  Since idmap is now 
2375          * wrapped up behind winbind, this means you have to run winbindd if you
2376          * want new accounts to get a new RID when "enable rid algorithm = no".
2377          * Tough.  We now have a uniform way of allocating RIDs regardless
2378          * of what ever passdb backend people may use.
2379          *                                             --jerry (2003-07-10)
2380          *********************************************************************/
2381
2382         pw = Get_Pwnam(account);
2383
2384         /* determine which user right we need to check based on the acb_info */
2385         
2386         if ( acb_info & ACB_WSTRUST )
2387         {
2388                 pstrcpy(add_script, lp_addmachine_script());
2389                 se_priv_copy( &se_rights, &se_machine_account );
2390                 can_add_account = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
2391         } 
2392         /* usrmgr.exe (and net rpc trustdom grant) creates a normal user 
2393            account for domain trusts and changes the ACB flags later */
2394         else if ( acb_info & ACB_NORMAL && (account[strlen(account)-1] != '$') )
2395         {
2396                 pstrcpy(add_script, lp_adduser_script());
2397                 se_priv_copy( &se_rights, &se_add_users );
2398                 can_add_account = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
2399         } 
2400         else    /* implicit assumption of a BDC or domain trust account here (we already check the flags earlier) */
2401         {
2402                 pstrcpy(add_script, lp_addmachine_script());
2403                 if ( lp_enable_privileges() ) {
2404                         /* only Domain Admins can add a BDC or domain trust */
2405                         se_priv_copy( &se_rights, &se_priv_none );
2406                         can_add_account = nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS );
2407         }
2408         }
2409                 
2410         DEBUG(5, ("_samr_create_user: %s can add this account : %s\n",
2411                 p->pipe_user_name, can_add_account ? "True":"False" ));
2412                 
2413         /********** BEGIN Admin BLOCK **********/
2414
2415         if ( can_add_account )
2416                 become_root();
2417
2418         if ( !pw ) {
2419                 if (*add_script) {
2420                         int add_ret;
2421
2422                         all_string_sub(add_script, "%u", account, sizeof(add_script));
2423                         add_ret = smbrun(add_script,NULL);
2424                         DEBUG(add_ret ? 0 : 3,("_samr_create_user: Running the command `%s' gave %d\n", add_script, add_ret));
2425                 }
2426         }
2427
2428         /* implicit call to getpwnam() next.  we have a valid SID coming out of this call */
2429
2430         flush_pwnam_cache();
2431         nt_status = pdb_init_sam_new(&sam_pass, account, new_rid);
2432
2433         /* this code is order such that we have no unnecessary retuns 
2434            out of the admin block of code */    
2435            
2436         if ( NT_STATUS_IS_OK(nt_status) ) {
2437                 pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
2438         
2439                 if ( !(ret = pdb_add_sam_account(sam_pass)) ) {
2440                         pdb_free_sam(&sam_pass);
2441                         DEBUG(0, ("could not add user/computer %s to passdb.  Check permissions?\n", 
2442                                 account));
2443                         nt_status = NT_STATUS_ACCESS_DENIED;
2444                 }
2445         }
2446                 
2447         if ( can_add_account )
2448                 unbecome_root();
2449
2450         /********** END Admin BLOCK **********/
2451         
2452         /* now check for failure */
2453         
2454         if ( !NT_STATUS_IS_OK(nt_status) )
2455                 return nt_status;
2456                         
2457         /* Get the user's SID */
2458         
2459         sid_copy(&sid, pdb_get_user_sid(sam_pass));
2460         
2461         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
2462         se_map_generic(&des_access, &usr_generic_mapping);
2463         
2464         nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
2465                 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access, 
2466                 &acc_granted, "_samr_create_user");
2467                 
2468         if ( !NT_STATUS_IS_OK(nt_status) ) {
2469                 return nt_status;
2470         }
2471
2472         /* associate the user's SID with the new handle. */
2473         if ((info = get_samr_info_by_sid(&sid)) == NULL) {
2474                 pdb_free_sam(&sam_pass);
2475                 return NT_STATUS_NO_MEMORY;
2476         }
2477
2478         ZERO_STRUCTP(info);
2479         info->sid = sid;
2480         info->acc_granted = acc_granted;
2481
2482         /* get a (unique) handle.  open a policy on it. */
2483         if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info)) {
2484                 pdb_free_sam(&sam_pass);
2485                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2486         }
2487
2488         /* After a "set" ensure we have no cached display info. */
2489         force_flush_samr_cache(info->disp_info);
2490
2491         r_u->user_rid=pdb_get_user_rid(sam_pass);
2492
2493         r_u->access_granted = acc_granted;
2494
2495         pdb_free_sam(&sam_pass);
2496
2497         return NT_STATUS_OK;
2498 }
2499
2500 /*******************************************************************
2501  samr_reply_connect_anon
2502  ********************************************************************/
2503
2504 NTSTATUS _samr_connect_anon(pipes_struct *p, SAMR_Q_CONNECT_ANON *q_u, SAMR_R_CONNECT_ANON *r_u)
2505 {
2506         struct samr_info *info = NULL;
2507         uint32    des_access = q_u->access_mask;
2508
2509         /* Access check */
2510
2511         if (!pipe_access_check(p)) {
2512                 DEBUG(3, ("access denied to samr_connect_anon\n"));
2513                 r_u->status = NT_STATUS_ACCESS_DENIED;
2514                 return r_u->status;
2515         }
2516
2517         /* set up the SAMR connect_anon response */
2518
2519         r_u->status = NT_STATUS_OK;
2520
2521         /* associate the user's SID with the new handle. */
2522         if ((info = get_samr_info_by_sid(NULL)) == NULL)
2523                 return NT_STATUS_NO_MEMORY;
2524
2525         /* don't give away the farm but this is probably ok.  The SA_RIGHT_SAM_ENUM_DOMAINS
2526            was observed from a win98 client trying to enumerate users (when configured  
2527            user level access control on shares)   --jerry */
2528            
2529         se_map_generic( &des_access, &sam_generic_mapping );
2530         info->acc_granted = des_access & (SA_RIGHT_SAM_ENUM_DOMAINS|SA_RIGHT_SAM_OPEN_DOMAIN);
2531         
2532         info->status = q_u->unknown_0;
2533
2534         /* get a (unique) handle.  open a policy on it. */
2535         if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2536                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2537
2538         return r_u->status;
2539 }
2540
2541 /*******************************************************************
2542  samr_reply_connect
2543  ********************************************************************/
2544
2545 NTSTATUS _samr_connect(pipes_struct *p, SAMR_Q_CONNECT *q_u, SAMR_R_CONNECT *r_u)
2546 {
2547         struct samr_info *info = NULL;
2548         SEC_DESC *psd = NULL;
2549         uint32    acc_granted;
2550         uint32    des_access = q_u->access_mask;
2551         NTSTATUS  nt_status;
2552         size_t    sd_size;
2553
2554
2555         DEBUG(5,("_samr_connect: %d\n", __LINE__));
2556
2557         /* Access check */
2558
2559         if (!pipe_access_check(p)) {
2560                 DEBUG(3, ("access denied to samr_connect\n"));
2561                 r_u->status = NT_STATUS_ACCESS_DENIED;
2562                 return r_u->status;
2563         }
2564
2565         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2566         se_map_generic(&des_access, &sam_generic_mapping);
2567         
2568         nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
2569                 NULL, 0, des_access, &acc_granted, "_samr_connect");
2570         
2571         if ( !NT_STATUS_IS_OK(nt_status) ) 
2572                 return nt_status;
2573
2574         r_u->status = NT_STATUS_OK;
2575
2576         /* associate the user's SID and access granted with the new handle. */
2577         if ((info = get_samr_info_by_sid(NULL)) == NULL)
2578                 return NT_STATUS_NO_MEMORY;
2579
2580         info->acc_granted = acc_granted;
2581         info->status = q_u->access_mask;
2582
2583         /* get a (unique) handle.  open a policy on it. */
2584         if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2585                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2586
2587         DEBUG(5,("_samr_connect: %d\n", __LINE__));
2588
2589         return r_u->status;
2590 }
2591
2592 /*******************************************************************
2593  samr_connect4
2594  ********************************************************************/
2595
2596 NTSTATUS _samr_connect4(pipes_struct *p, SAMR_Q_CONNECT4 *q_u, SAMR_R_CONNECT4 *r_u)
2597 {
2598         struct samr_info *info = NULL;
2599         SEC_DESC *psd = NULL;
2600         uint32    acc_granted;
2601         uint32    des_access = q_u->access_mask;
2602         NTSTATUS  nt_status;
2603         size_t    sd_size;
2604
2605
2606         DEBUG(5,("_samr_connect4: %d\n", __LINE__));
2607
2608         /* Access check */
2609
2610         if (!pipe_access_check(p)) {
2611                 DEBUG(3, ("access denied to samr_connect4\n"));
2612                 r_u->status = NT_STATUS_ACCESS_DENIED;
2613                 return r_u->status;
2614         }
2615
2616         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2617         se_map_generic(&des_access, &sam_generic_mapping);
2618         
2619         nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
2620                 NULL, 0, des_access, &acc_granted, "_samr_connect4");
2621         
2622         if ( !NT_STATUS_IS_OK(nt_status) ) 
2623                 return nt_status;
2624
2625         r_u->status = NT_STATUS_OK;
2626
2627         /* associate the user's SID and access granted with the new handle. */
2628         if ((info = get_samr_info_by_sid(NULL)) == NULL)
2629                 return NT_STATUS_NO_MEMORY;
2630
2631         info->acc_granted = acc_granted;
2632         info->status = q_u->access_mask;
2633
2634         /* get a (unique) handle.  open a policy on it. */
2635         if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2636                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2637
2638         DEBUG(5,("_samr_connect: %d\n", __LINE__));
2639
2640         return r_u->status;
2641 }
2642
2643 /*******************************************************************
2644  samr_connect5
2645  ********************************************************************/
2646
2647 NTSTATUS _samr_connect5(pipes_struct *p, SAMR_Q_CONNECT5 *q_u, SAMR_R_CONNECT5 *r_u)
2648 {
2649         struct samr_info *info = NULL;
2650         SEC_DESC *psd = NULL;
2651         uint32    acc_granted;
2652         uint32    des_access = q_u->access_mask;
2653         NTSTATUS  nt_status;
2654         POLICY_HND pol;
2655         size_t    sd_size;
2656
2657
2658         DEBUG(5,("_samr_connect5: %d\n", __LINE__));
2659
2660         ZERO_STRUCTP(r_u);
2661
2662         /* Access check */
2663
2664         if (!pipe_access_check(p)) {
2665                 DEBUG(3, ("access denied to samr_connect5\n"));
2666                 r_u->status = NT_STATUS_ACCESS_DENIED;
2667                 return r_u->status;
2668         }
2669
2670         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2671         se_map_generic(&des_access, &sam_generic_mapping);
2672         
2673         nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
2674                 NULL, 0, des_access, &acc_granted, "_samr_connect5");
2675         
2676         if ( !NT_STATUS_IS_OK(nt_status) ) 
2677                 return nt_status;
2678
2679         /* associate the user's SID and access granted with the new handle. */
2680         if ((info = get_samr_info_by_sid(NULL)) == NULL)
2681                 return NT_STATUS_NO_MEMORY;
2682
2683         info->acc_granted = acc_granted;
2684         info->status = q_u->access_mask;
2685
2686         /* get a (unique) handle.  open a policy on it. */
2687         if (!create_policy_hnd(p, &pol, free_samr_info, (void *)info))
2688                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2689
2690         DEBUG(5,("_samr_connect: %d\n", __LINE__));
2691
2692         init_samr_r_connect5(r_u, &pol, NT_STATUS_OK);
2693
2694         return r_u->status;
2695 }
2696
2697 /**********************************************************************
2698  api_samr_lookup_domain
2699  **********************************************************************/
2700
2701 NTSTATUS _samr_lookup_domain(pipes_struct *p, SAMR_Q_LOOKUP_DOMAIN *q_u, SAMR_R_LOOKUP_DOMAIN *r_u)
2702 {
2703         struct samr_info *info;
2704         fstring domain_name;
2705         DOM_SID sid;
2706
2707         r_u->status = NT_STATUS_OK;
2708
2709         if (!find_policy_by_hnd(p, &q_u->connect_pol, (void**)(void *)&info))
2710                 return NT_STATUS_INVALID_HANDLE;
2711
2712         /* win9x user manager likes to use SA_RIGHT_SAM_ENUM_DOMAINS here.  
2713            Reverted that change so we will work with RAS servers again */
2714
2715         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted, 
2716                 SA_RIGHT_SAM_OPEN_DOMAIN, "_samr_lookup_domain"))) 
2717         {
2718                 return r_u->status;
2719         }
2720
2721         rpcstr_pull(domain_name, q_u->uni_domain.buffer, sizeof(domain_name), q_u->uni_domain.uni_str_len*2, 0);
2722
2723         ZERO_STRUCT(sid);
2724
2725         if (strequal(domain_name, builtin_domain_name())) {
2726                 sid_copy(&sid, &global_sid_Builtin);
2727         } else {
2728                 if (!secrets_fetch_domain_sid(domain_name, &sid)) {
2729                         r_u->status = NT_STATUS_NO_SUCH_DOMAIN;
2730                 }
2731         }
2732
2733         DEBUG(2,("Returning domain sid for domain %s -> %s\n", domain_name, sid_string_static(&sid)));
2734
2735         init_samr_r_lookup_domain(r_u, &sid, r_u->status);
2736
2737         return r_u->status;
2738 }
2739
2740 /******************************************************************
2741 makes a SAMR_R_ENUM_DOMAINS structure.
2742 ********************************************************************/
2743
2744 static BOOL make_enum_domains(TALLOC_CTX *ctx, SAM_ENTRY **pp_sam,
2745                         UNISTR2 **pp_uni_name, uint32 num_sam_entries, fstring doms[])
2746 {
2747         uint32 i;
2748         SAM_ENTRY *sam;
2749         UNISTR2 *uni_name;
2750
2751         DEBUG(5, ("make_enum_domains\n"));
2752
2753         *pp_sam = NULL;
2754         *pp_uni_name = NULL;
2755
2756         if (num_sam_entries == 0)
2757                 return True;
2758
2759         sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_sam_entries);
2760         uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_sam_entries);
2761
2762         if (sam == NULL || uni_name == NULL)
2763                 return False;
2764
2765         for (i = 0; i < num_sam_entries; i++) {
2766                 init_unistr2(&uni_name[i], doms[i], UNI_FLAGS_NONE);
2767                 init_sam_entry(&sam[i], &uni_name[i], 0);
2768         }
2769
2770         *pp_sam = sam;
2771         *pp_uni_name = uni_name;
2772
2773         return True;
2774 }
2775
2776 /**********************************************************************
2777  api_samr_enum_domains
2778  **********************************************************************/
2779
2780 NTSTATUS _samr_enum_domains(pipes_struct *p, SAMR_Q_ENUM_DOMAINS *q_u, SAMR_R_ENUM_DOMAINS *r_u)
2781 {
2782         struct samr_info *info;
2783         uint32 num_entries = 2;
2784         fstring dom[2];
2785         const char *name;
2786
2787         r_u->status = NT_STATUS_OK;
2788         
2789         if (!find_policy_by_hnd(p, &q_u->pol, (void**)(void *)&info))
2790                 return NT_STATUS_INVALID_HANDLE;
2791         
2792         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted, SA_RIGHT_SAM_ENUM_DOMAINS, "_samr_enum_domains"))) {
2793                 return r_u->status;
2794         }
2795
2796         name = get_global_sam_name();
2797
2798         fstrcpy(dom[0],name);
2799         strupper_m(dom[0]);
2800         fstrcpy(dom[1],"Builtin");
2801
2802         if (!make_enum_domains(p->mem_ctx, &r_u->sam, &r_u->uni_dom_name, num_entries, dom))
2803                 return NT_STATUS_NO_MEMORY;
2804
2805         init_samr_r_enum_domains(r_u, q_u->start_idx + num_entries, num_entries);
2806
2807         return r_u->status;
2808 }
2809
2810 /*******************************************************************
2811  api_samr_open_alias
2812  ********************************************************************/
2813
2814 NTSTATUS _samr_open_alias(pipes_struct *p, SAMR_Q_OPEN_ALIAS *q_u, SAMR_R_OPEN_ALIAS *r_u)
2815 {
2816         DOM_SID sid;
2817         POLICY_HND domain_pol = q_u->dom_pol;
2818         uint32 alias_rid = q_u->rid_alias;
2819         POLICY_HND *alias_pol = &r_u->pol;
2820         struct    samr_info *info = NULL;
2821         SEC_DESC *psd = NULL;
2822         uint32    acc_granted;
2823         uint32    des_access = q_u->access_mask;
2824         size_t    sd_size;
2825         NTSTATUS  status;
2826         SE_PRIV se_rights;
2827
2828         r_u->status = NT_STATUS_OK;
2829
2830         /* find the domain policy and get the SID / access bits stored in the domain policy */
2831         
2832         if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
2833                 return NT_STATUS_INVALID_HANDLE;
2834         
2835         status = access_check_samr_function(acc_granted, 
2836                 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_alias");
2837                 
2838         if ( !NT_STATUS_IS_OK(status) ) 
2839                 return status;
2840
2841         /* append the alias' RID to it */
2842         
2843         if (!sid_append_rid(&sid, alias_rid))
2844                 return NT_STATUS_NO_SUCH_ALIAS;
2845                 
2846         /*check if access can be granted as requested by client. */
2847         
2848         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &ali_generic_mapping, NULL, 0);
2849         se_map_generic(&des_access,&ali_generic_mapping);
2850         
2851         se_priv_copy( &se_rights, &se_add_users );
2852         
2853         
2854         status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
2855                 &se_rights, GENERIC_RIGHTS_ALIAS_WRITE, des_access, 
2856                 &acc_granted, "_samr_open_alias");
2857                 
2858         if ( !NT_STATUS_IS_OK(status) )
2859                 return status;
2860
2861         {
2862                 /* Check we actually have the requested alias */
2863                 enum SID_NAME_USE type;
2864                 BOOL result;
2865
2866                 become_root();
2867                 result = lookup_sid(NULL, &sid, NULL, NULL, &type);
2868                 unbecome_root();
2869
2870                 if (!result || (type != SID_NAME_ALIAS)) {
2871                         return NT_STATUS_NO_SUCH_ALIAS;
2872                 }
2873         }
2874
2875         /* associate the alias SID with the new handle. */
2876         if ((info = get_samr_info_by_sid(&sid)) == NULL)
2877                 return NT_STATUS_NO_MEMORY;
2878                 
2879         info->acc_granted = acc_granted;
2880
2881         /* get a (unique) handle.  open a policy on it. */
2882         if (!create_policy_hnd(p, alias_pol, free_samr_info, (void *)info))
2883                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2884
2885         return r_u->status;
2886 }
2887
2888 /*******************************************************************
2889  set_user_info_7
2890  ********************************************************************/
2891 static NTSTATUS set_user_info_7(TALLOC_CTX *mem_ctx,
2892                                 const SAM_USER_INFO_7 *id7, SAM_ACCOUNT *pwd)
2893 {
2894         fstring new_name;
2895         NTSTATUS rc;
2896
2897         if (id7 == NULL) {
2898                 DEBUG(5, ("set_user_info_7: NULL id7\n"));
2899                 pdb_free_sam(&pwd);
2900                 return NT_STATUS_ACCESS_DENIED;
2901         }
2902
2903         if(!rpcstr_pull(new_name, id7->uni_name.buffer, sizeof(new_name), id7->uni_name.uni_str_len*2, 0)) {
2904                 DEBUG(5, ("set_user_info_7: failed to get new username\n"));
2905                 pdb_free_sam(&pwd);
2906                 return NT_STATUS_ACCESS_DENIED;
2907         }
2908
2909         /* check to see if the new username already exists.  Note: we can't
2910            reliably lock all backends, so there is potentially the 
2911            possibility that a user can be created in between this check and
2912            the rename.  The rename should fail, but may not get the
2913            exact same failure status code.  I think this is small enough
2914            of a window for this type of operation and the results are
2915            simply that the rename fails with a slightly different status
2916            code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
2917
2918         rc = can_create(mem_ctx, new_name);
2919         if (!NT_STATUS_IS_OK(rc)) {
2920                 return rc;
2921         }
2922
2923         rc = pdb_rename_sam_account(pwd, new_name);
2924
2925         pdb_free_sam(&pwd);
2926         return rc;
2927 }
2928
2929 /*******************************************************************
2930  set_user_info_16
2931  ********************************************************************/
2932
2933 static BOOL set_user_info_16(const SAM_USER_INFO_16 *id16, SAM_ACCOUNT *pwd)
2934 {
2935         if (id16 == NULL) {
2936                 DEBUG(5, ("set_user_info_16: NULL id16\n"));
2937                 pdb_free_sam(&pwd);
2938                 return False;
2939         }
2940         
2941         /* FIX ME: check if the value is really changed --metze */
2942         if (!pdb_set_acct_ctrl(pwd, id16->acb_info, PDB_CHANGED)) {
2943                 pdb_free_sam(&pwd);
2944                 return False;
2945         }
2946
2947         if(!pdb_update_sam_account(pwd)) {
2948                 pdb_free_sam(&pwd);
2949                 return False;
2950         }
2951
2952         pdb_free_sam(&pwd);
2953
2954         return True;
2955 }
2956
2957 /*******************************************************************
2958  set_user_info_18
2959  ********************************************************************/
2960
2961 static BOOL set_user_info_18(SAM_USER_INFO_18 *id18, SAM_ACCOUNT *pwd)
2962 {
2963
2964         if (id18 == NULL) {
2965                 DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
2966                 pdb_free_sam(&pwd);
2967                 return False;
2968         }
2969  
2970         if (!pdb_set_lanman_passwd (pwd, id18->lm_pwd, PDB_CHANGED)) {
2971                 pdb_free_sam(&pwd);
2972                 return False;
2973         }
2974         if (!pdb_set_nt_passwd     (pwd, id18->nt_pwd, PDB_CHANGED)) {
2975                 pdb_free_sam(&pwd);
2976                 return False;
2977         }
2978         if (!pdb_set_pass_changed_now (pwd)) {
2979                 pdb_free_sam(&pwd);
2980                 return False; 
2981         }
2982  
2983         if(!pdb_update_sam_account(pwd)) {
2984                 pdb_free_sam(&pwd);
2985                 return False;
2986         }
2987
2988         pdb_free_sam(&pwd);
2989         return True;
2990 }
2991
2992 /*******************************************************************
2993  The GROUPSID field in the SAM_ACCOUNT changed. Try to tell unix.
2994  ********************************************************************/
2995 static BOOL set_unix_primary_group(SAM_ACCOUNT *sampass)
2996 {
2997         struct group *grp;
2998         gid_t gid;
2999
3000         if (!NT_STATUS_IS_OK(sid_to_gid(pdb_get_group_sid(sampass),
3001                                         &gid))) {
3002                 DEBUG(2,("Could not get gid for primary group of "
3003                          "user %s\n", pdb_get_username(sampass)));
3004                 return False;
3005         }
3006
3007         grp = getgrgid(gid);
3008
3009         if (grp == NULL) {
3010                 DEBUG(2,("Could not find primary group %lu for "
3011                          "user %s\n", (unsigned long)gid, 
3012                          pdb_get_username(sampass)));
3013                 return False;
3014         }
3015
3016         if (smb_set_primary_group(grp->gr_name,
3017                                   pdb_get_username(sampass)) != 0) {
3018                 DEBUG(2,("Could not set primary group for user %s to "
3019                          "%s\n",
3020                          pdb_get_username(sampass), grp->gr_name));
3021                 return False;
3022         }
3023
3024         return True;
3025 }
3026         
3027
3028 /*******************************************************************
3029  set_user_info_20
3030  ********************************************************************/
3031
3032 static BOOL set_user_info_20(SAM_USER_INFO_20 *id20, SAM_ACCOUNT *pwd)
3033 {
3034         if (id20 == NULL) {
3035                 DEBUG(5, ("set_user_info_20: NULL id20\n"));
3036                 return False;
3037         }
3038  
3039         copy_id20_to_sam_passwd(pwd, id20);
3040
3041         /* write the change out */
3042         if(!pdb_update_sam_account(pwd)) {
3043                 pdb_free_sam(&pwd);
3044                 return False;
3045         }
3046
3047         pdb_free_sam(&pwd);
3048
3049         return True;
3050 }
3051 /*******************************************************************
3052  set_user_info_21
3053  ********************************************************************/
3054
3055 static BOOL set_user_info_21(SAM_USER_INFO_21 *id21, SAM_ACCOUNT *pwd)
3056 {
3057  
3058         if (id21 == NULL) {
3059                 DEBUG(5, ("set_user_info_21: NULL id21\n"));
3060                 return False;
3061         }
3062  
3063         copy_id21_to_sam_passwd(pwd, id21);
3064  
3065         /*
3066          * The funny part about the previous two calls is
3067          * that pwd still has the password hashes from the
3068          * passdb entry.  These have not been updated from
3069          * id21.  I don't know if they need to be set.    --jerry
3070          */
3071  
3072         if (IS_SAM_CHANGED(pwd, PDB_GROUPSID))
3073                 set_unix_primary_group(pwd);
3074
3075         /* write the change out */
3076         if(!pdb_update_sam_account(pwd)) {
3077                 pdb_free_sam(&pwd);
3078                 return False;
3079         }
3080
3081         pdb_free_sam(&pwd);
3082
3083         return True;
3084 }
3085
3086 /*******************************************************************
3087  set_user_info_23
3088  ********************************************************************/
3089
3090 static BOOL set_user_info_23(SAM_USER_INFO_23 *id23, SAM_ACCOUNT *pwd)
3091 {
3092         pstring plaintext_buf;
3093         uint32 len;
3094         uint16 acct_ctrl;
3095  
3096         if (id23 == NULL) {
3097                 DEBUG(5, ("set_user_info_23: NULL id23\n"));
3098                 return False;
3099         }
3100  
3101         DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
3102                   pdb_get_username(pwd)));
3103
3104         acct_ctrl = pdb_get_acct_ctrl(pwd);
3105
3106         if (!decode_pw_buffer(id23->pass, plaintext_buf, 256, &len, STR_UNICODE)) {
3107                 pdb_free_sam(&pwd);
3108                 return False;
3109         }
3110   
3111         if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
3112                 pdb_free_sam(&pwd);
3113                 return False;
3114         }
3115  
3116         copy_id23_to_sam_passwd(pwd, id23);
3117  
3118         /* if it's a trust account, don't update /etc/passwd */
3119         if (    ( (acct_ctrl &  ACB_DOMTRUST) == ACB_DOMTRUST ) ||
3120                 ( (acct_ctrl &  ACB_WSTRUST) ==  ACB_WSTRUST) ||
3121                 ( (acct_ctrl &  ACB_SVRTRUST) ==  ACB_SVRTRUST) ) {
3122                 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
3123         } else  {
3124                 /* update the UNIX password */
3125                 if (lp_unix_password_sync() ) {
3126                         struct passwd *passwd = Get_Pwnam(pdb_get_username(pwd));
3127                         if (!passwd) {
3128                                 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
3129                         }
3130                         
3131                         if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
3132                                 pdb_free_sam(&pwd);
3133                                 return False;
3134                         }
3135                 }
3136         }
3137  
3138         ZERO_STRUCT(plaintext_buf);
3139  
3140         if (IS_SAM_CHANGED(pwd, PDB_GROUPSID))
3141                 set_unix_primary_group(pwd);
3142
3143         if(!pdb_update_sam_account(pwd)) {
3144                 pdb_free_sam(&pwd);
3145                 return False;
3146         }
3147  
3148         pdb_free_sam(&pwd);
3149
3150         return True;
3151 }
3152
3153 /*******************************************************************
3154  set_user_info_pw
3155  ********************************************************************/
3156
3157 static BOOL set_user_info_pw(uint8 *pass, SAM_ACCOUNT *pwd)
3158 {
3159         uint32 len;
3160         pstring plaintext_buf;
3161         uint16 acct_ctrl;
3162  
3163         DEBUG(5, ("Attempting administrator password change for user %s\n",
3164                   pdb_get_username(pwd)));
3165
3166         acct_ctrl = pdb_get_acct_ctrl(pwd);
3167
3168         ZERO_STRUCT(plaintext_buf);
3169  
3170         if (!decode_pw_buffer(pass, plaintext_buf, 256, &len, STR_UNICODE)) {
3171                 pdb_free_sam(&pwd);
3172                 return False;
3173         }
3174
3175         if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
3176                 pdb_free_sam(&pwd);
3177                 return False;
3178         }
3179  
3180         /* if it's a trust account, don't update /etc/passwd */
3181         if ( ( (acct_ctrl &  ACB_DOMTRUST) == ACB_DOMTRUST ) ||
3182                 ( (acct_ctrl &  ACB_WSTRUST) ==  ACB_WSTRUST) ||
3183                 ( (acct_ctrl &  ACB_SVRTRUST) ==  ACB_SVRTRUST) ) {
3184                 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
3185         } else {
3186                 /* update the UNIX password */
3187                 if (lp_unix_password_sync()) {
3188                         struct passwd *passwd = Get_Pwnam(pdb_get_username(pwd));
3189                         if (!passwd) {
3190                                 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
3191                         }
3192                         
3193                         if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
3194                                 pdb_free_sam(&pwd);
3195                                 return False;
3196                         }
3197                 }
3198         }
3199  
3200         ZERO_STRUCT(plaintext_buf);
3201  
3202         DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
3203  
3204         /* update the SAMBA password */
3205         if(!pdb_update_sam_account(pwd)) {
3206                 pdb_free_sam(&pwd);
3207                 return False;
3208         }
3209
3210         pdb_free_sam(&pwd);
3211
3212         return True;
3213 }
3214
3215 /*******************************************************************
3216  samr_reply_set_userinfo
3217  ********************************************************************/
3218
3219 NTSTATUS _samr_set_userinfo(pipes_struct *p, SAMR_Q_SET_USERINFO *q_u, SAMR_R_SET_USERINFO *r_u)
3220 {
3221         SAM_ACCOUNT *pwd = NULL;
3222         DOM_SID sid;
3223         POLICY_HND *pol = &q_u->pol;
3224         uint16 switch_value = q_u->switch_value;
3225         SAM_USERINFO_CTR *ctr = q_u->ctr;
3226         uint32 acc_granted;
3227         uint32 acc_required;
3228         BOOL ret;
3229         BOOL has_enough_rights = False;
3230         uint32 acb_info;
3231         DISP_INFO *disp_info = NULL;
3232
3233         DEBUG(5, ("_samr_set_userinfo: %d\n", __LINE__));
3234
3235         r_u->status = NT_STATUS_OK;
3236
3237         /* find the policy handle.  open a policy on it. */
3238         if (!get_lsa_policy_samr_sid(p, pol, &sid, &acc_granted, &disp_info))
3239                 return NT_STATUS_INVALID_HANDLE;
3240
3241         /* observed when joining an XP client to a Samba domain */
3242         
3243         acc_required = SA_RIGHT_USER_SET_PASSWORD | SA_RIGHT_USER_SET_ATTRIBUTES | SA_RIGHT_USER_ACCT_FLAGS_EXPIRY;     
3244
3245         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, acc_required, "_samr_set_userinfo"))) {
3246                 return r_u->status;
3247         }
3248
3249         DEBUG(5, ("_samr_set_userinfo: sid:%s, level:%d\n", sid_string_static(&sid), switch_value));
3250
3251         if (ctr == NULL) {
3252                 DEBUG(5, ("_samr_set_userinfo: NULL info level\n"));
3253                 return NT_STATUS_INVALID_INFO_CLASS;
3254         }
3255         
3256         pdb_init_sam(&pwd);     
3257         
3258         become_root();
3259         ret = pdb_getsampwsid(pwd, &sid);
3260         unbecome_root();
3261         
3262         if ( !ret ) {
3263                 pdb_free_sam(&pwd);
3264                 return NT_STATUS_NO_SUCH_USER;
3265         }
3266         
3267         /* deal with machine password changes differently from userinfo changes */
3268         /* check to see if we have the sufficient rights */
3269         
3270         acb_info = pdb_get_acct_ctrl(pwd);
3271         if ( acb_info & ACB_WSTRUST ) 
3272                 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_machine_account);
3273         else if ( acb_info & ACB_NORMAL )
3274                 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
3275         else if ( acb_info & (ACB_SVRTRUST|ACB_DOMTRUST) ) {
3276                 if ( lp_enable_privileges() )
3277                         has_enough_rights = nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS );
3278         }
3279         
3280         DEBUG(5, ("_samr_set_userinfo: %s does%s possess sufficient rights\n",
3281                 p->pipe_user_name, has_enough_rights ? "" : " not"));
3282
3283         /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
3284         
3285         if ( has_enough_rights )                                
3286                 become_root(); 
3287         
3288         /* ok!  user info levels (lots: see MSDEV help), off we go... */
3289
3290         switch (switch_value) {
3291                 case 18:
3292                         if (!set_user_info_18(ctr->info.id18, pwd))
3293                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3294                         break;
3295
3296                 case 24:
3297                         if (!p->session_key.length) {
3298                                 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
3299                         }
3300                         SamOEMhashBlob(ctr->info.id24->pass, 516, &p->session_key);
3301
3302                         dump_data(100, (char *)ctr->info.id24->pass, 516);
3303
3304                         if (!set_user_info_pw(ctr->info.id24->pass, pwd))
3305                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3306                         break;
3307
3308                 case 25:
3309 #if 0
3310                         /*
3311                          * Currently we don't really know how to unmarshall
3312                          * the level 25 struct, and the password encryption
3313                          * is different. This is a placeholder for when we
3314                          * do understand it. In the meantime just return INVALID
3315                          * info level and W2K SP2 drops down to level 23... JRA.
3316                          */
3317
3318                         if (!p->session_key.length) {
3319                                 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
3320                         }
3321                         SamOEMhashBlob(ctr->info.id25->pass, 532, &p->session_key);
3322
3323                         dump_data(100, (char *)ctr->info.id25->pass, 532);
3324
3325                         if (!set_user_info_pw(ctr->info.id25->pass, &sid))
3326                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3327                         break;
3328 #endif
3329                         r_u->status = NT_STATUS_INVALID_INFO_CLASS;
3330                         break;
3331
3332                 case 23:
3333                         if (!p->session_key.length) {
3334                                 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
3335                         }
3336                         SamOEMhashBlob(ctr->info.id23->pass, 516, &p->session_key);
3337
3338                         dump_data(100, (char *)ctr->info.id23->pass, 516);
3339
3340                         if (!set_user_info_23(ctr->info.id23, pwd))
3341                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3342                         break;
3343
3344                 default:
3345                         r_u->status = NT_STATUS_INVALID_INFO_CLASS;
3346         }
3347
3348         
3349         if ( has_enough_rights )                                
3350                 unbecome_root();
3351                 
3352         /* ================ END SeMachineAccountPrivilege BLOCK ================ */
3353
3354         if (NT_STATUS_IS_OK(r_u->status)) {
3355                 force_flush_samr_cache(disp_info);
3356         }
3357
3358         return r_u->status;
3359 }
3360
3361 /*******************************************************************
3362  samr_reply_set_userinfo2
3363  ********************************************************************/
3364
3365 NTSTATUS _samr_set_userinfo2(pipes_struct *p, SAMR_Q_SET_USERINFO2 *q_u, SAMR_R_SET_USERINFO2 *r_u)
3366 {
3367         SAM_ACCOUNT *pwd = NULL;
3368         DOM_SID sid;
3369         SAM_USERINFO_CTR *ctr = q_u->ctr;
3370         POLICY_HND *pol = &q_u->pol;
3371         uint16 switch_value = q_u->switch_value;
3372         uint32 acc_granted;
3373         uint32 acc_required;
3374         BOOL ret;
3375         BOOL has_enough_rights = False;
3376         uint32 acb_info;
3377         DISP_INFO *disp_info = NULL;
3378
3379         DEBUG(5, ("samr_reply_set_userinfo2: %d\n", __LINE__));
3380
3381         r_u->status = NT_STATUS_OK;
3382
3383         /* find the policy handle.  open a policy on it. */
3384         if (!get_lsa_policy_samr_sid(p, pol, &sid, &acc_granted, &disp_info))
3385                 return NT_STATUS_INVALID_HANDLE;
3386
3387         /* observed when joining XP client to Samba domain */
3388                 
3389         acc_required = SA_RIGHT_USER_SET_PASSWORD | SA_RIGHT_USER_SET_ATTRIBUTES | SA_RIGHT_USER_ACCT_FLAGS_EXPIRY;
3390         
3391         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, acc_required, "_samr_set_userinfo2"))) {
3392                 return r_u->status;
3393         }
3394
3395         DEBUG(5, ("samr_reply_set_userinfo2: sid:%s\n", sid_string_static(&sid)));
3396
3397         if (ctr == NULL) {
3398                 DEBUG(5, ("samr_reply_set_userinfo2: NULL info level\n"));
3399                 return NT_STATUS_INVALID_INFO_CLASS;
3400         }
3401
3402         switch_value=ctr->switch_value;
3403
3404         pdb_init_sam(&pwd);     
3405         
3406         become_root();
3407         ret = pdb_getsampwsid(pwd, &sid);
3408         unbecome_root();
3409         
3410         if ( !ret ) {
3411                 pdb_free_sam(&pwd);
3412                 return NT_STATUS_NO_SUCH_USER;
3413         }
3414         
3415         acb_info = pdb_get_acct_ctrl(pwd);
3416         if ( acb_info & ACB_WSTRUST ) 
3417                 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_machine_account);
3418         else if ( acb_info & ACB_NORMAL )
3419                 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
3420         else if ( acb_info & (ACB_SVRTRUST|ACB_DOMTRUST) ) {
3421                 if ( lp_enable_privileges() )
3422                         has_enough_rights = nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS );
3423         }
3424         
3425         DEBUG(5, ("_samr_set_userinfo: %s does%s possess sufficient rights\n",
3426                 p->pipe_user_name, has_enough_rights ? "" : " not"));
3427
3428         /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
3429         
3430         if ( has_enough_rights )                                
3431                 become_root(); 
3432         
3433         /* ok!  user info levels (lots: see MSDEV help), off we go... */
3434         
3435         switch (switch_value) {
3436                 case 7:
3437                         r_u->status = set_user_info_7(p->mem_ctx,
3438                                                       ctr->info.id7, pwd);
3439                         break;
3440                 case 16:
3441                         if (!set_user_info_16(ctr->info.id16, pwd))
3442                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3443                         break;
3444                 case 18:
3445                         /* Used by AS/U JRA. */
3446                         if (!set_user_info_18(ctr->info.id18, pwd))
3447                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3448                         break;
3449                 case 20:
3450                         if (!set_user_info_20(ctr->info.id20, pwd))
3451                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3452                         break;
3453                 case 21:
3454                         if (!set_user_info_21(ctr->info.id21, pwd))
3455                                 return NT_STATUS_ACCESS_DENIED;
3456                         break;
3457                 default:
3458                         r_u->status = NT_STATUS_INVALID_INFO_CLASS;
3459         }
3460
3461         if ( has_enough_rights )                                
3462                 unbecome_root();
3463                 
3464         /* ================ END SeMachineAccountPrivilege BLOCK ================ */
3465
3466         if (NT_STATUS_IS_OK(r_u->status)) {
3467                 force_flush_samr_cache(disp_info);
3468         }
3469
3470         return r_u->status;
3471 }
3472
3473 /*********************************************************************
3474  _samr_query_aliasmem
3475 *********************************************************************/
3476
3477 NTSTATUS _samr_query_useraliases(pipes_struct *p, SAMR_Q_QUERY_USERALIASES *q_u, SAMR_R_QUERY_USERALIASES *r_u)
3478 {
3479         size_t num_alias_rids;
3480         uint32 *alias_rids;
3481         struct samr_info *info = NULL;
3482         size_t i;
3483                 
3484         NTSTATUS ntstatus1;
3485         NTSTATUS ntstatus2;
3486
3487         DOM_SID *members;
3488         BOOL res;
3489
3490         r_u->status = NT_STATUS_OK;
3491
3492         DEBUG(5,("_samr_query_useraliases: %d\n", __LINE__));
3493
3494         /* find the policy handle.  open a policy on it. */
3495         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
3496                 return NT_STATUS_INVALID_HANDLE;
3497                 
3498         ntstatus1 = access_check_samr_function(info->acc_granted, SA_RIGHT_DOMAIN_LOOKUP_ALIAS_BY_MEM, "_samr_query_useraliases");
3499         ntstatus2 = access_check_samr_function(info->acc_granted, SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_query_useraliases");
3500         
3501         if (!NT_STATUS_IS_OK(ntstatus1) || !NT_STATUS_IS_OK(ntstatus2)) {
3502                 if (!(NT_STATUS_EQUAL(ntstatus1,NT_STATUS_ACCESS_DENIED) && NT_STATUS_IS_OK(ntstatus2)) &&
3503                     !(NT_STATUS_EQUAL(ntstatus1,NT_STATUS_ACCESS_DENIED) && NT_STATUS_IS_OK(ntstatus1))) {
3504                         return (NT_STATUS_IS_OK(ntstatus1)) ? ntstatus2 : ntstatus1;
3505                 }
3506         }               
3507
3508         if (!sid_check_is_domain(&info->sid) &&
3509             !sid_check_is_builtin(&info->sid))
3510                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
3511
3512         members = TALLOC_ARRAY(p->mem_ctx, DOM_SID, q_u->num_sids1);
3513
3514         if (members == NULL)
3515                 return NT_STATUS_NO_MEMORY;
3516
3517         for (i=0; i<q_u->num_sids1; i++)
3518                 sid_copy(&members[i], &q_u->sid[i].sid);
3519
3520         alias_rids = NULL;
3521         num_alias_rids = 0;
3522
3523         become_root();
3524         res = pdb_enum_alias_memberships(p->mem_ctx, &info->sid, members,
3525                                          q_u->num_sids1,
3526                                          &alias_rids, &num_alias_rids);
3527         unbecome_root();
3528
3529         if (!res)
3530                 return NT_STATUS_UNSUCCESSFUL;
3531
3532         init_samr_r_query_useraliases(r_u, num_alias_rids, alias_rids,
3533                                       NT_STATUS_OK);
3534         return NT_STATUS_OK;
3535 }
3536
3537 /*********************************************************************
3538  _samr_query_aliasmem
3539 *********************************************************************/
3540
3541 NTSTATUS _samr_query_aliasmem(pipes_struct *p, SAMR_Q_QUERY_ALIASMEM *q_u, SAMR_R_QUERY_ALIASMEM *r_u)
3542 {
3543         size_t i;
3544         size_t num_sids = 0;
3545         DOM_SID2 *sid;
3546         DOM_SID *sids=NULL;
3547
3548         DOM_SID alias_sid;
3549
3550         uint32 acc_granted;
3551
3552         /* find the policy handle.  open a policy on it. */
3553         if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted, NULL)) 
3554                 return NT_STATUS_INVALID_HANDLE;
3555         
3556         if (!NT_STATUS_IS_OK(r_u->status = 
3557                 access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_GET_MEMBERS, "_samr_query_aliasmem"))) {
3558                 return r_u->status;
3559         }
3560
3561         DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
3562
3563         if (!pdb_enum_aliasmem(&alias_sid, &sids, &num_sids))
3564                 return NT_STATUS_NO_SUCH_ALIAS;
3565
3566         sid = TALLOC_ZERO_ARRAY(p->mem_ctx, DOM_SID2, num_sids);        
3567         if (num_sids!=0 && sid == NULL) {
3568                 SAFE_FREE(sids);
3569                 return NT_STATUS_NO_MEMORY;
3570         }
3571
3572         for (i = 0; i < num_sids; i++) {
3573                 init_dom_sid2(&sid[i], &sids[i]);
3574         }
3575
3576         init_samr_r_query_aliasmem(r_u, num_sids, sid, NT_STATUS_OK);
3577
3578         SAFE_FREE(sids);
3579
3580         return NT_STATUS_OK;
3581 }
3582
3583 static void add_uid_to_array_unique(uid_t uid, uid_t **uids, int *num)
3584 {
3585         int i;
3586
3587         for (i=0; i<*num; i++) {
3588                 if ((*uids)[i] == uid)
3589                         return;
3590         }
3591         
3592         *uids = SMB_REALLOC_ARRAY(*uids, uid_t, *num+1);
3593
3594         if (*uids == NULL)
3595                 return;
3596
3597         (*uids)[*num] = uid;
3598         *num += 1;
3599 }
3600
3601
3602 static BOOL get_memberuids(gid_t gid, uid_t **uids, int *num)
3603 {
3604         struct group *grp;
3605         char **gr;
3606         struct sys_pwent *userlist, *user;
3607  
3608         *uids = NULL;
3609         *num = 0;
3610
3611         /* We only look at our own sam, so don't care about imported stuff */
3612
3613         winbind_off();
3614
3615         if ((grp = getgrgid(gid)) == NULL) {
3616                 winbind_on();
3617                 return False;
3618         }
3619
3620         /* Primary group members */
3621
3622         userlist = getpwent_list();
3623
3624         for (user = userlist; user != NULL; user = user->next) {
3625                 if (user->pw_gid != gid)
3626                         continue;
3627                 add_uid_to_array_unique(user->pw_uid, uids, num);
3628         }
3629
3630         pwent_free(userlist);
3631
3632         /* Secondary group members */
3633
3634         for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
3635                 struct passwd *pw = getpwnam(*gr);
3636
3637                 if (pw == NULL)
3638                         continue;
3639                 add_uid_to_array_unique(pw->pw_uid, uids, num);
3640         }
3641
3642         winbind_on();
3643
3644         return True;
3645 }       
3646
3647 /*********************************************************************
3648  _samr_query_groupmem
3649 *********************************************************************/
3650
3651 NTSTATUS _samr_query_groupmem(pipes_struct *p, SAMR_Q_QUERY_GROUPMEM *q_u, SAMR_R_QUERY_GROUPMEM *r_u)
3652 {
3653         DOM_SID group_sid;
3654         fstring group_sid_str;
3655         size_t i, num_members;
3656
3657         uint32 *rid=NULL;
3658         uint32 *attr=NULL;
3659
3660         uint32 acc_granted;
3661
3662         NTSTATUS result;
3663
3664         /* find the policy handle.  open a policy on it. */
3665         if (!get_lsa_policy_samr_sid(p, &q_u->group_pol, &group_sid, &acc_granted, NULL)) 
3666                 return NT_STATUS_INVALID_HANDLE;
3667                 
3668         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_GET_MEMBERS, "_samr_query_groupmem"))) {
3669                 return r_u->status;
3670         }
3671                 
3672         sid_to_string(group_sid_str, &group_sid);
3673         DEBUG(10, ("sid is %s\n", group_sid_str));
3674
3675         if (!sid_check_is_in_our_domain(&group_sid)) {
3676                 DEBUG(3, ("sid %s is not in our domain\n", group_sid_str));
3677                 return NT_STATUS_NO_SUCH_GROUP;
3678         }
3679
3680         DEBUG(10, ("lookup on Domain SID\n"));
3681
3682         become_root();
3683         result = pdb_enum_group_members(p->mem_ctx, &group_sid,
3684                                         &rid, &num_members);
3685         unbecome_root();
3686
3687         if (!NT_STATUS_IS_OK(result))
3688                 return result;
3689
3690         attr=TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_members);
3691         
3692         if ((num_members!=0) && (rid==NULL))
3693                 return NT_STATUS_NO_MEMORY;
3694         
3695         for (i=0; i<num_members; i++)
3696                 attr[i] = SID_NAME_USER;
3697
3698         init_samr_r_query_groupmem(r_u, num_members, rid, attr, NT_STATUS_OK);
3699
3700         return NT_STATUS_OK;
3701 }
3702
3703 /*********************************************************************
3704  _samr_add_aliasmem
3705 *********************************************************************/
3706
3707 NTSTATUS _samr_add_aliasmem(pipes_struct *p, SAMR_Q_ADD_ALIASMEM *q_u, SAMR_R_ADD_ALIASMEM *r_u)
3708 {
3709         DOM_SID alias_sid;
3710         uint32 acc_granted;
3711         SE_PRIV se_rights;
3712         BOOL can_add_accounts;
3713         BOOL ret;
3714         DISP_INFO *disp_info = NULL;
3715
3716         /* Find the policy handle. Open a policy on it. */
3717         if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted, &disp_info)) 
3718                 return NT_STATUS_INVALID_HANDLE;
3719         
3720         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_ADD_MEMBER, "_samr_add_aliasmem"))) {
3721                 return r_u->status;
3722         }
3723                 
3724         DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
3725         
3726         se_priv_copy( &se_rights, &se_add_users );
3727         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3728
3729         /******** BEGIN SeAddUsers BLOCK *********/
3730         
3731         if ( can_add_accounts )
3732                 become_root();
3733         
3734         ret = pdb_add_aliasmem(&alias_sid, &q_u->sid.sid);
3735         
3736         if ( can_add_accounts )
3737                 unbecome_root();
3738                 
3739         /******** END SeAddUsers BLOCK *********/
3740         
3741         if (ret) {
3742                 force_flush_samr_cache(disp_info);
3743         }
3744
3745         return ret ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3746 }
3747
3748 /*********************************************************************
3749  _samr_del_aliasmem
3750 *********************************************************************/
3751
3752 NTSTATUS _samr_del_aliasmem(pipes_struct *p, SAMR_Q_DEL_ALIASMEM *q_u, SAMR_R_DEL_ALIASMEM *r_u)
3753 {
3754         DOM_SID alias_sid;
3755         uint32 acc_granted;
3756         SE_PRIV se_rights;
3757         BOOL can_add_accounts;
3758         BOOL ret;
3759         DISP_INFO *disp_info = NULL;
3760
3761         /* Find the policy handle. Open a policy on it. */
3762         if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted, &disp_info)) 
3763                 return NT_STATUS_INVALID_HANDLE;
3764         
3765         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_REMOVE_MEMBER, "_samr_del_aliasmem"))) {
3766                 return r_u->status;
3767         }
3768         
3769         DEBUG(10, ("_samr_del_aliasmem:sid is %s\n",
3770                    sid_string_static(&alias_sid)));
3771
3772         se_priv_copy( &se_rights, &se_add_users );
3773         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3774
3775         /******** BEGIN SeAddUsers BLOCK *********/
3776         
3777         if ( can_add_accounts )
3778                 become_root();
3779
3780         ret = pdb_del_aliasmem(&alias_sid, &q_u->sid.sid);
3781         
3782         if ( can_add_accounts )
3783                 unbecome_root();
3784                 
3785         /******** END SeAddUsers BLOCK *********/
3786         
3787         if (ret) {
3788                 force_flush_samr_cache(disp_info);
3789         }
3790
3791         return ret ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3792 }
3793
3794 /*********************************************************************
3795  _samr_add_groupmem
3796 *********************************************************************/
3797
3798 NTSTATUS _samr_add_groupmem(pipes_struct *p, SAMR_Q_ADD_GROUPMEM *q_u, SAMR_R_ADD_GROUPMEM *r_u)
3799 {
3800         DOM_SID group_sid;
3801         DOM_SID user_sid;
3802         fstring group_sid_str;
3803         uid_t uid;
3804         struct passwd *pwd;
3805         struct group *grp;
3806         fstring grp_name;
3807         GROUP_MAP map;
3808         NTSTATUS ret;
3809         SAM_ACCOUNT *sam_user=NULL;
3810         BOOL check;
3811         uint32 acc_granted;
3812         SE_PRIV se_rights;
3813         BOOL can_add_accounts;
3814         DISP_INFO *disp_info = NULL;
3815
3816         /* Find the policy handle. Open a policy on it. */
3817         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted, &disp_info)) 
3818                 return NT_STATUS_INVALID_HANDLE;
3819         
3820         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_ADD_MEMBER, "_samr_add_groupmem"))) {
3821                 return r_u->status;
3822         }
3823
3824         sid_to_string(group_sid_str, &group_sid);
3825         DEBUG(10, ("sid is %s\n", group_sid_str));
3826
3827         if (sid_compare(&group_sid, get_global_sam_sid())<=0)
3828                 return NT_STATUS_NO_SUCH_GROUP;
3829
3830         DEBUG(10, ("lookup on Domain SID\n"));
3831
3832         if(!get_domain_group_from_sid(group_sid, &map))
3833                 return NT_STATUS_NO_SUCH_GROUP;
3834
3835         sid_copy(&user_sid, get_global_sam_sid());
3836         sid_append_rid(&user_sid, q_u->rid);
3837
3838         ret = pdb_init_sam(&sam_user);
3839         if (!NT_STATUS_IS_OK(ret))
3840                 return ret;
3841         
3842         check = pdb_getsampwsid(sam_user, &user_sid);
3843         
3844         if (check != True) {
3845                 pdb_free_sam(&sam_user);
3846                 return NT_STATUS_NO_SUCH_USER;
3847         }
3848
3849         /* check a real user exist before we run the script to add a user to a group */
3850         if (!NT_STATUS_IS_OK(sid_to_uid(pdb_get_user_sid(sam_user), &uid))) {
3851                 pdb_free_sam(&sam_user);
3852                 return NT_STATUS_NO_SUCH_USER;
3853         }
3854
3855         pdb_free_sam(&sam_user);
3856
3857         if ((pwd=getpwuid_alloc(uid)) == NULL) {
3858                 return NT_STATUS_NO_SUCH_USER;
3859         }
3860
3861         if ((grp=getgrgid(map.gid)) == NULL) {
3862                 passwd_free(&pwd);
3863                 return NT_STATUS_NO_SUCH_GROUP;
3864         }
3865
3866         /* we need to copy the name otherwise it's overloaded in user_in_unix_group_list */
3867         fstrcpy(grp_name, grp->gr_name);
3868
3869         /* if the user is already in the group */
3870         if(user_in_unix_group_list(pwd->pw_name, grp_name)) {
3871                 passwd_free(&pwd);
3872                 return NT_STATUS_MEMBER_IN_GROUP;
3873         }
3874
3875         se_priv_copy( &se_rights, &se_add_users );
3876         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3877
3878         /******** BEGIN SeAddUsers BLOCK *********/
3879         
3880         if ( can_add_accounts )
3881                 become_root();
3882                 
3883         /* 
3884          * ok, the group exist, the user exist, the user is not in the group,
3885          *
3886          * we can (finally) add it to the group !
3887          */
3888
3889         smb_add_user_group(grp_name, pwd->pw_name);
3890
3891         if ( can_add_accounts )
3892                 unbecome_root();
3893                 
3894         /******** END SeAddUsers BLOCK *********/
3895         
3896         /* check if the user has been added then ... */
3897         if(!user_in_unix_group_list(pwd->pw_name, grp_name)) {
3898                 passwd_free(&pwd);
3899                 return NT_STATUS_MEMBER_NOT_IN_GROUP;           /* don't know what to reply else */
3900         }
3901
3902         passwd_free(&pwd);
3903
3904         force_flush_samr_cache(disp_info);
3905
3906         return NT_STATUS_OK;
3907 }
3908
3909 /*********************************************************************
3910  _samr_del_groupmem
3911 *********************************************************************/
3912
3913 NTSTATUS _samr_del_groupmem(pipes_struct *p, SAMR_Q_DEL_GROUPMEM *q_u, SAMR_R_DEL_GROUPMEM *r_u)
3914 {
3915         DOM_SID group_sid;
3916         DOM_SID user_sid;
3917         SAM_ACCOUNT *sam_pass=NULL;
3918         GROUP_MAP map;
3919         fstring grp_name;
3920         struct group *grp;
3921         uint32 acc_granted;
3922         SE_PRIV se_rights;
3923         BOOL can_add_accounts;
3924         DISP_INFO *disp_info = NULL;
3925
3926         /*
3927          * delete the group member named q_u->rid
3928          * who is a member of the sid associated with the handle
3929          * the rid is a user's rid as the group is a domain group.
3930          */
3931
3932         /* Find the policy handle. Open a policy on it. */
3933         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted, &disp_info)) 
3934                 return NT_STATUS_INVALID_HANDLE;
3935         
3936         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_REMOVE_MEMBER, "_samr_del_groupmem"))) {
3937                 return r_u->status;
3938         }
3939                 
3940         if (!sid_check_is_in_our_domain(&group_sid))
3941                 return NT_STATUS_NO_SUCH_GROUP;
3942
3943         sid_copy(&user_sid, get_global_sam_sid());
3944         sid_append_rid(&user_sid, q_u->rid);
3945
3946         if (!get_domain_group_from_sid(group_sid, &map))
3947                 return NT_STATUS_NO_SUCH_GROUP;
3948
3949         if ((grp=getgrgid(map.gid)) == NULL)
3950                 return NT_STATUS_NO_SUCH_GROUP;
3951
3952         /* we need to copy the name otherwise it's overloaded in user_in_group_list */
3953         fstrcpy(grp_name, grp->gr_name);
3954
3955         /* check if the user exists before trying to remove it from the group */
3956         pdb_init_sam(&sam_pass);
3957         if (!pdb_getsampwsid(sam_pass, &user_sid)) {
3958                 DEBUG(5,("User %s doesn't exist.\n", pdb_get_username(sam_pass)));
3959                 pdb_free_sam(&sam_pass);
3960                 return NT_STATUS_NO_SUCH_USER;
3961         }
3962
3963         /* if the user is not in the group */
3964         if (!user_in_unix_group_list(pdb_get_username(sam_pass), grp_name)) {
3965                 pdb_free_sam(&sam_pass);
3966                 return NT_STATUS_MEMBER_NOT_IN_GROUP;
3967         }
3968         
3969
3970         se_priv_copy( &se_rights, &se_add_users );
3971         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3972
3973         /******** BEGIN SeAddUsers BLOCK *********/
3974         
3975         if ( can_add_accounts )
3976                 become_root();
3977                 
3978         smb_delete_user_group(grp_name, pdb_get_username(sam_pass));
3979
3980         if ( can_add_accounts )
3981                 unbecome_root();
3982                 
3983         /******** END SeAddUsers BLOCK *********/
3984         
3985         /* check if the user has been removed then ... */
3986         if (user_in_unix_group_list(pdb_get_username(sam_pass), grp_name)) {
3987                 pdb_free_sam(&sam_pass);
3988                 return NT_STATUS_ACCESS_DENIED;         /* don't know what to reply else */
3989         }
3990         
3991         pdb_free_sam(&sam_pass);
3992
3993         force_flush_samr_cache(disp_info);
3994
3995         return NT_STATUS_OK;
3996
3997 }
3998
3999 /****************************************************************************
4000  Delete a UNIX user on demand.
4001 ****************************************************************************/
4002
4003 static int smb_delete_user(const char *unix_user)
4004 {
4005         pstring del_script;
4006         int ret;
4007
4008         pstrcpy(del_script, lp_deluser_script());
4009         if (! *del_script)
4010                 return -1;
4011         all_string_sub(del_script, "%u", unix_user, sizeof(del_script));
4012         ret = smbrun(del_script,NULL);
4013         flush_pwnam_cache();
4014         DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
4015
4016         return ret;
4017 }
4018
4019 /*********************************************************************
4020  _samr_delete_dom_user
4021 *********************************************************************/
4022
4023 NTSTATUS _samr_delete_dom_user(pipes_struct *p, SAMR_Q_DELETE_DOM_USER *q_u, SAMR_R_DELETE_DOM_USER *r_u )
4024 {
4025         DOM_SID user_sid;
4026         SAM_ACCOUNT *sam_pass=NULL;
4027         uint32 acc_granted;
4028         BOOL can_add_accounts;
4029         BOOL ret;
4030         DISP_INFO *disp_info = NULL;
4031
4032         DEBUG(5, ("_samr_delete_dom_user: %d\n", __LINE__));
4033
4034         /* Find the policy handle. Open a policy on it. */
4035         if (!get_lsa_policy_samr_sid(p, &q_u->user_pol, &user_sid, &acc_granted, &disp_info)) 
4036                 return NT_STATUS_INVALID_HANDLE;
4037                 
4038         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_user"))) {
4039                 return r_u->status;
4040         }
4041                 
4042         if (!sid_check_is_in_our_domain(&user_sid))
4043                 return NT_STATUS_CANNOT_DELETE;
4044
4045         /* check if the user exists before trying to delete */
4046         pdb_init_sam(&sam_pass);
4047         if(!pdb_getsampwsid(sam_pass, &user_sid)) {
4048                 DEBUG(5,("_samr_delete_dom_user:User %s doesn't exist.\n", 
4049                         sid_string_static(&user_sid)));
4050                 pdb_free_sam(&sam_pass);
4051                 return NT_STATUS_NO_SUCH_USER;
4052         }
4053         
4054         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
4055
4056         /******** BEGIN SeAddUsers BLOCK *********/
4057         
4058         if ( can_add_accounts )
4059                 become_root();
4060
4061         /* First delete the samba side....
4062            code is order to prevent unnecessary returns out of the admin 
4063            block of code */
4064            
4065         if ( (ret = pdb_delete_sam_account(sam_pass)) == True ) {
4066                 /*
4067                  * Now delete the unix side ....
4068                  * note: we don't check if the delete really happened
4069                  * as the script is not necessary present
4070                  * and maybe the sysadmin doesn't want to delete the unix side
4071                  */
4072                 smb_delete_user( pdb_get_username(sam_pass) );
4073         }
4074         
4075         if ( can_add_accounts )
4076                 unbecome_root();
4077                 
4078         /******** END SeAddUsers BLOCK *********/
4079                 
4080         if ( !ret ) {
4081                 DEBUG(5,("_samr_delete_dom_user:Failed to delete entry for user %s.\n", pdb_get_username(sam_pass)));
4082                 pdb_free_sam(&sam_pass);
4083                 return NT_STATUS_CANNOT_DELETE;
4084         }
4085
4086
4087         pdb_free_sam(&sam_pass);
4088
4089         if (!close_policy_hnd(p, &q_u->user_pol))
4090                 return NT_STATUS_OBJECT_NAME_INVALID;
4091
4092         force_flush_samr_cache(disp_info);
4093
4094         return NT_STATUS_OK;
4095 }
4096
4097 /*********************************************************************
4098  _samr_delete_dom_group
4099 *********************************************************************/
4100
4101 NTSTATUS _samr_delete_dom_group(pipes_struct *p, SAMR_Q_DELETE_DOM_GROUP *q_u, SAMR_R_DELETE_DOM_GROUP *r_u)
4102 {
4103         DOM_SID group_sid;
4104         DOM_SID dom_sid;
4105         uint32 group_rid;
4106         fstring group_sid_str;
4107         gid_t gid;
4108         struct group *grp;
4109         GROUP_MAP map;
4110         uint32 acc_granted;
4111         SE_PRIV se_rights;
4112         BOOL can_add_accounts;
4113         BOOL ret;
4114         DISP_INFO *disp_info = NULL;
4115
4116         DEBUG(5, ("samr_delete_dom_group: %d\n", __LINE__));
4117
4118         /* Find the policy handle. Open a policy on it. */
4119         if (!get_lsa_policy_samr_sid(p, &q_u->group_pol, &group_sid, &acc_granted, &disp_info)) 
4120                 return NT_STATUS_INVALID_HANDLE;
4121                 
4122         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_group"))) {
4123                 return r_u->status;
4124         }
4125                 
4126         sid_copy(&dom_sid, &group_sid);
4127         sid_to_string(group_sid_str, &dom_sid);
4128         sid_split_rid(&dom_sid, &group_rid);
4129
4130         DEBUG(10, ("sid is %s\n", group_sid_str));
4131
4132         /* we check if it's our SID before deleting */
4133         if (!sid_equal(&dom_sid, get_global_sam_sid()))
4134                 return NT_STATUS_NO_SUCH_GROUP;
4135
4136         DEBUG(10, ("lookup on Domain SID\n"));
4137
4138         if(!get_domain_group_from_sid(group_sid, &map))
4139                 return NT_STATUS_NO_SUCH_GROUP;
4140
4141         gid=map.gid;
4142
4143         /* check if group really exists */
4144         if ( (grp=getgrgid(gid)) == NULL) 
4145                 return NT_STATUS_NO_SUCH_GROUP;
4146
4147         se_priv_copy( &se_rights, &se_add_users );
4148         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4149
4150         /******** BEGIN SeAddUsers BLOCK *********/
4151         
4152         if ( can_add_accounts )
4153                 become_root();
4154
4155         /* delete mapping first */
4156         
4157         if ( (ret = pdb_delete_group_mapping_entry(group_sid)) == True ) {
4158                 smb_delete_group( grp->gr_name );
4159         }
4160
4161         if ( can_add_accounts )
4162                 unbecome_root();
4163                 
4164         /******** END SeAddUsers BLOCK *********/
4165         
4166         if ( !ret ) {
4167                 DEBUG(5,("_samr_delete_dom_group: Failed to delete mapping entry for group %s.\n", 
4168                         group_sid_str));
4169                 return NT_STATUS_ACCESS_DENIED;
4170         }
4171         
4172         /* don't check that the unix group has been deleted.  Work like 
4173            _samr_delet_dom_user() */
4174
4175         if (!close_policy_hnd(p, &q_u->group_pol))
4176                 return NT_STATUS_OBJECT_NAME_INVALID;
4177
4178         force_flush_samr_cache(disp_info);
4179
4180         return NT_STATUS_OK;
4181 }
4182
4183 /*********************************************************************
4184  _samr_delete_dom_alias
4185 *********************************************************************/
4186
4187 NTSTATUS _samr_delete_dom_alias(pipes_struct *p, SAMR_Q_DELETE_DOM_ALIAS *q_u, SAMR_R_DELETE_DOM_ALIAS *r_u)
4188 {
4189         DOM_SID alias_sid;
4190         uint32 acc_granted;
4191         SE_PRIV se_rights;
4192         BOOL can_add_accounts;
4193         BOOL ret;
4194         DISP_INFO *disp_info = NULL;
4195
4196         DEBUG(5, ("_samr_delete_dom_alias: %d\n", __LINE__));
4197
4198         /* Find the policy handle. Open a policy on it. */
4199         if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted, &disp_info)) 
4200                 return NT_STATUS_INVALID_HANDLE;
4201         
4202         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_alias"))) {
4203                 return r_u->status;
4204         }
4205
4206         DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
4207
4208         if (!sid_check_is_in_our_domain(&alias_sid))
4209                 return NT_STATUS_NO_SUCH_ALIAS;
4210                 
4211         DEBUG(10, ("lookup on Local SID\n"));
4212
4213         se_priv_copy( &se_rights, &se_add_users );
4214         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4215
4216         /******** BEGIN SeAddUsers BLOCK *********/
4217         
4218         if ( can_add_accounts )
4219                 become_root();
4220
4221         /* Have passdb delete the alias */
4222         ret = pdb_delete_alias(&alias_sid);
4223         
4224         if ( can_add_accounts )
4225                 unbecome_root();
4226                 
4227         /******** END SeAddUsers BLOCK *********/
4228
4229         if ( !ret )
4230                 return NT_STATUS_ACCESS_DENIED;
4231
4232         if (!close_policy_hnd(p, &q_u->alias_pol))
4233                 return NT_STATUS_OBJECT_NAME_INVALID;
4234
4235         force_flush_samr_cache(disp_info);
4236
4237         return NT_STATUS_OK;
4238 }
4239
4240 /*********************************************************************
4241  _samr_create_dom_group
4242 *********************************************************************/
4243
4244 NTSTATUS _samr_create_dom_group(pipes_struct *p, SAMR_Q_CREATE_DOM_GROUP *q_u, SAMR_R_CREATE_DOM_GROUP *r_u)
4245 {
4246         DOM_SID dom_sid;
4247         DOM_SID info_sid;
4248         fstring name;
4249         fstring sid_string;
4250         struct group *grp;
4251         struct samr_info *info;
4252         uint32 acc_granted;
4253         gid_t gid;
4254         SE_PRIV se_rights;
4255         BOOL can_add_accounts;
4256         NTSTATUS result;
4257         DISP_INFO *disp_info = NULL;
4258
4259         /* Find the policy handle. Open a policy on it. */
4260         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &dom_sid, &acc_granted, &disp_info)) 
4261                 return NT_STATUS_INVALID_HANDLE;
4262         
4263         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_DOMAIN_CREATE_GROUP, "_samr_create_dom_group"))) {
4264                 return r_u->status;
4265         }
4266                 
4267         if (!sid_equal(&dom_sid, get_global_sam_sid()))
4268                 return NT_STATUS_ACCESS_DENIED;
4269
4270         unistr2_to_ascii(name, &q_u->uni_acct_desc, sizeof(name)-1);
4271
4272         r_u->status = can_create(p->mem_ctx, name);
4273         if (!NT_STATUS_IS_OK(r_u->status)) {
4274                 return r_u->status;
4275         }
4276
4277         se_priv_copy( &se_rights, &se_add_users );
4278         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4279
4280         /******** BEGIN SeAddUsers BLOCK *********/
4281         
4282         if ( can_add_accounts )
4283                 become_root();
4284         
4285         /* check that we successfully create the UNIX group */
4286         
4287         result = NT_STATUS_ACCESS_DENIED;
4288         if ( (smb_create_group(name, &gid) == 0) && ((grp=getgrgid(gid)) != NULL) ) {
4289         
4290                 /* so far, so good */
4291                 
4292                 result = NT_STATUS_OK;
4293                 
4294                 r_u->rid = pdb_gid_to_group_rid( grp->gr_gid );
4295
4296                 /* add the group to the mapping table */
4297                 
4298                 sid_copy( &info_sid, get_global_sam_sid() );
4299                 sid_append_rid( &info_sid, r_u->rid );
4300                 sid_to_string( sid_string, &info_sid );
4301                 
4302                 /* reset the error code if we fail to add the mapping entry */
4303                 
4304                 if ( !add_initial_entry(grp->gr_gid, sid_string, SID_NAME_DOM_GRP, name, NULL) )
4305                         result = NT_STATUS_ACCESS_DENIED;
4306         }
4307
4308         if ( can_add_accounts )
4309                 unbecome_root();
4310                 
4311         /******** END SeAddUsers BLOCK *********/
4312         
4313         /* check if we should bail out here */
4314         
4315         if ( !NT_STATUS_IS_OK(result) )
4316                 return result;
4317         
4318         if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
4319                 return NT_STATUS_NO_MEMORY;
4320
4321
4322         /* they created it; let the user do what he wants with it */
4323
4324         info->acc_granted = GENERIC_RIGHTS_GROUP_ALL_ACCESS;
4325
4326         /* get a (unique) handle.  open a policy on it. */
4327         if (!create_policy_hnd(p, &r_u->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_create_dom_alias
4337 *********************************************************************/
4338
4339 NTSTATUS _samr_create_dom_alias(pipes_struct *p, SAMR_Q_CREATE_DOM_ALIAS *q_u, SAMR_R_CREATE_DOM_ALIAS *r_u)
4340 {
4341         DOM_SID dom_sid;
4342         DOM_SID info_sid;
4343         fstring name;
4344         struct samr_info *info;
4345         uint32 acc_granted;
4346         gid_t gid;
4347         NTSTATUS result;
4348         SE_PRIV se_rights;
4349         BOOL can_add_accounts;
4350         DISP_INFO *disp_info = NULL;
4351
4352         /* Find the policy handle. Open a policy on it. */
4353         if (!get_lsa_policy_samr_sid(p, &q_u->dom_pol, &dom_sid, &acc_granted, &disp_info)) 
4354                 return NT_STATUS_INVALID_HANDLE;
4355                 
4356         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_DOMAIN_CREATE_ALIAS, "_samr_create_alias"))) {
4357                 return r_u->status;
4358         }
4359                 
4360         if (!sid_equal(&dom_sid, get_global_sam_sid()))
4361                 return NT_STATUS_ACCESS_DENIED;
4362
4363         unistr2_to_ascii(name, &q_u->uni_acct_desc, sizeof(name)-1);
4364
4365         se_priv_copy( &se_rights, &se_add_users );
4366         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4367
4368         result = can_create(p->mem_ctx, name);
4369         if (!NT_STATUS_IS_OK(result)) {
4370                 return result;
4371         }
4372
4373         /******** BEGIN SeAddUsers BLOCK *********/
4374         
4375         if ( can_add_accounts )
4376                 become_root();
4377
4378         /* Have passdb create the alias */
4379         result = pdb_create_alias(name, &r_u->rid);
4380
4381         if ( can_add_accounts )
4382                 unbecome_root();
4383                 
4384         /******** END SeAddUsers BLOCK *********/
4385
4386         if (!NT_STATUS_IS_OK(result))
4387                 return result;
4388
4389         sid_copy(&info_sid, get_global_sam_sid());
4390         sid_append_rid(&info_sid, r_u->rid);
4391
4392         if (!NT_STATUS_IS_OK(sid_to_gid(&info_sid, &gid)))
4393                 return NT_STATUS_ACCESS_DENIED;
4394
4395         /* check if the group has been successfully created */
4396         if ( getgrgid(gid) == NULL )
4397                 return NT_STATUS_ACCESS_DENIED;
4398
4399         if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
4400                 return NT_STATUS_NO_MEMORY;
4401
4402         /* they created it; let the user do what he wants with it */
4403
4404         info->acc_granted = GENERIC_RIGHTS_ALIAS_ALL_ACCESS;
4405
4406         /* get a (unique) handle.  open a policy on it. */
4407         if (!create_policy_hnd(p, &r_u->alias_pol, free_samr_info, (void *)info))
4408                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4409
4410         force_flush_samr_cache(disp_info);
4411
4412         return NT_STATUS_OK;
4413 }
4414
4415 /*********************************************************************
4416  _samr_query_groupinfo
4417
4418 sends the name/comment pair of a domain group
4419 level 1 send also the number of users of that group
4420 *********************************************************************/
4421
4422 NTSTATUS _samr_query_groupinfo(pipes_struct *p, SAMR_Q_QUERY_GROUPINFO *q_u, SAMR_R_QUERY_GROUPINFO *r_u)
4423 {
4424         DOM_SID group_sid;
4425         GROUP_MAP map;
4426         DOM_SID *sids=NULL;
4427         uid_t *uids;
4428         int num=0;
4429         GROUP_INFO_CTR *ctr;
4430         uint32 acc_granted;
4431         BOOL ret;
4432
4433         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted, NULL)) 
4434                 return NT_STATUS_INVALID_HANDLE;
4435         
4436         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_LOOKUP_INFO, "_samr_query_groupinfo"))) {
4437                 return r_u->status;
4438         }
4439                 
4440         become_root();
4441         ret = get_domain_group_from_sid(group_sid, &map);
4442         unbecome_root();
4443         if (!ret)
4444                 return NT_STATUS_INVALID_HANDLE;
4445
4446         ctr=TALLOC_ZERO_P(p->mem_ctx, GROUP_INFO_CTR);
4447         if (ctr==NULL)
4448                 return NT_STATUS_NO_MEMORY;
4449
4450         switch (q_u->switch_level) {
4451                 case 1:
4452                         ctr->switch_value1 = 1;
4453                         if(!get_memberuids(map.gid, &uids, &num))
4454                                 return NT_STATUS_NO_SUCH_GROUP;
4455                         SAFE_FREE(uids);
4456                         init_samr_group_info1(&ctr->group.info1, map.nt_name, map.comment, num);
4457                         SAFE_FREE(sids);
4458                         break;
4459                 case 3:
4460                         ctr->switch_value1 = 3;
4461                         init_samr_group_info3(&ctr->group.info3);
4462                         break;
4463                 case 4:
4464                         ctr->switch_value1 = 4;
4465                         init_samr_group_info4(&ctr->group.info4, map.comment);
4466                         break;
4467                 default:
4468                         return NT_STATUS_INVALID_INFO_CLASS;
4469         }
4470
4471         init_samr_r_query_groupinfo(r_u, ctr, NT_STATUS_OK);
4472
4473         return NT_STATUS_OK;
4474 }
4475
4476 /*********************************************************************
4477  _samr_set_groupinfo
4478  
4479  update a domain group's comment.
4480 *********************************************************************/
4481
4482 NTSTATUS _samr_set_groupinfo(pipes_struct *p, SAMR_Q_SET_GROUPINFO *q_u, SAMR_R_SET_GROUPINFO *r_u)
4483 {
4484         DOM_SID group_sid;
4485         GROUP_MAP map;
4486         GROUP_INFO_CTR *ctr;
4487         uint32 acc_granted;
4488         BOOL ret;
4489         BOOL can_mod_accounts;
4490         DISP_INFO *disp_info = NULL;
4491
4492         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted, &disp_info))
4493                 return NT_STATUS_INVALID_HANDLE;
4494         
4495         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_SET_INFO, "_samr_set_groupinfo"))) {
4496                 return r_u->status;
4497         }
4498
4499         become_root();
4500         ret = get_domain_group_from_sid(group_sid, &map);
4501         unbecome_root();
4502         if (!ret)
4503                 return NT_STATUS_NO_SUCH_GROUP;
4504         
4505         ctr=q_u->ctr;
4506
4507         switch (ctr->switch_value1) {
4508                 case 1:
4509                         unistr2_to_ascii(map.comment, &(ctr->group.info1.uni_acct_desc), sizeof(map.comment)-1);
4510                         break;
4511                 case 4:
4512                         unistr2_to_ascii(map.comment, &(ctr->group.info4.uni_acct_desc), sizeof(map.comment)-1);
4513                         break;
4514                 default:
4515                         return NT_STATUS_INVALID_INFO_CLASS;
4516         }
4517
4518         can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
4519
4520         /******** BEGIN SeAddUsers BLOCK *********/
4521
4522         if ( can_mod_accounts )
4523                 become_root();
4524           
4525         ret = pdb_update_group_mapping_entry(&map);
4526
4527         if ( can_mod_accounts )
4528                 unbecome_root();
4529
4530         /******** End SeAddUsers BLOCK *********/
4531
4532         if (ret) {
4533                 force_flush_samr_cache(disp_info);
4534         }
4535
4536         return ret ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4537 }
4538
4539 /*********************************************************************
4540  _samr_set_aliasinfo
4541  
4542  update an alias's comment.
4543 *********************************************************************/
4544
4545 NTSTATUS _samr_set_aliasinfo(pipes_struct *p, SAMR_Q_SET_ALIASINFO *q_u, SAMR_R_SET_ALIASINFO *r_u)
4546 {
4547         DOM_SID group_sid;
4548         struct acct_info info;
4549         ALIAS_INFO_CTR *ctr;
4550         uint32 acc_granted;
4551         BOOL ret;
4552         BOOL can_mod_accounts;
4553         DISP_INFO *disp_info = NULL;
4554
4555         if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &group_sid, &acc_granted, &disp_info))
4556                 return NT_STATUS_INVALID_HANDLE;
4557         
4558         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_SET_INFO, "_samr_set_aliasinfo"))) {
4559                 return r_u->status;
4560         }
4561                 
4562         ctr=&q_u->ctr;
4563
4564         switch (ctr->level) {
4565                 case 3:
4566                         if ( ctr->alias.info3.description.string ) {
4567                                 unistr2_to_ascii( info.acct_desc, 
4568                                         ctr->alias.info3.description.string, 
4569                                         sizeof(info.acct_desc)-1 );
4570                         }
4571                         else
4572                                 fstrcpy( info.acct_desc, "" );
4573                         break;
4574                 default:
4575                         return NT_STATUS_INVALID_INFO_CLASS;
4576         }
4577
4578         can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
4579
4580         /******** BEGIN SeAddUsers BLOCK *********/
4581
4582         if ( can_mod_accounts )
4583                 become_root();
4584
4585         ret = pdb_set_aliasinfo( &group_sid, &info );
4586
4587         if ( can_mod_accounts )
4588                 unbecome_root();
4589
4590         /******** End SeAddUsers BLOCK *********/
4591
4592         if (ret) {
4593                 force_flush_samr_cache(disp_info);
4594         }
4595
4596         return ret ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4597 }
4598
4599 /*********************************************************************
4600  _samr_get_dom_pwinfo
4601 *********************************************************************/
4602
4603 NTSTATUS _samr_get_dom_pwinfo(pipes_struct *p, SAMR_Q_GET_DOM_PWINFO *q_u, SAMR_R_GET_DOM_PWINFO *r_u)
4604 {
4605         /* Perform access check.  Since this rpc does not require a
4606            policy handle it will not be caught by the access checks on
4607            SAMR_CONNECT or SAMR_CONNECT_ANON. */
4608
4609         if (!pipe_access_check(p)) {
4610                 DEBUG(3, ("access denied to samr_get_dom_pwinfo\n"));
4611                 r_u->status = NT_STATUS_ACCESS_DENIED;
4612                 return r_u->status;
4613         }
4614
4615         /* Actually, returning zeros here works quite well :-). */
4616
4617         return NT_STATUS_OK;
4618 }
4619
4620 /*********************************************************************
4621  _samr_open_group
4622 *********************************************************************/
4623
4624 NTSTATUS _samr_open_group(pipes_struct *p, SAMR_Q_OPEN_GROUP *q_u, SAMR_R_OPEN_GROUP *r_u)
4625 {
4626         DOM_SID sid;
4627         DOM_SID info_sid;
4628         GROUP_MAP map;
4629         struct samr_info *info;
4630         SEC_DESC         *psd = NULL;
4631         uint32            acc_granted;
4632         uint32            des_access = q_u->access_mask;
4633         size_t            sd_size;
4634         NTSTATUS          status;
4635         fstring sid_string;
4636         BOOL ret;
4637         SE_PRIV se_rights;
4638
4639         if (!get_lsa_policy_samr_sid(p, &q_u->domain_pol, &sid, &acc_granted, NULL)) 
4640                 return NT_STATUS_INVALID_HANDLE;
4641         
4642         status = access_check_samr_function(acc_granted, 
4643                 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_group");
4644                 
4645         if ( !NT_STATUS_IS_OK(status) )
4646                 return status;
4647                 
4648         /*check if access can be granted as requested by client. */
4649         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &grp_generic_mapping, NULL, 0);
4650         se_map_generic(&des_access,&grp_generic_mapping);
4651
4652         se_priv_copy( &se_rights, &se_add_users );
4653
4654         status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
4655                 &se_rights, GENERIC_RIGHTS_GROUP_WRITE, des_access, 
4656                 &acc_granted, "_samr_open_group");
4657                 
4658         if ( !NT_STATUS_IS_OK(status) ) 
4659                 return status;
4660
4661         /* this should not be hard-coded like this */
4662         
4663         if (!sid_equal(&sid, get_global_sam_sid()))
4664                 return NT_STATUS_ACCESS_DENIED;
4665
4666         sid_copy(&info_sid, get_global_sam_sid());
4667         sid_append_rid(&info_sid, q_u->rid_group);
4668         sid_to_string(sid_string, &info_sid);
4669
4670         if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
4671                 return NT_STATUS_NO_MEMORY;
4672                 
4673         info->acc_granted = acc_granted;
4674
4675         DEBUG(10, ("_samr_open_group:Opening SID: %s\n", sid_string));
4676
4677         /* check if that group really exists */
4678         become_root();
4679         ret = get_domain_group_from_sid(info->sid, &map);
4680         unbecome_root();
4681         if (!ret)
4682                 return NT_STATUS_NO_SUCH_GROUP;
4683
4684         /* get a (unique) handle.  open a policy on it. */
4685         if (!create_policy_hnd(p, &r_u->pol, free_samr_info, (void *)info))
4686                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4687
4688         return NT_STATUS_OK;
4689 }
4690
4691 /*********************************************************************
4692  _samr_remove_sid_foreign_domain
4693 *********************************************************************/
4694
4695 NTSTATUS _samr_remove_sid_foreign_domain(pipes_struct *p, 
4696                                           SAMR_Q_REMOVE_SID_FOREIGN_DOMAIN *q_u, 
4697                                           SAMR_R_REMOVE_SID_FOREIGN_DOMAIN *r_u)
4698 {
4699         DOM_SID                 delete_sid, domain_sid;
4700         uint32                  acc_granted;
4701         NTSTATUS                result;
4702         DISP_INFO *disp_info = NULL;
4703
4704         sid_copy( &delete_sid, &q_u->sid.sid );
4705         
4706         DEBUG(5,("_samr_remove_sid_foreign_domain: removing SID [%s]\n",
4707                 sid_string_static(&delete_sid)));
4708                 
4709         /* Find the policy handle. Open a policy on it. */
4710         
4711         if (!get_lsa_policy_samr_sid(p, &q_u->dom_pol, &domain_sid,
4712                                      &acc_granted, &disp_info)) 
4713                 return NT_STATUS_INVALID_HANDLE;
4714         
4715         result = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, 
4716                 "_samr_remove_sid_foreign_domain");
4717                 
4718         if (!NT_STATUS_IS_OK(result)) 
4719                 return result;
4720                         
4721         DEBUG(8, ("_samr_remove_sid_foreign_domain:sid is %s\n", 
4722                 sid_string_static(&domain_sid)));
4723
4724         /* we can only delete a user from a group since we don't have 
4725            nested groups anyways.  So in the latter case, just say OK */
4726
4727         /* TODO: The above comment nowadays is bogus. Since we have nested
4728          * groups now, and aliases members are never reported out of the unix
4729          * group membership, the "just say OK" makes this call a no-op. For
4730          * us. This needs fixing however. */
4731
4732         /* I've only ever seen this in the wild when deleting a user from
4733          * usrmgr.exe. domain_sid is the builtin domain, and the sid to delete
4734          * is the user about to be deleted. I very much suspect this is the
4735          * only application of this call. To verify this, let people report
4736          * other cases. */
4737
4738         if (!sid_check_is_builtin(&domain_sid)) {
4739                 DEBUG(1,("_samr_remove_sid_foreign_domain: domain_sid = %s, "
4740                          "global_sam_sid() = %s\n",
4741                          sid_string_static(&domain_sid),
4742                          sid_string_static(get_global_sam_sid())));
4743                 DEBUGADD(1,("please report to samba-technical@samba.org!\n"));
4744                 return NT_STATUS_OK;
4745         }
4746
4747         force_flush_samr_cache(disp_info);
4748
4749         result = NT_STATUS_OK;
4750
4751         return result;
4752 }
4753
4754 /*******************************************************************
4755  _samr_query_domain_info2
4756  ********************************************************************/
4757
4758 NTSTATUS _samr_query_domain_info2(pipes_struct *p,
4759                 SAMR_Q_QUERY_DOMAIN_INFO2 *q_u,
4760                 SAMR_R_QUERY_DOMAIN_INFO2 *r_u)
4761 {
4762         struct samr_info *info = NULL;
4763         SAM_UNK_CTR *ctr;
4764         uint32 min_pass_len,pass_hist,flag;
4765         time_t u_expire, u_min_age;
4766         NTTIME nt_expire, nt_min_age;
4767
4768         time_t u_lock_duration, u_reset_time;
4769         NTTIME nt_lock_duration, nt_reset_time;
4770         uint32 lockout;
4771         
4772         time_t u_logout;
4773         NTTIME nt_logout;
4774
4775         uint32 num_users=0, num_groups=0, num_aliases=0;
4776
4777         uint32 account_policy_temp;
4778
4779         time_t seq_num;
4780         uint32 server_role;
4781
4782         if ((ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_UNK_CTR)) == NULL)
4783                 return NT_STATUS_NO_MEMORY;
4784
4785         ZERO_STRUCTP(ctr);
4786
4787         r_u->status = NT_STATUS_OK;
4788
4789         DEBUG(5,("_samr_query_domain_info2: %d\n", __LINE__));
4790
4791         /* find the policy handle.  open a policy on it. */
4792         if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)(void *)&info))
4793                 return NT_STATUS_INVALID_HANDLE;
4794
4795         switch (q_u->switch_value) {
4796                 case 0x01:
4797                         pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
4798                         min_pass_len = account_policy_temp;
4799
4800                         pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
4801                         pass_hist = account_policy_temp;
4802
4803                         pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
4804                         flag = account_policy_temp;
4805
4806                         pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
4807                         u_expire = account_policy_temp;
4808
4809                         pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
4810                         u_min_age = account_policy_temp;
4811
4812                         unix_to_nt_time_abs(&nt_expire, u_expire);
4813                         unix_to_nt_time_abs(&nt_min_age, u_min_age);
4814
4815                         init_unk_info1(&ctr->info.inf1, (uint16)min_pass_len, (uint16)pass_hist, 
4816                                        flag, nt_expire, nt_min_age);
4817                         break;
4818                 case 0x02:
4819                         become_root();          
4820                         num_users = count_sam_users(info->disp_info, ACB_NORMAL);
4821                         num_groups = count_sam_groups(info->disp_info);
4822                         num_aliases = count_sam_aliases(info->disp_info);
4823                         unbecome_root();
4824
4825                         pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
4826                         u_logout = account_policy_temp;
4827
4828                         unix_to_nt_time_abs(&nt_logout, u_logout);
4829
4830                         if (!pdb_get_seq_num(&seq_num))
4831                                 seq_num = time(NULL);
4832
4833                         server_role = ROLE_DOMAIN_PDC;
4834                         if (lp_server_role() == ROLE_DOMAIN_BDC)
4835                                 server_role = ROLE_DOMAIN_BDC;
4836
4837                         init_unk_info2(&ctr->info.inf2, lp_serverstring(), lp_workgroup(), global_myname(), seq_num, 
4838                                        num_users, num_groups, num_aliases, nt_logout, server_role);
4839                         break;
4840                 case 0x03:
4841                         pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
4842                         u_logout = account_policy_temp;
4843
4844                         unix_to_nt_time_abs(&nt_logout, u_logout);
4845                         
4846                         init_unk_info3(&ctr->info.inf3, nt_logout);
4847                         break;
4848                 case 0x05:
4849                         init_unk_info5(&ctr->info.inf5, global_myname());
4850                         break;
4851                 case 0x06:
4852                         init_unk_info6(&ctr->info.inf6);
4853                         break;
4854                 case 0x07:
4855                         server_role = ROLE_DOMAIN_PDC;
4856                         if (lp_server_role() == ROLE_DOMAIN_BDC)
4857                                 server_role = ROLE_DOMAIN_BDC;
4858
4859                         init_unk_info7(&ctr->info.inf7, server_role);
4860                         break;
4861                 case 0x08:
4862                         if (!pdb_get_seq_num(&seq_num))
4863                                 seq_num = time(NULL);
4864
4865                         init_unk_info8(&ctr->info.inf8, (uint32) seq_num);
4866                         break;
4867                 case 0x0c:
4868                         pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION, &account_policy_temp);
4869                         u_lock_duration = account_policy_temp;
4870                         if (u_lock_duration != -1)
4871                                 u_lock_duration *= 60;
4872
4873                         pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
4874                         u_reset_time = account_policy_temp * 60;
4875
4876                         pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
4877                         lockout = account_policy_temp;
4878         
4879                         unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
4880                         unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
4881         
4882                         init_unk_info12(&ctr->info.inf12, nt_lock_duration, nt_reset_time, (uint16)lockout);
4883                         break;
4884                 default:
4885                         return NT_STATUS_INVALID_INFO_CLASS;
4886         }
4887
4888         init_samr_r_samr_query_domain_info2(r_u, q_u->switch_value, ctr, NT_STATUS_OK);
4889
4890         DEBUG(5,("_samr_query_domain_info2: %d\n", __LINE__));
4891
4892         return r_u->status;
4893 }
4894
4895 /*******************************************************************
4896  _samr_
4897  ********************************************************************/
4898
4899 NTSTATUS _samr_set_dom_info(pipes_struct *p, SAMR_Q_SET_DOMAIN_INFO *q_u, SAMR_R_SET_DOMAIN_INFO *r_u)
4900 {
4901         time_t u_expire, u_min_age;
4902         time_t u_logout;
4903         time_t u_lock_duration, u_reset_time;
4904
4905         r_u->status = NT_STATUS_OK;
4906
4907         DEBUG(5,("_samr_set_dom_info: %d\n", __LINE__));
4908
4909         /* find the policy handle.  open a policy on it. */
4910         if (!find_policy_by_hnd(p, &q_u->domain_pol, NULL))
4911                 return NT_STATUS_INVALID_HANDLE;
4912
4913         DEBUG(5,("_samr_set_dom_info: switch_value: %d\n", q_u->switch_value));
4914
4915         switch (q_u->switch_value) {
4916                 case 0x01:
4917                         u_expire=nt_time_to_unix_abs(&q_u->ctr->info.inf1.expire);
4918                         u_min_age=nt_time_to_unix_abs(&q_u->ctr->info.inf1.min_passwordage);
4919                         
4920                         pdb_set_account_policy(AP_MIN_PASSWORD_LEN, (uint32)q_u->ctr->info.inf1.min_length_password);
4921                         pdb_set_account_policy(AP_PASSWORD_HISTORY, (uint32)q_u->ctr->info.inf1.password_history);
4922                         pdb_set_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, (uint32)q_u->ctr->info.inf1.password_properties);
4923                         pdb_set_account_policy(AP_MAX_PASSWORD_AGE, (int)u_expire);
4924                         pdb_set_account_policy(AP_MIN_PASSWORD_AGE, (int)u_min_age);
4925                         break;
4926                 case 0x02:
4927                         break;
4928                 case 0x03:
4929                         u_logout=nt_time_to_unix_abs(&q_u->ctr->info.inf3.logout);
4930                         pdb_set_account_policy(AP_TIME_TO_LOGOUT, (int)u_logout);
4931                         break;
4932                 case 0x05:
4933                         break;
4934                 case 0x06:
4935                         break;
4936                 case 0x07:
4937                         break;
4938                 case 0x0c:
4939                         u_lock_duration=nt_time_to_unix_abs(&q_u->ctr->info.inf12.duration);
4940                         if (u_lock_duration != -1)
4941                                 u_lock_duration /= 60;
4942
4943                         u_reset_time=nt_time_to_unix_abs(&q_u->ctr->info.inf12.reset_count)/60;
4944                         
4945                         pdb_set_account_policy(AP_LOCK_ACCOUNT_DURATION, (int)u_lock_duration);
4946                         pdb_set_account_policy(AP_RESET_COUNT_TIME, (int)u_reset_time);
4947                         pdb_set_account_policy(AP_BAD_ATTEMPT_LOCKOUT, (uint32)q_u->ctr->info.inf12.bad_attempt_lockout);
4948                         break;
4949                 default:
4950                         return NT_STATUS_INVALID_INFO_CLASS;
4951         }
4952
4953         init_samr_r_set_domain_info(r_u, NT_STATUS_OK);
4954
4955         DEBUG(5,("_samr_set_dom_info: %d\n", __LINE__));
4956
4957         return r_u->status;
4958 }