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)
161 return NT_STATUS_UNSUCCESSFUL;
164 pdb_fill_default_sam(sam_account);
166 pdb_set_username(sam_account, pwd->pw_name);
167 pdb_set_fullname(sam_account, pwd->pw_gecos);
169 pdb_set_unix_homedir(sam_account, pwd->pw_dir);
171 pdb_set_domain (sam_account, lp_workgroup());
173 pdb_set_uid(sam_account, pwd->pw_uid);
174 pdb_set_gid(sam_account, pwd->pw_gid);
176 /* When we get a proper uid -> SID and SID -> uid allocation
177 mechinism, we should call it here.
179 We can't just set this to 0 or allow it only to be filled
180 in when added to the backend, becouse the user's SID
181 may already be in security descriptors etc.
183 -- abartlet 11-May-02
186 if (!pdb_set_user_sid_from_rid(sam_account,
187 fallback_pdb_uid_to_user_rid(pwd->pw_uid))) {
188 DEBUG(0,("Can't set User SID from RID!\n"));
189 return NT_STATUS_INVALID_PARAMETER;
192 /* call the mapping code here */
193 if(get_group_map_from_gid(pwd->pw_gid, &map, MAPPING_WITHOUT_PRIV)) {
194 if (!pdb_set_group_sid(sam_account,&map.sid)){
195 DEBUG(0,("Can't set Group SID!\n"));
196 return NT_STATUS_INVALID_PARAMETER;
200 if (!pdb_set_group_sid_from_rid(sam_account,pdb_gid_to_group_rid(pwd->pw_gid))) {
201 DEBUG(0,("Can't set Group SID\n"));
202 return NT_STATUS_INVALID_PARAMETER;
206 /* check if this is a user account or a machine account */
207 if (pwd->pw_name[strlen(pwd->pw_name)-1] != '$')
209 pdb_set_profile_path(sam_account,
210 talloc_sub_specified((sam_account)->mem_ctx,
212 pwd->pw_name, global_myname,
213 pwd->pw_uid, pwd->pw_gid),
216 pdb_set_homedir(sam_account,
217 talloc_sub_specified((sam_account)->mem_ctx,
219 pwd->pw_name, global_myname,
220 pwd->pw_uid, pwd->pw_gid),
223 pdb_set_dir_drive(sam_account,
224 talloc_sub_specified((sam_account)->mem_ctx,
226 pwd->pw_name, global_myname,
227 pwd->pw_uid, pwd->pw_gid),
230 pdb_set_logon_script(sam_account,
231 talloc_sub_specified((sam_account)->mem_ctx,
233 pwd->pw_name, global_myname,
234 pwd->pw_uid, pwd->pw_gid),
241 /*************************************************************
242 Initialises a struct sam_passwd with sane values.
243 ************************************************************/
245 NTSTATUS pdb_init_sam_pw(SAM_ACCOUNT **new_sam_acct, const struct passwd *pwd)
251 return NT_STATUS_UNSUCCESSFUL;
254 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam(new_sam_acct))) {
259 if (!NT_STATUS_IS_OK(nt_status = pdb_fill_sam_pw(*new_sam_acct, pwd))) {
260 pdb_free_sam(new_sam_acct);
270 * Free the contets of the SAM_ACCOUNT, but not the structure.
272 * Also wipes the LM and NT hashes and plaintext passwrod from
275 * @param user SAM_ACCOUNT to free members of.
278 static void pdb_free_sam_contents(SAM_ACCOUNT *user)
281 /* Kill off sensitive data. Free()ed by the
284 data_blob_clear_free(&(user->private.lm_pw));
285 data_blob_clear_free(&(user->private.nt_pw));
286 data_blob_clear_free(&(user->private.plaintext_pw));
290 /************************************************************
291 Reset the SAM_ACCOUNT and free the NT/LM hashes.
292 ***********************************************************/
294 NTSTATUS pdb_reset_sam(SAM_ACCOUNT *user)
297 DEBUG(0,("pdb_reset_sam: SAM_ACCOUNT was NULL\n"));
299 smb_panic("NULL pointer passed to pdb_free_sam\n");
301 return NT_STATUS_UNSUCCESSFUL;
304 pdb_free_sam_contents(user);
306 pdb_fill_default_sam(user);
312 /************************************************************
313 Free the SAM_ACCOUNT and the member pointers.
314 ***********************************************************/
316 NTSTATUS pdb_free_sam(SAM_ACCOUNT **user)
319 DEBUG(0,("pdb_free_sam: SAM_ACCOUNT was NULL\n"));
321 smb_panic("NULL pointer passed to pdb_free_sam\n");
323 return NT_STATUS_UNSUCCESSFUL;
326 pdb_free_sam_contents(*user);
328 if ((*user)->free_fn) {
329 (*user)->free_fn(user);
336 /**********************************************************
337 Encode the account control bits into a string.
338 length = length of string to encode into (including terminating
339 null). length *MUST BE MORE THAN 2* !
340 **********************************************************/
342 char *pdb_encode_acct_ctrl(uint16 acct_ctrl, size_t length)
344 static fstring acct_str;
349 if (acct_ctrl & ACB_PWNOTREQ ) acct_str[i++] = 'N';
350 if (acct_ctrl & ACB_DISABLED ) acct_str[i++] = 'D';
351 if (acct_ctrl & ACB_HOMDIRREQ) acct_str[i++] = 'H';
352 if (acct_ctrl & ACB_TEMPDUP ) acct_str[i++] = 'T';
353 if (acct_ctrl & ACB_NORMAL ) acct_str[i++] = 'U';
354 if (acct_ctrl & ACB_MNS ) acct_str[i++] = 'M';
355 if (acct_ctrl & ACB_WSTRUST ) acct_str[i++] = 'W';
356 if (acct_ctrl & ACB_SVRTRUST ) acct_str[i++] = 'S';
357 if (acct_ctrl & ACB_AUTOLOCK ) acct_str[i++] = 'L';
358 if (acct_ctrl & ACB_PWNOEXP ) acct_str[i++] = 'X';
359 if (acct_ctrl & ACB_DOMTRUST ) acct_str[i++] = 'I';
361 for ( ; i < length - 2 ; i++ )
366 acct_str[i++] = '\0';
371 /**********************************************************
372 Decode the account control bits from a string.
373 **********************************************************/
375 uint16 pdb_decode_acct_ctrl(const char *p)
377 uint16 acct_ctrl = 0;
378 BOOL finished = False;
381 * Check if the account type bits have been encoded after the
382 * NT password (in the form [NDHTUWSLXI]).
388 for (p++; *p && !finished; p++) {
390 case 'N': { acct_ctrl |= ACB_PWNOTREQ ; break; /* 'N'o password. */ }
391 case 'D': { acct_ctrl |= ACB_DISABLED ; break; /* 'D'isabled. */ }
392 case 'H': { acct_ctrl |= ACB_HOMDIRREQ; break; /* 'H'omedir required. */ }
393 case 'T': { acct_ctrl |= ACB_TEMPDUP ; break; /* 'T'emp account. */ }
394 case 'U': { acct_ctrl |= ACB_NORMAL ; break; /* 'U'ser account (normal). */ }
395 case 'M': { acct_ctrl |= ACB_MNS ; break; /* 'M'NS logon user account. What is this ? */ }
396 case 'W': { acct_ctrl |= ACB_WSTRUST ; break; /* 'W'orkstation account. */ }
397 case 'S': { acct_ctrl |= ACB_SVRTRUST ; break; /* 'S'erver account. */ }
398 case 'L': { acct_ctrl |= ACB_AUTOLOCK ; break; /* 'L'ocked account. */ }
399 case 'X': { acct_ctrl |= ACB_PWNOEXP ; break; /* No 'X'piry on password */ }
400 case 'I': { acct_ctrl |= ACB_DOMTRUST ; break; /* 'I'nterdomain trust account. */ }
406 default: { finished = True; }
413 /*************************************************************
414 Routine to set 32 hex password characters from a 16 byte array.
415 **************************************************************/
417 void pdb_sethexpwd(char *p, const unsigned char *pwd, uint16 acct_ctrl)
421 for (i = 0; i < 16; i++)
422 slprintf(&p[i*2], 3, "%02X", pwd[i]);
424 if (acct_ctrl & ACB_PWNOTREQ)
425 safe_strcpy(p, "NO PASSWORDXXXXXXXXXXXXXXXXXXXXX", 33);
427 safe_strcpy(p, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 33);
431 /*************************************************************
432 Routine to get the 32 hex characters and turn them
433 into a 16 byte array.
434 **************************************************************/
436 BOOL pdb_gethexpwd(const char *p, unsigned char *pwd)
439 unsigned char lonybble, hinybble;
440 char *hexchars = "0123456789ABCDEF";
446 for (i = 0; i < 32; i += 2) {
447 hinybble = toupper(p[i]);
448 lonybble = toupper(p[i + 1]);
450 p1 = strchr(hexchars, hinybble);
451 p2 = strchr(hexchars, lonybble);
456 hinybble = PTR_DIFF(p1, hexchars);
457 lonybble = PTR_DIFF(p2, hexchars);
459 pwd[i / 2] = (hinybble << 4) | lonybble;
464 /*******************************************************************
465 Converts NT user RID to a UNIX uid.
466 ********************************************************************/
468 uid_t fallback_pdb_user_rid_to_uid(uint32 user_rid)
470 int rid_offset = lp_algorithmic_rid_base();
471 return (uid_t)(((user_rid & (~USER_RID_TYPE))- rid_offset)/RID_MULTIPLIER);
475 /*******************************************************************
476 converts UNIX uid to an NT User RID.
477 ********************************************************************/
479 uint32 fallback_pdb_uid_to_user_rid(uid_t uid)
481 int rid_offset = lp_algorithmic_rid_base();
482 return (((((uint32)uid)*RID_MULTIPLIER) + rid_offset) | USER_RID_TYPE);
485 /*******************************************************************
486 Converts NT group RID to a UNIX gid.
487 ********************************************************************/
489 gid_t pdb_group_rid_to_gid(uint32 group_rid)
491 int rid_offset = lp_algorithmic_rid_base();
492 return (gid_t)(((group_rid & (~GROUP_RID_TYPE))- rid_offset)/RID_MULTIPLIER);
495 /*******************************************************************
496 converts NT Group RID to a UNIX uid.
498 warning: you must not call that function only
499 you must do a call to the group mapping first.
500 there is not anymore a direct link between the gid and the rid.
501 ********************************************************************/
503 uint32 pdb_gid_to_group_rid(gid_t gid)
505 int rid_offset = lp_algorithmic_rid_base();
506 return (((((uint32)gid)*RID_MULTIPLIER) + rid_offset) | GROUP_RID_TYPE);
509 /*******************************************************************
510 Decides if a RID is a well known RID.
511 ********************************************************************/
513 static BOOL pdb_rid_is_well_known(uint32 rid)
515 /* Not using rid_offset here, becouse this is the actual
516 NT fixed value (1000) */
518 return (rid < BASE_RID);
521 /*******************************************************************
522 Decides if a RID is a user or group RID.
523 ********************************************************************/
525 BOOL pdb_rid_is_user(uint32 rid)
527 /* lkcl i understand that NT attaches an enumeration to a RID
528 * such that it can be identified as either a user, group etc
529 * type. there are 5 such categories, and they are documented.
531 /* However, they are not in the RID, just somthing you can query
532 seperatly. Sorry luke :-) */
534 if(pdb_rid_is_well_known(rid)) {
536 * The only well known user RIDs are DOMAIN_USER_RID_ADMIN
537 * and DOMAIN_USER_RID_GUEST.
539 if(rid == DOMAIN_USER_RID_ADMIN || rid == DOMAIN_USER_RID_GUEST)
541 } else if((rid & RID_TYPE_MASK) == USER_RID_TYPE) {
547 /*******************************************************************
548 Convert a rid into a name. Used in the lookup SID rpc.
549 ********************************************************************/
551 BOOL local_lookup_sid(DOM_SID *sid, char *name, enum SID_NAME_USE *psid_name_use)
554 SAM_ACCOUNT *sam_account = NULL;
557 if (!sid_peek_check_rid(get_global_sam_sid(), sid, &rid)){
558 DEBUG(0,("local_sid_to_gid: sid_peek_check_rid return False! SID: %s\n",
559 sid_string_static(&map.sid)));
562 *psid_name_use = SID_NAME_UNKNOWN;
564 DEBUG(5,("local_lookup_sid: looking up RID %u.\n", (unsigned int)rid));
566 if (rid == DOMAIN_USER_RID_ADMIN) {
567 char **admin_list = lp_admin_users(-1);
568 *psid_name_use = SID_NAME_USER;
570 char *p = *admin_list;
571 if(!next_token(&p, name, NULL, sizeof(fstring)))
572 fstrcpy(name, "Administrator");
574 fstrcpy(name, "Administrator");
578 } else if (rid == DOMAIN_USER_RID_GUEST) {
579 char *p = lp_guestaccount();
580 *psid_name_use = SID_NAME_USER;
581 if(!next_token(&p, name, NULL, sizeof(fstring)))
582 fstrcpy(name, "Guest");
588 * Don't try to convert the rid to a name if
589 * running in appliance mode
592 if (lp_hide_local_users())
595 if (!NT_STATUS_IS_OK(pdb_init_sam(&sam_account))) {
599 /* This now does the 'generic' mapping in pdb_unix */
600 if (pdb_getsampwsid(sam_account, sid)) {
601 fstrcpy(name, pdb_get_username(sam_account));
602 *psid_name_use = SID_NAME_USER;
604 pdb_free_sam(&sam_account);
609 pdb_free_sam(&sam_account);
611 if (get_group_map_from_sid(*sid, &map, MAPPING_WITHOUT_PRIV)) {
613 DEBUG(5,("local_lookup_sid: mapped group %s to gid %u\n", map.nt_name, (unsigned int)map.gid));
615 DEBUG(5,("local_lookup_sid: mapped group %s to no unix gid. Returning name.\n", map.nt_name));
618 fstrcpy(name, map.nt_name);
619 *psid_name_use = map.sid_name_use;
623 if (pdb_rid_is_user(rid)) {
626 DEBUG(5, ("assuming RID %u is a user\n", (unsigned)rid));
628 uid = fallback_pdb_user_rid_to_uid(rid);
629 slprintf(name, sizeof(fstring)-1, "unix_user.%u", (unsigned int)uid);
631 return False; /* Indicates that this user was 'not mapped' */
636 DEBUG(5, ("assuming RID %u is a group\n", (unsigned)rid));
638 gid = pdb_group_rid_to_gid(rid);
641 *psid_name_use = SID_NAME_ALIAS;
643 DEBUG(5,("local_lookup_sid: looking up gid %u %s\n", (unsigned int)gid,
644 gr ? "succeeded" : "failed" ));
647 slprintf(name, sizeof(fstring)-1, "unix_group.%u", (unsigned int)gid);
648 return False; /* Indicates that this group was 'not mapped' */
651 fstrcpy( name, gr->gr_name);
653 DEBUG(5,("local_lookup_sid: found group %s for rid %u\n", name,
654 (unsigned int)rid ));
659 /*******************************************************************
660 Convert a name into a SID. Used in the lookup name rpc.
661 ********************************************************************/
663 BOOL local_lookup_name(const char *c_user, DOM_SID *psid, enum SID_NAME_USE *psid_name_use)
665 extern DOM_SID global_sid_World_Domain;
668 SAM_ACCOUNT *sam_account = NULL;
672 *psid_name_use = SID_NAME_UNKNOWN;
675 * user may be quoted a const string, and map_username and
676 * friends can modify it. Make a modifiable copy. JRA.
679 fstrcpy(user, c_user);
681 sid_copy(&local_sid, get_global_sam_sid());
684 * Special case for MACHINE\Everyone. Map to the world_sid.
687 if(strequal(user, "Everyone")) {
688 sid_copy( psid, &global_sid_World_Domain);
689 sid_append_rid(psid, 0);
690 *psid_name_use = SID_NAME_ALIAS;
695 * Don't lookup local unix users if running in appliance mode
697 if (lp_hide_local_users())
700 (void)map_username(user);
702 if (!NT_STATUS_IS_OK(pdb_init_sam(&sam_account))) {
706 if (pdb_getsampwnam(sam_account, user)) {
707 sid_copy(psid, pdb_get_user_sid(sam_account));
708 *psid_name_use = SID_NAME_USER;
710 pdb_free_sam(&sam_account);
714 pdb_free_sam(&sam_account);
717 * Maybe it was a group ?
720 /* check if it's a mapped group */
721 if (get_group_map_from_ntname(user, &map, MAPPING_WITHOUT_PRIV)) {
723 /* yes it's a mapped group to a valid unix group */
724 sid_copy(&local_sid, &map.sid);
725 *psid_name_use = map.sid_name_use;
728 /* it's a correct name but not mapped so it points to nothing*/
732 /* it's not a mapped group */
733 grp = getgrnam(user);
738 *check if it's mapped, if it is reply it doesn't exist
740 * that's to prevent this case:
742 * unix group ug is mapped to nt group ng
743 * someone does a lookup on ug
744 * we must not reply as it doesn't "exist" anymore
745 * for NT. For NT only ng exists.
749 if (get_group_map_from_gid(grp->gr_gid, &map, MAPPING_WITHOUT_PRIV)){
753 sid_append_rid( &local_sid, pdb_gid_to_group_rid(grp->gr_gid));
754 *psid_name_use = SID_NAME_ALIAS;
757 sid_copy( psid, &local_sid);
762 /****************************************************************************
763 Convert a uid to SID - locally.
764 ****************************************************************************/
766 DOM_SID *local_uid_to_sid(DOM_SID *psid, uid_t uid)
769 SAM_ACCOUNT *sam_user = NULL;
770 fstring str; /* sid string buffer */
772 sid_copy(psid, get_global_sam_sid());
774 if((pass = getpwuid_alloc(uid))) {
776 if (NT_STATUS_IS_ERR(pdb_init_sam(&sam_user))) {
781 if (pdb_getsampwnam(sam_user, pass->pw_name)) {
782 sid_copy(psid, pdb_get_user_sid(sam_user));
784 sid_append_rid(psid, fallback_pdb_uid_to_user_rid(uid));
787 DEBUG(10,("local_uid_to_sid: uid %u -> SID (%s) (%s).\n",
788 (unsigned)uid, sid_to_string( str, psid),
792 pdb_free_sam(&sam_user);
795 sid_append_rid(psid, fallback_pdb_uid_to_user_rid(uid));
797 DEBUG(10,("local_uid_to_sid: uid %u -> SID (%s) (unknown user).\n",
798 (unsigned)uid, sid_to_string( str, psid)));
804 /****************************************************************************
805 Convert a SID to uid - locally.
806 ****************************************************************************/
808 BOOL local_sid_to_uid(uid_t *puid, DOM_SID *psid, enum SID_NAME_USE *name_type)
813 SAM_ACCOUNT *sam_user = NULL;
815 *name_type = SID_NAME_UNKNOWN;
817 sid_copy(&dom_sid, psid);
818 sid_split_rid(&dom_sid, &rid);
821 * We can only convert to a uid if this is our local
822 * Domain SID (ie. we are the controling authority).
824 if (!sid_equal(get_global_sam_sid(), &dom_sid))
827 if (NT_STATUS_IS_ERR(pdb_init_sam(&sam_user)))
830 if (pdb_getsampwsid(sam_user, psid)) {
831 *puid = pdb_get_uid(sam_user);
833 pdb_free_sam(&sam_user);
836 DEBUG(10,("local_sid_to_uid: SID %s -> uid (%u) (%s).\n", sid_to_string( str, psid),
837 (unsigned int)*puid, pdb_get_username(sam_user)));
839 DEBUG(5,("local_sid_to_uid: SID %s not mapped becouse RID was not found in passdb.\n", sid_to_string( str, psid)));
840 pdb_free_sam(&sam_user);
843 pdb_free_sam(&sam_user);
845 *name_type = SID_NAME_USER;
850 /****************************************************************************
851 Convert a gid to SID - locally.
852 ****************************************************************************/
854 DOM_SID *local_gid_to_sid(DOM_SID *psid, gid_t gid)
858 sid_copy(psid, get_global_sam_sid());
860 if (get_group_map_from_gid(gid, &map, MAPPING_WITHOUT_PRIV)) {
861 sid_copy(psid, &map.sid);
864 sid_append_rid(psid, pdb_gid_to_group_rid(gid));
870 /****************************************************************************
871 Convert a SID to gid - locally.
872 ****************************************************************************/
874 BOOL local_sid_to_gid(gid_t *pgid, DOM_SID *psid, enum SID_NAME_USE *name_type)
881 *name_type = SID_NAME_UNKNOWN;
883 sid_copy(&dom_sid, psid);
884 sid_split_rid(&dom_sid, &rid);
887 * We can only convert to a gid if this is our local
888 * Domain SID (ie. we are the controling authority).
890 * Or in the Builtin SID too. JFM, 11/30/2001
893 if (!sid_equal(get_global_sam_sid(), &dom_sid))
896 if (get_group_map_from_sid(*psid, &map, MAPPING_WITHOUT_PRIV)) {
898 /* the SID is in the mapping table but not mapped */
902 if (!sid_peek_check_rid(get_global_sam_sid(), &map.sid, &rid)){
903 DEBUG(0,("local_sid_to_gid: sid_peek_check_rid return False! SID: %s\n",
904 sid_string_static(&map.sid)));
908 *name_type = map.sid_name_use;
909 DEBUG(10,("local_sid_to_gid: mapped SID %s (%s) -> gid (%u).\n", sid_to_string( str, psid),
910 map.nt_name, (unsigned int)*pgid));
913 if (pdb_rid_is_user(rid))
916 *pgid = pdb_group_rid_to_gid(rid);
917 *name_type = SID_NAME_ALIAS;
918 DEBUG(10,("local_sid_to_gid: SID %s -> gid (%u).\n", sid_to_string( str, psid),
919 (unsigned int)*pgid));
926 * Quick hack to do an easy ucs2 -> mulitbyte conversion
927 * @return static buffer containing the converted string
930 const char *pdb_unistr2_convert(const UNISTR2 *from)
932 static pstring convert_buffer;
935 return convert_buffer;
938 unistr2_to_ascii(convert_buffer, from, sizeof(pstring));
939 return convert_buffer;
942 /*************************************************************
943 Change a password entry in the local smbpasswd file.
945 FIXME!! The function needs to be abstracted into the
946 passdb interface or something. It is currently being called
947 by _api_samr_create_user() in rpc_server/srv_samr.c,
948 in SWAT and by smbpasswd/pdbedit.
951 *************************************************************/
953 BOOL local_password_change(const char *user_name, int local_flags,
954 const char *new_passwd,
955 char *err_str, size_t err_str_len,
956 char *msg_str, size_t msg_str_len)
958 struct passwd *pwd = NULL;
959 SAM_ACCOUNT *sam_pass=NULL;
964 /* Get the smb passwd entry for this user */
965 pdb_init_sam(&sam_pass);
966 if(!pdb_getsampwnam(sam_pass, user_name)) {
967 pdb_free_sam(&sam_pass);
969 if (local_flags & LOCAL_ADD_USER) {
970 pwd = getpwnam_alloc(user_name);
971 } else if (local_flags & LOCAL_DELETE_USER) {
972 /* Might not exist in /etc/passwd */
974 slprintf(err_str, err_str_len-1,"Failed to find entry for user %s.\n", user_name);
979 /* Local user found, so init from this */
980 if (!NT_STATUS_IS_OK(pdb_init_sam_pw(&sam_pass, pwd))){
981 slprintf(err_str, err_str_len-1, "Failed initialise SAM_ACCOUNT for user %s.\n", user_name);
988 if (!NT_STATUS_IS_OK(pdb_init_sam(&sam_pass))){
989 slprintf(err_str, err_str_len-1, "Failed initialise SAM_ACCOUNT for user %s.\n", user_name);
993 if (!pdb_set_username(sam_pass, user_name)) {
994 slprintf(err_str, err_str_len - 1, "Failed to set username for user %s.\n", user_name);
995 pdb_free_sam(&sam_pass);
999 if (local_flags & LOCAL_TRUST_ACCOUNT) {
1000 if (!pdb_set_acct_ctrl(sam_pass, ACB_WSTRUST)) {
1001 slprintf(err_str, err_str_len - 1, "Failed to set 'trusted workstation account' flags for user %s.\n", user_name);
1002 pdb_free_sam(&sam_pass);
1005 } else if (local_flags & LOCAL_INTERDOM_ACCOUNT) {
1006 if (!pdb_set_acct_ctrl(sam_pass, ACB_DOMTRUST)) {
1007 slprintf(err_str, err_str_len - 1, "Failed to set 'domain trust account' flags for user %s.\n", user_name);
1008 pdb_free_sam(&sam_pass);
1012 if (!pdb_set_acct_ctrl(sam_pass, ACB_NORMAL)) {
1013 slprintf(err_str, err_str_len - 1, "Failed to set 'normal account' flags for user %s.\n", user_name);
1014 pdb_free_sam(&sam_pass);
1020 /* the entry already existed */
1021 local_flags &= ~LOCAL_ADD_USER;
1025 * We are root - just write the new password
1026 * and the valid last change time.
1029 if (local_flags & LOCAL_DISABLE_USER) {
1030 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)|ACB_DISABLED)) {
1031 slprintf(err_str, err_str_len-1, "Failed to set 'disabled' flag for user %s.\n", user_name);
1032 pdb_free_sam(&sam_pass);
1035 } else if (local_flags & LOCAL_ENABLE_USER) {
1036 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_DISABLED))) {
1037 slprintf(err_str, err_str_len-1, "Failed to unset 'disabled' flag for user %s.\n", user_name);
1038 pdb_free_sam(&sam_pass);
1043 if (local_flags & LOCAL_SET_NO_PASSWORD) {
1044 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)|ACB_PWNOTREQ)) {
1045 slprintf(err_str, err_str_len-1, "Failed to set 'no password required' flag for user %s.\n", user_name);
1046 pdb_free_sam(&sam_pass);
1049 } else if (local_flags & LOCAL_SET_PASSWORD) {
1051 * If we're dealing with setting a completely empty user account
1052 * ie. One with a password of 'XXXX', but not set disabled (like
1053 * an account created from scratch) then if the old password was
1054 * 'XX's then getsmbpwent will have set the ACB_DISABLED flag.
1055 * We remove that as we're giving this user their first password
1056 * and the decision hasn't really been made to disable them (ie.
1057 * don't create them disabled). JRA.
1059 if ((pdb_get_lanman_passwd(sam_pass)==NULL) && (pdb_get_acct_ctrl(sam_pass)&ACB_DISABLED)) {
1060 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_DISABLED))) {
1061 slprintf(err_str, err_str_len-1, "Failed to unset 'disabled' flag for user %s.\n", user_name);
1062 pdb_free_sam(&sam_pass);
1066 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_PWNOTREQ))) {
1067 slprintf(err_str, err_str_len-1, "Failed to unset 'no password required' flag for user %s.\n", user_name);
1068 pdb_free_sam(&sam_pass);
1072 if (!pdb_set_plaintext_passwd (sam_pass, new_passwd)) {
1073 slprintf(err_str, err_str_len-1, "Failed to set password for user %s.\n", user_name);
1074 pdb_free_sam(&sam_pass);
1079 if (local_flags & LOCAL_ADD_USER) {
1080 if (pdb_add_sam_account(sam_pass)) {
1081 slprintf(msg_str, msg_str_len-1, "Added user %s.\n", user_name);
1082 pdb_free_sam(&sam_pass);
1085 slprintf(err_str, err_str_len-1, "Failed to add entry for user %s.\n", user_name);
1086 pdb_free_sam(&sam_pass);
1089 } else if (local_flags & LOCAL_DELETE_USER) {
1090 if (!pdb_delete_sam_account(sam_pass)) {
1091 slprintf(err_str,err_str_len-1, "Failed to delete entry for user %s.\n", user_name);
1092 pdb_free_sam(&sam_pass);
1095 slprintf(msg_str, msg_str_len-1, "Deleted user %s.\n", user_name);
1097 if(!pdb_update_sam_account(sam_pass)) {
1098 slprintf(err_str, err_str_len-1, "Failed to modify entry for user %s.\n", user_name);
1099 pdb_free_sam(&sam_pass);
1102 if(local_flags & LOCAL_DISABLE_USER)
1103 slprintf(msg_str, msg_str_len-1, "Disabled user %s.\n", user_name);
1104 else if (local_flags & LOCAL_ENABLE_USER)
1105 slprintf(msg_str, msg_str_len-1, "Enabled user %s.\n", user_name);
1106 else if (local_flags & LOCAL_SET_NO_PASSWORD)
1107 slprintf(msg_str, msg_str_len-1, "User %s password set to none.\n", user_name);
1110 pdb_free_sam(&sam_pass);