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