2 * Unix SMB/Netbios implementation.
4 * RPC Pipe client / server routines
5 * Copyright (C) Andrew Tridgell 1992-1997,
6 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
7 * Copyright (C) Paul Ashton 1997.
8 * Copyright (C) Marc Jacobsen 1999.
9 * Copyright (C) Jeremy Allison 2001-2002.
10 * Copyright (C) Jean François Micouleau 1998-2001.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 * This is the implementation of the SAMR code.
33 extern fstring global_myworkgroup;
34 extern pstring global_myname;
35 extern DOM_SID global_sam_sid;
36 extern DOM_SID global_sid_Builtin;
38 extern rid_name domain_group_rids[];
39 extern rid_name domain_alias_rids[];
40 extern rid_name builtin_alias_rids[];
43 typedef struct _disp_info {
45 uint32 num_user_account;
46 DISP_USER_INFO *disp_user_info;
48 uint32 num_group_account;
49 DISP_GROUP_INFO *disp_group_info;
53 /* for use by the \PIPE\samr policy */
55 uint32 status; /* some sort of flag. best to record it. comes from opnum 0x39 */
59 /*******************************************************************
60 Create a samr_info struct.
61 ********************************************************************/
63 static struct samr_info *get_samr_info_by_sid(DOM_SID *psid)
65 struct samr_info *info;
68 if ((info = (struct samr_info *)malloc(sizeof(struct samr_info))) == NULL)
73 DEBUG(10,("get_samr_info_by_sid: created new info for sid %s\n", sid_to_string(sid_str, psid) ));
74 sid_copy( &info->sid, psid);
76 DEBUG(10,("get_samr_info_by_sid: created new info for NULL sid.\n"));
81 /*******************************************************************
82 Function to free the per handle data.
83 ********************************************************************/
84 static void free_samr_db(struct samr_info *info)
88 if (info->disp_info.group_dbloaded) {
89 for (i=0; i<info->disp_info.num_group_account; i++)
90 SAFE_FREE(info->disp_info.disp_group_info[i].grp);
92 SAFE_FREE(info->disp_info.disp_group_info);
95 if (info->disp_info.user_dbloaded){
96 for (i=0; i<info->disp_info.num_user_account; i++)
97 pdb_free_sam(&info->disp_info.disp_user_info[i].sam);
99 SAFE_FREE(info->disp_info.disp_user_info);
102 info->disp_info.user_dbloaded=False;
103 info->disp_info.group_dbloaded=False;
104 info->disp_info.num_group_account=0;
105 info->disp_info.num_user_account=0;
109 static void free_samr_info(void *ptr)
111 struct samr_info *info=(struct samr_info *) ptr;
117 /*******************************************************************
118 Ensure password info is never given out. Paranioa... JRA.
119 ********************************************************************/
121 static void samr_clear_passwd_fields( SAM_USER_INFO_21 *pass, int num_entries)
128 for (i = 0; i < num_entries; i++) {
129 memset(&pass[i].lm_pwd, '\0', sizeof(pass[i].lm_pwd));
130 memset(&pass[i].nt_pwd, '\0', sizeof(pass[i].nt_pwd));
134 static void samr_clear_sam_passwd(SAM_ACCOUNT *sam_pass)
140 /* These now zero out the old password */
142 pdb_set_lanman_passwd(sam_pass, NULL);
143 pdb_set_nt_passwd(sam_pass, NULL);
147 static NTSTATUS load_sampwd_entries(struct samr_info *info, uint16 acb_mask)
149 SAM_ACCOUNT *pwd = NULL;
150 DISP_USER_INFO *pwd_array = NULL;
152 DEBUG(10,("load_sampwd_entries\n"));
154 /* if the snapshoot is already loaded, return */
155 if (info->disp_info.user_dbloaded==True) {
156 DEBUG(10,("load_sampwd_entries: already in memory\n"));
160 if (!pdb_setsampwent(False)) {
161 DEBUG(0, ("load_sampwd_entries: Unable to open passdb.\n"));
162 return NT_STATUS_ACCESS_DENIED;
165 for (pdb_init_sam(&pwd); pdb_getsampwent(pwd) == True; pwd=NULL, pdb_init_sam(&pwd) ) {
167 if (acb_mask != 0 && !(pdb_get_acct_ctrl(pwd) & acb_mask)) {
169 DEBUG(5,(" acb_mask %x reject\n", acb_mask));
173 /* Realloc some memory for the array of ptr to the SAM_ACCOUNT structs */
174 if (info->disp_info.num_user_account % MAX_SAM_ENTRIES == 0) {
176 DEBUG(10,("load_sampwd_entries: allocating more memory\n"));
177 pwd_array=(DISP_USER_INFO *)Realloc(info->disp_info.disp_user_info,
178 (info->disp_info.num_user_account+MAX_SAM_ENTRIES)*sizeof(DISP_USER_INFO));
181 return NT_STATUS_NO_MEMORY;
183 info->disp_info.disp_user_info=pwd_array;
186 /* link the SAM_ACCOUNT to the array */
187 info->disp_info.disp_user_info[info->disp_info.num_user_account].sam=pwd;
189 DEBUG(10,("load_sampwd_entries: entry: %d\n", info->disp_info.num_user_account));
191 info->disp_info.num_user_account++;
196 /* the snapshoot is in memory, we're ready to enumerate fast */
198 info->disp_info.user_dbloaded=True;
200 DEBUG(12,("load_sampwd_entries: done\n"));
205 static NTSTATUS load_group_domain_entries(struct samr_info *info, DOM_SID *sid)
208 DISP_GROUP_INFO *grp_array = NULL;
209 uint32 group_entries = 0;
212 DEBUG(10,("load_group_domain_entries\n"));
214 /* if the snapshoot is already loaded, return */
215 if (info->disp_info.group_dbloaded==True) {
216 DEBUG(10,("load_group_domain_entries: already in memory\n"));
220 enum_group_mapping(SID_NAME_DOM_GRP, &map, (int *)&group_entries, ENUM_ONLY_MAPPED, MAPPING_WITHOUT_PRIV);
222 info->disp_info.num_group_account=group_entries;
224 grp_array=(DISP_GROUP_INFO *)malloc(info->disp_info.num_group_account*sizeof(DISP_GROUP_INFO));
226 if (group_entries!=0 && grp_array==NULL) {
228 return NT_STATUS_NO_MEMORY;
231 info->disp_info.disp_group_info=grp_array;
233 for (i=0; i<group_entries; i++) {
235 grp_array[i].grp=(DOMAIN_GRP *)malloc(sizeof(DOMAIN_GRP));
237 fstrcpy(grp_array[i].grp->name, map[i].nt_name);
238 fstrcpy(grp_array[i].grp->comment, map[i].comment);
239 sid_split_rid(&map[i].sid, &grp_array[i].grp->rid);
240 grp_array[i].grp->attr=SID_NAME_DOM_GRP;
245 /* the snapshoot is in memory, we're ready to enumerate fast */
247 info->disp_info.group_dbloaded=True;
249 DEBUG(12,("load_group_domain_entries: done\n"));
255 /*******************************************************************
256 This next function should be replaced with something that
257 dynamically returns the correct user info..... JRA.
258 ********************************************************************/
260 static NTSTATUS get_sampwd_entries(SAM_USER_INFO_21 *pw_buf, int start_idx,
261 int *total_entries, int *num_entries,
262 int max_num_entries, uint16 acb_mask)
264 SAM_ACCOUNT *pwd = NULL;
265 BOOL not_finished = True;
268 (*total_entries) = 0;
271 return NT_STATUS_NO_MEMORY;
275 if (!pdb_setsampwent(False)) {
276 DEBUG(0, ("get_sampwd_entries: Unable to open passdb.\n"));
278 return NT_STATUS_ACCESS_DENIED;
281 while (((not_finished = pdb_getsampwent(pwd)) != False)
282 && (*num_entries) < max_num_entries)
290 /* skip the requested number of entries.
291 not very efficient, but hey... */
296 user_name_len = strlen(pdb_get_username(pwd))+1;
297 init_unistr2(&pw_buf[(*num_entries)].uni_user_name, pdb_get_username(pwd), user_name_len);
298 init_uni_hdr(&pw_buf[(*num_entries)].hdr_user_name, user_name_len);
299 pw_buf[(*num_entries)].user_rid = pdb_get_user_rid(pwd);
300 memset((char *)pw_buf[(*num_entries)].nt_pwd, '\0', 16);
302 /* Now check if the NT compatible password is available. */
303 if (pdb_get_nt_passwd(pwd))
304 memcpy( pw_buf[(*num_entries)].nt_pwd , pdb_get_nt_passwd(pwd), 16);
306 pw_buf[(*num_entries)].acb_info = pdb_get_acct_ctrl(pwd);
308 DEBUG(5, ("entry idx: %d user %s, rid 0x%x, acb %x",
309 (*num_entries), pdb_get_username(pwd), pdb_get_user_rid(pwd), pdb_get_acct_ctrl(pwd) ));
311 if (acb_mask == 0 || (pdb_get_acct_ctrl(pwd) & acb_mask)) {
312 DEBUG(5,(" acb_mask %x accepts\n", acb_mask));
315 DEBUG(5,(" acb_mask %x rejects\n", acb_mask));
328 return STATUS_MORE_ENTRIES;
333 /*******************************************************************
335 ********************************************************************/
337 NTSTATUS _samr_close_hnd(pipes_struct *p, SAMR_Q_CLOSE_HND *q_u, SAMR_R_CLOSE_HND *r_u)
339 r_u->status = NT_STATUS_OK;
341 /* close the policy handle */
342 if (!close_policy_hnd(p, &q_u->pol))
343 return NT_STATUS_OBJECT_NAME_INVALID;
345 DEBUG(5,("samr_reply_close_hnd: %d\n", __LINE__));
350 /*******************************************************************
351 samr_reply_open_domain
352 ********************************************************************/
354 NTSTATUS _samr_open_domain(pipes_struct *p, SAMR_Q_OPEN_DOMAIN *q_u, SAMR_R_OPEN_DOMAIN *r_u)
356 struct samr_info *info;
358 r_u->status = NT_STATUS_OK;
360 /* find the connection policy handle. */
361 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
362 return NT_STATUS_INVALID_HANDLE;
364 /* associate the domain SID with the (unique) handle. */
365 if ((info = get_samr_info_by_sid(&q_u->dom_sid.sid))==NULL)
366 return NT_STATUS_NO_MEMORY;
368 /* get a (unique) handle. open a policy on it. */
369 if (!create_policy_hnd(p, &r_u->domain_pol, free_samr_info, (void *)info))
370 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
372 DEBUG(5,("samr_open_domain: %d\n", __LINE__));
377 /*******************************************************************
378 _samr_get_usrdom_pwinfo
379 ********************************************************************/
381 NTSTATUS _samr_get_usrdom_pwinfo(pipes_struct *p, SAMR_Q_GET_USRDOM_PWINFO *q_u, SAMR_R_GET_USRDOM_PWINFO *r_u)
383 struct samr_info *info = NULL;
385 r_u->status = NT_STATUS_OK;
387 /* find the policy handle. open a policy on it. */
388 if (!find_policy_by_hnd(p, &q_u->user_pol, (void **)&info))
389 return NT_STATUS_INVALID_HANDLE;
391 if (!sid_check_is_in_our_domain(&info->sid))
392 return NT_STATUS_OBJECT_TYPE_MISMATCH;
394 init_samr_r_get_usrdom_pwinfo(r_u, NT_STATUS_OK);
396 DEBUG(5,("_samr_get_usrdom_pwinfo: %d\n", __LINE__));
399 * NT sometimes return NT_STATUS_ACCESS_DENIED
400 * I don't know yet why.
406 /*******************************************************************
408 ********************************************************************/
410 static NTSTATUS samr_make_usr_obj_sd(TALLOC_CTX *ctx, SEC_DESC_BUF **buf, DOM_SID *usr_sid)
412 extern DOM_SID global_sid_World;
420 SEC_DESC *psd = NULL;
423 sid_copy(&adm_sid, &global_sid_Builtin);
424 sid_append_rid(&adm_sid, BUILTIN_ALIAS_RID_ADMINS);
426 sid_copy(&act_sid, &global_sid_Builtin);
427 sid_append_rid(&act_sid, BUILTIN_ALIAS_RID_ACCOUNT_OPS);
429 init_sec_access(&mask, 0x2035b);
430 init_sec_ace(&ace[0], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
432 init_sec_access(&mask, 0xf07ff);
433 init_sec_ace(&ace[1], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
434 init_sec_ace(&ace[2], &act_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
436 init_sec_access(&mask,0x20044);
437 init_sec_ace(&ace[3], usr_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
439 if((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 4, ace)) == NULL)
440 return NT_STATUS_NO_MEMORY;
442 if((psd = make_sec_desc(ctx, SEC_DESC_REVISION, NULL, NULL, NULL, psa, &sd_size)) == NULL)
443 return NT_STATUS_NO_MEMORY;
445 if((*buf = make_sec_desc_buf(ctx, sd_size, psd)) == NULL)
446 return NT_STATUS_NO_MEMORY;
451 static BOOL get_lsa_policy_samr_sid(pipes_struct *p, POLICY_HND *pol, DOM_SID *sid)
453 struct samr_info *info = NULL;
455 /* find the policy handle. open a policy on it. */
456 if (!find_policy_by_hnd(p, pol, (void **)&info))
466 /*******************************************************************
468 ********************************************************************/
470 NTSTATUS _samr_query_sec_obj(pipes_struct *p, SAMR_Q_QUERY_SEC_OBJ *q_u, SAMR_R_QUERY_SEC_OBJ *r_u)
475 r_u->status = NT_STATUS_OK;
479 if (!get_lsa_policy_samr_sid(p, &q_u->user_pol, &pol_sid))
480 return NT_STATUS_INVALID_HANDLE;
482 DEBUG(10,("_samr_query_sec_obj: querying security on SID: %s\n", sid_to_string(str_sid, &pol_sid)));
484 r_u->status = samr_make_usr_obj_sd(p->mem_ctx, &r_u->buf, &pol_sid);
486 if (NT_STATUS_IS_OK(r_u->status))
492 /*******************************************************************
493 makes a SAM_ENTRY / UNISTR2* structure from a user list.
494 ********************************************************************/
496 static void make_user_sam_entry_list(TALLOC_CTX *ctx, SAM_ENTRY **sam_pp, UNISTR2 **uni_name_pp,
497 uint32 num_sam_entries, SAM_USER_INFO_21 *pass)
506 if (num_sam_entries == 0)
509 sam = (SAM_ENTRY *)talloc_zero(ctx, sizeof(SAM_ENTRY)*num_sam_entries);
511 uni_name = (UNISTR2 *)talloc_zero(ctx, sizeof(UNISTR2)*num_sam_entries);
513 if (sam == NULL || uni_name == NULL) {
514 DEBUG(0, ("NULL pointers in SAMR_R_QUERY_DISPINFO\n"));
519 ZERO_STRUCTP(uni_name);
521 for (i = 0; i < num_sam_entries; i++) {
522 int len = pass[i].uni_user_name.uni_str_len;
524 init_sam_entry(&sam[i], len, pass[i].user_rid);
525 copy_unistr2(&uni_name[i], &pass[i].uni_user_name);
529 *uni_name_pp = uni_name;
532 /*******************************************************************
533 samr_reply_enum_dom_users
534 ********************************************************************/
536 NTSTATUS _samr_enum_dom_users(pipes_struct *p, SAMR_Q_ENUM_DOM_USERS *q_u, SAMR_R_ENUM_DOM_USERS *r_u)
538 SAM_USER_INFO_21 pass[MAX_SAM_ENTRIES];
540 int total_entries = 0;
542 r_u->status = NT_STATUS_OK;
544 /* find the policy handle. open a policy on it. */
545 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
546 return NT_STATUS_INVALID_HANDLE;
548 DEBUG(5,("_samr_enum_dom_users: %d\n", __LINE__));
551 r_u->status = get_sampwd_entries(pass, q_u->start_idx, &total_entries, &num_entries,
552 MAX_SAM_ENTRIES, q_u->acb_mask);
555 if (NT_STATUS_IS_ERR(r_u->status))
558 samr_clear_passwd_fields(pass, num_entries);
561 * Note from JRA. total_entries is not being used here. Currently if there is a
562 * large user base then it looks like NT will enumerate until get_sampwd_entries
563 * returns False due to num_entries being zero. This will cause an access denied
564 * return. I don't think this is right and needs further investigation. Note that
565 * this is also the same in the TNG code (I don't think that has been tested with
566 * a very large user list as MAX_SAM_ENTRIES is set to 600).
568 * I also think that one of the 'num_entries' return parameters is probably
569 * the "max entries" parameter - but in the TNG code they're all currently set to the same
570 * value (again I think this is wrong).
573 make_user_sam_entry_list(p->mem_ctx, &r_u->sam, &r_u->uni_acct_name, num_entries, pass);
575 init_samr_r_enum_dom_users(r_u, q_u->start_idx + num_entries, num_entries);
577 DEBUG(5,("_samr_enum_dom_users: %d\n", __LINE__));
582 /*******************************************************************
583 makes a SAM_ENTRY / UNISTR2* structure from a group list.
584 ********************************************************************/
586 static void make_group_sam_entry_list(TALLOC_CTX *ctx, SAM_ENTRY **sam_pp, UNISTR2 **uni_name_pp,
587 uint32 num_sam_entries, DOMAIN_GRP *grp)
596 if (num_sam_entries == 0)
599 sam = (SAM_ENTRY *)talloc_zero(ctx, sizeof(SAM_ENTRY)*num_sam_entries);
601 uni_name = (UNISTR2 *)talloc_zero(ctx, sizeof(UNISTR2)*num_sam_entries);
603 if (sam == NULL || uni_name == NULL) {
604 DEBUG(0, ("NULL pointers in SAMR_R_QUERY_DISPINFO\n"));
608 for (i = 0; i < num_sam_entries; i++) {
610 * JRA. I think this should include the null. TNG does not.
612 int len = strlen(grp[i].name)+1;
614 init_sam_entry(&sam[i], len, grp[i].rid);
615 init_unistr2(&uni_name[i], grp[i].name, len);
619 *uni_name_pp = uni_name;
622 /*******************************************************************
623 Get the group entries - similar to get_sampwd_entries().
624 ********************************************************************/
626 static NTSTATUS get_group_alias_entries(TALLOC_CTX *ctx, DOMAIN_GRP **d_grp, DOM_SID *sid, uint32 start_idx,
627 uint32 *p_num_entries, uint32 max_entries)
630 uint32 num_entries = 0;
635 sid_to_string(sid_str, sid);
636 DEBUG(5, ("get_group_alias_entries: enumerating aliases on SID: %s\n", sid_str));
640 /* well-known aliases */
641 if (sid_equal(sid, &global_sid_Builtin) && !lp_hide_local_users()) {
643 enum_group_mapping(SID_NAME_ALIAS, &map, (int *)&num_entries, ENUM_ONLY_MAPPED, MAPPING_WITHOUT_PRIV);
645 if (num_entries != 0) {
646 *d_grp=(DOMAIN_GRP *)talloc_zero(ctx, num_entries*sizeof(DOMAIN_GRP));
648 return NT_STATUS_NO_MEMORY;
650 for(i=0; i<num_entries && i<max_entries; i++) {
651 fstrcpy((*d_grp)[i].name, map[i+start_idx].nt_name);
652 sid_split_rid(&map[i+start_idx].sid, &(*d_grp)[i].rid);
658 } else if (sid_equal(sid, &global_sam_sid) && !lp_hide_local_users()) {
660 struct sys_grent *glist;
661 struct sys_grent *grp;
663 sep = lp_winbind_separator();
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 (strchr_m(smap.nt_name, *sep) != NULL) {
694 DEBUG(10,("get_group_alias_entries: not returing %s, not local.\n", smap.nt_name ));
698 /* Don't return user private groups... */
699 if (Get_Pwnam(smap.nt_name) != 0) {
700 DEBUG(10,("get_group_alias_entries: not returing %s, clashes with user.\n", smap.nt_name ));
704 for( i = 0; i < num_entries; i++)
705 if ( (*d_grp)[i].rid == trid )
708 if ( i < num_entries ) {
709 continue; /* rid was there, dup! */
712 /* JRA - added this for large group db enumeration... */
715 /* skip the requested number of entries.
716 not very efficient, but hey...
722 *d_grp=talloc_realloc(ctx,*d_grp, (num_entries+1)*sizeof(DOMAIN_GRP));
725 return NT_STATUS_NO_MEMORY;
728 fstrcpy((*d_grp)[num_entries].name, smap.nt_name);
729 (*d_grp)[num_entries].rid = trid;
731 DEBUG(10,("get_group_alias_entries: added entry %d, rid:%d\n", num_entries, trid));
737 *p_num_entries = num_entries;
739 DEBUG(10,("get_group_alias_entries: returning %d entries\n", *p_num_entries));
741 if (num_entries >= max_entries)
742 return STATUS_MORE_ENTRIES;
746 /*******************************************************************
747 Get the group entries - similar to get_sampwd_entries().
748 ********************************************************************/
750 static NTSTATUS get_group_domain_entries(TALLOC_CTX *ctx, DOMAIN_GRP **d_grp, DOM_SID *sid, uint32 start_idx,
751 uint32 *p_num_entries, uint32 max_entries)
755 uint32 group_entries = 0;
756 uint32 num_entries = 0;
760 enum_group_mapping(SID_NAME_DOM_GRP, &map, (int *)&group_entries, ENUM_ONLY_MAPPED, MAPPING_WITHOUT_PRIV);
762 num_entries=group_entries-start_idx;
764 /* limit the number of entries */
765 if (num_entries>max_entries) {
766 DEBUG(5,("Limiting to %d entries\n", max_entries));
767 num_entries=max_entries;
770 *d_grp=(DOMAIN_GRP *)talloc_zero(ctx, num_entries*sizeof(DOMAIN_GRP));
771 if (num_entries!=0 && *d_grp==NULL){
773 return NT_STATUS_NO_MEMORY;
776 for (i=0; i<num_entries; i++) {
777 fstrcpy((*d_grp)[i].name, map[i+start_idx].nt_name);
778 fstrcpy((*d_grp)[i].comment, map[i+start_idx].comment);
779 sid_split_rid(&map[i+start_idx].sid, &(*d_grp)[i].rid);
780 (*d_grp)[i].attr=SID_NAME_DOM_GRP;
785 *p_num_entries = num_entries;
790 /*******************************************************************
791 samr_reply_enum_dom_groups
792 Only reply with one group - domain admins. This must be fixed for
794 ********************************************************************/
796 NTSTATUS _samr_enum_dom_groups(pipes_struct *p, SAMR_Q_ENUM_DOM_GROUPS *q_u, SAMR_R_ENUM_DOM_GROUPS *r_u)
798 DOMAIN_GRP *grp=NULL;
802 r_u->status = NT_STATUS_OK;
804 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &sid))
805 return NT_STATUS_INVALID_HANDLE;
807 DEBUG(5,("samr_reply_enum_dom_groups: %d\n", __LINE__));
809 /* the domain group array is being allocated in the function below */
810 get_group_domain_entries(p->mem_ctx, &grp, &sid, q_u->start_idx, &num_entries, MAX_SAM_ENTRIES);
812 make_group_sam_entry_list(p->mem_ctx, &r_u->sam, &r_u->uni_grp_name, num_entries, grp);
814 init_samr_r_enum_dom_groups(r_u, q_u->start_idx, num_entries);
816 DEBUG(5,("samr_enum_dom_groups: %d\n", __LINE__));
822 /*******************************************************************
823 samr_reply_enum_dom_aliases
824 ********************************************************************/
826 NTSTATUS _samr_enum_dom_aliases(pipes_struct *p, SAMR_Q_ENUM_DOM_ALIASES *q_u, SAMR_R_ENUM_DOM_ALIASES *r_u)
828 DOMAIN_GRP *grp=NULL;
829 uint32 num_entries = 0;
834 r_u->status = NT_STATUS_OK;
836 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &sid))
837 return NT_STATUS_INVALID_HANDLE;
839 sid_to_string(sid_str, &sid);
840 DEBUG(5,("samr_reply_enum_dom_aliases: sid %s\n", sid_str));
842 status = get_group_alias_entries(p->mem_ctx, &grp, &sid, q_u->start_idx,
843 &num_entries, MAX_SAM_ENTRIES);
844 if (NT_STATUS_IS_ERR(status)) return status;
846 make_group_sam_entry_list(p->mem_ctx, &r_u->sam, &r_u->uni_grp_name, num_entries, grp);
850 init_samr_r_enum_dom_aliases(r_u, q_u->start_idx + num_entries, num_entries);
852 DEBUG(5,("samr_enum_dom_aliases: %d\n", __LINE__));
857 /*******************************************************************
858 samr_reply_query_dispinfo
859 ********************************************************************/
860 NTSTATUS _samr_query_dispinfo(pipes_struct *p, SAMR_Q_QUERY_DISPINFO *q_u, SAMR_R_QUERY_DISPINFO *r_u)
862 struct samr_info *info = NULL;
863 uint32 struct_size=0x20; /* W2K always reply that, client doesn't care */
866 uint32 max_entries=q_u->max_entries;
867 uint32 enum_context=q_u->start_idx;
868 uint32 max_size=q_u->max_size;
870 SAM_DISPINFO_CTR *ctr;
871 uint32 temp_size=0, total_data_size=0;
873 uint32 num_account = 0;
874 enum remote_arch_types ra_type = get_remote_arch();
877 max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
879 DEBUG(5, ("samr_reply_query_dispinfo: %d\n", __LINE__));
880 r_u->status = NT_STATUS_OK;
882 /* find the policy handle. open a policy on it. */
883 if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)&info))
884 return NT_STATUS_INVALID_HANDLE;
887 * calculate how many entries we will return.
889 * - the number of entries the client asked
890 * - our limit on that
891 * - the starting point (enumeration context)
892 * - the buffer size the client will accept
896 * We are a lot more like W2K. Instead of reading the SAM
897 * each time to find the records we need to send back,
898 * we read it once and link that copy to the sam handle.
899 * For large user list (over the MAX_SAM_ENTRIES)
900 * it's a definitive win.
901 * second point to notice: between enumerations
902 * our sam is now the same as it's a snapshoot.
903 * third point: got rid of the static SAM_USER_21 struct
904 * no more intermediate.
905 * con: it uses much more memory, as a full copy is stored
908 * If you want to change it, think twice and think
909 * of the second point , that's really important.
914 /* Get what we need from the password database */
916 if (q_u->switch_level==2)
917 acb_mask = ACB_WSTRUST;
919 acb_mask = ACB_NORMAL;
921 /* Get what we need from the password database */
922 switch (q_u->switch_level) {
927 r_u->status=load_sampwd_entries(info, acb_mask);
929 if (NT_STATUS_IS_ERR(r_u->status)) {
930 DEBUG(5, ("_samr_query_dispinfo: load_sampwd_entries failed\n"));
933 num_account = info->disp_info.num_user_account;
937 r_u->status = load_group_domain_entries(info, &info->sid);
938 if (NT_STATUS_IS_ERR(r_u->status))
940 num_account = info->disp_info.num_group_account;
943 DEBUG(0,("_samr_query_dispinfo: Unknown info level (%u)\n", (unsigned int)q_u->switch_level ));
944 return NT_STATUS_INVALID_INFO_CLASS;
947 /* first limit the number of entries we will return */
948 if(max_entries > max_sam_entries) {
949 DEBUG(5, ("samr_reply_query_dispinfo: client requested %d entries, limiting to %d\n", max_entries, max_sam_entries));
950 max_entries = max_sam_entries;
953 if (enum_context > num_account) {
954 DEBUG(5, ("samr_reply_query_dispinfo: enumeration handle over total entries\n"));
958 /* verify we won't overflow */
959 if (max_entries > num_account-enum_context) {
960 max_entries = num_account-enum_context;
961 DEBUG(5, ("samr_reply_query_dispinfo: only %d entries to return\n", max_entries));
964 /* calculate the size and limit on the number of entries we will return */
965 temp_size=(enum_context+max_entries)*struct_size;
967 if (temp_size>max_size) {
968 max_entries=max_size/struct_size;
969 DEBUG(5, ("samr_reply_query_dispinfo: buffer size limits to only %d entries\n", max_entries));
972 if (!(ctr = (SAM_DISPINFO_CTR *)talloc_zero(p->mem_ctx,sizeof(SAM_DISPINFO_CTR))))
973 return NT_STATUS_NO_MEMORY;
977 /* Now create reply structure */
978 switch (q_u->switch_level) {
981 if (!(ctr->sam.info1 = (SAM_DISPINFO_1 *)talloc_zero(p->mem_ctx,max_entries*sizeof(SAM_DISPINFO_1))))
982 return NT_STATUS_NO_MEMORY;
984 disp_ret = init_sam_dispinfo_1(p->mem_ctx, ctr->sam.info1, max_entries, enum_context, info->disp_info.disp_user_info);
985 if (NT_STATUS_IS_ERR(disp_ret))
990 if (!(ctr->sam.info2 = (SAM_DISPINFO_2 *)talloc_zero(p->mem_ctx,max_entries*sizeof(SAM_DISPINFO_2))))
991 return NT_STATUS_NO_MEMORY;
993 disp_ret = init_sam_dispinfo_2(p->mem_ctx, ctr->sam.info2, max_entries, enum_context, info->disp_info.disp_user_info);
994 if (NT_STATUS_IS_ERR(disp_ret))
999 if (!(ctr->sam.info3 = (SAM_DISPINFO_3 *)talloc_zero(p->mem_ctx,max_entries*sizeof(SAM_DISPINFO_3))))
1000 return NT_STATUS_NO_MEMORY;
1002 disp_ret = init_sam_dispinfo_3(p->mem_ctx, ctr->sam.info3, max_entries, enum_context, info->disp_info.disp_group_info);
1003 if (NT_STATUS_IS_ERR(disp_ret))
1008 if (!(ctr->sam.info4 = (SAM_DISPINFO_4 *)talloc_zero(p->mem_ctx,max_entries*sizeof(SAM_DISPINFO_4))))
1009 return NT_STATUS_NO_MEMORY;
1011 disp_ret = init_sam_dispinfo_4(p->mem_ctx, ctr->sam.info4, max_entries, enum_context, info->disp_info.disp_user_info);
1012 if (NT_STATUS_IS_ERR(disp_ret))
1017 if (!(ctr->sam.info5 = (SAM_DISPINFO_5 *)talloc_zero(p->mem_ctx,max_entries*sizeof(SAM_DISPINFO_5))))
1018 return NT_STATUS_NO_MEMORY;
1020 disp_ret = init_sam_dispinfo_5(p->mem_ctx, ctr->sam.info5, max_entries, enum_context, info->disp_info.disp_group_info);
1021 if (NT_STATUS_IS_ERR(disp_ret))
1026 ctr->sam.info = NULL;
1027 return NT_STATUS_INVALID_INFO_CLASS;
1030 /* calculate the total size */
1031 total_data_size=num_account*struct_size;
1033 if (enum_context+max_entries < num_account)
1034 r_u->status = STATUS_MORE_ENTRIES;
1036 DEBUG(5, ("_samr_query_dispinfo: %d\n", __LINE__));
1038 init_samr_r_query_dispinfo(r_u, max_entries, total_data_size, temp_size, q_u->switch_level, ctr, r_u->status);
1044 /*******************************************************************
1045 samr_reply_query_aliasinfo
1046 ********************************************************************/
1048 NTSTATUS _samr_query_aliasinfo(pipes_struct *p, SAMR_Q_QUERY_ALIASINFO *q_u, SAMR_R_QUERY_ALIASINFO *r_u)
1050 struct samr_info *info = NULL;
1053 r_u->status = NT_STATUS_OK;
1055 DEBUG(5,("_samr_query_aliasinfo: %d\n", __LINE__));
1057 /* find the policy handle. open a policy on it. */
1058 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1059 return NT_STATUS_INVALID_HANDLE;
1061 if (!sid_check_is_in_our_domain(&info->sid) &&
1062 !sid_check_is_in_builtin(&info->sid))
1063 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1065 if(!get_local_group_from_sid(info->sid, &map, MAPPING_WITHOUT_PRIV))
1066 return NT_STATUS_NO_SUCH_ALIAS;
1068 switch (q_u->switch_level) {
1071 r_u->ctr.switch_value1 = 1;
1072 init_samr_alias_info1(&r_u->ctr.alias.info1, map.nt_name, 1, map.comment);
1076 r_u->ctr.switch_value1 = 3;
1077 init_samr_alias_info3(&r_u->ctr.alias.info3, map.comment);
1080 return NT_STATUS_INVALID_INFO_CLASS;
1083 DEBUG(5,("_samr_query_aliasinfo: %d\n", __LINE__));
1089 /*******************************************************************
1090 samr_reply_lookup_ids
1091 ********************************************************************/
1093 uint32 _samr_lookup_ids(pipes_struct *p, SAMR_Q_LOOKUP_IDS *q_u, SAMR_R_LOOKUP_IDS *r_u)
1095 uint32 rid[MAX_SAM_ENTRIES];
1096 int num_rids = q_u->num_sids1;
1098 r_u->status = NT_STATUS_OK;
1100 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1102 if (num_rids > MAX_SAM_ENTRIES) {
1103 num_rids = MAX_SAM_ENTRIES;
1104 DEBUG(5,("_samr_lookup_ids: truncating entries to %d\n", num_rids));
1109 SMB_ASSERT_ARRAY(q_u->uni_user_name, num_rids);
1111 for (i = 0; i < num_rids && status == 0; i++)
1113 struct sam_passwd *sam_pass;
1117 fstrcpy(user_name, unistrn2(q_u->uni_user_name[i].buffer,
1118 q_u->uni_user_name[i].uni_str_len));
1120 /* find the user account */
1122 sam_pass = get_smb21pwd_entry(user_name, 0);
1125 if (sam_pass == NULL)
1127 status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
1132 rid[i] = sam_pass->user_rid;
1138 rid[0] = BUILTIN_ALIAS_RID_USERS;
1140 init_samr_r_lookup_ids(&r_u, num_rids, rid, NT_STATUS_OK);
1142 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1148 /*******************************************************************
1150 ********************************************************************/
1152 NTSTATUS _samr_lookup_names(pipes_struct *p, SAMR_Q_LOOKUP_NAMES *q_u, SAMR_R_LOOKUP_NAMES *r_u)
1154 uint32 rid[MAX_SAM_ENTRIES];
1156 enum SID_NAME_USE type[MAX_SAM_ENTRIES];
1157 enum SID_NAME_USE local_type;
1159 int num_rids = q_u->num_names2;
1163 r_u->status = NT_STATUS_OK;
1165 DEBUG(5,("_samr_lookup_names: %d\n", __LINE__));
1170 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &pol_sid)) {
1171 init_samr_r_lookup_names(p->mem_ctx, r_u, 0, NULL, NULL, NT_STATUS_OBJECT_TYPE_MISMATCH);
1175 if (num_rids > MAX_SAM_ENTRIES) {
1176 num_rids = MAX_SAM_ENTRIES;
1177 DEBUG(5,("_samr_lookup_names: truncating entries to %d\n", num_rids));
1180 DEBUG(5,("_samr_lookup_names: looking name on SID %s\n", sid_to_string(sid_str, &pol_sid)));
1182 for (i = 0; i < num_rids; i++) {
1186 r_u->status = NT_STATUS_NONE_MAPPED;
1188 rid [i] = 0xffffffff;
1189 type[i] = SID_NAME_UNKNOWN;
1191 rpcstr_pull(name, q_u->uni_name[i].buffer, sizeof(name), q_u->uni_name[i].uni_str_len*2, 0);
1194 * we are only looking for a name
1195 * the SID we get back can be outside
1196 * the scope of the pol_sid
1198 * in clear: it prevents to reply to domain\group: yes
1199 * when only builtin\group exists.
1201 * a cleaner code is to add the sid of the domain we're looking in
1202 * to the local_lookup_name function.
1204 if(local_lookup_name(global_myname, name, &sid, &local_type)) {
1205 sid_split_rid(&sid, &local_rid);
1207 if (sid_equal(&sid, &pol_sid)) {
1210 r_u->status = NT_STATUS_OK;
1215 init_samr_r_lookup_names(p->mem_ctx, r_u, num_rids, rid, (uint32 *)type, r_u->status);
1217 DEBUG(5,("_samr_lookup_names: %d\n", __LINE__));
1222 /*******************************************************************
1223 _samr_chgpasswd_user
1224 ********************************************************************/
1226 NTSTATUS _samr_chgpasswd_user(pipes_struct *p, SAMR_Q_CHGPASSWD_USER *q_u, SAMR_R_CHGPASSWD_USER *r_u)
1231 DEBUG(5,("_samr_chgpasswd_user: %d\n", __LINE__));
1233 r_u->status = NT_STATUS_OK;
1235 rpcstr_pull(user_name, q_u->uni_user_name.buffer, sizeof(user_name), q_u->uni_user_name.uni_str_len*2, 0);
1236 rpcstr_pull(wks, q_u->uni_dest_host.buffer, sizeof(wks), q_u->uni_dest_host.uni_str_len*2,0);
1238 DEBUG(5,("samr_chgpasswd_user: user: %s wks: %s\n", user_name, wks));
1241 * Pass the user through the NT -> unix user mapping
1245 (void)map_username(user_name);
1248 * Do any UNIX username case mangling.
1250 (void)Get_Pwnam_Modify( user_name);
1252 if (!pass_oem_change(user_name, q_u->lm_newpass.pass, q_u->lm_oldhash.hash,
1253 q_u->nt_newpass.pass, q_u->nt_oldhash.hash))
1254 r_u->status = NT_STATUS_WRONG_PASSWORD;
1256 init_samr_r_chgpasswd_user(r_u, r_u->status);
1258 DEBUG(5,("_samr_chgpasswd_user: %d\n", __LINE__));
1263 /*******************************************************************
1264 makes a SAMR_R_LOOKUP_RIDS structure.
1265 ********************************************************************/
1267 static BOOL make_samr_lookup_rids(TALLOC_CTX *ctx, uint32 num_names, fstring names[],
1268 UNIHDR **pp_hdr_name, UNISTR2 **pp_uni_name)
1271 UNIHDR *hdr_name=NULL;
1272 UNISTR2 *uni_name=NULL;
1274 *pp_uni_name = NULL;
1275 *pp_hdr_name = NULL;
1277 if (num_names != 0) {
1278 hdr_name = (UNIHDR *)talloc_zero(ctx, sizeof(UNIHDR)*num_names);
1279 if (hdr_name == NULL)
1282 uni_name = (UNISTR2 *)talloc_zero(ctx,sizeof(UNISTR2)*num_names);
1283 if (uni_name == NULL)
1287 for (i = 0; i < num_names; i++) {
1288 int len = names[i] != NULL ? strlen(names[i]) : 0;
1289 DEBUG(10, ("names[%d]:%s\n", i, names[i]));
1290 init_uni_hdr(&hdr_name[i], len);
1291 init_unistr2(&uni_name[i], names[i], len);
1294 *pp_uni_name = uni_name;
1295 *pp_hdr_name = hdr_name;
1300 /*******************************************************************
1302 ********************************************************************/
1304 NTSTATUS _samr_lookup_rids(pipes_struct *p, SAMR_Q_LOOKUP_RIDS *q_u, SAMR_R_LOOKUP_RIDS *r_u)
1306 fstring group_names[MAX_SAM_ENTRIES];
1307 uint32 *group_attrs = NULL;
1308 UNIHDR *hdr_name = NULL;
1309 UNISTR2 *uni_name = NULL;
1311 int num_rids = q_u->num_rids1;
1314 r_u->status = NT_STATUS_OK;
1316 DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
1318 /* find the policy handle. open a policy on it. */
1319 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &pol_sid))
1320 return NT_STATUS_INVALID_HANDLE;
1322 if (num_rids > MAX_SAM_ENTRIES) {
1323 num_rids = MAX_SAM_ENTRIES;
1324 DEBUG(5,("_samr_lookup_rids: truncating entries to %d\n", num_rids));
1328 if ((group_attrs = (uint32 *)talloc_zero(p->mem_ctx, num_rids * sizeof(uint32))) == NULL)
1329 return NT_STATUS_NO_MEMORY;
1332 r_u->status = NT_STATUS_NONE_MAPPED;
1334 for (i = 0; i < num_rids; i++) {
1338 enum SID_NAME_USE type;
1340 group_attrs[i] = SID_NAME_UNKNOWN;
1341 *group_names[i] = '\0';
1343 if (sid_equal(&pol_sid, &global_sam_sid)) {
1344 sid_copy(&sid, &pol_sid);
1345 sid_append_rid(&sid, q_u->rid[i]);
1347 if (lookup_sid(&sid, domname, tmpname, &type)) {
1348 r_u->status = NT_STATUS_OK;
1349 group_attrs[i] = (uint32)type;
1350 fstrcpy(group_names[i],tmpname);
1351 DEBUG(5,("_samr_lookup_rids: %s:%d\n", group_names[i], group_attrs[i]));
1356 if(!make_samr_lookup_rids(p->mem_ctx, num_rids, group_names, &hdr_name, &uni_name))
1357 return NT_STATUS_NO_MEMORY;
1359 init_samr_r_lookup_rids(r_u, num_rids, hdr_name, uni_name, group_attrs);
1361 DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
1366 /*******************************************************************
1367 _api_samr_open_user. Safe - gives out no passwd info.
1368 ********************************************************************/
1370 NTSTATUS _api_samr_open_user(pipes_struct *p, SAMR_Q_OPEN_USER *q_u, SAMR_R_OPEN_USER *r_u)
1372 SAM_ACCOUNT *sampass=NULL;
1374 POLICY_HND domain_pol = q_u->domain_pol;
1375 uint32 user_rid = q_u->user_rid;
1376 POLICY_HND *user_pol = &r_u->user_pol;
1377 struct samr_info *info = NULL;
1380 r_u->status = NT_STATUS_OK;
1382 /* find the domain policy handle. */
1383 if (!find_policy_by_hnd(p, &domain_pol, NULL))
1384 return NT_STATUS_INVALID_HANDLE;
1386 pdb_init_sam(&sampass);
1389 ret=pdb_getsampwrid(sampass, user_rid);
1392 /* check that the RID exists in our domain. */
1394 pdb_free_sam(&sampass);
1395 return NT_STATUS_NO_SUCH_USER;
1398 pdb_free_sam(&sampass);
1400 /* Get the domain SID stored in the domain policy */
1401 if(!get_lsa_policy_samr_sid(p, &domain_pol, &sid))
1402 return NT_STATUS_INVALID_HANDLE;
1404 /* append the user's RID to it */
1405 if(!sid_append_rid(&sid, user_rid))
1406 return NT_STATUS_NO_SUCH_USER;
1408 /* associate the user's SID with the new handle. */
1409 if ((info = get_samr_info_by_sid(&sid)) == NULL)
1410 return NT_STATUS_NO_MEMORY;
1412 /* get a (unique) handle. open a policy on it. */
1413 if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info))
1414 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1419 /*************************************************************************
1420 get_user_info_10. Safe. Only gives out acb bits.
1421 *************************************************************************/
1423 static BOOL get_user_info_10(SAM_USER_INFO_10 *id10, uint32 user_rid)
1425 SAM_ACCOUNT *smbpass=NULL;
1428 if (!pdb_rid_is_user(user_rid)) {
1429 DEBUG(4,("RID 0x%x is not a user RID\n", user_rid));
1433 pdb_init_sam(&smbpass);
1436 ret = pdb_getsampwrid(smbpass, user_rid);
1440 DEBUG(4,("User 0x%x not found\n", user_rid));
1441 pdb_free_sam(&smbpass);
1445 DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1448 init_sam_user_info10(id10, pdb_get_acct_ctrl(smbpass) );
1450 pdb_free_sam(&smbpass);
1455 /*************************************************************************
1456 get_user_info_12. OK - this is the killer as it gives out password info.
1457 Ensure that this is only allowed on an encrypted connection with a root
1459 *************************************************************************/
1461 static NTSTATUS get_user_info_12(pipes_struct *p, SAM_USER_INFO_12 * id12, uint32 user_rid)
1463 SAM_ACCOUNT *smbpass=NULL;
1466 if (!p->ntlmssp_auth_validated)
1467 return NT_STATUS_ACCESS_DENIED;
1469 if (!(p->ntlmssp_chal_flags & NTLMSSP_NEGOTIATE_SIGN) || !(p->ntlmssp_chal_flags & NTLMSSP_NEGOTIATE_SEAL))
1470 return NT_STATUS_ACCESS_DENIED;
1473 * Do *NOT* do become_root()/unbecome_root() here ! JRA.
1475 pdb_init_sam(&smbpass);
1477 ret = pdb_getsampwrid(smbpass, user_rid);
1480 DEBUG(4, ("User 0x%x not found\n", user_rid));
1481 pdb_free_sam(&smbpass);
1482 return (geteuid() == (uid_t)0) ? NT_STATUS_NO_SUCH_USER : NT_STATUS_ACCESS_DENIED;
1485 DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
1487 if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
1488 pdb_free_sam(&smbpass);
1489 return NT_STATUS_ACCOUNT_DISABLED;
1493 init_sam_user_info12(id12, pdb_get_lanman_passwd(smbpass), pdb_get_nt_passwd(smbpass));
1495 pdb_free_sam(&smbpass);
1497 return NT_STATUS_OK;
1500 /*************************************************************************
1502 *************************************************************************/
1504 static BOOL get_user_info_20(SAM_USER_INFO_20 *id20, uint32 user_rid)
1506 SAM_ACCOUNT *sampass=NULL;
1509 if (!pdb_rid_is_user(user_rid)) {
1510 DEBUG(4,("RID 0x%x is not a user RID\n", user_rid));
1514 pdb_init_sam(&sampass);
1517 ret = pdb_getsampwrid(sampass, user_rid);
1521 DEBUG(4,("User 0x%x not found\n", user_rid));
1522 pdb_free_sam(&sampass);
1526 samr_clear_sam_passwd(sampass);
1528 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
1531 init_sam_user_info20A(id20, sampass);
1533 pdb_free_sam(&sampass);
1538 /*************************************************************************
1540 *************************************************************************/
1542 static BOOL get_user_info_21(SAM_USER_INFO_21 *id21, uint32 user_rid)
1544 SAM_ACCOUNT *sampass=NULL;
1547 if (!pdb_rid_is_user(user_rid)) {
1548 DEBUG(4,("RID 0x%x is not a user RID\n", user_rid));
1552 pdb_init_sam(&sampass);
1555 ret = pdb_getsampwrid(sampass, user_rid);
1559 DEBUG(4,("User 0x%x not found\n", user_rid));
1560 pdb_free_sam(&sampass);
1564 samr_clear_sam_passwd(sampass);
1566 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
1569 init_sam_user_info21A(id21, sampass);
1571 pdb_free_sam(&sampass);
1576 /*******************************************************************
1577 _samr_query_userinfo
1578 ********************************************************************/
1580 NTSTATUS _samr_query_userinfo(pipes_struct *p, SAMR_Q_QUERY_USERINFO *q_u, SAMR_R_QUERY_USERINFO *r_u)
1582 SAM_USERINFO_CTR *ctr;
1584 struct samr_info *info = NULL;
1586 r_u->status=NT_STATUS_OK;
1588 /* search for the handle */
1589 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1590 return NT_STATUS_INVALID_HANDLE;
1592 if (!sid_check_is_in_our_domain(&info->sid))
1593 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1595 sid_peek_rid(&info->sid, &rid);
1597 DEBUG(5,("_samr_query_userinfo: rid:0x%x\n", rid));
1599 ctr = (SAM_USERINFO_CTR *)talloc_zero(p->mem_ctx, sizeof(SAM_USERINFO_CTR));
1601 return NT_STATUS_NO_MEMORY;
1605 /* ok! user info levels (lots: see MSDEV help), off we go... */
1606 ctr->switch_value = q_u->switch_value;
1608 switch (q_u->switch_value) {
1610 ctr->info.id10 = (SAM_USER_INFO_10 *)talloc_zero(p->mem_ctx, sizeof(SAM_USER_INFO_10));
1611 if (ctr->info.id10 == NULL)
1612 return NT_STATUS_NO_MEMORY;
1614 if (!get_user_info_10(ctr->info.id10, rid))
1615 return NT_STATUS_NO_SUCH_USER;
1619 /* whoops - got this wrong. i think. or don't understand what's happening. */
1623 info = (void *)&id11;
1625 expire.low = 0xffffffff;
1626 expire.high = 0x7fffffff;
1628 ctr->info.id = (SAM_USER_INFO_11 *)talloc_zero(p->mem_ctx,
1633 ZERO_STRUCTP(ctr->info.id11);
1634 init_sam_user_info11(ctr->info.id11, &expire,
1635 "BROOKFIELDS$", /* name */
1636 0x03ef, /* user rid */
1637 0x201, /* group rid */
1638 0x0080); /* acb info */
1645 ctr->info.id12 = (SAM_USER_INFO_12 *)talloc_zero(p->mem_ctx, sizeof(SAM_USER_INFO_12));
1646 if (ctr->info.id12 == NULL)
1647 return NT_STATUS_NO_MEMORY;
1649 if (NT_STATUS_IS_ERR(r_u->status = get_user_info_12(p, ctr->info.id12, rid)))
1654 ctr->info.id20 = (SAM_USER_INFO_20 *)talloc_zero(p->mem_ctx,sizeof(SAM_USER_INFO_20));
1655 if (ctr->info.id20 == NULL)
1656 return NT_STATUS_NO_MEMORY;
1657 if (!get_user_info_20(ctr->info.id20, rid))
1658 return NT_STATUS_NO_SUCH_USER;
1662 ctr->info.id21 = (SAM_USER_INFO_21 *)talloc_zero(p->mem_ctx,sizeof(SAM_USER_INFO_21));
1663 if (ctr->info.id21 == NULL)
1664 return NT_STATUS_NO_MEMORY;
1665 if (!get_user_info_21(ctr->info.id21, rid))
1666 return NT_STATUS_NO_SUCH_USER;
1670 return NT_STATUS_INVALID_INFO_CLASS;
1673 init_samr_r_query_userinfo(r_u, ctr, r_u->status);
1675 DEBUG(5,("_samr_query_userinfo: %d\n", __LINE__));
1680 /*******************************************************************
1681 samr_reply_query_usergroups
1682 ********************************************************************/
1684 NTSTATUS _samr_query_usergroups(pipes_struct *p, SAMR_Q_QUERY_USERGROUPS *q_u, SAMR_R_QUERY_USERGROUPS *r_u)
1686 SAM_ACCOUNT *sam_pass=NULL;
1687 DOM_GID *gids = NULL;
1690 struct samr_info *info = NULL;
1694 * from the SID in the request:
1695 * we should send back the list of DOMAIN GROUPS
1696 * the user is a member of
1698 * and only the DOMAIN GROUPS
1699 * no ALIASES !!! neither aliases of the domain
1700 * nor aliases of the builtin SID
1705 r_u->status = NT_STATUS_OK;
1707 DEBUG(5,("_samr_query_usergroups: %d\n", __LINE__));
1709 /* find the policy handle. open a policy on it. */
1710 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1711 return NT_STATUS_INVALID_HANDLE;
1713 if (!sid_check_is_in_our_domain(&info->sid))
1714 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1716 sid_peek_rid(&info->sid, &rid);
1718 pdb_init_sam(&sam_pass);
1721 ret = pdb_getsampwrid(sam_pass, rid);
1725 pdb_free_sam(&sam_pass);
1726 return NT_STATUS_NO_SUCH_USER;
1729 if(!get_domain_user_groups(p->mem_ctx, &num_groups, &gids, sam_pass)) {
1730 pdb_free_sam(&sam_pass);
1731 return NT_STATUS_NO_SUCH_GROUP;
1734 /* construct the response. lkclXXXX: gids are not copied! */
1735 init_samr_r_query_usergroups(r_u, num_groups, gids, r_u->status);
1737 DEBUG(5,("_samr_query_usergroups: %d\n", __LINE__));
1739 pdb_free_sam(&sam_pass);
1744 /*******************************************************************
1745 _samr_query_dom_info
1746 ********************************************************************/
1748 NTSTATUS _samr_query_dom_info(pipes_struct *p, SAMR_Q_QUERY_DOMAIN_INFO *q_u, SAMR_R_QUERY_DOMAIN_INFO *r_u)
1750 struct samr_info *info = NULL;
1752 uint32 min_pass_len,pass_hist,flag;
1753 time_t u_expire, u_min_age;
1754 NTTIME nt_expire, nt_min_age;
1756 time_t u_lock_duration, u_reset_time;
1757 NTTIME nt_lock_duration, nt_reset_time;
1763 uint32 num_users=0, num_groups=0, num_aliases=0;
1765 if ((ctr = (SAM_UNK_CTR *)talloc_zero(p->mem_ctx, sizeof(SAM_UNK_CTR))) == NULL)
1766 return NT_STATUS_NO_MEMORY;
1770 r_u->status = NT_STATUS_OK;
1772 DEBUG(5,("_samr_query_dom_info: %d\n", __LINE__));
1774 /* find the policy handle. open a policy on it. */
1775 if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)&info))
1776 return NT_STATUS_INVALID_HANDLE;
1778 switch (q_u->switch_value) {
1780 account_policy_get(AP_MIN_PASSWORD_LEN, &min_pass_len);
1781 account_policy_get(AP_PASSWORD_HISTORY, &pass_hist);
1782 account_policy_get(AP_USER_MUST_LOGON_TO_CHG_PASS, &flag);
1783 account_policy_get(AP_MAX_PASSWORD_AGE, (int *)&u_expire);
1784 account_policy_get(AP_MIN_PASSWORD_AGE, (int *)&u_min_age);
1786 unix_to_nt_time_abs(&nt_expire, u_expire);
1787 unix_to_nt_time_abs(&nt_min_age, u_min_age);
1789 init_unk_info1(&ctr->info.inf1, (uint16)min_pass_len, (uint16)pass_hist,
1790 flag, nt_expire, nt_min_age);
1794 r_u->status=load_sampwd_entries(info, ACB_NORMAL);
1796 if (NT_STATUS_IS_ERR(r_u->status)) {
1797 DEBUG(5, ("_samr_query_dispinfo: load_sampwd_entries failed\n"));
1800 num_users=info->disp_info.num_user_account;
1803 r_u->status=load_group_domain_entries(info, &global_sam_sid);
1804 if (NT_STATUS_IS_ERR(r_u->status)) {
1805 DEBUG(5, ("_samr_query_dispinfo: load_group_domain_entries failed\n"));
1808 num_groups=info->disp_info.num_group_account;
1811 /* The time call below is to get a sequence number for the sam. FIXME !!! JRA. */
1812 init_unk_info2(&ctr->info.inf2, global_myworkgroup, global_myname, (uint32) time(NULL),
1813 num_users, num_groups, num_aliases);
1816 account_policy_get(AP_TIME_TO_LOGOUT, (int *)&u_logout);
1817 unix_to_nt_time_abs(&nt_logout, u_logout);
1819 init_unk_info3(&ctr->info.inf3, nt_logout);
1822 init_unk_info5(&ctr->info.inf5, global_myname);
1825 init_unk_info6(&ctr->info.inf6);
1828 init_unk_info7(&ctr->info.inf7);
1831 account_policy_get(AP_LOCK_ACCOUNT_DURATION, (int *)&u_lock_duration);
1832 account_policy_get(AP_RESET_COUNT_TIME, (int *)&u_reset_time);
1833 account_policy_get(AP_BAD_ATTEMPT_LOCKOUT, &lockout);
1835 unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
1836 unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
1838 init_unk_info12(&ctr->info.inf12, nt_lock_duration, nt_reset_time, (uint16)lockout);
1841 return NT_STATUS_INVALID_INFO_CLASS;
1844 init_samr_r_query_dom_info(r_u, q_u->switch_value, ctr, NT_STATUS_OK);
1846 DEBUG(5,("_samr_query_dom_info: %d\n", __LINE__));
1851 /*******************************************************************
1852 _api_samr_create_user
1853 Create an account, can be either a normal user or a machine.
1854 This funcion will need to be updated for bdc/domain trusts.
1855 ********************************************************************/
1857 NTSTATUS _api_samr_create_user(pipes_struct *p, SAMR_Q_CREATE_USER *q_u, SAMR_R_CREATE_USER *r_u)
1859 SAM_ACCOUNT *sam_pass=NULL;
1866 POLICY_HND dom_pol = q_u->domain_pol;
1867 UNISTR2 user_account = q_u->uni_name;
1868 uint16 acb_info = q_u->acb_info;
1869 POLICY_HND *user_pol = &r_u->user_pol;
1870 struct samr_info *info = NULL;
1873 /* find the policy handle. open a policy on it. */
1874 if (!find_policy_by_hnd(p, &dom_pol, NULL))
1875 return NT_STATUS_INVALID_HANDLE;
1877 /* find the account: tell the caller if it exists.
1878 lkclXXXX i have *no* idea if this is a problem or not
1879 or even if you are supposed to construct a different
1880 reply if the account already exists...
1883 rpcstr_pull(account, user_account.buffer, sizeof(account), user_account.uni_str_len*2, 0);
1886 pdb_init_sam(&sam_pass);
1889 ret = pdb_getsampwnam(sam_pass, account);
1892 /* this account exists: say so */
1893 pdb_free_sam(&sam_pass);
1894 return NT_STATUS_USER_EXISTS;
1897 local_flags=LOCAL_ADD_USER|LOCAL_DISABLE_USER|LOCAL_SET_NO_PASSWORD;
1898 local_flags|= (acb_info & ACB_WSTRUST) ? LOCAL_TRUST_ACCOUNT:0;
1901 * NB. VERY IMPORTANT ! This call must be done as the current pipe user,
1902 * *NOT* surrounded by a become_root()/unbecome_root() call. This ensures
1903 * that only people with write access to the smbpasswd file will be able
1904 * to create a user. JRA.
1908 * add the user in the /etc/passwd file or the unix authority system.
1909 * We don't check if the smb_create_user() function succed or not for 2 reasons:
1910 * a) local_password_change() checks for us if the /etc/passwd account really exists
1911 * b) smb_create_user() would return an error if the account already exists
1912 * and as it could return an error also if it can't create the account, it would be tricky.
1914 * So we go the easy way, only check after if the account exists.
1915 * JFM (2/3/2001), to clear any possible bad understanding (-:
1917 * We now have seperate script paramaters for adding users/machines so we
1918 * now have some sainity-checking to match.
1921 DEBUG(10,("checking account %s at pos %d for $ termination\n",account, strlen(account)-1));
1923 if ((acb_info & ACB_WSTRUST) && (account[strlen(account)-1] == '$')) {
1924 pstrcpy(add_script, lp_addmachine_script());
1925 } else if ((!(acb_info & ACB_WSTRUST)) && (account[strlen(account)-1] != '$')) {
1926 pstrcpy(add_script, lp_adduser_script());
1928 DEBUG(0, ("_api_samr_create_user: mismatch between trust flags and $ termination\n"));
1929 pdb_free_sam(&sam_pass);
1930 return NT_STATUS_UNSUCCESSFUL;
1935 * we can't check both the ending $ and the acb_info.
1937 * UserManager creates trust accounts (ending in $,
1938 * normal that hidden accounts) with the acb_info equals to ACB_NORMAL.
1941 if (account[strlen(account)-1] == '$')
1942 pstrcpy(add_script, lp_addmachine_script());
1944 pstrcpy(add_script, lp_adduser_script());
1948 all_string_sub(add_script, "%u", account, sizeof(account));
1949 add_ret = smbrun(add_script,NULL);
1950 DEBUG(3,("_api_samr_create_user: Running the command `%s' gave %d\n",add_script,add_ret));
1953 /* add the user in the smbpasswd file or the Samba authority database */
1954 if (!local_password_change(account, local_flags, NULL, err_str,
1955 sizeof(err_str), msg_str, sizeof(msg_str))) {
1956 DEBUG(0, ("%s\n", err_str));
1957 pdb_free_sam(&sam_pass);
1958 return NT_STATUS_ACCESS_DENIED;
1962 ret = pdb_getsampwnam(sam_pass, account);
1965 /* account doesn't exist: say so */
1966 pdb_free_sam(&sam_pass);
1967 return NT_STATUS_ACCESS_DENIED;
1970 /* Get the domain SID stored in the domain policy */
1971 if(!get_lsa_policy_samr_sid(p, &dom_pol, &sid)) {
1972 pdb_free_sam(&sam_pass);
1973 return NT_STATUS_INVALID_HANDLE;
1976 /* append the user's RID to it */
1977 if(!sid_append_rid(&sid, pdb_get_user_rid(sam_pass) )) {
1978 pdb_free_sam(&sam_pass);
1979 return NT_STATUS_NO_SUCH_USER;
1982 /* associate the user's SID with the new handle. */
1983 if ((info = get_samr_info_by_sid(&sid)) == NULL) {
1984 pdb_free_sam(&sam_pass);
1985 return NT_STATUS_NO_MEMORY;
1991 /* get a (unique) handle. open a policy on it. */
1992 if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info)) {
1993 pdb_free_sam(&sam_pass);
1994 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1997 r_u->user_rid=pdb_get_user_rid(sam_pass);
1998 r_u->unknown_0 = 0x000703ff;
2000 pdb_free_sam(&sam_pass);
2002 return NT_STATUS_OK;
2005 /*******************************************************************
2006 samr_reply_connect_anon
2007 ********************************************************************/
2009 NTSTATUS _samr_connect_anon(pipes_struct *p, SAMR_Q_CONNECT_ANON *q_u, SAMR_R_CONNECT_ANON *r_u)
2011 struct samr_info *info = NULL;
2013 /* set up the SAMR connect_anon response */
2015 r_u->status = NT_STATUS_OK;
2017 /* associate the user's SID with the new handle. */
2018 if ((info = get_samr_info_by_sid(NULL)) == NULL)
2019 return NT_STATUS_NO_MEMORY;
2021 info->status = q_u->unknown_0;
2023 /* get a (unique) handle. open a policy on it. */
2024 if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2025 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2030 /*******************************************************************
2032 ********************************************************************/
2034 NTSTATUS _samr_connect(pipes_struct *p, SAMR_Q_CONNECT *q_u, SAMR_R_CONNECT *r_u)
2036 struct samr_info *info = NULL;
2038 DEBUG(5,("_samr_connect: %d\n", __LINE__));
2040 r_u->status = NT_STATUS_OK;
2042 /* associate the user's SID with the new handle. */
2043 if ((info = get_samr_info_by_sid(NULL)) == NULL)
2044 return NT_STATUS_NO_MEMORY;
2046 info->status = q_u->access_mask;
2048 /* get a (unique) handle. open a policy on it. */
2049 if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2050 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2052 DEBUG(5,("_samr_connect: %d\n", __LINE__));
2057 /**********************************************************************
2058 api_samr_lookup_domain
2059 **********************************************************************/
2061 NTSTATUS _samr_lookup_domain(pipes_struct *p, SAMR_Q_LOOKUP_DOMAIN *q_u, SAMR_R_LOOKUP_DOMAIN *r_u)
2063 r_u->status = NT_STATUS_OK;
2065 if (!find_policy_by_hnd(p, &q_u->connect_pol, NULL))
2066 return NT_STATUS_INVALID_HANDLE;
2068 /* assume the domain name sent is our global_myname and
2069 send global_sam_sid */
2070 init_samr_r_lookup_domain(r_u, &global_sam_sid, r_u->status);
2075 /******************************************************************
2076 makes a SAMR_R_ENUM_DOMAINS structure.
2077 ********************************************************************/
2079 static BOOL make_enum_domains(TALLOC_CTX *ctx, SAM_ENTRY **pp_sam,
2080 UNISTR2 **pp_uni_name, uint32 num_sam_entries, fstring doms[])
2086 DEBUG(5, ("make_enum_domains\n"));
2089 *pp_uni_name = NULL;
2091 if (num_sam_entries == 0)
2094 sam = (SAM_ENTRY *)talloc_zero(ctx, sizeof(SAM_ENTRY)*num_sam_entries);
2095 uni_name = (UNISTR2 *)talloc_zero(ctx, sizeof(UNISTR2)*num_sam_entries);
2097 if (sam == NULL || uni_name == NULL)
2100 for (i = 0; i < num_sam_entries; i++) {
2101 int len = doms[i] != NULL ? strlen(doms[i]) : 0;
2103 init_sam_entry(&sam[i], len, 0);
2104 init_unistr2(&uni_name[i], doms[i], len);
2108 *pp_uni_name = uni_name;
2113 /**********************************************************************
2114 api_samr_enum_domains
2115 **********************************************************************/
2117 NTSTATUS _samr_enum_domains(pipes_struct *p, SAMR_Q_ENUM_DOMAINS *q_u, SAMR_R_ENUM_DOMAINS *r_u)
2119 uint32 num_entries = 2;
2122 r_u->status = NT_STATUS_OK;
2124 fstrcpy(dom[0],global_myworkgroup);
2125 fstrcpy(dom[1],"Builtin");
2127 if (!make_enum_domains(p->mem_ctx, &r_u->sam, &r_u->uni_dom_name, num_entries, dom))
2128 return NT_STATUS_NO_MEMORY;
2130 init_samr_r_enum_domains(r_u, q_u->start_idx + num_entries, num_entries);
2135 /*******************************************************************
2137 ********************************************************************/
2139 NTSTATUS _api_samr_open_alias(pipes_struct *p, SAMR_Q_OPEN_ALIAS *q_u, SAMR_R_OPEN_ALIAS *r_u)
2142 POLICY_HND domain_pol = q_u->dom_pol;
2143 uint32 alias_rid = q_u->rid_alias;
2144 POLICY_HND *alias_pol = &r_u->pol;
2145 struct samr_info *info = NULL;
2147 r_u->status = NT_STATUS_OK;
2149 /* get the domain policy. */
2150 if (!find_policy_by_hnd(p, &domain_pol, NULL))
2151 return NT_STATUS_INVALID_HANDLE;
2153 /* Get the domain SID stored in the domain policy */
2154 if(!get_lsa_policy_samr_sid(p, &domain_pol, &sid))
2155 return NT_STATUS_INVALID_HANDLE;
2157 /* append the alias' RID to it */
2158 if(!sid_append_rid(&sid, alias_rid))
2159 return NT_STATUS_NO_SUCH_USER;
2162 * we should check if the rid really exist !!!
2166 /* associate the user's SID with the new handle. */
2167 if ((info = get_samr_info_by_sid(&sid)) == NULL)
2168 return NT_STATUS_NO_MEMORY;
2170 /* get a (unique) handle. open a policy on it. */
2171 if (!create_policy_hnd(p, alias_pol, free_samr_info, (void *)info))
2172 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2177 /*******************************************************************
2179 ********************************************************************/
2181 static BOOL set_user_info_10(const SAM_USER_INFO_10 *id10, uint32 rid)
2183 SAM_ACCOUNT *pwd =NULL;
2188 ret = pdb_getsampwrid(pwd, rid);
2196 DEBUG(5, ("set_user_info_10: NULL id10\n"));
2201 if (!pdb_set_acct_ctrl(pwd, id10->acb_info)) {
2206 if(!pdb_update_sam_account(pwd, True)) {
2216 /*******************************************************************
2218 ********************************************************************/
2220 static BOOL set_user_info_12(SAM_USER_INFO_12 *id12, uint32 rid)
2222 SAM_ACCOUNT *pwd = NULL;
2226 if(!pdb_getsampwrid(pwd, rid)) {
2232 DEBUG(2, ("set_user_info_12: id12 is NULL\n"));
2237 if (!pdb_set_lanman_passwd (pwd, id12->lm_pwd)) {
2241 if (!pdb_set_nt_passwd (pwd, id12->nt_pwd)) {
2245 if (!pdb_set_pass_changed_now (pwd)) {
2250 if(!pdb_update_sam_account(pwd, True)) {
2259 /*******************************************************************
2261 ********************************************************************/
2263 static BOOL set_user_info_21(SAM_USER_INFO_21 *id21, uint32 rid)
2265 SAM_ACCOUNT *pwd = NULL;
2268 DEBUG(5, ("set_user_info_21: NULL id21\n"));
2274 if (!pdb_getsampwrid(pwd, rid)) {
2279 copy_id21_to_sam_passwd(pwd, id21);
2282 * The funny part about the previous two calls is
2283 * that pwd still has the password hashes from the
2284 * passdb entry. These have not been updated from
2285 * id21. I don't know if they need to be set. --jerry
2288 /* write the change out */
2289 if(!pdb_update_sam_account(pwd, True)) {
2299 /*******************************************************************
2301 ********************************************************************/
2303 static BOOL set_user_info_23(SAM_USER_INFO_23 *id23, uint32 rid)
2305 SAM_ACCOUNT *pwd = NULL;
2306 pstring plaintext_buf;
2311 DEBUG(5, ("set_user_info_23: NULL id23\n"));
2317 if (!pdb_getsampwrid(pwd, rid)) {
2322 acct_ctrl = pdb_get_acct_ctrl(pwd);
2324 copy_id23_to_sam_passwd(pwd, id23);
2326 if (!decode_pw_buffer((char*)id23->pass, plaintext_buf, 256, &len)) {
2331 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
2336 /* if it's a trust account, don't update /etc/passwd */
2337 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
2338 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
2339 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
2340 DEBUG(5, ("Changing trust account password, not updating /etc/passwd\n"));
2342 /* update the UNIX password */
2343 if (lp_unix_password_sync() )
2344 if(!chgpasswd(pdb_get_username(pwd), "", plaintext_buf, True)) {
2350 ZERO_STRUCT(plaintext_buf);
2352 if(!pdb_update_sam_account(pwd, True)) {
2362 /*******************************************************************
2364 ********************************************************************/
2366 static BOOL set_user_info_pw(char *pass, uint32 rid)
2368 SAM_ACCOUNT *pwd = NULL;
2370 pstring plaintext_buf;
2375 if (!pdb_getsampwrid(pwd, rid)) {
2380 acct_ctrl = pdb_get_acct_ctrl(pwd);
2382 ZERO_STRUCT(plaintext_buf);
2384 if (!decode_pw_buffer(pass, plaintext_buf, 256, &len)) {
2389 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
2394 /* if it's a trust account, don't update /etc/passwd */
2395 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
2396 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
2397 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
2398 DEBUG(5, ("Changing trust account password, not updating /etc/passwd\n"));
2400 /* update the UNIX password */
2401 if (lp_unix_password_sync())
2402 if(!chgpasswd(pdb_get_username(pwd), "", plaintext_buf, True)) {
2408 ZERO_STRUCT(plaintext_buf);
2410 DEBUG(5,("set_user_info_pw: pdb_update_sam_account()\n"));
2412 /* update the SAMBA password */
2413 if(!pdb_update_sam_account(pwd, True)) {
2423 /*******************************************************************
2424 samr_reply_set_userinfo
2425 ********************************************************************/
2427 NTSTATUS _samr_set_userinfo(pipes_struct *p, SAMR_Q_SET_USERINFO *q_u, SAMR_R_SET_USERINFO *r_u)
2431 struct current_user user;
2432 SAM_ACCOUNT *sam_pass=NULL;
2433 unsigned char sess_key[16];
2434 POLICY_HND *pol = &q_u->pol;
2435 uint16 switch_value = q_u->switch_value;
2436 SAM_USERINFO_CTR *ctr = q_u->ctr;
2439 DEBUG(5, ("_samr_set_userinfo: %d\n", __LINE__));
2441 r_u->status = NT_STATUS_OK;
2443 if (p->ntlmssp_auth_validated) {
2444 memcpy(&user, &p->pipe_user, sizeof(user));
2446 extern struct current_user current_user;
2447 memcpy(&user, ¤t_user, sizeof(user));
2450 /* find the policy handle. open a policy on it. */
2451 if (!get_lsa_policy_samr_sid(p, pol, &sid))
2452 return NT_STATUS_INVALID_HANDLE;
2454 sid_split_rid(&sid, &rid);
2456 DEBUG(5, ("_samr_set_userinfo: rid:0x%x, level:%d\n", rid, switch_value));
2459 DEBUG(5, ("_samr_set_userinfo: NULL info level\n"));
2460 return NT_STATUS_INVALID_INFO_CLASS;
2464 pdb_init_sam(&sam_pass);
2467 * We need the NT hash of the user who is changing the user's password.
2468 * This NT hash is used to generate a "user session key"
2469 * This "user session key" is in turn used to encrypt/decrypt the user's password.
2473 ret = pdb_getsampwuid(sam_pass, user.uid);
2476 DEBUG(0,("_samr_set_userinfo: Unable to get smbpasswd entry for uid %u\n", (unsigned int)user.uid ));
2477 pdb_free_sam(&sam_pass);
2478 return NT_STATUS_ACCESS_DENIED;
2481 memset(sess_key, '\0', 16);
2482 mdfour(sess_key, pdb_get_nt_passwd(sam_pass), 16);
2484 pdb_free_sam(&sam_pass);
2486 /* ok! user info levels (lots: see MSDEV help), off we go... */
2487 switch (switch_value) {
2489 if (!set_user_info_12(ctr->info.id12, rid))
2490 return NT_STATUS_ACCESS_DENIED;
2494 SamOEMhash(ctr->info.id24->pass, sess_key, 516);
2496 dump_data(100, (char *)ctr->info.id24->pass, 516);
2498 if (!set_user_info_pw((char *)ctr->info.id24->pass, rid))
2499 return NT_STATUS_ACCESS_DENIED;
2505 * Currently we don't really know how to unmarshall
2506 * the level 25 struct, and the password encryption
2507 * is different. This is a placeholder for when we
2508 * do understand it. In the meantime just return INVALID
2509 * info level and W2K SP2 drops down to level 23... JRA.
2512 SamOEMhash(ctr->info.id25->pass, sess_key, 532);
2514 dump_data(100, (char *)ctr->info.id25->pass, 532);
2516 if (!set_user_info_pw(ctr->info.id25->pass, rid))
2517 return NT_STATUS_ACCESS_DENIED;
2520 return NT_STATUS_INVALID_INFO_CLASS;
2523 SamOEMhash(ctr->info.id23->pass, sess_key, 516);
2525 dump_data(100, (char *)ctr->info.id23->pass, 516);
2527 if (!set_user_info_23(ctr->info.id23, rid))
2528 return NT_STATUS_ACCESS_DENIED;
2532 return NT_STATUS_INVALID_INFO_CLASS;
2538 /*******************************************************************
2539 samr_reply_set_userinfo2
2540 ********************************************************************/
2542 NTSTATUS _samr_set_userinfo2(pipes_struct *p, SAMR_Q_SET_USERINFO2 *q_u, SAMR_R_SET_USERINFO2 *r_u)
2546 SAM_USERINFO_CTR *ctr = q_u->ctr;
2547 POLICY_HND *pol = &q_u->pol;
2548 uint16 switch_value = q_u->switch_value;
2550 DEBUG(5, ("samr_reply_set_userinfo2: %d\n", __LINE__));
2552 r_u->status = NT_STATUS_OK;
2554 /* find the policy handle. open a policy on it. */
2555 if (!get_lsa_policy_samr_sid(p, pol, &sid))
2556 return NT_STATUS_INVALID_HANDLE;
2558 sid_split_rid(&sid, &rid);
2560 DEBUG(5, ("samr_reply_set_userinfo2: rid:0x%x\n", rid));
2563 DEBUG(5, ("samr_reply_set_userinfo2: NULL info level\n"));
2564 return NT_STATUS_INVALID_INFO_CLASS;
2567 switch_value=ctr->switch_value;
2569 /* ok! user info levels (lots: see MSDEV help), off we go... */
2570 switch (switch_value) {
2572 if (!set_user_info_21(ctr->info.id21, rid))
2573 return NT_STATUS_ACCESS_DENIED;
2576 if (!set_user_info_10(ctr->info.id10, rid))
2577 return NT_STATUS_ACCESS_DENIED;
2580 /* Used by AS/U JRA. */
2581 if (!set_user_info_12(ctr->info.id12, rid))
2582 return NT_STATUS_ACCESS_DENIED;
2585 return NT_STATUS_INVALID_INFO_CLASS;
2591 /*********************************************************************
2592 _samr_query_aliasmem
2593 *********************************************************************/
2595 NTSTATUS _samr_query_useraliases(pipes_struct *p, SAMR_Q_QUERY_USERALIASES *q_u, SAMR_R_QUERY_USERALIASES *r_u)
2597 int num_groups = 0, tmp_num_groups=0;
2598 uint32 *rids=NULL, *new_rids=NULL, *tmp_rids=NULL;
2599 struct samr_info *info = NULL;
2601 /* until i see a real useraliases query, we fack one up */
2603 /* I have seen one, JFM 2/12/2001 */
2605 * Explanation of what this call does:
2606 * for all the SID given in the request:
2607 * return a list of alias (local groups)
2608 * that have those SID as members.
2610 * and that's the alias in the domain specified
2611 * in the policy_handle
2613 * if the policy handle is on an incorrect sid
2614 * for example a user's sid
2615 * we should reply NT_STATUS_OBJECT_TYPE_MISMATCH
2618 r_u->status = NT_STATUS_OK;
2620 DEBUG(5,("_samr_query_useraliases: %d\n", __LINE__));
2622 /* find the policy handle. open a policy on it. */
2623 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
2624 return NT_STATUS_INVALID_HANDLE;
2626 if (!sid_check_is_domain(&info->sid) &&
2627 !sid_check_is_builtin(&info->sid))
2628 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2631 for (i=0; i<q_u->num_sids1; i++) {
2633 r_u->status=get_alias_user_groups(p->mem_ctx, &info->sid, &tmp_num_groups, &tmp_rids, &(q_u->sid[i].sid));
2636 * if there is an error, we just continue as
2637 * it can be an unfound user or group
2639 if (NT_STATUS_IS_ERR(r_u->status)) {
2640 DEBUG(10,("_samr_query_useraliases: an error occured while getting groups\n"));
2644 if (tmp_num_groups==0) {
2645 DEBUG(10,("_samr_query_useraliases: no groups found\n"));
2649 new_rids=(uint32 *)talloc_realloc(p->mem_ctx, rids, (num_groups+tmp_num_groups)*sizeof(uint32));
2650 if (new_rids==NULL) {
2651 DEBUG(0,("_samr_query_useraliases: could not realloc memory\n"));
2652 return NT_STATUS_NO_MEMORY;
2656 for (j=0; j<tmp_num_groups; j++)
2657 rids[j+num_groups]=tmp_rids[j];
2659 safe_free(tmp_rids);
2661 num_groups+=tmp_num_groups;
2664 init_samr_r_query_useraliases(r_u, num_groups, rids, NT_STATUS_OK);
2665 return NT_STATUS_OK;
2668 /*********************************************************************
2669 _samr_query_aliasmem
2670 *********************************************************************/
2672 NTSTATUS _samr_query_aliasmem(pipes_struct *p, SAMR_Q_QUERY_ALIASMEM *q_u, SAMR_R_QUERY_ALIASMEM *r_u)
2684 fstring alias_sid_str;
2688 /* find the policy handle. open a policy on it. */
2689 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid))
2690 return NT_STATUS_INVALID_HANDLE;
2692 sid_copy(&als_sid, &alias_sid);
2693 sid_to_string(alias_sid_str, &alias_sid);
2694 sid_split_rid(&alias_sid, &alias_rid);
2696 DEBUG(10, ("sid is %s\n", alias_sid_str));
2698 if (sid_equal(&alias_sid, &global_sid_Builtin)) {
2699 DEBUG(10, ("lookup on Builtin SID (S-1-5-32)\n"));
2700 if(!get_local_group_from_sid(als_sid, &map, MAPPING_WITHOUT_PRIV))
2701 return NT_STATUS_NO_SUCH_ALIAS;
2703 if (sid_equal(&alias_sid, &global_sam_sid)) {
2704 DEBUG(10, ("lookup on Server SID\n"));
2705 if(!get_local_group_from_sid(als_sid, &map, MAPPING_WITHOUT_PRIV))
2706 return NT_STATUS_NO_SUCH_ALIAS;
2710 if(!get_uid_list_of_group(map.gid, &uid, &num_uids))
2711 return NT_STATUS_NO_SUCH_ALIAS;
2713 DEBUG(10, ("sid is %s\n", alias_sid_str));
2714 sid = (DOM_SID2 *)talloc_zero(p->mem_ctx, sizeof(DOM_SID2) * num_uids);
2715 if (num_uids!=0 && sid == NULL)
2716 return NT_STATUS_NO_MEMORY;
2718 for (i = 0; i < num_uids; i++) {
2719 sid_copy(&temp_sid, &global_sam_sid);
2720 sid_append_rid(&temp_sid, pdb_uid_to_user_rid(uid[i]));
2722 init_dom_sid2(&sid[i], &temp_sid);
2725 DEBUG(10, ("sid is %s\n", alias_sid_str));
2726 init_samr_r_query_aliasmem(r_u, num_uids, sid, NT_STATUS_OK);
2728 return NT_STATUS_OK;
2731 /*********************************************************************
2732 _samr_query_groupmem
2733 *********************************************************************/
2735 NTSTATUS _samr_query_groupmem(pipes_struct *p, SAMR_Q_QUERY_GROUPMEM *q_u, SAMR_R_QUERY_GROUPMEM *r_u)
2741 fstring group_sid_str;
2750 /* find the policy handle. open a policy on it. */
2751 if (!get_lsa_policy_samr_sid(p, &q_u->group_pol, &group_sid))
2752 return NT_STATUS_INVALID_HANDLE;
2754 /* todo: change to use sid_compare_front */
2756 sid_split_rid(&group_sid, &group_rid);
2757 sid_to_string(group_sid_str, &group_sid);
2758 DEBUG(10, ("sid is %s\n", group_sid_str));
2760 /* can we get a query for an SID outside our domain ? */
2761 if (!sid_equal(&group_sid, &global_sam_sid))
2762 return NT_STATUS_NO_SUCH_GROUP;
2764 sid_append_rid(&group_sid, group_rid);
2765 DEBUG(10, ("lookup on Domain SID\n"));
2767 if(!get_domain_group_from_sid(group_sid, &map, MAPPING_WITHOUT_PRIV))
2768 return NT_STATUS_NO_SUCH_GROUP;
2770 if(!get_uid_list_of_group(map.gid, &uid, &num_uids))
2771 return NT_STATUS_NO_SUCH_GROUP;
2773 rid=talloc_zero(p->mem_ctx, sizeof(uint32)*num_uids);
2774 attr=talloc_zero(p->mem_ctx, sizeof(uint32)*num_uids);
2776 if (num_uids!=0 && (rid==NULL || attr==NULL))
2777 return NT_STATUS_NO_MEMORY;
2779 for (i=0; i<num_uids; i++) {
2780 rid[i]=pdb_uid_to_user_rid(uid[i]);
2781 attr[i] = SID_NAME_USER;
2784 init_samr_r_query_groupmem(r_u, num_uids, rid, attr, NT_STATUS_OK);
2786 return NT_STATUS_OK;
2789 /*********************************************************************
2791 *********************************************************************/
2793 NTSTATUS _samr_add_aliasmem(pipes_struct *p, SAMR_Q_ADD_ALIASMEM *q_u, SAMR_R_ADD_ALIASMEM *r_u)
2796 fstring alias_sid_str;
2804 /* Find the policy handle. Open a policy on it. */
2805 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid))
2806 return NT_STATUS_INVALID_HANDLE;
2808 sid_to_string(alias_sid_str, &alias_sid);
2809 DEBUG(10, ("sid is %s\n", alias_sid_str));
2811 if (sid_compare(&alias_sid, &global_sam_sid)>0) {
2812 DEBUG(10, ("adding member on Server SID\n"));
2813 if(!get_local_group_from_sid(alias_sid, &map, MAPPING_WITHOUT_PRIV))
2814 return NT_STATUS_NO_SUCH_ALIAS;
2817 if (sid_compare(&alias_sid, &global_sid_Builtin)>0) {
2818 DEBUG(10, ("adding member on BUILTIN SID\n"));
2819 if( !get_local_group_from_sid(alias_sid, &map, MAPPING_WITHOUT_PRIV))
2820 return NT_STATUS_NO_SUCH_ALIAS;
2823 return NT_STATUS_NO_SUCH_ALIAS;
2826 sid_split_rid(&q_u->sid.sid, &rid);
2827 uid=pdb_user_rid_to_uid(rid);
2829 if ((pwd=getpwuid(uid)) == NULL)
2830 return NT_STATUS_NO_SUCH_USER;
2832 if ((grp=getgrgid(map.gid)) == NULL)
2833 return NT_STATUS_NO_SUCH_ALIAS;
2835 /* we need to copy the name otherwise it's overloaded in user_in_group_list */
2836 fstrcpy(grp_name, grp->gr_name);
2838 /* if the user is already in the group */
2839 if(user_in_group_list(pwd->pw_name, grp_name))
2840 return NT_STATUS_MEMBER_IN_ALIAS;
2843 * ok, the group exist, the user exist, the user is not in the group,
2844 * we can (finally) add it to the group !
2846 smb_add_user_group(grp_name, pwd->pw_name);
2848 /* check if the user has been added then ... */
2849 if(!user_in_group_list(pwd->pw_name, grp_name))
2850 return NT_STATUS_MEMBER_NOT_IN_ALIAS; /* don't know what to reply else */
2852 return NT_STATUS_OK;
2855 /*********************************************************************
2857 *********************************************************************/
2859 NTSTATUS _samr_del_aliasmem(pipes_struct *p, SAMR_Q_DEL_ALIASMEM *q_u, SAMR_R_DEL_ALIASMEM *r_u)
2862 fstring alias_sid_str;
2867 SAM_ACCOUNT *sam_pass=NULL;
2869 /* Find the policy handle. Open a policy on it. */
2870 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid))
2871 return NT_STATUS_INVALID_HANDLE;
2873 sid_to_string(alias_sid_str, &alias_sid);
2874 DEBUG(10, ("_samr_del_aliasmem:sid is %s\n", alias_sid_str));
2876 if (!sid_check_is_in_our_domain(&alias_sid) &&
2877 !sid_check_is_in_builtin(&alias_sid)) {
2878 DEBUG(10, ("_samr_del_aliasmem:invalid alias group\n"));
2879 return NT_STATUS_NO_SUCH_ALIAS;
2882 if( !get_local_group_from_sid(alias_sid, &map, MAPPING_WITHOUT_PRIV))
2883 return NT_STATUS_NO_SUCH_ALIAS;
2885 if ((grp=getgrgid(map.gid)) == NULL)
2886 return NT_STATUS_NO_SUCH_ALIAS;
2888 /* we need to copy the name otherwise it's overloaded in user_in_group_list */
2889 fstrcpy(grp_name, grp->gr_name);
2891 sid_peek_rid(&q_u->sid.sid, &rid);
2893 /* check if the user exists before trying to remove it from the group */
2894 pdb_init_sam(&sam_pass);
2895 if(!pdb_getsampwrid(sam_pass, rid)) {
2896 DEBUG(5,("_samr_del_aliasmem:User %s doesn't exist.\n", pdb_get_username(sam_pass)));
2897 pdb_free_sam(&sam_pass);
2898 return NT_STATUS_NO_SUCH_USER;
2901 /* if the user is not in the group */
2902 if(!user_in_group_list(pdb_get_username(sam_pass), grp_name)) {
2903 pdb_free_sam(&sam_pass);
2904 return NT_STATUS_MEMBER_IN_ALIAS;
2907 smb_delete_user_group(grp_name, pdb_get_username(sam_pass));
2909 /* check if the user has been removed then ... */
2910 if(user_in_group_list(pdb_get_username(sam_pass), grp_name)) {
2911 pdb_free_sam(&sam_pass);
2912 return NT_STATUS_MEMBER_NOT_IN_ALIAS; /* don't know what to reply else */
2915 pdb_free_sam(&sam_pass);
2916 return NT_STATUS_OK;
2919 /*********************************************************************
2921 *********************************************************************/
2923 NTSTATUS _samr_add_groupmem(pipes_struct *p, SAMR_Q_ADD_GROUPMEM *q_u, SAMR_R_ADD_GROUPMEM *r_u)
2926 fstring group_sid_str;
2932 /* Find the policy handle. Open a policy on it. */
2933 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid))
2934 return NT_STATUS_INVALID_HANDLE;
2936 sid_to_string(group_sid_str, &group_sid);
2937 DEBUG(10, ("sid is %s\n", group_sid_str));
2939 if (sid_compare(&group_sid, &global_sam_sid)<=0)
2940 return NT_STATUS_NO_SUCH_GROUP;
2942 DEBUG(10, ("lookup on Domain SID\n"));
2944 if(!get_domain_group_from_sid(group_sid, &map, MAPPING_WITHOUT_PRIV))
2945 return NT_STATUS_NO_SUCH_GROUP;
2947 if ((pwd=getpwuid(pdb_user_rid_to_uid(q_u->rid))) ==NULL)
2948 return NT_STATUS_NO_SUCH_USER;
2950 if ((grp=getgrgid(map.gid)) == NULL)
2951 return NT_STATUS_NO_SUCH_GROUP;
2953 /* we need to copy the name otherwise it's overloaded in user_in_group_list */
2954 fstrcpy(grp_name, grp->gr_name);
2956 /* if the user is already in the group */
2957 if(user_in_group_list(pwd->pw_name, grp_name))
2958 return NT_STATUS_MEMBER_IN_GROUP;
2961 * ok, the group exist, the user exist, the user is not in the group,
2963 * we can (finally) add it to the group !
2966 smb_add_user_group(grp_name, pwd->pw_name);
2968 /* check if the user has been added then ... */
2969 if(!user_in_group_list(pwd->pw_name, grp_name))
2970 return NT_STATUS_MEMBER_NOT_IN_GROUP; /* don't know what to reply else */
2972 return NT_STATUS_OK;
2975 /*********************************************************************
2977 *********************************************************************/
2979 NTSTATUS _samr_del_groupmem(pipes_struct *p, SAMR_Q_DEL_GROUPMEM *q_u, SAMR_R_DEL_GROUPMEM *r_u)
2982 SAM_ACCOUNT *sam_pass=NULL;
2989 * delete the group member named q_u->rid
2990 * who is a member of the sid associated with the handle
2991 * the rid is a user's rid as the group is a domain group.
2994 /* Find the policy handle. Open a policy on it. */
2995 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid))
2996 return NT_STATUS_INVALID_HANDLE;
2998 if(!sid_check_is_in_our_domain(&group_sid))
2999 return NT_STATUS_NO_SUCH_GROUP;
3003 if(!get_domain_group_from_sid(group_sid, &map, MAPPING_WITHOUT_PRIV))
3004 return NT_STATUS_NO_SUCH_GROUP;
3006 if ((grp=getgrgid(map.gid)) == NULL)
3007 return NT_STATUS_NO_SUCH_GROUP;
3009 /* we need to copy the name otherwise it's overloaded in user_in_group_list */
3010 fstrcpy(grp_name, grp->gr_name);
3012 /* check if the user exists before trying to remove it from the group */
3013 pdb_init_sam(&sam_pass);
3014 if(!pdb_getsampwrid(sam_pass, rid)) {
3015 DEBUG(5,("User %s doesn't exist.\n", pdb_get_username(sam_pass)));
3016 pdb_free_sam(&sam_pass);
3017 return NT_STATUS_NO_SUCH_USER;
3020 /* if the user is not in the group */
3021 if(!user_in_group_list(pdb_get_username(sam_pass), grp_name)) {
3022 pdb_free_sam(&sam_pass);
3023 return NT_STATUS_MEMBER_NOT_IN_GROUP;
3026 smb_delete_user_group(grp_name, pdb_get_username(sam_pass));
3028 /* check if the user has been removed then ... */
3029 if(user_in_group_list(pdb_get_username(sam_pass), grp_name)) {
3030 pdb_free_sam(&sam_pass);
3031 return NT_STATUS_ACCESS_DENIED; /* don't know what to reply else */
3034 pdb_free_sam(&sam_pass);
3035 return NT_STATUS_OK;
3039 /*********************************************************************
3040 _samr_delete_dom_user
3041 *********************************************************************/
3043 NTSTATUS _samr_delete_dom_user(pipes_struct *p, SAMR_Q_DELETE_DOM_USER *q_u, SAMR_R_DELETE_DOM_USER *r_u )
3046 SAM_ACCOUNT *sam_pass=NULL;
3049 DEBUG(5, ("_samr_delete_dom_user: %d\n", __LINE__));
3051 /* Find the policy handle. Open a policy on it. */
3052 if (!get_lsa_policy_samr_sid(p, &q_u->user_pol, &user_sid))
3053 return NT_STATUS_INVALID_HANDLE;
3055 if (!sid_check_is_in_our_domain(&user_sid))
3056 return NT_STATUS_CANNOT_DELETE;
3058 sid_peek_rid(&user_sid, &rid);
3060 /* check if the user exists before trying to delete */
3061 pdb_init_sam(&sam_pass);
3062 if(!pdb_getsampwrid(sam_pass, rid)) {
3063 DEBUG(5,("_samr_delete_dom_user:User %s doesn't exist.\n", pdb_get_username(sam_pass)));
3064 pdb_free_sam(&sam_pass);
3065 return NT_STATUS_NO_SUCH_USER;
3068 /* delete the unix side */
3070 * note: we don't check if the delete really happened
3071 * as the script is not necessary present
3072 * and maybe the sysadmin doesn't want to delete the unix side
3074 smb_delete_user(pdb_get_username(sam_pass));
3076 /* and delete the samba side */
3077 if (!pdb_delete_sam_account(pdb_get_username(sam_pass))) {
3078 DEBUG(5,("_samr_delete_dom_user:Failed to delete entry for user %s.\n", pdb_get_username(sam_pass)));
3079 pdb_free_sam(&sam_pass);
3080 return NT_STATUS_CANNOT_DELETE;
3083 pdb_free_sam(&sam_pass);
3085 if (!close_policy_hnd(p, &q_u->user_pol))
3086 return NT_STATUS_OBJECT_NAME_INVALID;
3088 return NT_STATUS_OK;
3091 /*********************************************************************
3092 _samr_delete_dom_group
3093 *********************************************************************/
3095 NTSTATUS _samr_delete_dom_group(pipes_struct *p, SAMR_Q_DELETE_DOM_GROUP *q_u, SAMR_R_DELETE_DOM_GROUP *r_u)
3100 fstring group_sid_str;
3105 DEBUG(5, ("samr_delete_dom_group: %d\n", __LINE__));
3107 /* Find the policy handle. Open a policy on it. */
3108 if (!get_lsa_policy_samr_sid(p, &q_u->group_pol, &group_sid))
3109 return NT_STATUS_INVALID_HANDLE;
3111 sid_copy(&dom_sid, &group_sid);
3112 sid_to_string(group_sid_str, &dom_sid);
3113 sid_split_rid(&dom_sid, &group_rid);
3115 DEBUG(10, ("sid is %s\n", group_sid_str));
3117 /* we check if it's our SID before deleting */
3118 if (!sid_equal(&dom_sid, &global_sam_sid))
3119 return NT_STATUS_NO_SUCH_GROUP;
3121 DEBUG(10, ("lookup on Domain SID\n"));
3123 if(!get_domain_group_from_sid(group_sid, &map, MAPPING_WITHOUT_PRIV))
3124 return NT_STATUS_NO_SUCH_GROUP;
3128 /* check if group really exists */
3129 if ( (grp=getgrgid(gid)) == NULL)
3130 return NT_STATUS_NO_SUCH_GROUP;
3132 /* we can delete the UNIX group */
3133 smb_delete_group(grp->gr_name);
3135 /* check if the group has been successfully deleted */
3136 if ( (grp=getgrgid(gid)) != NULL)
3137 return NT_STATUS_ACCESS_DENIED;
3139 if(!group_map_remove(group_sid))
3140 return NT_STATUS_ACCESS_DENIED;
3142 if (!close_policy_hnd(p, &q_u->group_pol))
3143 return NT_STATUS_OBJECT_NAME_INVALID;
3145 return NT_STATUS_OK;
3148 /*********************************************************************
3149 _samr_delete_dom_alias
3150 *********************************************************************/
3152 NTSTATUS _samr_delete_dom_alias(pipes_struct *p, SAMR_Q_DELETE_DOM_ALIAS *q_u, SAMR_R_DELETE_DOM_ALIAS *r_u)
3157 fstring alias_sid_str;
3162 DEBUG(5, ("_samr_delete_dom_alias: %d\n", __LINE__));
3164 /* Find the policy handle. Open a policy on it. */
3165 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid))
3166 return NT_STATUS_INVALID_HANDLE;
3168 sid_copy(&dom_sid, &alias_sid);
3169 sid_to_string(alias_sid_str, &dom_sid);
3170 sid_split_rid(&dom_sid, &alias_rid);
3172 DEBUG(10, ("sid is %s\n", alias_sid_str));
3174 /* we check if it's our SID before deleting */
3175 if (!sid_equal(&dom_sid, &global_sam_sid))
3176 return NT_STATUS_NO_SUCH_ALIAS;
3178 DEBUG(10, ("lookup on Local SID\n"));
3180 if(!get_local_group_from_sid(alias_sid, &map, MAPPING_WITHOUT_PRIV))
3181 return NT_STATUS_NO_SUCH_ALIAS;
3185 /* check if group really exists */
3186 if ( (grp=getgrgid(gid)) == NULL)
3187 return NT_STATUS_NO_SUCH_ALIAS;
3189 /* we can delete the UNIX group */
3190 smb_delete_group(grp->gr_name);
3192 /* check if the group has been successfully deleted */
3193 if ( (grp=getgrgid(gid)) != NULL)
3194 return NT_STATUS_ACCESS_DENIED;
3196 /* don't check if we removed it as it could be an un-mapped group */
3197 group_map_remove(alias_sid);
3199 if (!close_policy_hnd(p, &q_u->alias_pol))
3200 return NT_STATUS_OBJECT_NAME_INVALID;
3202 return NT_STATUS_OK;
3205 /*********************************************************************
3206 _samr_create_dom_group
3207 *********************************************************************/
3209 NTSTATUS _samr_create_dom_group(pipes_struct *p, SAMR_Q_CREATE_DOM_GROUP *q_u, SAMR_R_CREATE_DOM_GROUP *r_u)
3216 struct samr_info *info;
3217 PRIVILEGE_SET priv_set;
3219 init_privilege(&priv_set);
3221 /* Find the policy handle. Open a policy on it. */
3222 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &dom_sid))
3223 return NT_STATUS_INVALID_HANDLE;
3225 if (!sid_equal(&dom_sid, &global_sam_sid))
3226 return NT_STATUS_ACCESS_DENIED;
3228 /* TODO: check if allowed to create group and add a become_root/unbecome_root pair.*/
3230 unistr2_to_ascii(name, &q_u->uni_acct_desc, sizeof(name)-1);
3232 /* check if group already exist */
3233 if ((grp=getgrnam(name)) != NULL)
3234 return NT_STATUS_GROUP_EXISTS;
3236 /* we can create the UNIX group */
3237 smb_create_group(name);
3239 /* check if the group has been successfully created */
3240 if ((grp=getgrnam(name)) == NULL)
3241 return NT_STATUS_ACCESS_DENIED;
3243 r_u->rid=pdb_gid_to_group_rid(grp->gr_gid);
3245 /* add the group to the mapping table */
3246 sid_copy(&info_sid, &global_sam_sid);
3247 sid_append_rid(&info_sid, r_u->rid);
3248 sid_to_string(sid_string, &info_sid);
3250 if(!add_initial_entry(grp->gr_gid, sid_string, SID_NAME_DOM_GRP, name, NULL, priv_set, PR_ACCESS_FROM_NETWORK))
3251 return NT_STATUS_ACCESS_DENIED;
3253 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
3254 return NT_STATUS_NO_MEMORY;
3256 /* get a (unique) handle. open a policy on it. */
3257 if (!create_policy_hnd(p, &r_u->pol, free_samr_info, (void *)info))
3258 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3260 return NT_STATUS_OK;
3263 /*********************************************************************
3264 _samr_create_dom_alias
3265 *********************************************************************/
3267 NTSTATUS _samr_create_dom_alias(pipes_struct *p, SAMR_Q_CREATE_DOM_ALIAS *q_u, SAMR_R_CREATE_DOM_ALIAS *r_u)
3274 struct samr_info *info;
3275 PRIVILEGE_SET priv_set;
3277 init_privilege(&priv_set);
3279 /* Find the policy handle. Open a policy on it. */
3280 if (!get_lsa_policy_samr_sid(p, &q_u->dom_pol, &dom_sid))
3281 return NT_STATUS_INVALID_HANDLE;
3283 if (!sid_equal(&dom_sid, &global_sam_sid))
3284 return NT_STATUS_ACCESS_DENIED;
3286 /* TODO: check if allowed to create group and add a become_root/unbecome_root pair.*/
3288 unistr2_to_ascii(name, &q_u->uni_acct_desc, sizeof(name)-1);
3290 /* check if group already exists */
3291 if ( (grp=getgrnam(name)) != NULL)
3292 return NT_STATUS_GROUP_EXISTS;
3294 /* we can create the UNIX group */
3295 smb_create_group(name);
3297 /* check if the group has been successfully created */
3298 if ((grp=getgrnam(name)) == NULL)
3299 return NT_STATUS_ACCESS_DENIED;
3301 r_u->rid=pdb_gid_to_group_rid(grp->gr_gid);
3303 sid_copy(&info_sid, &global_sam_sid);
3304 sid_append_rid(&info_sid, r_u->rid);
3305 sid_to_string(sid_string, &info_sid);
3307 /* add the group to the mapping table */
3308 if(!add_initial_entry(grp->gr_gid, sid_string, SID_NAME_ALIAS, name, NULL, priv_set, PR_ACCESS_FROM_NETWORK))
3309 return NT_STATUS_ACCESS_DENIED;
3311 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
3312 return NT_STATUS_NO_MEMORY;
3314 /* get a (unique) handle. open a policy on it. */
3315 if (!create_policy_hnd(p, &r_u->alias_pol, free_samr_info, (void *)info))
3316 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3318 return NT_STATUS_OK;
3321 /*********************************************************************
3322 _samr_query_groupinfo
3324 sends the name/comment pair of a domain group
3325 level 1 send also the number of users of that group
3326 *********************************************************************/
3328 NTSTATUS _samr_query_groupinfo(pipes_struct *p, SAMR_Q_QUERY_GROUPINFO *q_u, SAMR_R_QUERY_GROUPINFO *r_u)
3334 GROUP_INFO_CTR *ctr;
3336 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid))
3337 return NT_STATUS_INVALID_HANDLE;
3339 if (!get_domain_group_from_sid(group_sid, &map, MAPPING_WITHOUT_PRIV))
3340 return NT_STATUS_INVALID_HANDLE;
3342 ctr=(GROUP_INFO_CTR *)talloc_zero(p->mem_ctx, sizeof(GROUP_INFO_CTR));
3344 return NT_STATUS_NO_MEMORY;
3346 switch (q_u->switch_level) {
3348 ctr->switch_value1 = 1;
3349 if(!get_uid_list_of_group(map.gid, &uid, &num_uids))
3350 return NT_STATUS_NO_SUCH_GROUP;
3351 init_samr_group_info1(&ctr->group.info1, map.nt_name, map.comment, num_uids);
3355 ctr->switch_value1 = 3;
3356 init_samr_group_info3(&ctr->group.info3);
3359 ctr->switch_value1 = 4;
3360 init_samr_group_info4(&ctr->group.info4, map.comment);
3363 return NT_STATUS_INVALID_INFO_CLASS;
3366 init_samr_r_query_groupinfo(r_u, ctr, NT_STATUS_OK);
3368 return NT_STATUS_OK;
3371 /*********************************************************************
3374 update a domain group's comment.
3375 *********************************************************************/
3377 NTSTATUS _samr_set_groupinfo(pipes_struct *p, SAMR_Q_SET_GROUPINFO *q_u, SAMR_R_SET_GROUPINFO *r_u)
3381 GROUP_INFO_CTR *ctr;
3383 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid))
3384 return NT_STATUS_INVALID_HANDLE;
3386 if (!get_domain_group_from_sid(group_sid, &map, MAPPING_WITH_PRIV))
3387 return NT_STATUS_NO_SUCH_GROUP;
3391 switch (ctr->switch_value1) {
3393 unistr2_to_ascii(map.comment, &(ctr->group.info1.uni_acct_desc), sizeof(map.comment)-1);
3396 unistr2_to_ascii(map.comment, &(ctr->group.info4.uni_acct_desc), sizeof(map.comment)-1);
3399 free_privilege(&map.priv_set);
3400 return NT_STATUS_INVALID_INFO_CLASS;
3403 if(!add_mapping_entry(&map, TDB_REPLACE)) {
3404 free_privilege(&map.priv_set);
3405 return NT_STATUS_NO_SUCH_GROUP;
3408 free_privilege(&map.priv_set);
3410 return NT_STATUS_OK;
3413 /*********************************************************************
3416 update a domain group's comment.
3417 *********************************************************************/
3419 NTSTATUS _samr_set_aliasinfo(pipes_struct *p, SAMR_Q_SET_ALIASINFO *q_u, SAMR_R_SET_ALIASINFO *r_u)
3423 ALIAS_INFO_CTR *ctr;
3425 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &group_sid))
3426 return NT_STATUS_INVALID_HANDLE;
3428 if (!get_local_group_from_sid(group_sid, &map, MAPPING_WITH_PRIV))
3429 return NT_STATUS_NO_SUCH_GROUP;
3433 switch (ctr->switch_value1) {
3435 unistr2_to_ascii(map.comment, &(ctr->alias.info3.uni_acct_desc), sizeof(map.comment)-1);
3438 free_privilege(&map.priv_set);
3439 return NT_STATUS_INVALID_INFO_CLASS;
3442 if(!add_mapping_entry(&map, TDB_REPLACE)) {
3443 free_privilege(&map.priv_set);
3444 return NT_STATUS_NO_SUCH_GROUP;
3447 free_privilege(&map.priv_set);
3449 return NT_STATUS_OK;
3452 /*********************************************************************
3453 _samr_get_dom_pwinfo
3454 *********************************************************************/
3456 NTSTATUS _samr_get_dom_pwinfo(pipes_struct *p, SAMR_Q_GET_DOM_PWINFO *q_u, SAMR_R_GET_DOM_PWINFO *r_u)
3458 /* Actually, returning zeros here works quite well :-). */
3459 return NT_STATUS_OK;
3462 /*********************************************************************
3464 *********************************************************************/
3466 NTSTATUS _samr_open_group(pipes_struct *p, SAMR_Q_OPEN_GROUP *q_u, SAMR_R_OPEN_GROUP *r_u)
3471 struct samr_info *info;
3474 if (!get_lsa_policy_samr_sid(p, &q_u->domain_pol, &sid))
3475 return NT_STATUS_INVALID_HANDLE;
3477 /* this should not be hard-coded like this */
3478 if (!sid_equal(&sid, &global_sam_sid))
3479 return NT_STATUS_ACCESS_DENIED;
3481 sid_copy(&info_sid, &global_sam_sid);
3482 sid_append_rid(&info_sid, q_u->rid_group);
3483 sid_to_string(sid_string, &info_sid);
3485 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
3486 return NT_STATUS_NO_MEMORY;
3488 DEBUG(10, ("_samr_open_group:Opening SID: %s\n", sid_string));
3490 /* check if that group really exists */
3491 if (!get_domain_group_from_sid(info->sid, &map, MAPPING_WITHOUT_PRIV))
3492 return NT_STATUS_NO_SUCH_GROUP;
3494 /* get a (unique) handle. open a policy on it. */
3495 if (!create_policy_hnd(p, &r_u->pol, free_samr_info, (void *)info))
3496 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3498 return NT_STATUS_OK;
3501 /*********************************************************************
3503 *********************************************************************/
3505 NTSTATUS _samr_unknown_2d(pipes_struct *p, SAMR_Q_UNKNOWN_2D *q_u, SAMR_R_UNKNOWN_2D *r_u)
3507 DEBUG(0,("_samr_unknown_2d: Not yet implemented.\n"));
3508 return NT_STATUS_NOT_IMPLEMENTED;
3511 /*******************************************************************
3513 ********************************************************************/
3515 NTSTATUS _samr_unknown_2e(pipes_struct *p, SAMR_Q_UNKNOWN_2E *q_u, SAMR_R_UNKNOWN_2E *r_u)
3517 struct samr_info *info = NULL;
3519 uint32 min_pass_len,pass_hist,flag;
3520 time_t u_expire, u_min_age;
3521 NTTIME nt_expire, nt_min_age;
3523 time_t u_lock_duration, u_reset_time;
3524 NTTIME nt_lock_duration, nt_reset_time;
3530 uint32 num_users=0, num_groups=0, num_aliases=0;
3532 if ((ctr = (SAM_UNK_CTR *)talloc_zero(p->mem_ctx, sizeof(SAM_UNK_CTR))) == NULL)
3533 return NT_STATUS_NO_MEMORY;
3537 r_u->status = NT_STATUS_OK;
3539 DEBUG(5,("_samr_unknown_2e: %d\n", __LINE__));
3541 /* find the policy handle. open a policy on it. */
3542 if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)&info))
3543 return NT_STATUS_INVALID_HANDLE;
3545 switch (q_u->switch_value) {
3547 account_policy_get(AP_MIN_PASSWORD_LEN, &min_pass_len);
3548 account_policy_get(AP_PASSWORD_HISTORY, &pass_hist);
3549 account_policy_get(AP_USER_MUST_LOGON_TO_CHG_PASS, &flag);
3550 account_policy_get(AP_MAX_PASSWORD_AGE, (int *)&u_expire);
3551 account_policy_get(AP_MIN_PASSWORD_AGE, (int *)&u_min_age);
3553 unix_to_nt_time_abs(&nt_expire, u_expire);
3554 unix_to_nt_time_abs(&nt_min_age, u_min_age);
3556 init_unk_info1(&ctr->info.inf1, (uint16)min_pass_len, (uint16)pass_hist,
3557 flag, nt_expire, nt_min_age);
3561 r_u->status=load_sampwd_entries(info, ACB_NORMAL);
3563 if (NT_STATUS_IS_ERR(r_u->status)) {
3564 DEBUG(5, ("_samr_query_dispinfo: load_sampwd_entries failed\n"));
3567 num_users=info->disp_info.num_user_account;
3570 r_u->status=load_group_domain_entries(info, &global_sam_sid);
3571 if (NT_STATUS_IS_ERR(r_u->status)) {
3572 DEBUG(5, ("_samr_query_dispinfo: load_group_domain_entries failed\n"));
3575 num_groups=info->disp_info.num_group_account;
3578 /* The time call below is to get a sequence number for the sam. FIXME !!! JRA. */
3579 init_unk_info2(&ctr->info.inf2, global_myworkgroup, global_myname, (uint32) time(NULL),
3580 num_users, num_groups, num_aliases);
3583 account_policy_get(AP_TIME_TO_LOGOUT, (int *)&u_logout);
3584 unix_to_nt_time_abs(&nt_logout, u_logout);
3586 init_unk_info3(&ctr->info.inf3, nt_logout);
3589 init_unk_info5(&ctr->info.inf5, global_myname);
3592 init_unk_info6(&ctr->info.inf6);
3595 init_unk_info7(&ctr->info.inf7);
3598 account_policy_get(AP_LOCK_ACCOUNT_DURATION, (int *)&u_lock_duration);
3599 account_policy_get(AP_RESET_COUNT_TIME, (int *)&u_reset_time);
3600 account_policy_get(AP_BAD_ATTEMPT_LOCKOUT, &lockout);
3602 unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
3603 unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
3605 init_unk_info12(&ctr->info.inf12, nt_lock_duration, nt_reset_time, (uint16)lockout);
3608 return NT_STATUS_INVALID_INFO_CLASS;
3611 init_samr_r_samr_unknown_2e(r_u, q_u->switch_value, ctr, NT_STATUS_OK);
3613 DEBUG(5,("_samr_unknown_2e: %d\n", __LINE__));
3618 /*******************************************************************
3620 ********************************************************************/
3622 NTSTATUS _samr_set_dom_info(pipes_struct *p, SAMR_Q_SET_DOMAIN_INFO *q_u, SAMR_R_SET_DOMAIN_INFO *r_u)
3624 time_t u_expire, u_min_age;
3626 time_t u_lock_duration, u_reset_time;
3628 r_u->status = NT_STATUS_OK;
3630 DEBUG(5,("_samr_set_dom_info: %d\n", __LINE__));
3632 /* find the policy handle. open a policy on it. */
3633 if (!find_policy_by_hnd(p, &q_u->domain_pol, NULL))
3634 return NT_STATUS_INVALID_HANDLE;
3636 DEBUG(5,("_samr_set_dom_info: switch_value: %d\n", q_u->switch_value));
3638 switch (q_u->switch_value) {
3640 u_expire=nt_time_to_unix_abs(&q_u->ctr->info.inf1.expire);
3641 u_min_age=nt_time_to_unix_abs(&q_u->ctr->info.inf1.min_passwordage);
3643 account_policy_set(AP_MIN_PASSWORD_LEN, (uint32)q_u->ctr->info.inf1.min_length_password);
3644 account_policy_set(AP_PASSWORD_HISTORY, (uint32)q_u->ctr->info.inf1.password_history);
3645 account_policy_set(AP_USER_MUST_LOGON_TO_CHG_PASS, (uint32)q_u->ctr->info.inf1.flag);
3646 account_policy_set(AP_MAX_PASSWORD_AGE, (int)u_expire);
3647 account_policy_set(AP_MIN_PASSWORD_AGE, (int)u_min_age);
3652 u_logout=nt_time_to_unix_abs(&q_u->ctr->info.inf3.logout);
3653 account_policy_set(AP_TIME_TO_LOGOUT, (int)u_logout);
3662 u_lock_duration=nt_time_to_unix_abs(&q_u->ctr->info.inf12.duration);
3663 u_reset_time=nt_time_to_unix_abs(&q_u->ctr->info.inf12.reset_count);
3665 account_policy_set(AP_LOCK_ACCOUNT_DURATION, (int)u_lock_duration);
3666 account_policy_set(AP_RESET_COUNT_TIME, (int)u_reset_time);
3667 account_policy_set(AP_BAD_ATTEMPT_LOCKOUT, (uint32)q_u->ctr->info.inf12.bad_attempt_lockout);
3670 return NT_STATUS_INVALID_INFO_CLASS;
3673 init_samr_r_set_domain_info(r_u, NT_STATUS_OK);
3675 DEBUG(5,("_samr_set_dom_info: %d\n", __LINE__));