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-2002.
9 * Copyright (C) Jean François Micouleau 1998-2001.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * This is the implementation of the SAMR code.
32 extern fstring global_myworkgroup;
33 extern pstring global_myname;
34 extern DOM_SID global_sam_sid;
35 extern DOM_SID global_sid_Builtin;
37 extern rid_name domain_group_rids[];
38 extern rid_name domain_alias_rids[];
39 extern rid_name builtin_alias_rids[];
42 typedef struct _disp_info {
44 uint32 num_user_account;
45 DISP_USER_INFO *disp_user_info;
47 uint32 num_group_account;
48 DISP_GROUP_INFO *disp_group_info;
52 /* for use by the \PIPE\samr policy */
54 uint32 status; /* some sort of flag. best to record it. comes from opnum 0x39 */
58 /*******************************************************************
59 Create a samr_info struct.
60 ********************************************************************/
62 static struct samr_info *get_samr_info_by_sid(DOM_SID *psid)
64 struct samr_info *info;
67 if ((info = (struct samr_info *)malloc(sizeof(struct samr_info))) == NULL)
72 DEBUG(10,("get_samr_info_by_sid: created new info for sid %s\n", sid_to_string(sid_str, psid) ));
73 sid_copy( &info->sid, psid);
75 DEBUG(10,("get_samr_info_by_sid: created new info for NULL sid.\n"));
80 /*******************************************************************
81 Function to free the per handle data.
82 ********************************************************************/
83 static void free_samr_db(struct samr_info *info)
87 if (info->disp_info.group_dbloaded) {
88 for (i=0; i<info->disp_info.num_group_account; i++)
89 SAFE_FREE(info->disp_info.disp_group_info[i].grp);
91 SAFE_FREE(info->disp_info.disp_group_info);
94 if (info->disp_info.user_dbloaded){
95 for (i=0; i<info->disp_info.num_user_account; i++)
96 pdb_free_sam(&info->disp_info.disp_user_info[i].sam);
98 SAFE_FREE(info->disp_info.disp_user_info);
101 info->disp_info.user_dbloaded=False;
102 info->disp_info.group_dbloaded=False;
103 info->disp_info.num_group_account=0;
104 info->disp_info.num_user_account=0;
108 static void free_samr_info(void *ptr)
110 struct samr_info *info=(struct samr_info *) ptr;
116 /*******************************************************************
117 Ensure password info is never given out. Paranioa... JRA.
118 ********************************************************************/
120 static void samr_clear_passwd_fields( SAM_USER_INFO_21 *pass, int num_entries)
127 for (i = 0; i < num_entries; i++) {
128 memset(&pass[i].lm_pwd, '\0', sizeof(pass[i].lm_pwd));
129 memset(&pass[i].nt_pwd, '\0', sizeof(pass[i].nt_pwd));
133 static void samr_clear_sam_passwd(SAM_ACCOUNT *sam_pass)
139 /* These now zero out the old password */
141 pdb_set_lanman_passwd(sam_pass, NULL);
142 pdb_set_nt_passwd(sam_pass, NULL);
146 static NTSTATUS load_sampwd_entries(struct samr_info *info, uint16 acb_mask)
148 SAM_ACCOUNT *pwd = NULL;
149 DISP_USER_INFO *pwd_array = NULL;
151 DEBUG(10,("load_sampwd_entries\n"));
153 /* if the snapshoot is already loaded, return */
154 if (info->disp_info.user_dbloaded==True) {
155 DEBUG(10,("load_sampwd_entries: already in memory\n"));
159 if (!pdb_setsampwent(False)) {
160 DEBUG(0, ("load_sampwd_entries: Unable to open passdb.\n"));
161 return NT_STATUS_ACCESS_DENIED;
164 for (pdb_init_sam(&pwd); pdb_getsampwent(pwd) == True; pwd=NULL, pdb_init_sam(&pwd) ) {
166 if (acb_mask != 0 && !(pdb_get_acct_ctrl(pwd) & acb_mask)) {
168 DEBUG(5,(" acb_mask %x reject\n", acb_mask));
172 /* Realloc some memory for the array of ptr to the SAM_ACCOUNT structs */
173 if (info->disp_info.num_user_account % MAX_SAM_ENTRIES == 0) {
175 DEBUG(10,("load_sampwd_entries: allocating more memory\n"));
176 pwd_array=(DISP_USER_INFO *)Realloc(info->disp_info.disp_user_info,
177 (info->disp_info.num_user_account+MAX_SAM_ENTRIES)*sizeof(DISP_USER_INFO));
180 return NT_STATUS_NO_MEMORY;
182 info->disp_info.disp_user_info=pwd_array;
185 /* link the SAM_ACCOUNT to the array */
186 info->disp_info.disp_user_info[info->disp_info.num_user_account].sam=pwd;
188 DEBUG(10,("load_sampwd_entries: entry: %d\n", info->disp_info.num_user_account));
190 info->disp_info.num_user_account++;
195 /* the snapshoot is in memory, we're ready to enumerate fast */
197 info->disp_info.user_dbloaded=True;
199 DEBUG(12,("load_sampwd_entries: done\n"));
204 static NTSTATUS load_group_domain_entries(struct samr_info *info, DOM_SID *sid)
207 DISP_GROUP_INFO *grp_array = NULL;
208 uint32 group_entries = 0;
211 DEBUG(10,("load_group_domain_entries\n"));
213 /* if the snapshoot is already loaded, return */
214 if (info->disp_info.group_dbloaded==True) {
215 DEBUG(10,("load_group_domain_entries: already in memory\n"));
219 enum_group_mapping(SID_NAME_DOM_GRP, &map, (int *)&group_entries, ENUM_ONLY_MAPPED, MAPPING_WITHOUT_PRIV);
221 info->disp_info.num_group_account=group_entries;
223 grp_array=(DISP_GROUP_INFO *)malloc(info->disp_info.num_group_account*sizeof(DISP_GROUP_INFO));
225 if (group_entries!=0 && grp_array==NULL) {
227 return NT_STATUS_NO_MEMORY;
230 info->disp_info.disp_group_info=grp_array;
232 for (i=0; i<group_entries; i++) {
234 grp_array[i].grp=(DOMAIN_GRP *)malloc(sizeof(DOMAIN_GRP));
236 fstrcpy(grp_array[i].grp->name, map[i].nt_name);
237 fstrcpy(grp_array[i].grp->comment, map[i].comment);
238 sid_split_rid(&map[i].sid, &grp_array[i].grp->rid);
239 grp_array[i].grp->attr=SID_NAME_DOM_GRP;
244 /* the snapshoot is in memory, we're ready to enumerate fast */
246 info->disp_info.group_dbloaded=True;
248 DEBUG(12,("load_group_domain_entries: done\n"));
254 /*******************************************************************
255 This next function should be replaced with something that
256 dynamically returns the correct user info..... JRA.
257 ********************************************************************/
259 static NTSTATUS get_sampwd_entries(SAM_USER_INFO_21 *pw_buf, int start_idx,
260 int *total_entries, int *num_entries,
261 int max_num_entries, uint16 acb_mask)
263 SAM_ACCOUNT *pwd = NULL;
264 BOOL not_finished = True;
267 (*total_entries) = 0;
270 return NT_STATUS_NO_MEMORY;
274 if (!pdb_setsampwent(False)) {
275 DEBUG(0, ("get_sampwd_entries: Unable to open passdb.\n"));
277 return NT_STATUS_ACCESS_DENIED;
280 while (((not_finished = pdb_getsampwent(pwd)) != False)
281 && (*num_entries) < max_num_entries)
289 /* skip the requested number of entries.
290 not very efficient, but hey... */
295 user_name_len = strlen(pdb_get_username(pwd))+1;
296 init_unistr2(&pw_buf[(*num_entries)].uni_user_name, pdb_get_username(pwd), user_name_len);
297 init_uni_hdr(&pw_buf[(*num_entries)].hdr_user_name, user_name_len);
298 pw_buf[(*num_entries)].user_rid = pdb_get_user_rid(pwd);
299 memset((char *)pw_buf[(*num_entries)].nt_pwd, '\0', 16);
301 /* Now check if the NT compatible password is available. */
302 if (pdb_get_nt_passwd(pwd))
303 memcpy( pw_buf[(*num_entries)].nt_pwd , pdb_get_nt_passwd(pwd), 16);
305 pw_buf[(*num_entries)].acb_info = pdb_get_acct_ctrl(pwd);
307 DEBUG(5, ("entry idx: %d user %s, rid 0x%x, acb %x",
308 (*num_entries), pdb_get_username(pwd), pdb_get_user_rid(pwd), pdb_get_acct_ctrl(pwd) ));
310 if (acb_mask == 0 || (pdb_get_acct_ctrl(pwd) & acb_mask)) {
311 DEBUG(5,(" acb_mask %x accepts\n", acb_mask));
314 DEBUG(5,(" acb_mask %x rejects\n", acb_mask));
327 return STATUS_MORE_ENTRIES;
332 /*******************************************************************
334 ********************************************************************/
336 NTSTATUS _samr_close_hnd(pipes_struct *p, SAMR_Q_CLOSE_HND *q_u, SAMR_R_CLOSE_HND *r_u)
338 r_u->status = NT_STATUS_OK;
340 /* close the policy handle */
341 if (!close_policy_hnd(p, &q_u->pol))
342 return NT_STATUS_OBJECT_NAME_INVALID;
344 DEBUG(5,("samr_reply_close_hnd: %d\n", __LINE__));
349 /*******************************************************************
350 samr_reply_open_domain
351 ********************************************************************/
353 NTSTATUS _samr_open_domain(pipes_struct *p, SAMR_Q_OPEN_DOMAIN *q_u, SAMR_R_OPEN_DOMAIN *r_u)
355 struct samr_info *info;
357 r_u->status = NT_STATUS_OK;
359 /* find the connection policy handle. */
360 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
361 return NT_STATUS_INVALID_HANDLE;
363 /* associate the domain SID with the (unique) handle. */
364 if ((info = get_samr_info_by_sid(&q_u->dom_sid.sid))==NULL)
365 return NT_STATUS_NO_MEMORY;
367 /* get a (unique) handle. open a policy on it. */
368 if (!create_policy_hnd(p, &r_u->domain_pol, free_samr_info, (void *)info))
369 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
371 DEBUG(5,("samr_open_domain: %d\n", __LINE__));
376 /*******************************************************************
377 _samr_get_usrdom_pwinfo
378 ********************************************************************/
380 NTSTATUS _samr_get_usrdom_pwinfo(pipes_struct *p, SAMR_Q_GET_USRDOM_PWINFO *q_u, SAMR_R_GET_USRDOM_PWINFO *r_u)
382 struct samr_info *info = NULL;
384 r_u->status = NT_STATUS_OK;
386 /* find the policy handle. open a policy on it. */
387 if (!find_policy_by_hnd(p, &q_u->user_pol, (void **)&info))
388 return NT_STATUS_INVALID_HANDLE;
390 if (!sid_check_is_in_our_domain(&info->sid))
391 return NT_STATUS_OBJECT_TYPE_MISMATCH;
393 init_samr_r_get_usrdom_pwinfo(r_u, NT_STATUS_OK);
395 DEBUG(5,("_samr_get_usrdom_pwinfo: %d\n", __LINE__));
398 * NT sometimes return NT_STATUS_ACCESS_DENIED
399 * I don't know yet why.
405 /*******************************************************************
407 ********************************************************************/
409 static NTSTATUS samr_make_usr_obj_sd(TALLOC_CTX *ctx, SEC_DESC_BUF **buf, DOM_SID *usr_sid)
411 extern DOM_SID global_sid_World;
419 SEC_DESC *psd = NULL;
422 sid_copy(&adm_sid, &global_sid_Builtin);
423 sid_append_rid(&adm_sid, BUILTIN_ALIAS_RID_ADMINS);
425 sid_copy(&act_sid, &global_sid_Builtin);
426 sid_append_rid(&act_sid, BUILTIN_ALIAS_RID_ACCOUNT_OPS);
428 init_sec_access(&mask, 0x2035b);
429 init_sec_ace(&ace[0], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
431 init_sec_access(&mask, 0xf07ff);
432 init_sec_ace(&ace[1], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
433 init_sec_ace(&ace[2], &act_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
435 init_sec_access(&mask,0x20044);
436 init_sec_ace(&ace[3], usr_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
438 if((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 4, ace)) == NULL)
439 return NT_STATUS_NO_MEMORY;
441 if((psd = make_sec_desc(ctx, SEC_DESC_REVISION, NULL, NULL, NULL, psa, &sd_size)) == NULL)
442 return NT_STATUS_NO_MEMORY;
444 if((*buf = make_sec_desc_buf(ctx, sd_size, psd)) == NULL)
445 return NT_STATUS_NO_MEMORY;
450 static BOOL get_lsa_policy_samr_sid(pipes_struct *p, POLICY_HND *pol, DOM_SID *sid)
452 struct samr_info *info = NULL;
454 /* find the policy handle. open a policy on it. */
455 if (!find_policy_by_hnd(p, pol, (void **)&info))
465 /*******************************************************************
467 ********************************************************************/
469 NTSTATUS _samr_query_sec_obj(pipes_struct *p, SAMR_Q_QUERY_SEC_OBJ *q_u, SAMR_R_QUERY_SEC_OBJ *r_u)
474 r_u->status = NT_STATUS_OK;
478 if (!get_lsa_policy_samr_sid(p, &q_u->user_pol, &pol_sid))
479 return NT_STATUS_INVALID_HANDLE;
481 DEBUG(10,("_samr_query_sec_obj: querying security on SID: %s\n", sid_to_string(str_sid, &pol_sid)));
483 r_u->status = samr_make_usr_obj_sd(p->mem_ctx, &r_u->buf, &pol_sid);
485 if (NT_STATUS_IS_OK(r_u->status))
491 /*******************************************************************
492 makes a SAM_ENTRY / UNISTR2* structure from a user list.
493 ********************************************************************/
495 static void make_user_sam_entry_list(TALLOC_CTX *ctx, SAM_ENTRY **sam_pp, UNISTR2 **uni_name_pp,
496 uint32 num_sam_entries, SAM_USER_INFO_21 *pass)
505 if (num_sam_entries == 0)
508 sam = (SAM_ENTRY *)talloc_zero(ctx, sizeof(SAM_ENTRY)*num_sam_entries);
510 uni_name = (UNISTR2 *)talloc_zero(ctx, sizeof(UNISTR2)*num_sam_entries);
512 if (sam == NULL || uni_name == NULL) {
513 DEBUG(0, ("NULL pointers in SAMR_R_QUERY_DISPINFO\n"));
518 ZERO_STRUCTP(uni_name);
520 for (i = 0; i < num_sam_entries; i++) {
521 int len = pass[i].uni_user_name.uni_str_len;
523 init_sam_entry(&sam[i], len, pass[i].user_rid);
524 copy_unistr2(&uni_name[i], &pass[i].uni_user_name);
528 *uni_name_pp = uni_name;
531 /*******************************************************************
532 samr_reply_enum_dom_users
533 ********************************************************************/
535 NTSTATUS _samr_enum_dom_users(pipes_struct *p, SAMR_Q_ENUM_DOM_USERS *q_u, SAMR_R_ENUM_DOM_USERS *r_u)
537 SAM_USER_INFO_21 pass[MAX_SAM_ENTRIES];
539 int total_entries = 0;
541 r_u->status = NT_STATUS_OK;
543 /* find the policy handle. open a policy on it. */
544 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
545 return NT_STATUS_INVALID_HANDLE;
547 DEBUG(5,("_samr_enum_dom_users: %d\n", __LINE__));
550 r_u->status = get_sampwd_entries(pass, q_u->start_idx, &total_entries, &num_entries,
551 MAX_SAM_ENTRIES, q_u->acb_mask);
554 if (NT_STATUS_IS_ERR(r_u->status))
557 samr_clear_passwd_fields(pass, num_entries);
560 * Note from JRA. total_entries is not being used here. Currently if there is a
561 * large user base then it looks like NT will enumerate until get_sampwd_entries
562 * returns False due to num_entries being zero. This will cause an access denied
563 * return. I don't think this is right and needs further investigation. Note that
564 * this is also the same in the TNG code (I don't think that has been tested with
565 * a very large user list as MAX_SAM_ENTRIES is set to 600).
567 * I also think that one of the 'num_entries' return parameters is probably
568 * the "max entries" parameter - but in the TNG code they're all currently set to the same
569 * value (again I think this is wrong).
572 make_user_sam_entry_list(p->mem_ctx, &r_u->sam, &r_u->uni_acct_name, num_entries, pass);
574 init_samr_r_enum_dom_users(r_u, q_u->start_idx + num_entries, num_entries);
576 DEBUG(5,("_samr_enum_dom_users: %d\n", __LINE__));
581 /*******************************************************************
582 makes a SAM_ENTRY / UNISTR2* structure from a group list.
583 ********************************************************************/
585 static void make_group_sam_entry_list(TALLOC_CTX *ctx, SAM_ENTRY **sam_pp, UNISTR2 **uni_name_pp,
586 uint32 num_sam_entries, DOMAIN_GRP *grp)
595 if (num_sam_entries == 0)
598 sam = (SAM_ENTRY *)talloc_zero(ctx, sizeof(SAM_ENTRY)*num_sam_entries);
600 uni_name = (UNISTR2 *)talloc_zero(ctx, sizeof(UNISTR2)*num_sam_entries);
602 if (sam == NULL || uni_name == NULL) {
603 DEBUG(0, ("NULL pointers in SAMR_R_QUERY_DISPINFO\n"));
607 for (i = 0; i < num_sam_entries; i++) {
609 * JRA. I think this should include the null. TNG does not.
611 int len = strlen(grp[i].name)+1;
613 init_sam_entry(&sam[i], len, grp[i].rid);
614 init_unistr2(&uni_name[i], grp[i].name, len);
618 *uni_name_pp = uni_name;
621 /*******************************************************************
622 Get the group entries - similar to get_sampwd_entries().
623 ********************************************************************/
625 static NTSTATUS get_group_alias_entries(TALLOC_CTX *ctx, DOMAIN_GRP **d_grp, DOM_SID *sid, uint32 start_idx,
626 uint32 *p_num_entries, uint32 max_entries)
629 uint32 num_entries = 0;
634 sid_to_string(sid_str, sid);
635 DEBUG(5, ("get_group_alias_entries: enumerating aliases on SID: %s\n", sid_str));
639 /* well-known aliases */
640 if (sid_equal(sid, &global_sid_Builtin) && !lp_hide_local_users()) {
642 enum_group_mapping(SID_NAME_ALIAS, &map, (int *)&num_entries, ENUM_ONLY_MAPPED, MAPPING_WITHOUT_PRIV);
644 if (num_entries != 0) {
645 *d_grp=(DOMAIN_GRP *)talloc_zero(ctx, num_entries*sizeof(DOMAIN_GRP));
647 return NT_STATUS_NO_MEMORY;
649 for(i=0; i<num_entries && i<max_entries; i++) {
650 fstrcpy((*d_grp)[i].name, map[i+start_idx].nt_name);
651 sid_split_rid(&map[i+start_idx].sid, &(*d_grp)[i].rid);
657 } else if (sid_equal(sid, &global_sam_sid) && !lp_hide_local_users()) {
658 struct sys_grent *glist;
659 struct sys_grent *grp;
661 gid_t winbind_gid_low, winbind_gid_high;
663 lp_winbind_gid(&winbind_gid_low, &winbind_gid_high);
666 /* we return the UNIX groups here. This seems to be the right */
667 /* thing to do, since NT member servers return their local */
668 /* groups in the same situation. */
670 /* use getgrent_list() to retrieve the list of groups to avoid
671 * problems with getgrent possible infinite loop by internal
672 * libc grent structures overwrites by called functions */
673 grp = glist = getgrent_list();
675 return NT_STATUS_NO_MEMORY;
677 for (; (num_entries < max_entries) && (grp != NULL); grp = grp->next) {
680 if(!get_group_from_gid(grp->gr_gid, &smap, MAPPING_WITHOUT_PRIV))
683 if (smap.sid_name_use!=SID_NAME_ALIAS) {
687 sid_split_rid(&smap.sid, &trid);
689 if (!sid_equal(sid, &smap.sid))
692 /* Don't return winbind groups as they are not local! */
693 if ((grp->gr_gid >= winbind_gid_low)&&(grp->gr_gid <= winbind_gid_high)) {
694 DEBUG(10,("get_group_alias_entries: not returing %s, not local.\n", smap.nt_name ));
698 /* Don't return user private groups... */
701 * We used to do a Get_Pwnam() here, but this has been
702 * trimmed back to the common case for private groups
703 * to save lookups and to use the _alloc interface.
705 * This also matches the group mapping code
708 if ((pw = getpwnam_alloc(smap.nt_name)) != 0) {
709 DEBUG(10,("get_group_alias_entries: not returing %s, clashes with user.\n", smap.nt_name ));
714 for( i = 0; i < num_entries; i++)
715 if ( (*d_grp)[i].rid == trid )
718 if ( i < num_entries ) {
719 continue; /* rid was there, dup! */
722 /* JRA - added this for large group db enumeration... */
725 /* skip the requested number of entries.
726 not very efficient, but hey...
732 *d_grp=talloc_realloc(ctx,*d_grp, (num_entries+1)*sizeof(DOMAIN_GRP));
735 return NT_STATUS_NO_MEMORY;
738 fstrcpy((*d_grp)[num_entries].name, smap.nt_name);
739 (*d_grp)[num_entries].rid = trid;
741 DEBUG(10,("get_group_alias_entries: added entry %d, rid:%d\n", num_entries, trid));
747 *p_num_entries = num_entries;
749 DEBUG(10,("get_group_alias_entries: returning %d entries\n", *p_num_entries));
751 if (num_entries >= max_entries)
752 return STATUS_MORE_ENTRIES;
756 /*******************************************************************
757 Get the group entries - similar to get_sampwd_entries().
758 ********************************************************************/
760 static NTSTATUS get_group_domain_entries(TALLOC_CTX *ctx, DOMAIN_GRP **d_grp, DOM_SID *sid, uint32 start_idx,
761 uint32 *p_num_entries, uint32 max_entries)
765 uint32 group_entries = 0;
766 uint32 num_entries = 0;
770 enum_group_mapping(SID_NAME_DOM_GRP, &map, (int *)&group_entries, ENUM_ONLY_MAPPED, MAPPING_WITHOUT_PRIV);
772 num_entries=group_entries-start_idx;
774 /* limit the number of entries */
775 if (num_entries>max_entries) {
776 DEBUG(5,("Limiting to %d entries\n", max_entries));
777 num_entries=max_entries;
780 *d_grp=(DOMAIN_GRP *)talloc_zero(ctx, num_entries*sizeof(DOMAIN_GRP));
781 if (num_entries!=0 && *d_grp==NULL){
783 return NT_STATUS_NO_MEMORY;
786 for (i=0; i<num_entries; i++) {
787 fstrcpy((*d_grp)[i].name, map[i+start_idx].nt_name);
788 fstrcpy((*d_grp)[i].comment, map[i+start_idx].comment);
789 sid_split_rid(&map[i+start_idx].sid, &(*d_grp)[i].rid);
790 (*d_grp)[i].attr=SID_NAME_DOM_GRP;
795 *p_num_entries = num_entries;
800 /*******************************************************************
801 samr_reply_enum_dom_groups
802 Only reply with one group - domain admins. This must be fixed for
804 ********************************************************************/
806 NTSTATUS _samr_enum_dom_groups(pipes_struct *p, SAMR_Q_ENUM_DOM_GROUPS *q_u, SAMR_R_ENUM_DOM_GROUPS *r_u)
808 DOMAIN_GRP *grp=NULL;
812 r_u->status = NT_STATUS_OK;
814 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &sid))
815 return NT_STATUS_INVALID_HANDLE;
817 DEBUG(5,("samr_reply_enum_dom_groups: %d\n", __LINE__));
819 /* the domain group array is being allocated in the function below */
820 get_group_domain_entries(p->mem_ctx, &grp, &sid, q_u->start_idx, &num_entries, MAX_SAM_ENTRIES);
822 make_group_sam_entry_list(p->mem_ctx, &r_u->sam, &r_u->uni_grp_name, num_entries, grp);
824 init_samr_r_enum_dom_groups(r_u, q_u->start_idx, num_entries);
826 DEBUG(5,("samr_enum_dom_groups: %d\n", __LINE__));
832 /*******************************************************************
833 samr_reply_enum_dom_aliases
834 ********************************************************************/
836 NTSTATUS _samr_enum_dom_aliases(pipes_struct *p, SAMR_Q_ENUM_DOM_ALIASES *q_u, SAMR_R_ENUM_DOM_ALIASES *r_u)
838 DOMAIN_GRP *grp=NULL;
839 uint32 num_entries = 0;
844 r_u->status = NT_STATUS_OK;
846 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &sid))
847 return NT_STATUS_INVALID_HANDLE;
849 sid_to_string(sid_str, &sid);
850 DEBUG(5,("samr_reply_enum_dom_aliases: sid %s\n", sid_str));
852 status = get_group_alias_entries(p->mem_ctx, &grp, &sid, q_u->start_idx,
853 &num_entries, MAX_SAM_ENTRIES);
854 if (NT_STATUS_IS_ERR(status)) return status;
856 make_group_sam_entry_list(p->mem_ctx, &r_u->sam, &r_u->uni_grp_name, num_entries, grp);
860 init_samr_r_enum_dom_aliases(r_u, q_u->start_idx + num_entries, num_entries);
862 DEBUG(5,("samr_enum_dom_aliases: %d\n", __LINE__));
867 /*******************************************************************
868 samr_reply_query_dispinfo
869 ********************************************************************/
870 NTSTATUS _samr_query_dispinfo(pipes_struct *p, SAMR_Q_QUERY_DISPINFO *q_u, SAMR_R_QUERY_DISPINFO *r_u)
872 struct samr_info *info = NULL;
873 uint32 struct_size=0x20; /* W2K always reply that, client doesn't care */
876 uint32 max_entries=q_u->max_entries;
877 uint32 enum_context=q_u->start_idx;
878 uint32 max_size=q_u->max_size;
880 SAM_DISPINFO_CTR *ctr;
881 uint32 temp_size=0, total_data_size=0;
883 uint32 num_account = 0;
884 enum remote_arch_types ra_type = get_remote_arch();
887 max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
889 DEBUG(5, ("samr_reply_query_dispinfo: %d\n", __LINE__));
890 r_u->status = NT_STATUS_OK;
892 /* find the policy handle. open a policy on it. */
893 if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)&info))
894 return NT_STATUS_INVALID_HANDLE;
897 * calculate how many entries we will return.
899 * - the number of entries the client asked
900 * - our limit on that
901 * - the starting point (enumeration context)
902 * - the buffer size the client will accept
906 * We are a lot more like W2K. Instead of reading the SAM
907 * each time to find the records we need to send back,
908 * we read it once and link that copy to the sam handle.
909 * For large user list (over the MAX_SAM_ENTRIES)
910 * it's a definitive win.
911 * second point to notice: between enumerations
912 * our sam is now the same as it's a snapshoot.
913 * third point: got rid of the static SAM_USER_21 struct
914 * no more intermediate.
915 * con: it uses much more memory, as a full copy is stored
918 * If you want to change it, think twice and think
919 * of the second point , that's really important.
924 /* Get what we need from the password database */
926 if (q_u->switch_level==2)
927 acb_mask = ACB_WSTRUST;
929 acb_mask = ACB_NORMAL;
931 /* Get what we need from the password database */
932 switch (q_u->switch_level) {
937 r_u->status=load_sampwd_entries(info, acb_mask);
939 if (NT_STATUS_IS_ERR(r_u->status)) {
940 DEBUG(5, ("_samr_query_dispinfo: load_sampwd_entries failed\n"));
943 num_account = info->disp_info.num_user_account;
947 r_u->status = load_group_domain_entries(info, &info->sid);
948 if (NT_STATUS_IS_ERR(r_u->status))
950 num_account = info->disp_info.num_group_account;
953 DEBUG(0,("_samr_query_dispinfo: Unknown info level (%u)\n", (unsigned int)q_u->switch_level ));
954 return NT_STATUS_INVALID_INFO_CLASS;
957 /* first limit the number of entries we will return */
958 if(max_entries > max_sam_entries) {
959 DEBUG(5, ("samr_reply_query_dispinfo: client requested %d entries, limiting to %d\n", max_entries, max_sam_entries));
960 max_entries = max_sam_entries;
963 if (enum_context > num_account) {
964 DEBUG(5, ("samr_reply_query_dispinfo: enumeration handle over total entries\n"));
968 /* verify we won't overflow */
969 if (max_entries > num_account-enum_context) {
970 max_entries = num_account-enum_context;
971 DEBUG(5, ("samr_reply_query_dispinfo: only %d entries to return\n", max_entries));
974 /* calculate the size and limit on the number of entries we will return */
975 temp_size=(enum_context+max_entries)*struct_size;
977 if (temp_size>max_size) {
978 max_entries=max_size/struct_size;
979 DEBUG(5, ("samr_reply_query_dispinfo: buffer size limits to only %d entries\n", max_entries));
982 if (!(ctr = (SAM_DISPINFO_CTR *)talloc_zero(p->mem_ctx,sizeof(SAM_DISPINFO_CTR))))
983 return NT_STATUS_NO_MEMORY;
987 /* Now create reply structure */
988 switch (q_u->switch_level) {
991 if (!(ctr->sam.info1 = (SAM_DISPINFO_1 *)talloc_zero(p->mem_ctx,max_entries*sizeof(SAM_DISPINFO_1))))
992 return NT_STATUS_NO_MEMORY;
994 disp_ret = init_sam_dispinfo_1(p->mem_ctx, ctr->sam.info1, max_entries, enum_context, info->disp_info.disp_user_info);
995 if (NT_STATUS_IS_ERR(disp_ret))
1000 if (!(ctr->sam.info2 = (SAM_DISPINFO_2 *)talloc_zero(p->mem_ctx,max_entries*sizeof(SAM_DISPINFO_2))))
1001 return NT_STATUS_NO_MEMORY;
1003 disp_ret = init_sam_dispinfo_2(p->mem_ctx, ctr->sam.info2, max_entries, enum_context, info->disp_info.disp_user_info);
1004 if (NT_STATUS_IS_ERR(disp_ret))
1009 if (!(ctr->sam.info3 = (SAM_DISPINFO_3 *)talloc_zero(p->mem_ctx,max_entries*sizeof(SAM_DISPINFO_3))))
1010 return NT_STATUS_NO_MEMORY;
1012 disp_ret = init_sam_dispinfo_3(p->mem_ctx, ctr->sam.info3, max_entries, enum_context, info->disp_info.disp_group_info);
1013 if (NT_STATUS_IS_ERR(disp_ret))
1018 if (!(ctr->sam.info4 = (SAM_DISPINFO_4 *)talloc_zero(p->mem_ctx,max_entries*sizeof(SAM_DISPINFO_4))))
1019 return NT_STATUS_NO_MEMORY;
1021 disp_ret = init_sam_dispinfo_4(p->mem_ctx, ctr->sam.info4, max_entries, enum_context, info->disp_info.disp_user_info);
1022 if (NT_STATUS_IS_ERR(disp_ret))
1027 if (!(ctr->sam.info5 = (SAM_DISPINFO_5 *)talloc_zero(p->mem_ctx,max_entries*sizeof(SAM_DISPINFO_5))))
1028 return NT_STATUS_NO_MEMORY;
1030 disp_ret = init_sam_dispinfo_5(p->mem_ctx, ctr->sam.info5, max_entries, enum_context, info->disp_info.disp_group_info);
1031 if (NT_STATUS_IS_ERR(disp_ret))
1036 ctr->sam.info = NULL;
1037 return NT_STATUS_INVALID_INFO_CLASS;
1040 /* calculate the total size */
1041 total_data_size=num_account*struct_size;
1043 if (enum_context+max_entries < num_account)
1044 r_u->status = STATUS_MORE_ENTRIES;
1046 DEBUG(5, ("_samr_query_dispinfo: %d\n", __LINE__));
1048 init_samr_r_query_dispinfo(r_u, max_entries, total_data_size, temp_size, q_u->switch_level, ctr, r_u->status);
1054 /*******************************************************************
1055 samr_reply_query_aliasinfo
1056 ********************************************************************/
1058 NTSTATUS _samr_query_aliasinfo(pipes_struct *p, SAMR_Q_QUERY_ALIASINFO *q_u, SAMR_R_QUERY_ALIASINFO *r_u)
1060 struct samr_info *info = NULL;
1063 r_u->status = NT_STATUS_OK;
1065 DEBUG(5,("_samr_query_aliasinfo: %d\n", __LINE__));
1067 /* find the policy handle. open a policy on it. */
1068 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1069 return NT_STATUS_INVALID_HANDLE;
1071 if (!sid_check_is_in_our_domain(&info->sid) &&
1072 !sid_check_is_in_builtin(&info->sid))
1073 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1075 if(!get_local_group_from_sid(info->sid, &map, MAPPING_WITHOUT_PRIV))
1076 return NT_STATUS_NO_SUCH_ALIAS;
1078 switch (q_u->switch_level) {
1081 r_u->ctr.switch_value1 = 1;
1082 init_samr_alias_info1(&r_u->ctr.alias.info1, map.nt_name, 1, map.comment);
1086 r_u->ctr.switch_value1 = 3;
1087 init_samr_alias_info3(&r_u->ctr.alias.info3, map.comment);
1090 return NT_STATUS_INVALID_INFO_CLASS;
1093 DEBUG(5,("_samr_query_aliasinfo: %d\n", __LINE__));
1099 /*******************************************************************
1100 samr_reply_lookup_ids
1101 ********************************************************************/
1103 uint32 _samr_lookup_ids(pipes_struct *p, SAMR_Q_LOOKUP_IDS *q_u, SAMR_R_LOOKUP_IDS *r_u)
1105 uint32 rid[MAX_SAM_ENTRIES];
1106 int num_rids = q_u->num_sids1;
1108 r_u->status = NT_STATUS_OK;
1110 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1112 if (num_rids > MAX_SAM_ENTRIES) {
1113 num_rids = MAX_SAM_ENTRIES;
1114 DEBUG(5,("_samr_lookup_ids: truncating entries to %d\n", num_rids));
1119 SMB_ASSERT_ARRAY(q_u->uni_user_name, num_rids);
1121 for (i = 0; i < num_rids && status == 0; i++)
1123 struct sam_passwd *sam_pass;
1127 fstrcpy(user_name, unistrn2(q_u->uni_user_name[i].buffer,
1128 q_u->uni_user_name[i].uni_str_len));
1130 /* find the user account */
1132 sam_pass = get_smb21pwd_entry(user_name, 0);
1135 if (sam_pass == NULL)
1137 status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
1142 rid[i] = sam_pass->user_rid;
1148 rid[0] = BUILTIN_ALIAS_RID_USERS;
1150 init_samr_r_lookup_ids(&r_u, num_rids, rid, NT_STATUS_OK);
1152 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1158 /*******************************************************************
1160 ********************************************************************/
1162 NTSTATUS _samr_lookup_names(pipes_struct *p, SAMR_Q_LOOKUP_NAMES *q_u, SAMR_R_LOOKUP_NAMES *r_u)
1164 uint32 rid[MAX_SAM_ENTRIES];
1166 enum SID_NAME_USE type[MAX_SAM_ENTRIES];
1167 enum SID_NAME_USE local_type;
1169 int num_rids = q_u->num_names2;
1173 r_u->status = NT_STATUS_OK;
1175 DEBUG(5,("_samr_lookup_names: %d\n", __LINE__));
1180 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &pol_sid)) {
1181 init_samr_r_lookup_names(p->mem_ctx, r_u, 0, NULL, NULL, NT_STATUS_OBJECT_TYPE_MISMATCH);
1185 if (num_rids > MAX_SAM_ENTRIES) {
1186 num_rids = MAX_SAM_ENTRIES;
1187 DEBUG(5,("_samr_lookup_names: truncating entries to %d\n", num_rids));
1190 DEBUG(5,("_samr_lookup_names: looking name on SID %s\n", sid_to_string(sid_str, &pol_sid)));
1192 become_root(); /* local_lookup_name can require root privs */
1194 for (i = 0; i < num_rids; i++) {
1198 r_u->status = NT_STATUS_NONE_MAPPED;
1200 rid [i] = 0xffffffff;
1201 type[i] = SID_NAME_UNKNOWN;
1203 rpcstr_pull(name, q_u->uni_name[i].buffer, sizeof(name), q_u->uni_name[i].uni_str_len*2, 0);
1206 * we are only looking for a name
1207 * the SID we get back can be outside
1208 * the scope of the pol_sid
1210 * in clear: it prevents to reply to domain\group: yes
1211 * when only builtin\group exists.
1213 * a cleaner code is to add the sid of the domain we're looking in
1214 * to the local_lookup_name function.
1216 if(local_lookup_name(name, &sid, &local_type)) {
1217 sid_split_rid(&sid, &local_rid);
1219 if (sid_equal(&sid, &pol_sid)) {
1222 r_u->status = NT_STATUS_OK;
1229 init_samr_r_lookup_names(p->mem_ctx, r_u, num_rids, rid, (uint32 *)type, r_u->status);
1231 DEBUG(5,("_samr_lookup_names: %d\n", __LINE__));
1236 /*******************************************************************
1237 _samr_chgpasswd_user
1238 ********************************************************************/
1240 NTSTATUS _samr_chgpasswd_user(pipes_struct *p, SAMR_Q_CHGPASSWD_USER *q_u, SAMR_R_CHGPASSWD_USER *r_u)
1245 DEBUG(5,("_samr_chgpasswd_user: %d\n", __LINE__));
1247 r_u->status = NT_STATUS_OK;
1249 rpcstr_pull(user_name, q_u->uni_user_name.buffer, sizeof(user_name), q_u->uni_user_name.uni_str_len*2, 0);
1250 rpcstr_pull(wks, q_u->uni_dest_host.buffer, sizeof(wks), q_u->uni_dest_host.uni_str_len*2,0);
1252 DEBUG(5,("samr_chgpasswd_user: user: %s wks: %s\n", user_name, wks));
1255 * Pass the user through the NT -> unix user mapping
1259 (void)map_username(user_name);
1262 * UNIX username case mangling not required, pass_oem_change
1263 * is case insensitive.
1266 if (!pass_oem_change(user_name, q_u->lm_newpass.pass, q_u->lm_oldhash.hash,
1267 q_u->nt_newpass.pass, q_u->nt_oldhash.hash))
1268 r_u->status = NT_STATUS_WRONG_PASSWORD;
1270 init_samr_r_chgpasswd_user(r_u, r_u->status);
1272 DEBUG(5,("_samr_chgpasswd_user: %d\n", __LINE__));
1277 /*******************************************************************
1278 makes a SAMR_R_LOOKUP_RIDS structure.
1279 ********************************************************************/
1281 static BOOL make_samr_lookup_rids(TALLOC_CTX *ctx, uint32 num_names, fstring names[],
1282 UNIHDR **pp_hdr_name, UNISTR2 **pp_uni_name)
1285 UNIHDR *hdr_name=NULL;
1286 UNISTR2 *uni_name=NULL;
1288 *pp_uni_name = NULL;
1289 *pp_hdr_name = NULL;
1291 if (num_names != 0) {
1292 hdr_name = (UNIHDR *)talloc_zero(ctx, sizeof(UNIHDR)*num_names);
1293 if (hdr_name == NULL)
1296 uni_name = (UNISTR2 *)talloc_zero(ctx,sizeof(UNISTR2)*num_names);
1297 if (uni_name == NULL)
1301 for (i = 0; i < num_names; i++) {
1302 int len = names[i] != NULL ? strlen(names[i]) : 0;
1303 DEBUG(10, ("names[%d]:%s\n", i, names[i]));
1304 init_uni_hdr(&hdr_name[i], len);
1305 init_unistr2(&uni_name[i], names[i], len);
1308 *pp_uni_name = uni_name;
1309 *pp_hdr_name = hdr_name;
1314 /*******************************************************************
1316 ********************************************************************/
1318 NTSTATUS _samr_lookup_rids(pipes_struct *p, SAMR_Q_LOOKUP_RIDS *q_u, SAMR_R_LOOKUP_RIDS *r_u)
1320 fstring group_names[MAX_SAM_ENTRIES];
1321 uint32 *group_attrs = NULL;
1322 UNIHDR *hdr_name = NULL;
1323 UNISTR2 *uni_name = NULL;
1325 int num_rids = q_u->num_rids1;
1328 r_u->status = NT_STATUS_OK;
1330 DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
1332 /* find the policy handle. open a policy on it. */
1333 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &pol_sid))
1334 return NT_STATUS_INVALID_HANDLE;
1336 if (num_rids > MAX_SAM_ENTRIES) {
1337 num_rids = MAX_SAM_ENTRIES;
1338 DEBUG(5,("_samr_lookup_rids: truncating entries to %d\n", num_rids));
1342 if ((group_attrs = (uint32 *)talloc_zero(p->mem_ctx, num_rids * sizeof(uint32))) == NULL)
1343 return NT_STATUS_NO_MEMORY;
1346 r_u->status = NT_STATUS_NONE_MAPPED;
1348 become_root(); /* lookup_sid can require root privs */
1350 for (i = 0; i < num_rids; i++) {
1354 enum SID_NAME_USE type;
1356 group_attrs[i] = SID_NAME_UNKNOWN;
1357 *group_names[i] = '\0';
1359 if (sid_equal(&pol_sid, &global_sam_sid)) {
1360 sid_copy(&sid, &pol_sid);
1361 sid_append_rid(&sid, q_u->rid[i]);
1363 if (lookup_sid(&sid, domname, tmpname, &type)) {
1364 r_u->status = NT_STATUS_OK;
1365 group_attrs[i] = (uint32)type;
1366 fstrcpy(group_names[i],tmpname);
1367 DEBUG(5,("_samr_lookup_rids: %s:%d\n", group_names[i], group_attrs[i]));
1374 if(!make_samr_lookup_rids(p->mem_ctx, num_rids, group_names, &hdr_name, &uni_name))
1375 return NT_STATUS_NO_MEMORY;
1377 init_samr_r_lookup_rids(r_u, num_rids, hdr_name, uni_name, group_attrs);
1379 DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
1384 /*******************************************************************
1385 _api_samr_open_user. Safe - gives out no passwd info.
1386 ********************************************************************/
1388 NTSTATUS _api_samr_open_user(pipes_struct *p, SAMR_Q_OPEN_USER *q_u, SAMR_R_OPEN_USER *r_u)
1390 SAM_ACCOUNT *sampass=NULL;
1392 POLICY_HND domain_pol = q_u->domain_pol;
1393 uint32 user_rid = q_u->user_rid;
1394 POLICY_HND *user_pol = &r_u->user_pol;
1395 struct samr_info *info = NULL;
1398 r_u->status = NT_STATUS_OK;
1400 /* find the domain policy handle. */
1401 if (!find_policy_by_hnd(p, &domain_pol, NULL))
1402 return NT_STATUS_INVALID_HANDLE;
1404 pdb_init_sam(&sampass);
1407 ret=pdb_getsampwrid(sampass, user_rid);
1410 /* check that the RID exists in our domain. */
1412 pdb_free_sam(&sampass);
1413 return NT_STATUS_NO_SUCH_USER;
1416 pdb_free_sam(&sampass);
1418 /* Get the domain SID stored in the domain policy */
1419 if(!get_lsa_policy_samr_sid(p, &domain_pol, &sid))
1420 return NT_STATUS_INVALID_HANDLE;
1422 /* append the user's RID to it */
1423 if(!sid_append_rid(&sid, user_rid))
1424 return NT_STATUS_NO_SUCH_USER;
1426 /* associate the user's SID with the new handle. */
1427 if ((info = get_samr_info_by_sid(&sid)) == NULL)
1428 return NT_STATUS_NO_MEMORY;
1430 /* get a (unique) handle. open a policy on it. */
1431 if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info))
1432 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1437 /*************************************************************************
1438 get_user_info_10. Safe. Only gives out acb bits.
1439 *************************************************************************/
1441 static BOOL get_user_info_10(SAM_USER_INFO_10 *id10, uint32 user_rid)
1443 SAM_ACCOUNT *smbpass=NULL;
1446 if (!pdb_rid_is_user(user_rid)) {
1447 DEBUG(4,("RID 0x%x is not a user RID\n", user_rid));
1451 pdb_init_sam(&smbpass);
1454 ret = pdb_getsampwrid(smbpass, user_rid);
1458 DEBUG(4,("User 0x%x not found\n", user_rid));
1459 pdb_free_sam(&smbpass);
1463 DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1466 init_sam_user_info10(id10, pdb_get_acct_ctrl(smbpass) );
1468 pdb_free_sam(&smbpass);
1473 /*************************************************************************
1474 get_user_info_12. OK - this is the killer as it gives out password info.
1475 Ensure that this is only allowed on an encrypted connection with a root
1477 *************************************************************************/
1479 static NTSTATUS get_user_info_12(pipes_struct *p, SAM_USER_INFO_12 * id12, uint32 user_rid)
1481 SAM_ACCOUNT *smbpass=NULL;
1484 if (!p->ntlmssp_auth_validated)
1485 return NT_STATUS_ACCESS_DENIED;
1487 if (!(p->ntlmssp_chal_flags & NTLMSSP_NEGOTIATE_SIGN) || !(p->ntlmssp_chal_flags & NTLMSSP_NEGOTIATE_SEAL))
1488 return NT_STATUS_ACCESS_DENIED;
1491 * Do *NOT* do become_root()/unbecome_root() here ! JRA.
1493 pdb_init_sam(&smbpass);
1495 ret = pdb_getsampwrid(smbpass, user_rid);
1498 DEBUG(4, ("User 0x%x not found\n", user_rid));
1499 pdb_free_sam(&smbpass);
1500 return (geteuid() == (uid_t)0) ? NT_STATUS_NO_SUCH_USER : NT_STATUS_ACCESS_DENIED;
1503 DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
1505 if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
1506 pdb_free_sam(&smbpass);
1507 return NT_STATUS_ACCOUNT_DISABLED;
1511 init_sam_user_info12(id12, pdb_get_lanman_passwd(smbpass), pdb_get_nt_passwd(smbpass));
1513 pdb_free_sam(&smbpass);
1515 return NT_STATUS_OK;
1518 /*************************************************************************
1520 *************************************************************************/
1522 static BOOL get_user_info_20(SAM_USER_INFO_20 *id20, uint32 user_rid)
1524 SAM_ACCOUNT *sampass=NULL;
1527 if (!pdb_rid_is_user(user_rid)) {
1528 DEBUG(4,("RID 0x%x is not a user RID\n", user_rid));
1532 pdb_init_sam(&sampass);
1535 ret = pdb_getsampwrid(sampass, user_rid);
1539 DEBUG(4,("User 0x%x not found\n", user_rid));
1540 pdb_free_sam(&sampass);
1544 samr_clear_sam_passwd(sampass);
1546 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
1549 init_sam_user_info20A(id20, sampass);
1551 pdb_free_sam(&sampass);
1556 /*************************************************************************
1558 *************************************************************************/
1560 static BOOL get_user_info_21(SAM_USER_INFO_21 *id21, uint32 user_rid)
1562 SAM_ACCOUNT *sampass=NULL;
1565 if (!pdb_rid_is_user(user_rid)) {
1566 DEBUG(4,("RID 0x%x is not a user RID\n", user_rid));
1570 pdb_init_sam(&sampass);
1573 ret = pdb_getsampwrid(sampass, user_rid);
1577 DEBUG(4,("User 0x%x not found\n", user_rid));
1578 pdb_free_sam(&sampass);
1582 samr_clear_sam_passwd(sampass);
1584 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
1587 init_sam_user_info21A(id21, sampass);
1589 pdb_free_sam(&sampass);
1594 /*******************************************************************
1595 _samr_query_userinfo
1596 ********************************************************************/
1598 NTSTATUS _samr_query_userinfo(pipes_struct *p, SAMR_Q_QUERY_USERINFO *q_u, SAMR_R_QUERY_USERINFO *r_u)
1600 SAM_USERINFO_CTR *ctr;
1602 struct samr_info *info = NULL;
1604 r_u->status=NT_STATUS_OK;
1606 /* search for the handle */
1607 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1608 return NT_STATUS_INVALID_HANDLE;
1610 if (!sid_check_is_in_our_domain(&info->sid))
1611 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1613 sid_peek_rid(&info->sid, &rid);
1615 DEBUG(5,("_samr_query_userinfo: rid:0x%x\n", rid));
1617 ctr = (SAM_USERINFO_CTR *)talloc_zero(p->mem_ctx, sizeof(SAM_USERINFO_CTR));
1619 return NT_STATUS_NO_MEMORY;
1623 /* ok! user info levels (lots: see MSDEV help), off we go... */
1624 ctr->switch_value = q_u->switch_value;
1626 switch (q_u->switch_value) {
1628 ctr->info.id10 = (SAM_USER_INFO_10 *)talloc_zero(p->mem_ctx, sizeof(SAM_USER_INFO_10));
1629 if (ctr->info.id10 == NULL)
1630 return NT_STATUS_NO_MEMORY;
1632 if (!get_user_info_10(ctr->info.id10, rid))
1633 return NT_STATUS_NO_SUCH_USER;
1637 /* whoops - got this wrong. i think. or don't understand what's happening. */
1641 info = (void *)&id11;
1643 expire.low = 0xffffffff;
1644 expire.high = 0x7fffffff;
1646 ctr->info.id = (SAM_USER_INFO_11 *)talloc_zero(p->mem_ctx,
1651 ZERO_STRUCTP(ctr->info.id11);
1652 init_sam_user_info11(ctr->info.id11, &expire,
1653 "BROOKFIELDS$", /* name */
1654 0x03ef, /* user rid */
1655 0x201, /* group rid */
1656 0x0080); /* acb info */
1663 ctr->info.id12 = (SAM_USER_INFO_12 *)talloc_zero(p->mem_ctx, sizeof(SAM_USER_INFO_12));
1664 if (ctr->info.id12 == NULL)
1665 return NT_STATUS_NO_MEMORY;
1667 if (NT_STATUS_IS_ERR(r_u->status = get_user_info_12(p, ctr->info.id12, rid)))
1672 ctr->info.id20 = (SAM_USER_INFO_20 *)talloc_zero(p->mem_ctx,sizeof(SAM_USER_INFO_20));
1673 if (ctr->info.id20 == NULL)
1674 return NT_STATUS_NO_MEMORY;
1675 if (!get_user_info_20(ctr->info.id20, rid))
1676 return NT_STATUS_NO_SUCH_USER;
1680 ctr->info.id21 = (SAM_USER_INFO_21 *)talloc_zero(p->mem_ctx,sizeof(SAM_USER_INFO_21));
1681 if (ctr->info.id21 == NULL)
1682 return NT_STATUS_NO_MEMORY;
1683 if (!get_user_info_21(ctr->info.id21, rid))
1684 return NT_STATUS_NO_SUCH_USER;
1688 return NT_STATUS_INVALID_INFO_CLASS;
1691 init_samr_r_query_userinfo(r_u, ctr, r_u->status);
1693 DEBUG(5,("_samr_query_userinfo: %d\n", __LINE__));
1698 /*******************************************************************
1699 samr_reply_query_usergroups
1700 ********************************************************************/
1702 NTSTATUS _samr_query_usergroups(pipes_struct *p, SAMR_Q_QUERY_USERGROUPS *q_u, SAMR_R_QUERY_USERGROUPS *r_u)
1704 SAM_ACCOUNT *sam_pass=NULL;
1705 DOM_GID *gids = NULL;
1708 struct samr_info *info = NULL;
1712 * from the SID in the request:
1713 * we should send back the list of DOMAIN GROUPS
1714 * the user is a member of
1716 * and only the DOMAIN GROUPS
1717 * no ALIASES !!! neither aliases of the domain
1718 * nor aliases of the builtin SID
1723 r_u->status = NT_STATUS_OK;
1725 DEBUG(5,("_samr_query_usergroups: %d\n", __LINE__));
1727 /* find the policy handle. open a policy on it. */
1728 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1729 return NT_STATUS_INVALID_HANDLE;
1731 if (!sid_check_is_in_our_domain(&info->sid))
1732 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1734 sid_peek_rid(&info->sid, &rid);
1736 pdb_init_sam(&sam_pass);
1739 ret = pdb_getsampwrid(sam_pass, rid);
1743 pdb_free_sam(&sam_pass);
1744 return NT_STATUS_NO_SUCH_USER;
1747 if(!get_domain_user_groups(p->mem_ctx, &num_groups, &gids, sam_pass)) {
1748 pdb_free_sam(&sam_pass);
1749 return NT_STATUS_NO_SUCH_GROUP;
1752 /* construct the response. lkclXXXX: gids are not copied! */
1753 init_samr_r_query_usergroups(r_u, num_groups, gids, r_u->status);
1755 DEBUG(5,("_samr_query_usergroups: %d\n", __LINE__));
1757 pdb_free_sam(&sam_pass);
1762 /*******************************************************************
1763 _samr_query_dom_info
1764 ********************************************************************/
1766 NTSTATUS _samr_query_dom_info(pipes_struct *p, SAMR_Q_QUERY_DOMAIN_INFO *q_u, SAMR_R_QUERY_DOMAIN_INFO *r_u)
1768 struct samr_info *info = NULL;
1770 uint32 min_pass_len,pass_hist,flag;
1771 time_t u_expire, u_min_age;
1772 NTTIME nt_expire, nt_min_age;
1774 time_t u_lock_duration, u_reset_time;
1775 NTTIME nt_lock_duration, nt_reset_time;
1781 uint32 num_users=0, num_groups=0, num_aliases=0;
1783 if ((ctr = (SAM_UNK_CTR *)talloc_zero(p->mem_ctx, sizeof(SAM_UNK_CTR))) == NULL)
1784 return NT_STATUS_NO_MEMORY;
1788 r_u->status = NT_STATUS_OK;
1790 DEBUG(5,("_samr_query_dom_info: %d\n", __LINE__));
1792 /* find the policy handle. open a policy on it. */
1793 if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)&info))
1794 return NT_STATUS_INVALID_HANDLE;
1796 switch (q_u->switch_value) {
1798 account_policy_get(AP_MIN_PASSWORD_LEN, &min_pass_len);
1799 account_policy_get(AP_PASSWORD_HISTORY, &pass_hist);
1800 account_policy_get(AP_USER_MUST_LOGON_TO_CHG_PASS, &flag);
1801 account_policy_get(AP_MAX_PASSWORD_AGE, (int *)&u_expire);
1802 account_policy_get(AP_MIN_PASSWORD_AGE, (int *)&u_min_age);
1804 unix_to_nt_time_abs(&nt_expire, u_expire);
1805 unix_to_nt_time_abs(&nt_min_age, u_min_age);
1807 init_unk_info1(&ctr->info.inf1, (uint16)min_pass_len, (uint16)pass_hist,
1808 flag, nt_expire, nt_min_age);
1812 r_u->status=load_sampwd_entries(info, ACB_NORMAL);
1814 if (NT_STATUS_IS_ERR(r_u->status)) {
1815 DEBUG(5, ("_samr_query_dispinfo: load_sampwd_entries failed\n"));
1818 num_users=info->disp_info.num_user_account;
1821 r_u->status=load_group_domain_entries(info, &global_sam_sid);
1822 if (NT_STATUS_IS_ERR(r_u->status)) {
1823 DEBUG(5, ("_samr_query_dispinfo: load_group_domain_entries failed\n"));
1826 num_groups=info->disp_info.num_group_account;
1829 /* The time call below is to get a sequence number for the sam. FIXME !!! JRA. */
1830 init_unk_info2(&ctr->info.inf2, global_myworkgroup, global_myname, (uint32) time(NULL),
1831 num_users, num_groups, num_aliases);
1834 account_policy_get(AP_TIME_TO_LOGOUT, (int *)&u_logout);
1835 unix_to_nt_time_abs(&nt_logout, u_logout);
1837 init_unk_info3(&ctr->info.inf3, nt_logout);
1840 init_unk_info5(&ctr->info.inf5, global_myname);
1843 init_unk_info6(&ctr->info.inf6);
1846 init_unk_info7(&ctr->info.inf7);
1849 account_policy_get(AP_LOCK_ACCOUNT_DURATION, (int *)&u_lock_duration);
1850 account_policy_get(AP_RESET_COUNT_TIME, (int *)&u_reset_time);
1851 account_policy_get(AP_BAD_ATTEMPT_LOCKOUT, &lockout);
1853 unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
1854 unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
1856 init_unk_info12(&ctr->info.inf12, nt_lock_duration, nt_reset_time, (uint16)lockout);
1859 return NT_STATUS_INVALID_INFO_CLASS;
1862 init_samr_r_query_dom_info(r_u, q_u->switch_value, ctr, NT_STATUS_OK);
1864 DEBUG(5,("_samr_query_dom_info: %d\n", __LINE__));
1869 /*******************************************************************
1870 _api_samr_create_user
1871 Create an account, can be either a normal user or a machine.
1872 This funcion will need to be updated for bdc/domain trusts.
1873 ********************************************************************/
1875 NTSTATUS _api_samr_create_user(pipes_struct *p, SAMR_Q_CREATE_USER *q_u, SAMR_R_CREATE_USER *r_u)
1877 SAM_ACCOUNT *sam_pass=NULL;
1881 POLICY_HND dom_pol = q_u->domain_pol;
1882 UNISTR2 user_account = q_u->uni_name;
1883 uint16 acb_info = q_u->acb_info;
1884 POLICY_HND *user_pol = &r_u->user_pol;
1885 struct samr_info *info = NULL;
1890 /* find the policy handle. open a policy on it. */
1891 if (!find_policy_by_hnd(p, &dom_pol, NULL))
1892 return NT_STATUS_INVALID_HANDLE;
1894 /* find the account: tell the caller if it exists.
1895 lkclXXXX i have *no* idea if this is a problem or not
1896 or even if you are supposed to construct a different
1897 reply if the account already exists...
1900 rpcstr_pull(account, user_account.buffer, sizeof(account), user_account.uni_str_len*2, 0);
1903 pdb_init_sam(&sam_pass);
1906 ret = pdb_getsampwnam(sam_pass, account);
1909 /* this account exists: say so */
1910 pdb_free_sam(&sam_pass);
1911 return NT_STATUS_USER_EXISTS;
1914 pdb_free_sam(&sam_pass);
1917 * NB. VERY IMPORTANT ! This call must be done as the current pipe user,
1918 * *NOT* surrounded by a become_root()/unbecome_root() call. This ensures
1919 * that only people with write access to the smbpasswd file will be able
1920 * to create a user. JRA.
1924 * add the user in the /etc/passwd file or the unix authority system.
1925 * We don't check if the smb_create_user() function succed or not for 2 reasons:
1926 * a) local_password_change() checks for us if the /etc/passwd account really exists
1927 * b) smb_create_user() would return an error if the account already exists
1928 * and as it could return an error also if it can't create the account, it would be tricky.
1930 * So we go the easy way, only check after if the account exists.
1931 * JFM (2/3/2001), to clear any possible bad understanding (-:
1933 * We now have seperate script paramaters for adding users/machines so we
1934 * now have some sainity-checking to match.
1937 DEBUG(10,("checking account %s at pos %d for $ termination\n",account, strlen(account)-1));
1939 if ((acb_info & ACB_WSTRUST) && (account[strlen(account)-1] == '$')) {
1940 pstrcpy(add_script, lp_addmachine_script());
1941 } else if ((!(acb_info & ACB_WSTRUST)) && (account[strlen(account)-1] != '$')) {
1942 pstrcpy(add_script, lp_adduser_script());
1944 DEBUG(0, ("_api_samr_create_user: mismatch between trust flags and $ termination\n"));
1945 pdb_free_sam(&sam_pass);
1946 return NT_STATUS_UNSUCCESSFUL;
1951 * we can't check both the ending $ and the acb_info.
1953 * UserManager creates trust accounts (ending in $,
1954 * normal that hidden accounts) with the acb_info equals to ACB_NORMAL.
1957 if (account[strlen(account)-1] == '$')
1958 pstrcpy(add_script, lp_addmachine_script());
1960 pstrcpy(add_script, lp_adduser_script());
1964 all_string_sub(add_script, "%u", account, sizeof(account));
1965 add_ret = smbrun(add_script,NULL);
1966 DEBUG(3,("_api_samr_create_user: Running the command `%s' gave %d\n", add_script, add_ret));
1969 pw = getpwnam_alloc(account);
1972 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_pw(&sam_pass, pw))) {
1976 passwd_free(&pw); /* done with this now */
1978 DEBUG(3,("attempting to create non-unix account %s\n", account));
1980 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam(&sam_pass))) {
1984 if (!pdb_set_username(sam_pass, account)) {
1985 pdb_free_sam(&sam_pass);
1986 return NT_STATUS_NO_MEMORY;
1990 pdb_set_acct_ctrl(sam_pass, acb_info);
1992 if (!pdb_add_sam_account(sam_pass)) {
1993 pdb_free_sam(&sam_pass);
1994 DEBUG(0, ("could not add user/computer %s to passdb. Check permissions?\n",
1996 return NT_STATUS_ACCESS_DENIED;
1999 pdb_reset_sam(sam_pass);
2001 if (!pdb_getsampwnam(sam_pass, account)) {
2002 pdb_free_sam(&sam_pass);
2003 DEBUG(0, ("could not find user/computer %s just added to passdb?!?\n",
2005 return NT_STATUS_ACCESS_DENIED;
2008 /* Get the domain SID stored in the domain policy */
2009 if(!get_lsa_policy_samr_sid(p, &dom_pol, &sid)) {
2010 pdb_free_sam(&sam_pass);
2011 return NT_STATUS_INVALID_HANDLE;
2014 /* append the user's RID to it */
2015 if(!sid_append_rid(&sid, pdb_get_user_rid(sam_pass) )) {
2016 pdb_free_sam(&sam_pass);
2017 return NT_STATUS_NO_SUCH_USER;
2020 /* associate the user's SID with the new handle. */
2021 if ((info = get_samr_info_by_sid(&sid)) == NULL) {
2022 pdb_free_sam(&sam_pass);
2023 return NT_STATUS_NO_MEMORY;
2029 /* get a (unique) handle. open a policy on it. */
2030 if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info)) {
2031 pdb_free_sam(&sam_pass);
2032 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2035 r_u->user_rid=pdb_get_user_rid(sam_pass);
2036 r_u->unknown_0 = 0x000703ff;
2038 pdb_free_sam(&sam_pass);
2040 return NT_STATUS_OK;
2043 /*******************************************************************
2044 samr_reply_connect_anon
2045 ********************************************************************/
2047 NTSTATUS _samr_connect_anon(pipes_struct *p, SAMR_Q_CONNECT_ANON *q_u, SAMR_R_CONNECT_ANON *r_u)
2049 struct samr_info *info = NULL;
2051 /* set up the SAMR connect_anon response */
2053 r_u->status = NT_STATUS_OK;
2055 /* associate the user's SID with the new handle. */
2056 if ((info = get_samr_info_by_sid(NULL)) == NULL)
2057 return NT_STATUS_NO_MEMORY;
2059 info->status = q_u->unknown_0;
2061 /* get a (unique) handle. open a policy on it. */
2062 if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2063 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2068 /*******************************************************************
2070 ********************************************************************/
2072 NTSTATUS _samr_connect(pipes_struct *p, SAMR_Q_CONNECT *q_u, SAMR_R_CONNECT *r_u)
2074 struct samr_info *info = NULL;
2076 DEBUG(5,("_samr_connect: %d\n", __LINE__));
2078 r_u->status = NT_STATUS_OK;
2080 /* associate the user's SID with the new handle. */
2081 if ((info = get_samr_info_by_sid(NULL)) == NULL)
2082 return NT_STATUS_NO_MEMORY;
2084 info->status = q_u->access_mask;
2086 /* get a (unique) handle. open a policy on it. */
2087 if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2088 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2090 DEBUG(5,("_samr_connect: %d\n", __LINE__));
2095 /**********************************************************************
2096 api_samr_lookup_domain
2097 **********************************************************************/
2099 NTSTATUS _samr_lookup_domain(pipes_struct *p, SAMR_Q_LOOKUP_DOMAIN *q_u, SAMR_R_LOOKUP_DOMAIN *r_u)
2101 r_u->status = NT_STATUS_OK;
2103 if (!find_policy_by_hnd(p, &q_u->connect_pol, NULL))
2104 return NT_STATUS_INVALID_HANDLE;
2106 /* assume the domain name sent is our global_myname and
2107 send global_sam_sid */
2108 init_samr_r_lookup_domain(r_u, &global_sam_sid, r_u->status);
2113 /******************************************************************
2114 makes a SAMR_R_ENUM_DOMAINS structure.
2115 ********************************************************************/
2117 static BOOL make_enum_domains(TALLOC_CTX *ctx, SAM_ENTRY **pp_sam,
2118 UNISTR2 **pp_uni_name, uint32 num_sam_entries, fstring doms[])
2124 DEBUG(5, ("make_enum_domains\n"));
2127 *pp_uni_name = NULL;
2129 if (num_sam_entries == 0)
2132 sam = (SAM_ENTRY *)talloc_zero(ctx, sizeof(SAM_ENTRY)*num_sam_entries);
2133 uni_name = (UNISTR2 *)talloc_zero(ctx, sizeof(UNISTR2)*num_sam_entries);
2135 if (sam == NULL || uni_name == NULL)
2138 for (i = 0; i < num_sam_entries; i++) {
2139 int len = doms[i] != NULL ? strlen(doms[i]) : 0;
2141 init_sam_entry(&sam[i], len, 0);
2142 init_unistr2(&uni_name[i], doms[i], len);
2146 *pp_uni_name = uni_name;
2151 /**********************************************************************
2152 api_samr_enum_domains
2153 **********************************************************************/
2155 NTSTATUS _samr_enum_domains(pipes_struct *p, SAMR_Q_ENUM_DOMAINS *q_u, SAMR_R_ENUM_DOMAINS *r_u)
2157 uint32 num_entries = 2;
2160 r_u->status = NT_STATUS_OK;
2162 fstrcpy(dom[0],global_myworkgroup);
2163 fstrcpy(dom[1],"Builtin");
2165 if (!make_enum_domains(p->mem_ctx, &r_u->sam, &r_u->uni_dom_name, num_entries, dom))
2166 return NT_STATUS_NO_MEMORY;
2168 init_samr_r_enum_domains(r_u, q_u->start_idx + num_entries, num_entries);
2173 /*******************************************************************
2175 ********************************************************************/
2177 NTSTATUS _api_samr_open_alias(pipes_struct *p, SAMR_Q_OPEN_ALIAS *q_u, SAMR_R_OPEN_ALIAS *r_u)
2180 POLICY_HND domain_pol = q_u->dom_pol;
2181 uint32 alias_rid = q_u->rid_alias;
2182 POLICY_HND *alias_pol = &r_u->pol;
2183 struct samr_info *info = NULL;
2185 r_u->status = NT_STATUS_OK;
2187 /* get the domain policy. */
2188 if (!find_policy_by_hnd(p, &domain_pol, NULL))
2189 return NT_STATUS_INVALID_HANDLE;
2191 /* Get the domain SID stored in the domain policy */
2192 if(!get_lsa_policy_samr_sid(p, &domain_pol, &sid))
2193 return NT_STATUS_INVALID_HANDLE;
2195 /* append the alias' RID to it */
2196 if(!sid_append_rid(&sid, alias_rid))
2197 return NT_STATUS_NO_SUCH_USER;
2200 * we should check if the rid really exist !!!
2204 /* associate the user's SID with the new handle. */
2205 if ((info = get_samr_info_by_sid(&sid)) == NULL)
2206 return NT_STATUS_NO_MEMORY;
2208 /* get a (unique) handle. open a policy on it. */
2209 if (!create_policy_hnd(p, alias_pol, free_samr_info, (void *)info))
2210 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2215 /*******************************************************************
2217 ********************************************************************/
2219 static BOOL set_user_info_10(const SAM_USER_INFO_10 *id10, uint32 rid)
2221 SAM_ACCOUNT *pwd =NULL;
2226 ret = pdb_getsampwrid(pwd, rid);
2234 DEBUG(5, ("set_user_info_10: NULL id10\n"));
2239 if (!pdb_set_acct_ctrl(pwd, id10->acb_info)) {
2244 if(!pdb_update_sam_account(pwd)) {
2254 /*******************************************************************
2256 ********************************************************************/
2258 static BOOL set_user_info_12(SAM_USER_INFO_12 *id12, uint32 rid)
2260 SAM_ACCOUNT *pwd = NULL;
2264 if(!pdb_getsampwrid(pwd, rid)) {
2270 DEBUG(2, ("set_user_info_12: id12 is NULL\n"));
2275 if (!pdb_set_lanman_passwd (pwd, id12->lm_pwd)) {
2279 if (!pdb_set_nt_passwd (pwd, id12->nt_pwd)) {
2283 if (!pdb_set_pass_changed_now (pwd)) {
2288 if(!pdb_update_sam_account(pwd)) {
2297 /*******************************************************************
2299 ********************************************************************/
2301 static BOOL set_user_info_21(SAM_USER_INFO_21 *id21, uint32 rid)
2303 SAM_ACCOUNT *pwd = NULL;
2306 DEBUG(5, ("set_user_info_21: NULL id21\n"));
2312 if (!pdb_getsampwrid(pwd, rid)) {
2317 copy_id21_to_sam_passwd(pwd, id21);
2320 * The funny part about the previous two calls is
2321 * that pwd still has the password hashes from the
2322 * passdb entry. These have not been updated from
2323 * id21. I don't know if they need to be set. --jerry
2326 /* write the change out */
2327 if(!pdb_update_sam_account(pwd)) {
2337 /*******************************************************************
2339 ********************************************************************/
2341 static BOOL set_user_info_23(SAM_USER_INFO_23 *id23, uint32 rid)
2343 SAM_ACCOUNT *pwd = NULL;
2344 pstring plaintext_buf;
2349 DEBUG(5, ("set_user_info_23: NULL id23\n"));
2355 if (!pdb_getsampwrid(pwd, rid)) {
2360 DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
2361 pdb_get_username(pwd)));
2363 acct_ctrl = pdb_get_acct_ctrl(pwd);
2365 copy_id23_to_sam_passwd(pwd, id23);
2367 if (!decode_pw_buffer((char*)id23->pass, plaintext_buf, 256, &len)) {
2372 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
2377 /* if it's a trust account, don't update /etc/passwd */
2378 if ( (!IS_SAM_UNIX_USER(pwd)) ||
2379 ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
2380 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
2381 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
2382 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
2384 /* update the UNIX password */
2385 if (lp_unix_password_sync() )
2386 if(!chgpasswd(pdb_get_username(pwd), "", plaintext_buf, True)) {
2392 ZERO_STRUCT(plaintext_buf);
2394 if(!pdb_update_sam_account(pwd)) {
2404 /*******************************************************************
2406 ********************************************************************/
2408 static BOOL set_user_info_pw(char *pass, uint32 rid)
2410 SAM_ACCOUNT *pwd = NULL;
2412 pstring plaintext_buf;
2417 if (!pdb_getsampwrid(pwd, rid)) {
2422 DEBUG(5, ("Attempting administrator password change for user %s\n",
2423 pdb_get_username(pwd)));
2425 acct_ctrl = pdb_get_acct_ctrl(pwd);
2427 ZERO_STRUCT(plaintext_buf);
2429 if (!decode_pw_buffer(pass, plaintext_buf, 256, &len)) {
2434 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
2439 /* if it's a trust account, don't update /etc/passwd */
2440 if ( (!IS_SAM_UNIX_USER(pwd)) ||
2441 ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
2442 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
2443 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
2444 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
2446 /* update the UNIX password */
2447 if (lp_unix_password_sync()) {
2448 if(!chgpasswd(pdb_get_username(pwd), "", plaintext_buf, True)) {
2455 ZERO_STRUCT(plaintext_buf);
2457 DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
2459 /* update the SAMBA password */
2460 if(!pdb_update_sam_account(pwd)) {
2470 /*******************************************************************
2471 samr_reply_set_userinfo
2472 ********************************************************************/
2474 NTSTATUS _samr_set_userinfo(pipes_struct *p, SAMR_Q_SET_USERINFO *q_u, SAMR_R_SET_USERINFO *r_u)
2478 POLICY_HND *pol = &q_u->pol;
2479 uint16 switch_value = q_u->switch_value;
2480 SAM_USERINFO_CTR *ctr = q_u->ctr;
2482 DEBUG(5, ("_samr_set_userinfo: %d\n", __LINE__));
2484 r_u->status = NT_STATUS_OK;
2486 /* find the policy handle. open a policy on it. */
2487 if (!get_lsa_policy_samr_sid(p, pol, &sid))
2488 return NT_STATUS_INVALID_HANDLE;
2490 sid_split_rid(&sid, &rid);
2492 DEBUG(5, ("_samr_set_userinfo: rid:0x%x, level:%d\n", rid, switch_value));
2495 DEBUG(5, ("_samr_set_userinfo: NULL info level\n"));
2496 return NT_STATUS_INVALID_INFO_CLASS;
2499 /* ok! user info levels (lots: see MSDEV help), off we go... */
2500 switch (switch_value) {
2502 if (!set_user_info_12(ctr->info.id12, rid))
2503 return NT_STATUS_ACCESS_DENIED;
2507 SamOEMhash(ctr->info.id24->pass, p->session_key, 516);
2509 dump_data(100, (char *)ctr->info.id24->pass, 516);
2511 if (!set_user_info_pw((char *)ctr->info.id24->pass, rid))
2512 return NT_STATUS_ACCESS_DENIED;
2518 * Currently we don't really know how to unmarshall
2519 * the level 25 struct, and the password encryption
2520 * is different. This is a placeholder for when we
2521 * do understand it. In the meantime just return INVALID
2522 * info level and W2K SP2 drops down to level 23... JRA.
2525 SamOEMhash(ctr->info.id25->pass, p->session_key, 532);
2527 dump_data(100, (char *)ctr->info.id25->pass, 532);
2529 if (!set_user_info_pw(ctr->info.id25->pass, rid))
2530 return NT_STATUS_ACCESS_DENIED;
2533 return NT_STATUS_INVALID_INFO_CLASS;
2536 SamOEMhash(ctr->info.id23->pass, p->session_key, 516);
2538 dump_data(100, (char *)ctr->info.id23->pass, 516);
2540 if (!set_user_info_23(ctr->info.id23, rid))
2541 return NT_STATUS_ACCESS_DENIED;
2545 return NT_STATUS_INVALID_INFO_CLASS;
2551 /*******************************************************************
2552 samr_reply_set_userinfo2
2553 ********************************************************************/
2555 NTSTATUS _samr_set_userinfo2(pipes_struct *p, SAMR_Q_SET_USERINFO2 *q_u, SAMR_R_SET_USERINFO2 *r_u)
2559 SAM_USERINFO_CTR *ctr = q_u->ctr;
2560 POLICY_HND *pol = &q_u->pol;
2561 uint16 switch_value = q_u->switch_value;
2563 DEBUG(5, ("samr_reply_set_userinfo2: %d\n", __LINE__));
2565 r_u->status = NT_STATUS_OK;
2567 /* find the policy handle. open a policy on it. */
2568 if (!get_lsa_policy_samr_sid(p, pol, &sid))
2569 return NT_STATUS_INVALID_HANDLE;
2571 sid_split_rid(&sid, &rid);
2573 DEBUG(5, ("samr_reply_set_userinfo2: rid:0x%x\n", rid));
2576 DEBUG(5, ("samr_reply_set_userinfo2: NULL info level\n"));
2577 return NT_STATUS_INVALID_INFO_CLASS;
2580 switch_value=ctr->switch_value;
2582 /* ok! user info levels (lots: see MSDEV help), off we go... */
2583 switch (switch_value) {
2585 if (!set_user_info_21(ctr->info.id21, rid))
2586 return NT_STATUS_ACCESS_DENIED;
2589 if (!set_user_info_10(ctr->info.id10, rid))
2590 return NT_STATUS_ACCESS_DENIED;
2593 /* Used by AS/U JRA. */
2594 if (!set_user_info_12(ctr->info.id12, rid))
2595 return NT_STATUS_ACCESS_DENIED;
2598 return NT_STATUS_INVALID_INFO_CLASS;
2604 /*********************************************************************
2605 _samr_query_aliasmem
2606 *********************************************************************/
2608 NTSTATUS _samr_query_useraliases(pipes_struct *p, SAMR_Q_QUERY_USERALIASES *q_u, SAMR_R_QUERY_USERALIASES *r_u)
2610 int num_groups = 0, tmp_num_groups=0;
2611 uint32 *rids=NULL, *new_rids=NULL, *tmp_rids=NULL;
2612 struct samr_info *info = NULL;
2614 /* until i see a real useraliases query, we fack one up */
2616 /* I have seen one, JFM 2/12/2001 */
2618 * Explanation of what this call does:
2619 * for all the SID given in the request:
2620 * return a list of alias (local groups)
2621 * that have those SID as members.
2623 * and that's the alias in the domain specified
2624 * in the policy_handle
2626 * if the policy handle is on an incorrect sid
2627 * for example a user's sid
2628 * we should reply NT_STATUS_OBJECT_TYPE_MISMATCH
2631 r_u->status = NT_STATUS_OK;
2633 DEBUG(5,("_samr_query_useraliases: %d\n", __LINE__));
2635 /* find the policy handle. open a policy on it. */
2636 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
2637 return NT_STATUS_INVALID_HANDLE;
2639 if (!sid_check_is_domain(&info->sid) &&
2640 !sid_check_is_builtin(&info->sid))
2641 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2644 for (i=0; i<q_u->num_sids1; i++) {
2646 r_u->status=get_alias_user_groups(p->mem_ctx, &info->sid, &tmp_num_groups, &tmp_rids, &(q_u->sid[i].sid));
2649 * if there is an error, we just continue as
2650 * it can be an unfound user or group
2652 if (NT_STATUS_IS_ERR(r_u->status)) {
2653 DEBUG(10,("_samr_query_useraliases: an error occured while getting groups\n"));
2657 if (tmp_num_groups==0) {
2658 DEBUG(10,("_samr_query_useraliases: no groups found\n"));
2662 new_rids=(uint32 *)talloc_realloc(p->mem_ctx, rids, (num_groups+tmp_num_groups)*sizeof(uint32));
2663 if (new_rids==NULL) {
2664 DEBUG(0,("_samr_query_useraliases: could not realloc memory\n"));
2665 return NT_STATUS_NO_MEMORY;
2669 for (j=0; j<tmp_num_groups; j++)
2670 rids[j+num_groups]=tmp_rids[j];
2672 safe_free(tmp_rids);
2674 num_groups+=tmp_num_groups;
2677 init_samr_r_query_useraliases(r_u, num_groups, rids, NT_STATUS_OK);
2678 return NT_STATUS_OK;
2681 /*********************************************************************
2682 _samr_query_aliasmem
2683 *********************************************************************/
2685 NTSTATUS _samr_query_aliasmem(pipes_struct *p, SAMR_Q_QUERY_ALIASMEM *q_u, SAMR_R_QUERY_ALIASMEM *r_u)
2697 fstring alias_sid_str;
2701 /* find the policy handle. open a policy on it. */
2702 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid))
2703 return NT_STATUS_INVALID_HANDLE;
2705 sid_copy(&als_sid, &alias_sid);
2706 sid_to_string(alias_sid_str, &alias_sid);
2707 sid_split_rid(&alias_sid, &alias_rid);
2709 DEBUG(10, ("sid is %s\n", alias_sid_str));
2711 if (sid_equal(&alias_sid, &global_sid_Builtin)) {
2712 DEBUG(10, ("lookup on Builtin SID (S-1-5-32)\n"));
2713 if(!get_local_group_from_sid(als_sid, &map, MAPPING_WITHOUT_PRIV))
2714 return NT_STATUS_NO_SUCH_ALIAS;
2716 if (sid_equal(&alias_sid, &global_sam_sid)) {
2717 DEBUG(10, ("lookup on Server SID\n"));
2718 if(!get_local_group_from_sid(als_sid, &map, MAPPING_WITHOUT_PRIV))
2719 return NT_STATUS_NO_SUCH_ALIAS;
2723 if(!get_uid_list_of_group(map.gid, &uid, &num_uids))
2724 return NT_STATUS_NO_SUCH_ALIAS;
2726 DEBUG(10, ("sid is %s\n", alias_sid_str));
2727 sid = (DOM_SID2 *)talloc_zero(p->mem_ctx, sizeof(DOM_SID2) * num_uids);
2728 if (num_uids!=0 && sid == NULL)
2729 return NT_STATUS_NO_MEMORY;
2731 for (i = 0; i < num_uids; i++) {
2732 sid_copy(&temp_sid, &global_sam_sid);
2733 sid_append_rid(&temp_sid, pdb_uid_to_user_rid(uid[i]));
2735 init_dom_sid2(&sid[i], &temp_sid);
2738 DEBUG(10, ("sid is %s\n", alias_sid_str));
2739 init_samr_r_query_aliasmem(r_u, num_uids, sid, NT_STATUS_OK);
2741 return NT_STATUS_OK;
2744 /*********************************************************************
2745 _samr_query_groupmem
2746 *********************************************************************/
2748 NTSTATUS _samr_query_groupmem(pipes_struct *p, SAMR_Q_QUERY_GROUPMEM *q_u, SAMR_R_QUERY_GROUPMEM *r_u)
2754 fstring group_sid_str;
2763 /* find the policy handle. open a policy on it. */
2764 if (!get_lsa_policy_samr_sid(p, &q_u->group_pol, &group_sid))
2765 return NT_STATUS_INVALID_HANDLE;
2767 /* todo: change to use sid_compare_front */
2769 sid_split_rid(&group_sid, &group_rid);
2770 sid_to_string(group_sid_str, &group_sid);
2771 DEBUG(10, ("sid is %s\n", group_sid_str));
2773 /* can we get a query for an SID outside our domain ? */
2774 if (!sid_equal(&group_sid, &global_sam_sid))
2775 return NT_STATUS_NO_SUCH_GROUP;
2777 sid_append_rid(&group_sid, group_rid);
2778 DEBUG(10, ("lookup on Domain SID\n"));
2780 if(!get_domain_group_from_sid(group_sid, &map, MAPPING_WITHOUT_PRIV))
2781 return NT_STATUS_NO_SUCH_GROUP;
2783 if(!get_uid_list_of_group(map.gid, &uid, &num_uids))
2784 return NT_STATUS_NO_SUCH_GROUP;
2786 rid=talloc_zero(p->mem_ctx, sizeof(uint32)*num_uids);
2787 attr=talloc_zero(p->mem_ctx, sizeof(uint32)*num_uids);
2789 if (num_uids!=0 && (rid==NULL || attr==NULL))
2790 return NT_STATUS_NO_MEMORY;
2792 for (i=0; i<num_uids; i++) {
2793 rid[i]=pdb_uid_to_user_rid(uid[i]);
2794 attr[i] = SID_NAME_USER;
2797 init_samr_r_query_groupmem(r_u, num_uids, rid, attr, NT_STATUS_OK);
2799 return NT_STATUS_OK;
2802 /*********************************************************************
2804 *********************************************************************/
2806 NTSTATUS _samr_add_aliasmem(pipes_struct *p, SAMR_Q_ADD_ALIASMEM *q_u, SAMR_R_ADD_ALIASMEM *r_u)
2809 fstring alias_sid_str;
2817 /* Find the policy handle. Open a policy on it. */
2818 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid))
2819 return NT_STATUS_INVALID_HANDLE;
2821 sid_to_string(alias_sid_str, &alias_sid);
2822 DEBUG(10, ("sid is %s\n", alias_sid_str));
2824 if (sid_compare(&alias_sid, &global_sam_sid)>0) {
2825 DEBUG(10, ("adding member on Server SID\n"));
2826 if(!get_local_group_from_sid(alias_sid, &map, MAPPING_WITHOUT_PRIV))
2827 return NT_STATUS_NO_SUCH_ALIAS;
2830 if (sid_compare(&alias_sid, &global_sid_Builtin)>0) {
2831 DEBUG(10, ("adding member on BUILTIN SID\n"));
2832 if( !get_local_group_from_sid(alias_sid, &map, MAPPING_WITHOUT_PRIV))
2833 return NT_STATUS_NO_SUCH_ALIAS;
2836 return NT_STATUS_NO_SUCH_ALIAS;
2839 sid_split_rid(&q_u->sid.sid, &rid);
2840 uid=pdb_user_rid_to_uid(rid);
2842 if ((pwd=getpwuid(uid)) == NULL)
2843 return NT_STATUS_NO_SUCH_USER;
2845 if ((grp=getgrgid(map.gid)) == NULL)
2846 return NT_STATUS_NO_SUCH_ALIAS;
2848 /* we need to copy the name otherwise it's overloaded in user_in_group_list */
2849 fstrcpy(grp_name, grp->gr_name);
2851 /* if the user is already in the group */
2852 if(user_in_group_list(pwd->pw_name, grp_name))
2853 return NT_STATUS_MEMBER_IN_ALIAS;
2856 * ok, the group exist, the user exist, the user is not in the group,
2857 * we can (finally) add it to the group !
2859 smb_add_user_group(grp_name, pwd->pw_name);
2861 /* check if the user has been added then ... */
2862 if(!user_in_group_list(pwd->pw_name, grp_name))
2863 return NT_STATUS_MEMBER_NOT_IN_ALIAS; /* don't know what to reply else */
2865 return NT_STATUS_OK;
2868 /*********************************************************************
2870 *********************************************************************/
2872 NTSTATUS _samr_del_aliasmem(pipes_struct *p, SAMR_Q_DEL_ALIASMEM *q_u, SAMR_R_DEL_ALIASMEM *r_u)
2875 fstring alias_sid_str;
2880 SAM_ACCOUNT *sam_pass=NULL;
2882 /* Find the policy handle. Open a policy on it. */
2883 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid))
2884 return NT_STATUS_INVALID_HANDLE;
2886 sid_to_string(alias_sid_str, &alias_sid);
2887 DEBUG(10, ("_samr_del_aliasmem:sid is %s\n", alias_sid_str));
2889 if (!sid_check_is_in_our_domain(&alias_sid) &&
2890 !sid_check_is_in_builtin(&alias_sid)) {
2891 DEBUG(10, ("_samr_del_aliasmem:invalid alias group\n"));
2892 return NT_STATUS_NO_SUCH_ALIAS;
2895 if( !get_local_group_from_sid(alias_sid, &map, MAPPING_WITHOUT_PRIV))
2896 return NT_STATUS_NO_SUCH_ALIAS;
2898 if ((grp=getgrgid(map.gid)) == NULL)
2899 return NT_STATUS_NO_SUCH_ALIAS;
2901 /* we need to copy the name otherwise it's overloaded in user_in_group_list */
2902 fstrcpy(grp_name, grp->gr_name);
2904 sid_peek_rid(&q_u->sid.sid, &rid);
2906 /* check if the user exists before trying to remove it from the group */
2907 pdb_init_sam(&sam_pass);
2908 if(!pdb_getsampwrid(sam_pass, rid)) {
2909 DEBUG(5,("_samr_del_aliasmem:User %s doesn't exist.\n", pdb_get_username(sam_pass)));
2910 pdb_free_sam(&sam_pass);
2911 return NT_STATUS_NO_SUCH_USER;
2914 /* if the user is not in the group */
2915 if(!user_in_group_list(pdb_get_username(sam_pass), grp_name)) {
2916 pdb_free_sam(&sam_pass);
2917 return NT_STATUS_MEMBER_IN_ALIAS;
2920 smb_delete_user_group(grp_name, pdb_get_username(sam_pass));
2922 /* check if the user has been removed then ... */
2923 if(user_in_group_list(pdb_get_username(sam_pass), grp_name)) {
2924 pdb_free_sam(&sam_pass);
2925 return NT_STATUS_MEMBER_NOT_IN_ALIAS; /* don't know what to reply else */
2928 pdb_free_sam(&sam_pass);
2929 return NT_STATUS_OK;
2932 /*********************************************************************
2934 *********************************************************************/
2936 NTSTATUS _samr_add_groupmem(pipes_struct *p, SAMR_Q_ADD_GROUPMEM *q_u, SAMR_R_ADD_GROUPMEM *r_u)
2939 fstring group_sid_str;
2945 /* Find the policy handle. Open a policy on it. */
2946 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid))
2947 return NT_STATUS_INVALID_HANDLE;
2949 sid_to_string(group_sid_str, &group_sid);
2950 DEBUG(10, ("sid is %s\n", group_sid_str));
2952 if (sid_compare(&group_sid, &global_sam_sid)<=0)
2953 return NT_STATUS_NO_SUCH_GROUP;
2955 DEBUG(10, ("lookup on Domain SID\n"));
2957 if(!get_domain_group_from_sid(group_sid, &map, MAPPING_WITHOUT_PRIV))
2958 return NT_STATUS_NO_SUCH_GROUP;
2960 if ((pwd=getpwuid(pdb_user_rid_to_uid(q_u->rid))) ==NULL)
2961 return NT_STATUS_NO_SUCH_USER;
2963 if ((grp=getgrgid(map.gid)) == NULL)
2964 return NT_STATUS_NO_SUCH_GROUP;
2966 /* we need to copy the name otherwise it's overloaded in user_in_group_list */
2967 fstrcpy(grp_name, grp->gr_name);
2969 /* if the user is already in the group */
2970 if(user_in_group_list(pwd->pw_name, grp_name))
2971 return NT_STATUS_MEMBER_IN_GROUP;
2974 * ok, the group exist, the user exist, the user is not in the group,
2976 * we can (finally) add it to the group !
2979 smb_add_user_group(grp_name, pwd->pw_name);
2981 /* check if the user has been added then ... */
2982 if(!user_in_group_list(pwd->pw_name, grp_name))
2983 return NT_STATUS_MEMBER_NOT_IN_GROUP; /* don't know what to reply else */
2985 return NT_STATUS_OK;
2988 /*********************************************************************
2990 *********************************************************************/
2992 NTSTATUS _samr_del_groupmem(pipes_struct *p, SAMR_Q_DEL_GROUPMEM *q_u, SAMR_R_DEL_GROUPMEM *r_u)
2995 SAM_ACCOUNT *sam_pass=NULL;
3002 * delete the group member named q_u->rid
3003 * who is a member of the sid associated with the handle
3004 * the rid is a user's rid as the group is a domain group.
3007 /* Find the policy handle. Open a policy on it. */
3008 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid))
3009 return NT_STATUS_INVALID_HANDLE;
3011 if(!sid_check_is_in_our_domain(&group_sid))
3012 return NT_STATUS_NO_SUCH_GROUP;
3016 if(!get_domain_group_from_sid(group_sid, &map, MAPPING_WITHOUT_PRIV))
3017 return NT_STATUS_NO_SUCH_GROUP;
3019 if ((grp=getgrgid(map.gid)) == NULL)
3020 return NT_STATUS_NO_SUCH_GROUP;
3022 /* we need to copy the name otherwise it's overloaded in user_in_group_list */
3023 fstrcpy(grp_name, grp->gr_name);
3025 /* check if the user exists before trying to remove it from the group */
3026 pdb_init_sam(&sam_pass);
3027 if(!pdb_getsampwrid(sam_pass, rid)) {
3028 DEBUG(5,("User %s doesn't exist.\n", pdb_get_username(sam_pass)));
3029 pdb_free_sam(&sam_pass);
3030 return NT_STATUS_NO_SUCH_USER;
3033 /* if the user is not in the group */
3034 if(!user_in_group_list(pdb_get_username(sam_pass), grp_name)) {
3035 pdb_free_sam(&sam_pass);
3036 return NT_STATUS_MEMBER_NOT_IN_GROUP;
3039 smb_delete_user_group(grp_name, pdb_get_username(sam_pass));
3041 /* check if the user has been removed then ... */
3042 if(user_in_group_list(pdb_get_username(sam_pass), grp_name)) {
3043 pdb_free_sam(&sam_pass);
3044 return NT_STATUS_ACCESS_DENIED; /* don't know what to reply else */
3047 pdb_free_sam(&sam_pass);
3048 return NT_STATUS_OK;
3052 /*********************************************************************
3053 _samr_delete_dom_user
3054 *********************************************************************/
3056 NTSTATUS _samr_delete_dom_user(pipes_struct *p, SAMR_Q_DELETE_DOM_USER *q_u, SAMR_R_DELETE_DOM_USER *r_u )
3059 SAM_ACCOUNT *sam_pass=NULL;
3062 DEBUG(5, ("_samr_delete_dom_user: %d\n", __LINE__));
3064 /* Find the policy handle. Open a policy on it. */
3065 if (!get_lsa_policy_samr_sid(p, &q_u->user_pol, &user_sid))
3066 return NT_STATUS_INVALID_HANDLE;
3068 if (!sid_check_is_in_our_domain(&user_sid))
3069 return NT_STATUS_CANNOT_DELETE;
3071 sid_peek_rid(&user_sid, &rid);
3073 /* check if the user exists before trying to delete */
3074 pdb_init_sam(&sam_pass);
3075 if(!pdb_getsampwrid(sam_pass, rid)) {
3076 DEBUG(5,("_samr_delete_dom_user:User %s doesn't exist.\n", pdb_get_username(sam_pass)));
3077 pdb_free_sam(&sam_pass);
3078 return NT_STATUS_NO_SUCH_USER;
3081 /* delete the unix side */
3083 * note: we don't check if the delete really happened
3084 * as the script is not necessary present
3085 * and maybe the sysadmin doesn't want to delete the unix side
3087 smb_delete_user(pdb_get_username(sam_pass));
3089 /* and delete the samba side */
3090 if (!pdb_delete_sam_account(sam_pass)) {
3091 DEBUG(5,("_samr_delete_dom_user:Failed to delete entry for user %s.\n", pdb_get_username(sam_pass)));
3092 pdb_free_sam(&sam_pass);
3093 return NT_STATUS_CANNOT_DELETE;
3096 pdb_free_sam(&sam_pass);
3098 if (!close_policy_hnd(p, &q_u->user_pol))
3099 return NT_STATUS_OBJECT_NAME_INVALID;
3101 return NT_STATUS_OK;
3104 /*********************************************************************
3105 _samr_delete_dom_group
3106 *********************************************************************/
3108 NTSTATUS _samr_delete_dom_group(pipes_struct *p, SAMR_Q_DELETE_DOM_GROUP *q_u, SAMR_R_DELETE_DOM_GROUP *r_u)
3113 fstring group_sid_str;
3118 DEBUG(5, ("samr_delete_dom_group: %d\n", __LINE__));
3120 /* Find the policy handle. Open a policy on it. */
3121 if (!get_lsa_policy_samr_sid(p, &q_u->group_pol, &group_sid))
3122 return NT_STATUS_INVALID_HANDLE;
3124 sid_copy(&dom_sid, &group_sid);
3125 sid_to_string(group_sid_str, &dom_sid);
3126 sid_split_rid(&dom_sid, &group_rid);
3128 DEBUG(10, ("sid is %s\n", group_sid_str));
3130 /* we check if it's our SID before deleting */
3131 if (!sid_equal(&dom_sid, &global_sam_sid))
3132 return NT_STATUS_NO_SUCH_GROUP;
3134 DEBUG(10, ("lookup on Domain SID\n"));
3136 if(!get_domain_group_from_sid(group_sid, &map, MAPPING_WITHOUT_PRIV))
3137 return NT_STATUS_NO_SUCH_GROUP;
3141 /* check if group really exists */
3142 if ( (grp=getgrgid(gid)) == NULL)
3143 return NT_STATUS_NO_SUCH_GROUP;
3145 /* we can delete the UNIX group */
3146 smb_delete_group(grp->gr_name);
3148 /* check if the group has been successfully deleted */
3149 if ( (grp=getgrgid(gid)) != NULL)
3150 return NT_STATUS_ACCESS_DENIED;
3152 if(!group_map_remove(group_sid))
3153 return NT_STATUS_ACCESS_DENIED;
3155 if (!close_policy_hnd(p, &q_u->group_pol))
3156 return NT_STATUS_OBJECT_NAME_INVALID;
3158 return NT_STATUS_OK;
3161 /*********************************************************************
3162 _samr_delete_dom_alias
3163 *********************************************************************/
3165 NTSTATUS _samr_delete_dom_alias(pipes_struct *p, SAMR_Q_DELETE_DOM_ALIAS *q_u, SAMR_R_DELETE_DOM_ALIAS *r_u)
3170 fstring alias_sid_str;
3175 DEBUG(5, ("_samr_delete_dom_alias: %d\n", __LINE__));
3177 /* Find the policy handle. Open a policy on it. */
3178 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid))
3179 return NT_STATUS_INVALID_HANDLE;
3181 sid_copy(&dom_sid, &alias_sid);
3182 sid_to_string(alias_sid_str, &dom_sid);
3183 sid_split_rid(&dom_sid, &alias_rid);
3185 DEBUG(10, ("sid is %s\n", alias_sid_str));
3187 /* we check if it's our SID before deleting */
3188 if (!sid_equal(&dom_sid, &global_sam_sid))
3189 return NT_STATUS_NO_SUCH_ALIAS;
3191 DEBUG(10, ("lookup on Local SID\n"));
3193 if(!get_local_group_from_sid(alias_sid, &map, MAPPING_WITHOUT_PRIV))
3194 return NT_STATUS_NO_SUCH_ALIAS;
3198 /* check if group really exists */
3199 if ( (grp=getgrgid(gid)) == NULL)
3200 return NT_STATUS_NO_SUCH_ALIAS;
3202 /* we can delete the UNIX group */
3203 smb_delete_group(grp->gr_name);
3205 /* check if the group has been successfully deleted */
3206 if ( (grp=getgrgid(gid)) != NULL)
3207 return NT_STATUS_ACCESS_DENIED;
3209 /* don't check if we removed it as it could be an un-mapped group */
3210 group_map_remove(alias_sid);
3212 if (!close_policy_hnd(p, &q_u->alias_pol))
3213 return NT_STATUS_OBJECT_NAME_INVALID;
3215 return NT_STATUS_OK;
3218 /*********************************************************************
3219 _samr_create_dom_group
3220 *********************************************************************/
3222 NTSTATUS _samr_create_dom_group(pipes_struct *p, SAMR_Q_CREATE_DOM_GROUP *q_u, SAMR_R_CREATE_DOM_GROUP *r_u)
3229 struct samr_info *info;
3230 PRIVILEGE_SET priv_set;
3232 init_privilege(&priv_set);
3234 /* Find the policy handle. Open a policy on it. */
3235 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &dom_sid))
3236 return NT_STATUS_INVALID_HANDLE;
3238 if (!sid_equal(&dom_sid, &global_sam_sid))
3239 return NT_STATUS_ACCESS_DENIED;
3241 /* TODO: check if allowed to create group and add a become_root/unbecome_root pair.*/
3243 unistr2_to_ascii(name, &q_u->uni_acct_desc, sizeof(name)-1);
3245 /* check if group already exist */
3246 if ((grp=getgrnam(name)) != NULL)
3247 return NT_STATUS_GROUP_EXISTS;
3249 /* we can create the UNIX group */
3250 smb_create_group(name);
3252 /* check if the group has been successfully created */
3253 if ((grp=getgrnam(name)) == NULL)
3254 return NT_STATUS_ACCESS_DENIED;
3256 r_u->rid=pdb_gid_to_group_rid(grp->gr_gid);
3258 /* add the group to the mapping table */
3259 sid_copy(&info_sid, &global_sam_sid);
3260 sid_append_rid(&info_sid, r_u->rid);
3261 sid_to_string(sid_string, &info_sid);
3263 if(!add_initial_entry(grp->gr_gid, sid_string, SID_NAME_DOM_GRP, name, NULL, priv_set, PR_ACCESS_FROM_NETWORK))
3264 return NT_STATUS_ACCESS_DENIED;
3266 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
3267 return NT_STATUS_NO_MEMORY;
3269 /* get a (unique) handle. open a policy on it. */
3270 if (!create_policy_hnd(p, &r_u->pol, free_samr_info, (void *)info))
3271 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3273 return NT_STATUS_OK;
3276 /*********************************************************************
3277 _samr_create_dom_alias
3278 *********************************************************************/
3280 NTSTATUS _samr_create_dom_alias(pipes_struct *p, SAMR_Q_CREATE_DOM_ALIAS *q_u, SAMR_R_CREATE_DOM_ALIAS *r_u)
3287 struct samr_info *info;
3288 PRIVILEGE_SET priv_set;
3290 init_privilege(&priv_set);
3292 /* Find the policy handle. Open a policy on it. */
3293 if (!get_lsa_policy_samr_sid(p, &q_u->dom_pol, &dom_sid))
3294 return NT_STATUS_INVALID_HANDLE;
3296 if (!sid_equal(&dom_sid, &global_sam_sid))
3297 return NT_STATUS_ACCESS_DENIED;
3299 /* TODO: check if allowed to create group and add a become_root/unbecome_root pair.*/
3301 unistr2_to_ascii(name, &q_u->uni_acct_desc, sizeof(name)-1);
3303 /* check if group already exists */
3304 if ( (grp=getgrnam(name)) != NULL)
3305 return NT_STATUS_GROUP_EXISTS;
3307 /* we can create the UNIX group */
3308 smb_create_group(name);
3310 /* check if the group has been successfully created */
3311 if ((grp=getgrnam(name)) == NULL)
3312 return NT_STATUS_ACCESS_DENIED;
3314 r_u->rid=pdb_gid_to_group_rid(grp->gr_gid);
3316 sid_copy(&info_sid, &global_sam_sid);
3317 sid_append_rid(&info_sid, r_u->rid);
3318 sid_to_string(sid_string, &info_sid);
3320 /* add the group to the mapping table */
3321 if(!add_initial_entry(grp->gr_gid, sid_string, SID_NAME_ALIAS, name, NULL, priv_set, PR_ACCESS_FROM_NETWORK))
3322 return NT_STATUS_ACCESS_DENIED;
3324 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
3325 return NT_STATUS_NO_MEMORY;
3327 /* get a (unique) handle. open a policy on it. */
3328 if (!create_policy_hnd(p, &r_u->alias_pol, free_samr_info, (void *)info))
3329 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3331 return NT_STATUS_OK;
3334 /*********************************************************************
3335 _samr_query_groupinfo
3337 sends the name/comment pair of a domain group
3338 level 1 send also the number of users of that group
3339 *********************************************************************/
3341 NTSTATUS _samr_query_groupinfo(pipes_struct *p, SAMR_Q_QUERY_GROUPINFO *q_u, SAMR_R_QUERY_GROUPINFO *r_u)
3347 GROUP_INFO_CTR *ctr;
3349 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid))
3350 return NT_STATUS_INVALID_HANDLE;
3352 if (!get_domain_group_from_sid(group_sid, &map, MAPPING_WITHOUT_PRIV))
3353 return NT_STATUS_INVALID_HANDLE;
3355 ctr=(GROUP_INFO_CTR *)talloc_zero(p->mem_ctx, sizeof(GROUP_INFO_CTR));
3357 return NT_STATUS_NO_MEMORY;
3359 switch (q_u->switch_level) {
3361 ctr->switch_value1 = 1;
3362 if(!get_uid_list_of_group(map.gid, &uid, &num_uids))
3363 return NT_STATUS_NO_SUCH_GROUP;
3364 init_samr_group_info1(&ctr->group.info1, map.nt_name, map.comment, num_uids);
3368 ctr->switch_value1 = 3;
3369 init_samr_group_info3(&ctr->group.info3);
3372 ctr->switch_value1 = 4;
3373 init_samr_group_info4(&ctr->group.info4, map.comment);
3376 return NT_STATUS_INVALID_INFO_CLASS;
3379 init_samr_r_query_groupinfo(r_u, ctr, NT_STATUS_OK);
3381 return NT_STATUS_OK;
3384 /*********************************************************************
3387 update a domain group's comment.
3388 *********************************************************************/
3390 NTSTATUS _samr_set_groupinfo(pipes_struct *p, SAMR_Q_SET_GROUPINFO *q_u, SAMR_R_SET_GROUPINFO *r_u)
3394 GROUP_INFO_CTR *ctr;
3396 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid))
3397 return NT_STATUS_INVALID_HANDLE;
3399 if (!get_domain_group_from_sid(group_sid, &map, MAPPING_WITH_PRIV))
3400 return NT_STATUS_NO_SUCH_GROUP;
3404 switch (ctr->switch_value1) {
3406 unistr2_to_ascii(map.comment, &(ctr->group.info1.uni_acct_desc), sizeof(map.comment)-1);
3409 unistr2_to_ascii(map.comment, &(ctr->group.info4.uni_acct_desc), sizeof(map.comment)-1);
3412 free_privilege(&map.priv_set);
3413 return NT_STATUS_INVALID_INFO_CLASS;
3416 if(!add_mapping_entry(&map, TDB_REPLACE)) {
3417 free_privilege(&map.priv_set);
3418 return NT_STATUS_NO_SUCH_GROUP;
3421 free_privilege(&map.priv_set);
3423 return NT_STATUS_OK;
3426 /*********************************************************************
3429 update a domain group's comment.
3430 *********************************************************************/
3432 NTSTATUS _samr_set_aliasinfo(pipes_struct *p, SAMR_Q_SET_ALIASINFO *q_u, SAMR_R_SET_ALIASINFO *r_u)
3436 ALIAS_INFO_CTR *ctr;
3438 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &group_sid))
3439 return NT_STATUS_INVALID_HANDLE;
3441 if (!get_local_group_from_sid(group_sid, &map, MAPPING_WITH_PRIV))
3442 return NT_STATUS_NO_SUCH_GROUP;
3446 switch (ctr->switch_value1) {
3448 unistr2_to_ascii(map.comment, &(ctr->alias.info3.uni_acct_desc), sizeof(map.comment)-1);
3451 free_privilege(&map.priv_set);
3452 return NT_STATUS_INVALID_INFO_CLASS;
3455 if(!add_mapping_entry(&map, TDB_REPLACE)) {
3456 free_privilege(&map.priv_set);
3457 return NT_STATUS_NO_SUCH_GROUP;
3460 free_privilege(&map.priv_set);
3462 return NT_STATUS_OK;
3465 /*********************************************************************
3466 _samr_get_dom_pwinfo
3467 *********************************************************************/
3469 NTSTATUS _samr_get_dom_pwinfo(pipes_struct *p, SAMR_Q_GET_DOM_PWINFO *q_u, SAMR_R_GET_DOM_PWINFO *r_u)
3471 /* Actually, returning zeros here works quite well :-). */
3472 return NT_STATUS_OK;
3475 /*********************************************************************
3477 *********************************************************************/
3479 NTSTATUS _samr_open_group(pipes_struct *p, SAMR_Q_OPEN_GROUP *q_u, SAMR_R_OPEN_GROUP *r_u)
3484 struct samr_info *info;
3487 if (!get_lsa_policy_samr_sid(p, &q_u->domain_pol, &sid))
3488 return NT_STATUS_INVALID_HANDLE;
3490 /* this should not be hard-coded like this */
3491 if (!sid_equal(&sid, &global_sam_sid))
3492 return NT_STATUS_ACCESS_DENIED;
3494 sid_copy(&info_sid, &global_sam_sid);
3495 sid_append_rid(&info_sid, q_u->rid_group);
3496 sid_to_string(sid_string, &info_sid);
3498 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
3499 return NT_STATUS_NO_MEMORY;
3501 DEBUG(10, ("_samr_open_group:Opening SID: %s\n", sid_string));
3503 /* check if that group really exists */
3504 if (!get_domain_group_from_sid(info->sid, &map, MAPPING_WITHOUT_PRIV))
3505 return NT_STATUS_NO_SUCH_GROUP;
3507 /* get a (unique) handle. open a policy on it. */
3508 if (!create_policy_hnd(p, &r_u->pol, free_samr_info, (void *)info))
3509 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3511 return NT_STATUS_OK;
3514 /*********************************************************************
3516 *********************************************************************/
3518 NTSTATUS _samr_unknown_2d(pipes_struct *p, SAMR_Q_UNKNOWN_2D *q_u, SAMR_R_UNKNOWN_2D *r_u)
3520 DEBUG(0,("_samr_unknown_2d: Not yet implemented.\n"));
3521 return NT_STATUS_NOT_IMPLEMENTED;
3524 /*******************************************************************
3526 ********************************************************************/
3528 NTSTATUS _samr_unknown_2e(pipes_struct *p, SAMR_Q_UNKNOWN_2E *q_u, SAMR_R_UNKNOWN_2E *r_u)
3530 struct samr_info *info = NULL;
3532 uint32 min_pass_len,pass_hist,flag;
3533 time_t u_expire, u_min_age;
3534 NTTIME nt_expire, nt_min_age;
3536 time_t u_lock_duration, u_reset_time;
3537 NTTIME nt_lock_duration, nt_reset_time;
3543 uint32 num_users=0, num_groups=0, num_aliases=0;
3545 if ((ctr = (SAM_UNK_CTR *)talloc_zero(p->mem_ctx, sizeof(SAM_UNK_CTR))) == NULL)
3546 return NT_STATUS_NO_MEMORY;
3550 r_u->status = NT_STATUS_OK;
3552 DEBUG(5,("_samr_unknown_2e: %d\n", __LINE__));
3554 /* find the policy handle. open a policy on it. */
3555 if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)&info))
3556 return NT_STATUS_INVALID_HANDLE;
3558 switch (q_u->switch_value) {
3560 account_policy_get(AP_MIN_PASSWORD_LEN, &min_pass_len);
3561 account_policy_get(AP_PASSWORD_HISTORY, &pass_hist);
3562 account_policy_get(AP_USER_MUST_LOGON_TO_CHG_PASS, &flag);
3563 account_policy_get(AP_MAX_PASSWORD_AGE, (int *)&u_expire);
3564 account_policy_get(AP_MIN_PASSWORD_AGE, (int *)&u_min_age);
3566 unix_to_nt_time_abs(&nt_expire, u_expire);
3567 unix_to_nt_time_abs(&nt_min_age, u_min_age);
3569 init_unk_info1(&ctr->info.inf1, (uint16)min_pass_len, (uint16)pass_hist,
3570 flag, nt_expire, nt_min_age);
3574 r_u->status=load_sampwd_entries(info, ACB_NORMAL);
3576 if (NT_STATUS_IS_ERR(r_u->status)) {
3577 DEBUG(5, ("_samr_query_dispinfo: load_sampwd_entries failed\n"));
3580 num_users=info->disp_info.num_user_account;
3583 r_u->status=load_group_domain_entries(info, &global_sam_sid);
3584 if (NT_STATUS_IS_ERR(r_u->status)) {
3585 DEBUG(5, ("_samr_query_dispinfo: load_group_domain_entries failed\n"));
3588 num_groups=info->disp_info.num_group_account;
3591 /* The time call below is to get a sequence number for the sam. FIXME !!! JRA. */
3592 init_unk_info2(&ctr->info.inf2, global_myworkgroup, global_myname, (uint32) time(NULL),
3593 num_users, num_groups, num_aliases);
3596 account_policy_get(AP_TIME_TO_LOGOUT, (int *)&u_logout);
3597 unix_to_nt_time_abs(&nt_logout, u_logout);
3599 init_unk_info3(&ctr->info.inf3, nt_logout);
3602 init_unk_info5(&ctr->info.inf5, global_myname);
3605 init_unk_info6(&ctr->info.inf6);
3608 init_unk_info7(&ctr->info.inf7);
3611 account_policy_get(AP_LOCK_ACCOUNT_DURATION, (int *)&u_lock_duration);
3612 account_policy_get(AP_RESET_COUNT_TIME, (int *)&u_reset_time);
3613 account_policy_get(AP_BAD_ATTEMPT_LOCKOUT, &lockout);
3615 unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
3616 unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
3618 init_unk_info12(&ctr->info.inf12, nt_lock_duration, nt_reset_time, (uint16)lockout);
3621 return NT_STATUS_INVALID_INFO_CLASS;
3624 init_samr_r_samr_unknown_2e(r_u, q_u->switch_value, ctr, NT_STATUS_OK);
3626 DEBUG(5,("_samr_unknown_2e: %d\n", __LINE__));
3631 /*******************************************************************
3633 ********************************************************************/
3635 NTSTATUS _samr_set_dom_info(pipes_struct *p, SAMR_Q_SET_DOMAIN_INFO *q_u, SAMR_R_SET_DOMAIN_INFO *r_u)
3637 time_t u_expire, u_min_age;
3639 time_t u_lock_duration, u_reset_time;
3641 r_u->status = NT_STATUS_OK;
3643 DEBUG(5,("_samr_set_dom_info: %d\n", __LINE__));
3645 /* find the policy handle. open a policy on it. */
3646 if (!find_policy_by_hnd(p, &q_u->domain_pol, NULL))
3647 return NT_STATUS_INVALID_HANDLE;
3649 DEBUG(5,("_samr_set_dom_info: switch_value: %d\n", q_u->switch_value));
3651 switch (q_u->switch_value) {
3653 u_expire=nt_time_to_unix_abs(&q_u->ctr->info.inf1.expire);
3654 u_min_age=nt_time_to_unix_abs(&q_u->ctr->info.inf1.min_passwordage);
3656 account_policy_set(AP_MIN_PASSWORD_LEN, (uint32)q_u->ctr->info.inf1.min_length_password);
3657 account_policy_set(AP_PASSWORD_HISTORY, (uint32)q_u->ctr->info.inf1.password_history);
3658 account_policy_set(AP_USER_MUST_LOGON_TO_CHG_PASS, (uint32)q_u->ctr->info.inf1.flag);
3659 account_policy_set(AP_MAX_PASSWORD_AGE, (int)u_expire);
3660 account_policy_set(AP_MIN_PASSWORD_AGE, (int)u_min_age);
3665 u_logout=nt_time_to_unix_abs(&q_u->ctr->info.inf3.logout);
3666 account_policy_set(AP_TIME_TO_LOGOUT, (int)u_logout);
3675 u_lock_duration=nt_time_to_unix_abs(&q_u->ctr->info.inf12.duration);
3676 u_reset_time=nt_time_to_unix_abs(&q_u->ctr->info.inf12.reset_count);
3678 account_policy_set(AP_LOCK_ACCOUNT_DURATION, (int)u_lock_duration);
3679 account_policy_set(AP_RESET_COUNT_TIME, (int)u_reset_time);
3680 account_policy_set(AP_BAD_ATTEMPT_LOCKOUT, (uint32)q_u->ctr->info.inf12.bad_attempt_lockout);
3683 return NT_STATUS_INVALID_INFO_CLASS;
3686 init_samr_r_set_domain_info(r_u, NT_STATUS_OK);
3688 DEBUG(5,("_samr_set_dom_info: %d\n", __LINE__));