2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Jeremy Allison 1996-2001
5 Copyright (C) Luke Kenneth Casson Leighton 1996-1998
6 Copyright (C) Gerald (Jerry) Carter 2000-2001
7 Copyright (C) Andrew Bartlett 2001-2002
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #define DBGC_CLASS DBGC_PASSDB
30 * This is set on startup - it defines the SID for this
31 * machine, and therefore the SAM database for which it is
35 extern pstring global_myname;
37 /************************************************************
38 Fill the SAM_ACCOUNT with default values.
39 ***********************************************************/
41 static void pdb_fill_default_sam(SAM_ACCOUNT *user)
43 ZERO_STRUCT(user->private); /* Don't touch the talloc context */
45 /* Don't change these timestamp settings without a good reason.
46 They are important for NT member server compatibility. */
48 user->private.init_flag = FLAG_SAM_UNINIT;
49 user->private.uid = user->private.gid = -1;
51 user->private.logon_time = (time_t)0;
52 user->private.pass_last_set_time = (time_t)0;
53 user->private.pass_can_change_time = (time_t)0;
54 user->private.logoff_time =
55 user->private.kickoff_time =
56 user->private.pass_must_change_time = get_time_t_max();
57 user->private.unknown_3 = 0x00ffffff; /* don't know */
58 user->private.logon_divs = 168; /* hours per week */
59 user->private.hours_len = 21; /* 21 times 8 bits = 168 */
60 memset(user->private.hours, 0xff, user->private.hours_len); /* available at all hours */
61 user->private.unknown_5 = 0x00000000; /* don't know */
62 user->private.unknown_6 = 0x000004ec; /* don't know */
64 /* Some parts of samba strlen their pdb_get...() returns,
65 so this keeps the interface unchanged for now. */
67 user->private.username = "";
68 user->private.domain = "";
69 user->private.nt_username = "";
70 user->private.full_name = "";
71 user->private.home_dir = "";
72 user->private.logon_script = "";
73 user->private.profile_path = "";
74 user->private.acct_desc = "";
75 user->private.workstations = "";
76 user->private.unknown_str = "";
77 user->private.munged_dial = "";
80 static void destroy_pdb_talloc(SAM_ACCOUNT **user)
83 talloc_destroy((*user)->mem_ctx);
89 /**********************************************************************
90 Alloc memory and initialises a struct sam_passwd on supplied mem_ctx.
91 ***********************************************************************/
93 NTSTATUS pdb_init_sam_talloc(TALLOC_CTX *mem_ctx, SAM_ACCOUNT **user)
96 DEBUG(0,("pdb_init_sam_talloc: SAM_ACCOUNT was non NULL\n"));
98 smb_panic("non-NULL pointer passed to pdb_init_sam\n");
100 return NT_STATUS_UNSUCCESSFUL;
104 DEBUG(0,("pdb_init_sam_talloc: mem_ctx was NULL!\n"));
105 return NT_STATUS_UNSUCCESSFUL;
108 *user=(SAM_ACCOUNT *)talloc(mem_ctx, sizeof(SAM_ACCOUNT));
111 DEBUG(0,("pdb_init_sam_talloc: error while allocating memory\n"));
112 return NT_STATUS_NO_MEMORY;
115 (*user)->mem_ctx = mem_ctx;
117 (*user)->free_fn = NULL;
119 pdb_fill_default_sam(*user);
125 /*************************************************************
126 Alloc memory and initialises a struct sam_passwd.
127 ************************************************************/
129 NTSTATUS pdb_init_sam(SAM_ACCOUNT **user)
134 mem_ctx = talloc_init_named("passdb internal SAM_ACCOUNT allocation");
137 DEBUG(0,("pdb_init_sam: error while doing talloc_init()\n"));
138 return NT_STATUS_NO_MEMORY;
141 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_talloc(mem_ctx, user))) {
142 talloc_destroy(mem_ctx);
146 (*user)->free_fn = destroy_pdb_talloc;
152 /*************************************************************
153 Initialises a struct sam_passwd with sane values.
154 ************************************************************/
156 NTSTATUS pdb_fill_sam_pw(SAM_ACCOUNT *sam_account, const struct passwd *pwd)
160 const char *guest_account = lp_guestaccount();
161 if (!(guest_account && *guest_account)) {
162 DEBUG(1, ("NULL guest account!?!?\n"));
163 return NT_STATUS_UNSUCCESSFUL;
167 return NT_STATUS_UNSUCCESSFUL;
170 pdb_fill_default_sam(sam_account);
172 pdb_set_username(sam_account, pwd->pw_name);
173 pdb_set_fullname(sam_account, pwd->pw_gecos);
175 pdb_set_unix_homedir(sam_account, pwd->pw_dir);
177 pdb_set_domain (sam_account, lp_workgroup());
179 pdb_set_uid(sam_account, pwd->pw_uid);
180 pdb_set_gid(sam_account, pwd->pw_gid);
182 /* When we get a proper uid -> SID and SID -> uid allocation
183 mechinism, we should call it here.
185 We can't just set this to 0 or allow it only to be filled
186 in when added to the backend, becouse the user's SID
187 may already be in security descriptors etc.
189 -- abartlet 11-May-02
193 /* Ensure this *must* be set right */
194 if (strcmp(pwd->pw_name, guest_account) == 0) {
195 if (!pdb_set_user_sid_from_rid(sam_account, DOMAIN_USER_RID_GUEST)) {
196 return NT_STATUS_UNSUCCESSFUL;
198 if (!pdb_set_group_sid_from_rid(sam_account, DOMAIN_GROUP_RID_GUESTS)) {
199 return NT_STATUS_UNSUCCESSFUL;
203 if (!pdb_set_user_sid_from_rid(sam_account,
204 fallback_pdb_uid_to_user_rid(pwd->pw_uid))) {
205 DEBUG(0,("Can't set User SID from RID!\n"));
206 return NT_STATUS_INVALID_PARAMETER;
209 /* call the mapping code here */
210 if(get_group_map_from_gid(pwd->pw_gid, &map, MAPPING_WITHOUT_PRIV)) {
211 if (!pdb_set_group_sid(sam_account,&map.sid)){
212 DEBUG(0,("Can't set Group SID!\n"));
213 return NT_STATUS_INVALID_PARAMETER;
217 if (!pdb_set_group_sid_from_rid(sam_account,pdb_gid_to_group_rid(pwd->pw_gid))) {
218 DEBUG(0,("Can't set Group SID\n"));
219 return NT_STATUS_INVALID_PARAMETER;
224 /* check if this is a user account or a machine account */
225 if (pwd->pw_name[strlen(pwd->pw_name)-1] != '$')
227 pdb_set_profile_path(sam_account,
228 talloc_sub_specified((sam_account)->mem_ctx,
230 pwd->pw_name, global_myname,
231 pwd->pw_uid, pwd->pw_gid),
234 pdb_set_homedir(sam_account,
235 talloc_sub_specified((sam_account)->mem_ctx,
237 pwd->pw_name, global_myname,
238 pwd->pw_uid, pwd->pw_gid),
241 pdb_set_dir_drive(sam_account,
242 talloc_sub_specified((sam_account)->mem_ctx,
244 pwd->pw_name, global_myname,
245 pwd->pw_uid, pwd->pw_gid),
248 pdb_set_logon_script(sam_account,
249 talloc_sub_specified((sam_account)->mem_ctx,
251 pwd->pw_name, global_myname,
252 pwd->pw_uid, pwd->pw_gid),
259 /*************************************************************
260 Initialises a struct sam_passwd with sane values.
261 ************************************************************/
263 NTSTATUS pdb_init_sam_pw(SAM_ACCOUNT **new_sam_acct, const struct passwd *pwd)
269 return NT_STATUS_UNSUCCESSFUL;
272 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam(new_sam_acct))) {
277 if (!NT_STATUS_IS_OK(nt_status = pdb_fill_sam_pw(*new_sam_acct, pwd))) {
278 pdb_free_sam(new_sam_acct);
288 * Free the contets of the SAM_ACCOUNT, but not the structure.
290 * Also wipes the LM and NT hashes and plaintext passwrod from
293 * @param user SAM_ACCOUNT to free members of.
296 static void pdb_free_sam_contents(SAM_ACCOUNT *user)
299 /* Kill off sensitive data. Free()ed by the
302 data_blob_clear_free(&(user->private.lm_pw));
303 data_blob_clear_free(&(user->private.nt_pw));
304 data_blob_clear_free(&(user->private.plaintext_pw));
308 /************************************************************
309 Reset the SAM_ACCOUNT and free the NT/LM hashes.
310 ***********************************************************/
312 NTSTATUS pdb_reset_sam(SAM_ACCOUNT *user)
315 DEBUG(0,("pdb_reset_sam: SAM_ACCOUNT was NULL\n"));
317 smb_panic("NULL pointer passed to pdb_free_sam\n");
319 return NT_STATUS_UNSUCCESSFUL;
322 pdb_free_sam_contents(user);
324 pdb_fill_default_sam(user);
330 /************************************************************
331 Free the SAM_ACCOUNT and the member pointers.
332 ***********************************************************/
334 NTSTATUS pdb_free_sam(SAM_ACCOUNT **user)
337 DEBUG(0,("pdb_free_sam: SAM_ACCOUNT was NULL\n"));
339 smb_panic("NULL pointer passed to pdb_free_sam\n");
341 return NT_STATUS_UNSUCCESSFUL;
344 pdb_free_sam_contents(*user);
346 if ((*user)->free_fn) {
347 (*user)->free_fn(user);
354 /**********************************************************
355 Encode the account control bits into a string.
356 length = length of string to encode into (including terminating
357 null). length *MUST BE MORE THAN 2* !
358 **********************************************************/
360 char *pdb_encode_acct_ctrl(uint16 acct_ctrl, size_t length)
362 static fstring acct_str;
367 if (acct_ctrl & ACB_PWNOTREQ ) acct_str[i++] = 'N';
368 if (acct_ctrl & ACB_DISABLED ) acct_str[i++] = 'D';
369 if (acct_ctrl & ACB_HOMDIRREQ) acct_str[i++] = 'H';
370 if (acct_ctrl & ACB_TEMPDUP ) acct_str[i++] = 'T';
371 if (acct_ctrl & ACB_NORMAL ) acct_str[i++] = 'U';
372 if (acct_ctrl & ACB_MNS ) acct_str[i++] = 'M';
373 if (acct_ctrl & ACB_WSTRUST ) acct_str[i++] = 'W';
374 if (acct_ctrl & ACB_SVRTRUST ) acct_str[i++] = 'S';
375 if (acct_ctrl & ACB_AUTOLOCK ) acct_str[i++] = 'L';
376 if (acct_ctrl & ACB_PWNOEXP ) acct_str[i++] = 'X';
377 if (acct_ctrl & ACB_DOMTRUST ) acct_str[i++] = 'I';
379 for ( ; i < length - 2 ; i++ )
384 acct_str[i++] = '\0';
389 /**********************************************************
390 Decode the account control bits from a string.
391 **********************************************************/
393 uint16 pdb_decode_acct_ctrl(const char *p)
395 uint16 acct_ctrl = 0;
396 BOOL finished = False;
399 * Check if the account type bits have been encoded after the
400 * NT password (in the form [NDHTUWSLXI]).
406 for (p++; *p && !finished; p++) {
408 case 'N': { acct_ctrl |= ACB_PWNOTREQ ; break; /* 'N'o password. */ }
409 case 'D': { acct_ctrl |= ACB_DISABLED ; break; /* 'D'isabled. */ }
410 case 'H': { acct_ctrl |= ACB_HOMDIRREQ; break; /* 'H'omedir required. */ }
411 case 'T': { acct_ctrl |= ACB_TEMPDUP ; break; /* 'T'emp account. */ }
412 case 'U': { acct_ctrl |= ACB_NORMAL ; break; /* 'U'ser account (normal). */ }
413 case 'M': { acct_ctrl |= ACB_MNS ; break; /* 'M'NS logon user account. What is this ? */ }
414 case 'W': { acct_ctrl |= ACB_WSTRUST ; break; /* 'W'orkstation account. */ }
415 case 'S': { acct_ctrl |= ACB_SVRTRUST ; break; /* 'S'erver account. */ }
416 case 'L': { acct_ctrl |= ACB_AUTOLOCK ; break; /* 'L'ocked account. */ }
417 case 'X': { acct_ctrl |= ACB_PWNOEXP ; break; /* No 'X'piry on password */ }
418 case 'I': { acct_ctrl |= ACB_DOMTRUST ; break; /* 'I'nterdomain trust account. */ }
424 default: { finished = True; }
431 /*************************************************************
432 Routine to set 32 hex password characters from a 16 byte array.
433 **************************************************************/
435 void pdb_sethexpwd(char *p, const unsigned char *pwd, uint16 acct_ctrl)
439 for (i = 0; i < 16; i++)
440 slprintf(&p[i*2], 3, "%02X", pwd[i]);
442 if (acct_ctrl & ACB_PWNOTREQ)
443 safe_strcpy(p, "NO PASSWORDXXXXXXXXXXXXXXXXXXXXX", 33);
445 safe_strcpy(p, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 33);
449 /*************************************************************
450 Routine to get the 32 hex characters and turn them
451 into a 16 byte array.
452 **************************************************************/
454 BOOL pdb_gethexpwd(const char *p, unsigned char *pwd)
457 unsigned char lonybble, hinybble;
458 char *hexchars = "0123456789ABCDEF";
464 for (i = 0; i < 32; i += 2) {
465 hinybble = toupper(p[i]);
466 lonybble = toupper(p[i + 1]);
468 p1 = strchr(hexchars, hinybble);
469 p2 = strchr(hexchars, lonybble);
474 hinybble = PTR_DIFF(p1, hexchars);
475 lonybble = PTR_DIFF(p2, hexchars);
477 pwd[i / 2] = (hinybble << 4) | lonybble;
482 /*******************************************************************
483 Converts NT user RID to a UNIX uid.
484 ********************************************************************/
486 uid_t fallback_pdb_user_rid_to_uid(uint32 user_rid)
488 int rid_offset = lp_algorithmic_rid_base();
489 return (uid_t)(((user_rid & (~USER_RID_TYPE))- rid_offset)/RID_MULTIPLIER);
493 /*******************************************************************
494 converts UNIX uid to an NT User RID.
495 ********************************************************************/
497 uint32 fallback_pdb_uid_to_user_rid(uid_t uid)
499 int rid_offset = lp_algorithmic_rid_base();
500 return (((((uint32)uid)*RID_MULTIPLIER) + rid_offset) | USER_RID_TYPE);
503 /*******************************************************************
504 Converts NT group RID to a UNIX gid.
505 ********************************************************************/
507 gid_t pdb_group_rid_to_gid(uint32 group_rid)
509 int rid_offset = lp_algorithmic_rid_base();
510 return (gid_t)(((group_rid & (~GROUP_RID_TYPE))- rid_offset)/RID_MULTIPLIER);
513 /*******************************************************************
514 converts NT Group RID to a UNIX uid.
516 warning: you must not call that function only
517 you must do a call to the group mapping first.
518 there is not anymore a direct link between the gid and the rid.
519 ********************************************************************/
521 uint32 pdb_gid_to_group_rid(gid_t gid)
523 int rid_offset = lp_algorithmic_rid_base();
524 return (((((uint32)gid)*RID_MULTIPLIER) + rid_offset) | GROUP_RID_TYPE);
527 /*******************************************************************
528 Decides if a RID is a well known RID.
529 ********************************************************************/
531 static BOOL pdb_rid_is_well_known(uint32 rid)
533 /* Not using rid_offset here, becouse this is the actual
534 NT fixed value (1000) */
536 return (rid < BASE_RID);
539 /*******************************************************************
540 Decides if a RID is a user or group RID.
541 ********************************************************************/
543 BOOL pdb_rid_is_user(uint32 rid)
545 /* lkcl i understand that NT attaches an enumeration to a RID
546 * such that it can be identified as either a user, group etc
547 * type. there are 5 such categories, and they are documented.
549 /* However, they are not in the RID, just somthing you can query
550 seperatly. Sorry luke :-) */
552 if(pdb_rid_is_well_known(rid)) {
554 * The only well known user RIDs are DOMAIN_USER_RID_ADMIN
555 * and DOMAIN_USER_RID_GUEST.
557 if(rid == DOMAIN_USER_RID_ADMIN || rid == DOMAIN_USER_RID_GUEST)
559 } else if((rid & RID_TYPE_MASK) == USER_RID_TYPE) {
565 /*******************************************************************
566 Convert a rid into a name. Used in the lookup SID rpc.
567 ********************************************************************/
569 BOOL local_lookup_sid(DOM_SID *sid, char *name, enum SID_NAME_USE *psid_name_use)
572 SAM_ACCOUNT *sam_account = NULL;
575 if (!sid_peek_check_rid(get_global_sam_sid(), sid, &rid)){
576 DEBUG(0,("local_sid_to_gid: sid_peek_check_rid return False! SID: %s\n",
577 sid_string_static(&map.sid)));
580 *psid_name_use = SID_NAME_UNKNOWN;
582 DEBUG(5,("local_lookup_sid: looking up RID %u.\n", (unsigned int)rid));
584 if (rid == DOMAIN_USER_RID_ADMIN) {
585 char **admin_list = lp_admin_users(-1);
586 *psid_name_use = SID_NAME_USER;
588 char *p = *admin_list;
589 if(!next_token(&p, name, NULL, sizeof(fstring)))
590 fstrcpy(name, "Administrator");
592 fstrcpy(name, "Administrator");
598 * Don't try to convert the rid to a name if
599 * running in appliance mode
602 if (lp_hide_local_users())
605 if (!NT_STATUS_IS_OK(pdb_init_sam(&sam_account))) {
609 /* This now does the 'generic' mapping in pdb_unix */
610 /* 'guest' is also handled there */
611 if (pdb_getsampwsid(sam_account, sid)) {
612 fstrcpy(name, pdb_get_username(sam_account));
613 *psid_name_use = SID_NAME_USER;
615 pdb_free_sam(&sam_account);
620 pdb_free_sam(&sam_account);
622 if (get_group_map_from_sid(*sid, &map, MAPPING_WITHOUT_PRIV)) {
624 DEBUG(5,("local_lookup_sid: mapped group %s to gid %u\n", map.nt_name, (unsigned int)map.gid));
626 DEBUG(5,("local_lookup_sid: mapped group %s to no unix gid. Returning name.\n", map.nt_name));
629 fstrcpy(name, map.nt_name);
630 *psid_name_use = map.sid_name_use;
634 if (pdb_rid_is_user(rid)) {
637 DEBUG(5, ("assuming RID %u is a user\n", (unsigned)rid));
639 uid = fallback_pdb_user_rid_to_uid(rid);
640 slprintf(name, sizeof(fstring)-1, "unix_user.%u", (unsigned int)uid);
642 return False; /* Indicates that this user was 'not mapped' */
647 DEBUG(5, ("assuming RID %u is a group\n", (unsigned)rid));
649 gid = pdb_group_rid_to_gid(rid);
652 *psid_name_use = SID_NAME_ALIAS;
654 DEBUG(5,("local_lookup_sid: looking up gid %u %s\n", (unsigned int)gid,
655 gr ? "succeeded" : "failed" ));
658 slprintf(name, sizeof(fstring)-1, "unix_group.%u", (unsigned int)gid);
659 return False; /* Indicates that this group was 'not mapped' */
662 fstrcpy( name, gr->gr_name);
664 DEBUG(5,("local_lookup_sid: found group %s for rid %u\n", name,
665 (unsigned int)rid ));
670 /*******************************************************************
671 Convert a name into a SID. Used in the lookup name rpc.
672 ********************************************************************/
674 BOOL local_lookup_name(const char *c_user, DOM_SID *psid, enum SID_NAME_USE *psid_name_use)
676 extern DOM_SID global_sid_World_Domain;
679 SAM_ACCOUNT *sam_account = NULL;
683 *psid_name_use = SID_NAME_UNKNOWN;
686 * user may be quoted a const string, and map_username and
687 * friends can modify it. Make a modifiable copy. JRA.
690 fstrcpy(user, c_user);
692 sid_copy(&local_sid, get_global_sam_sid());
695 * Special case for MACHINE\Everyone. Map to the world_sid.
698 if(strequal(user, "Everyone")) {
699 sid_copy( psid, &global_sid_World_Domain);
700 sid_append_rid(psid, 0);
701 *psid_name_use = SID_NAME_ALIAS;
706 * Don't lookup local unix users if running in appliance mode
708 if (lp_hide_local_users())
711 (void)map_username(user);
713 if (!NT_STATUS_IS_OK(pdb_init_sam(&sam_account))) {
717 if (pdb_getsampwnam(sam_account, user)) {
718 sid_copy(psid, pdb_get_user_sid(sam_account));
719 *psid_name_use = SID_NAME_USER;
721 pdb_free_sam(&sam_account);
725 pdb_free_sam(&sam_account);
728 * Maybe it was a group ?
731 /* check if it's a mapped group */
732 if (get_group_map_from_ntname(user, &map, MAPPING_WITHOUT_PRIV)) {
733 /* yes it's a mapped group */
734 sid_copy(&local_sid, &map.sid);
735 *psid_name_use = map.sid_name_use;
737 /* it's not a mapped group */
738 grp = getgrnam(user);
743 *check if it's mapped, if it is reply it doesn't exist
745 * that's to prevent this case:
747 * unix group ug is mapped to nt group ng
748 * someone does a lookup on ug
749 * we must not reply as it doesn't "exist" anymore
750 * for NT. For NT only ng exists.
754 if (get_group_map_from_gid(grp->gr_gid, &map, MAPPING_WITHOUT_PRIV)){
758 sid_append_rid( &local_sid, pdb_gid_to_group_rid(grp->gr_gid));
759 *psid_name_use = SID_NAME_ALIAS;
762 sid_copy( psid, &local_sid);
767 /****************************************************************************
768 Convert a uid to SID - locally.
769 ****************************************************************************/
771 DOM_SID *local_uid_to_sid(DOM_SID *psid, uid_t uid)
774 SAM_ACCOUNT *sam_user = NULL;
775 fstring str; /* sid string buffer */
777 sid_copy(psid, get_global_sam_sid());
779 if((pass = getpwuid_alloc(uid))) {
781 if (NT_STATUS_IS_ERR(pdb_init_sam(&sam_user))) {
786 if (pdb_getsampwnam(sam_user, pass->pw_name)) {
787 sid_copy(psid, pdb_get_user_sid(sam_user));
788 } else if (strcmp(pass->pw_name, lp_guestaccount()) == 0) {
789 sid_append_rid(psid, DOMAIN_USER_RID_GUEST);
791 sid_append_rid(psid, fallback_pdb_uid_to_user_rid(uid));
794 DEBUG(10,("local_uid_to_sid: uid %u -> SID (%s) (%s).\n",
795 (unsigned)uid, sid_to_string( str, psid),
799 pdb_free_sam(&sam_user);
802 sid_append_rid(psid, fallback_pdb_uid_to_user_rid(uid));
804 DEBUG(10,("local_uid_to_sid: uid %u -> SID (%s) (unknown user).\n",
805 (unsigned)uid, sid_to_string( str, psid)));
811 /****************************************************************************
812 Convert a SID to uid - locally.
813 ****************************************************************************/
815 BOOL local_sid_to_uid(uid_t *puid, const DOM_SID *psid, enum SID_NAME_USE *name_type)
818 SAM_ACCOUNT *sam_user = NULL;
820 *name_type = SID_NAME_UNKNOWN;
822 if (NT_STATUS_IS_ERR(pdb_init_sam(&sam_user)))
825 if (pdb_getsampwsid(sam_user, psid)) {
826 *puid = pdb_get_uid(sam_user);
828 pdb_free_sam(&sam_user);
831 DEBUG(10,("local_sid_to_uid: SID %s -> uid (%u) (%s).\n", sid_to_string( str, psid),
832 (unsigned int)*puid, pdb_get_username(sam_user)));
833 pdb_free_sam(&sam_user);
840 pdb_free_sam(&sam_user);
842 if (get_group_map_from_sid(*psid, &map, MAPPING_WITHOUT_PRIV)) {
843 DEBUG(3, ("local_sid_to_uid: SID '%s' is a group, not a user... \n", sid_to_string(str, psid)));
844 /* It's a group, not a user... */
848 sid_copy(&dom_sid, psid);
849 if (!sid_peek_check_rid(get_global_sam_sid(), psid, &rid)) {
850 DEBUG(3, ("sid_peek_rid failed - sid '%s' is not in our domain\n", sid_to_string(str, psid)));
854 if (!pdb_rid_is_user(rid)) {
855 DEBUG(3, ("local_sid_to_uid: sid '%s' cannot be mapped to a uid algorithmicly becouse it is a group\n", sid_to_string(str, psid)));
859 *puid = fallback_pdb_user_rid_to_uid(rid);
861 DEBUG(5,("local_sid_to_uid: SID %s algorithmicly mapped to %ld mapped becouse SID was not found in passdb.\n",
862 sid_to_string(str, psid), (signed long int)(*puid)));
865 *name_type = SID_NAME_USER;
870 /****************************************************************************
871 Convert a gid to SID - locally.
872 ****************************************************************************/
874 DOM_SID *local_gid_to_sid(DOM_SID *psid, gid_t gid)
878 sid_copy(psid, get_global_sam_sid());
880 if (get_group_map_from_gid(gid, &map, MAPPING_WITHOUT_PRIV)) {
881 sid_copy(psid, &map.sid);
884 sid_append_rid(psid, pdb_gid_to_group_rid(gid));
890 /****************************************************************************
891 Convert a SID to gid - locally.
892 ****************************************************************************/
894 BOOL local_sid_to_gid(gid_t *pgid, const DOM_SID *psid, enum SID_NAME_USE *name_type)
899 *name_type = SID_NAME_UNKNOWN;
902 * We can only convert to a gid if this is our local
903 * Domain SID (ie. we are the controling authority).
905 * Or in the Builtin SID too. JFM, 11/30/2001
908 if (get_group_map_from_sid(*psid, &map, MAPPING_WITHOUT_PRIV)) {
910 /* the SID is in the mapping table but not mapped */
915 *name_type = map.sid_name_use;
916 DEBUG(10,("local_sid_to_gid: mapped SID %s (%s) -> gid (%u).\n",
917 sid_to_string( str, psid),
918 map.nt_name, (unsigned int)*pgid));
922 SAM_ACCOUNT *sam_user = NULL;
923 if (NT_STATUS_IS_ERR(pdb_init_sam(&sam_user)))
926 if (pdb_getsampwsid(sam_user, psid)) {
928 pdb_free_sam(&sam_user);
931 pdb_free_sam(&sam_user);
933 if (!sid_peek_check_rid(get_global_sam_sid(), psid, &rid)) {
934 DEBUG(3, ("sid_peek_rid failed - sid '%s' is not in our domain\n", sid_to_string(str, psid)));
938 if (pdb_rid_is_user(rid))
941 *pgid = pdb_group_rid_to_gid(rid);
942 *name_type = SID_NAME_ALIAS;
943 DEBUG(10,("local_sid_to_gid: SID %s -> gid (%u).\n", sid_to_string( str, psid),
944 (unsigned int)*pgid));
951 * Quick hack to do an easy ucs2 -> mulitbyte conversion
952 * @return static buffer containing the converted string
955 const char *pdb_unistr2_convert(const UNISTR2 *from)
957 static pstring convert_buffer;
960 return convert_buffer;
963 unistr2_to_ascii(convert_buffer, from, sizeof(pstring));
964 return convert_buffer;
967 /*************************************************************
968 Change a password entry in the local smbpasswd file.
970 FIXME!! The function needs to be abstracted into the
971 passdb interface or something. It is currently being called
972 by _api_samr_create_user() in rpc_server/srv_samr.c,
973 in SWAT and by smbpasswd/pdbedit.
976 *************************************************************/
978 BOOL local_password_change(const char *user_name, int local_flags,
979 const char *new_passwd,
980 char *err_str, size_t err_str_len,
981 char *msg_str, size_t msg_str_len)
983 struct passwd *pwd = NULL;
984 SAM_ACCOUNT *sam_pass=NULL;
989 /* Get the smb passwd entry for this user */
990 pdb_init_sam(&sam_pass);
991 if(!pdb_getsampwnam(sam_pass, user_name)) {
992 pdb_free_sam(&sam_pass);
994 if (local_flags & LOCAL_ADD_USER) {
995 pwd = getpwnam_alloc(user_name);
996 } else if (local_flags & LOCAL_DELETE_USER) {
997 /* Might not exist in /etc/passwd */
999 slprintf(err_str, err_str_len-1,"Failed to find entry for user %s.\n", user_name);
1004 /* Local user found, so init from this */
1005 if (!NT_STATUS_IS_OK(pdb_init_sam_pw(&sam_pass, pwd))){
1006 slprintf(err_str, err_str_len-1, "Failed initialise SAM_ACCOUNT for user %s.\n", user_name);
1013 if (!NT_STATUS_IS_OK(pdb_init_sam(&sam_pass))){
1014 slprintf(err_str, err_str_len-1, "Failed initialise SAM_ACCOUNT for user %s.\n", user_name);
1018 if (!pdb_set_username(sam_pass, user_name)) {
1019 slprintf(err_str, err_str_len - 1, "Failed to set username for user %s.\n", user_name);
1020 pdb_free_sam(&sam_pass);
1024 if (local_flags & LOCAL_TRUST_ACCOUNT) {
1025 if (!pdb_set_acct_ctrl(sam_pass, ACB_WSTRUST)) {
1026 slprintf(err_str, err_str_len - 1, "Failed to set 'trusted workstation account' flags for user %s.\n", user_name);
1027 pdb_free_sam(&sam_pass);
1030 } else if (local_flags & LOCAL_INTERDOM_ACCOUNT) {
1031 if (!pdb_set_acct_ctrl(sam_pass, ACB_DOMTRUST)) {
1032 slprintf(err_str, err_str_len - 1, "Failed to set 'domain trust account' flags for user %s.\n", user_name);
1033 pdb_free_sam(&sam_pass);
1037 if (!pdb_set_acct_ctrl(sam_pass, ACB_NORMAL)) {
1038 slprintf(err_str, err_str_len - 1, "Failed to set 'normal account' flags for user %s.\n", user_name);
1039 pdb_free_sam(&sam_pass);
1045 /* the entry already existed */
1046 local_flags &= ~LOCAL_ADD_USER;
1050 * We are root - just write the new password
1051 * and the valid last change time.
1054 if (local_flags & LOCAL_DISABLE_USER) {
1055 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)|ACB_DISABLED)) {
1056 slprintf(err_str, err_str_len-1, "Failed to set 'disabled' flag for user %s.\n", user_name);
1057 pdb_free_sam(&sam_pass);
1060 } else if (local_flags & LOCAL_ENABLE_USER) {
1061 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_DISABLED))) {
1062 slprintf(err_str, err_str_len-1, "Failed to unset 'disabled' flag for user %s.\n", user_name);
1063 pdb_free_sam(&sam_pass);
1068 if (local_flags & LOCAL_SET_NO_PASSWORD) {
1069 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)|ACB_PWNOTREQ)) {
1070 slprintf(err_str, err_str_len-1, "Failed to set 'no password required' flag for user %s.\n", user_name);
1071 pdb_free_sam(&sam_pass);
1074 } else if (local_flags & LOCAL_SET_PASSWORD) {
1076 * If we're dealing with setting a completely empty user account
1077 * ie. One with a password of 'XXXX', but not set disabled (like
1078 * an account created from scratch) then if the old password was
1079 * 'XX's then getsmbpwent will have set the ACB_DISABLED flag.
1080 * We remove that as we're giving this user their first password
1081 * and the decision hasn't really been made to disable them (ie.
1082 * don't create them disabled). JRA.
1084 if ((pdb_get_lanman_passwd(sam_pass)==NULL) && (pdb_get_acct_ctrl(sam_pass)&ACB_DISABLED)) {
1085 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_DISABLED))) {
1086 slprintf(err_str, err_str_len-1, "Failed to unset 'disabled' flag for user %s.\n", user_name);
1087 pdb_free_sam(&sam_pass);
1091 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_PWNOTREQ))) {
1092 slprintf(err_str, err_str_len-1, "Failed to unset 'no password required' flag for user %s.\n", user_name);
1093 pdb_free_sam(&sam_pass);
1097 if (!pdb_set_plaintext_passwd (sam_pass, new_passwd)) {
1098 slprintf(err_str, err_str_len-1, "Failed to set password for user %s.\n", user_name);
1099 pdb_free_sam(&sam_pass);
1104 if (local_flags & LOCAL_ADD_USER) {
1105 if (pdb_add_sam_account(sam_pass)) {
1106 slprintf(msg_str, msg_str_len-1, "Added user %s.\n", user_name);
1107 pdb_free_sam(&sam_pass);
1110 slprintf(err_str, err_str_len-1, "Failed to add entry for user %s.\n", user_name);
1111 pdb_free_sam(&sam_pass);
1114 } else if (local_flags & LOCAL_DELETE_USER) {
1115 if (!pdb_delete_sam_account(sam_pass)) {
1116 slprintf(err_str,err_str_len-1, "Failed to delete entry for user %s.\n", user_name);
1117 pdb_free_sam(&sam_pass);
1120 slprintf(msg_str, msg_str_len-1, "Deleted user %s.\n", user_name);
1122 if(!pdb_update_sam_account(sam_pass)) {
1123 slprintf(err_str, err_str_len-1, "Failed to modify entry for user %s.\n", user_name);
1124 pdb_free_sam(&sam_pass);
1127 if(local_flags & LOCAL_DISABLE_USER)
1128 slprintf(msg_str, msg_str_len-1, "Disabled user %s.\n", user_name);
1129 else if (local_flags & LOCAL_ENABLE_USER)
1130 slprintf(msg_str, msg_str_len-1, "Enabled user %s.\n", user_name);
1131 else if (local_flags & LOCAL_SET_NO_PASSWORD)
1132 slprintf(msg_str, msg_str_len-1, "User %s password set to none.\n", user_name);
1135 pdb_free_sam(&sam_pass);