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
29 const char *get_global_sam_name(void)
31 if ((lp_server_role() == ROLE_DOMAIN_PDC) || (lp_server_role() == ROLE_DOMAIN_BDC)) {
32 return lp_workgroup();
34 return 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 /* no initial methods */
48 /* Don't change these timestamp settings without a good reason.
49 They are important for NT member server compatibility. */
51 user->private.uid = user->private.gid = -1;
53 user->private.logon_time = (time_t)0;
54 user->private.pass_last_set_time = (time_t)0;
55 user->private.pass_can_change_time = (time_t)0;
56 user->private.logoff_time =
57 user->private.kickoff_time =
58 user->private.pass_must_change_time = get_time_t_max();
59 user->private.unknown_3 = 0x00ffffff; /* don't know */
60 user->private.logon_divs = 168; /* hours per week */
61 user->private.hours_len = 21; /* 21 times 8 bits = 168 */
62 memset(user->private.hours, 0xff, user->private.hours_len); /* available at all hours */
63 user->private.unknown_5 = 0x00000000; /* don't know */
64 user->private.unknown_6 = 0x000004ec; /* don't know */
66 /* Some parts of samba strlen their pdb_get...() returns,
67 so this keeps the interface unchanged for now. */
69 user->private.username = "";
70 user->private.domain = "";
71 user->private.nt_username = "";
72 user->private.full_name = "";
73 user->private.home_dir = "";
74 user->private.logon_script = "";
75 user->private.profile_path = "";
76 user->private.acct_desc = "";
77 user->private.workstations = "";
78 user->private.unknown_str = "";
79 user->private.munged_dial = "";
81 user->private.plaintext_pw = NULL;
85 static void destroy_pdb_talloc(SAM_ACCOUNT **user)
88 data_blob_clear_free(&((*user)->private.lm_pw));
89 data_blob_clear_free(&((*user)->private.nt_pw));
91 if((*user)->private.plaintext_pw!=NULL)
92 memset((*user)->private.plaintext_pw,'\0',strlen((*user)->private.plaintext_pw));
93 talloc_destroy((*user)->mem_ctx);
99 /**********************************************************************
100 Alloc memory and initialises a struct sam_passwd on supplied mem_ctx.
101 ***********************************************************************/
103 NTSTATUS pdb_init_sam_talloc(TALLOC_CTX *mem_ctx, SAM_ACCOUNT **user)
106 DEBUG(0,("pdb_init_sam_talloc: SAM_ACCOUNT was non NULL\n"));
108 smb_panic("non-NULL pointer passed to pdb_init_sam\n");
110 return NT_STATUS_UNSUCCESSFUL;
114 DEBUG(0,("pdb_init_sam_talloc: mem_ctx was NULL!\n"));
115 return NT_STATUS_UNSUCCESSFUL;
118 *user=(SAM_ACCOUNT *)talloc(mem_ctx, sizeof(SAM_ACCOUNT));
121 DEBUG(0,("pdb_init_sam_talloc: error while allocating memory\n"));
122 return NT_STATUS_NO_MEMORY;
125 (*user)->mem_ctx = mem_ctx;
127 (*user)->free_fn = NULL;
129 pdb_fill_default_sam(*user);
135 /*************************************************************
136 Alloc memory and initialises a struct sam_passwd.
137 ************************************************************/
139 NTSTATUS pdb_init_sam(SAM_ACCOUNT **user)
144 mem_ctx = talloc_init("passdb internal SAM_ACCOUNT allocation");
147 DEBUG(0,("pdb_init_sam: error while doing talloc_init()\n"));
148 return NT_STATUS_NO_MEMORY;
151 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_talloc(mem_ctx, user))) {
152 talloc_destroy(mem_ctx);
156 (*user)->free_fn = destroy_pdb_talloc;
162 /*************************************************************
163 Initialises a struct sam_passwd with sane values.
164 ************************************************************/
166 NTSTATUS pdb_fill_sam_pw(SAM_ACCOUNT *sam_account, const struct passwd *pwd)
170 const char *guest_account = lp_guestaccount();
171 if (!(guest_account && *guest_account)) {
172 DEBUG(1, ("NULL guest account!?!?\n"));
173 return NT_STATUS_UNSUCCESSFUL;
177 return NT_STATUS_UNSUCCESSFUL;
180 pdb_fill_default_sam(sam_account);
182 pdb_set_username(sam_account, pwd->pw_name, PDB_SET);
183 pdb_set_fullname(sam_account, pwd->pw_gecos, PDB_SET);
185 pdb_set_unix_homedir(sam_account, pwd->pw_dir, PDB_SET);
187 pdb_set_domain (sam_account, get_global_sam_name(), PDB_DEFAULT);
189 pdb_set_uid(sam_account, pwd->pw_uid, PDB_SET);
190 pdb_set_gid(sam_account, pwd->pw_gid, PDB_SET);
192 /* When we get a proper uid -> SID and SID -> uid allocation
193 mechinism, we should call it here.
195 We can't just set this to 0 or allow it only to be filled
196 in when added to the backend, becouse the user's SID
197 may already be in security descriptors etc.
199 -- abartlet 11-May-02
203 /* Ensure this *must* be set right */
204 if (strcmp(pwd->pw_name, guest_account) == 0) {
205 if (!pdb_set_user_sid_from_rid(sam_account, DOMAIN_USER_RID_GUEST, PDB_SET)) {
206 return NT_STATUS_UNSUCCESSFUL;
208 if (!pdb_set_group_sid_from_rid(sam_account, DOMAIN_GROUP_RID_GUESTS, PDB_SET)) {
209 return NT_STATUS_UNSUCCESSFUL;
213 if (!pdb_set_user_sid_from_rid(sam_account,
214 fallback_pdb_uid_to_user_rid(pwd->pw_uid), PDB_SET)) {
215 DEBUG(0,("Can't set User SID from RID!\n"));
216 return NT_STATUS_INVALID_PARAMETER;
219 /* call the mapping code here */
220 if(pdb_getgrgid(&map, pwd->pw_gid, MAPPING_WITHOUT_PRIV)) {
221 if (!pdb_set_group_sid(sam_account,&map.sid, PDB_SET)){
222 DEBUG(0,("Can't set Group SID!\n"));
223 return NT_STATUS_INVALID_PARAMETER;
227 if (!pdb_set_group_sid_from_rid(sam_account,pdb_gid_to_group_rid(pwd->pw_gid), PDB_SET)) {
228 DEBUG(0,("Can't set Group SID\n"));
229 return NT_STATUS_INVALID_PARAMETER;
234 /* check if this is a user account or a machine account */
235 if (pwd->pw_name[strlen(pwd->pw_name)-1] != '$')
237 pdb_set_profile_path(sam_account,
238 talloc_sub_specified((sam_account)->mem_ctx,
240 pwd->pw_name, global_myname(),
241 pwd->pw_uid, pwd->pw_gid),
244 pdb_set_homedir(sam_account,
245 talloc_sub_specified((sam_account)->mem_ctx,
247 pwd->pw_name, global_myname(),
248 pwd->pw_uid, pwd->pw_gid),
251 pdb_set_dir_drive(sam_account,
252 talloc_sub_specified((sam_account)->mem_ctx,
254 pwd->pw_name, global_myname(),
255 pwd->pw_uid, pwd->pw_gid),
258 pdb_set_logon_script(sam_account,
259 talloc_sub_specified((sam_account)->mem_ctx,
261 pwd->pw_name, global_myname(),
262 pwd->pw_uid, pwd->pw_gid),
264 if (!pdb_set_acct_ctrl(sam_account, ACB_NORMAL, PDB_DEFAULT)) {
265 DEBUG(1, ("Failed to set 'normal account' flags for user %s.\n", pwd->pw_name));
266 return NT_STATUS_UNSUCCESSFUL;
269 if (!pdb_set_acct_ctrl(sam_account, ACB_WSTRUST, PDB_DEFAULT)) {
270 DEBUG(1, ("Failed to set 'trusted workstation account' flags for user %s.\n", pwd->pw_name));
271 return NT_STATUS_UNSUCCESSFUL;
278 /*************************************************************
279 Initialises a struct sam_passwd with sane values.
280 ************************************************************/
282 NTSTATUS pdb_init_sam_pw(SAM_ACCOUNT **new_sam_acct, const struct passwd *pwd)
288 return NT_STATUS_INVALID_PARAMETER;
291 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam(new_sam_acct))) {
296 if (!NT_STATUS_IS_OK(nt_status = pdb_fill_sam_pw(*new_sam_acct, pwd))) {
297 pdb_free_sam(new_sam_acct);
306 /*************************************************************
307 Initialises a SAM_ACCOUNT ready to add a new account, based
308 on the unix user if possible.
309 ************************************************************/
311 NTSTATUS pdb_init_sam_new(SAM_ACCOUNT **new_sam_acct, const char *username)
313 NTSTATUS nt_status = NT_STATUS_NO_MEMORY;
317 pwd = Get_Pwnam(username);
320 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_pw(new_sam_acct, pwd))) {
321 *new_sam_acct = NULL;
325 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam(new_sam_acct))) {
326 *new_sam_acct = NULL;
329 if (!pdb_set_username(*new_sam_acct, username, PDB_SET)) {
330 pdb_free_sam(new_sam_acct);
339 * Free the contets of the SAM_ACCOUNT, but not the structure.
341 * Also wipes the LM and NT hashes and plaintext password from
344 * @param user SAM_ACCOUNT to free members of.
347 static void pdb_free_sam_contents(SAM_ACCOUNT *user)
350 /* Kill off sensitive data. Free()ed by the
353 data_blob_clear_free(&(user->private.lm_pw));
354 data_blob_clear_free(&(user->private.nt_pw));
355 if (user->private.plaintext_pw!=NULL)
356 memset(user->private.plaintext_pw,'\0',strlen(user->private.plaintext_pw));
360 /************************************************************
361 Reset the SAM_ACCOUNT and free the NT/LM hashes.
362 ***********************************************************/
364 NTSTATUS pdb_reset_sam(SAM_ACCOUNT *user)
367 DEBUG(0,("pdb_reset_sam: SAM_ACCOUNT was NULL\n"));
369 smb_panic("NULL pointer passed to pdb_free_sam\n");
371 return NT_STATUS_UNSUCCESSFUL;
374 pdb_free_sam_contents(user);
376 pdb_fill_default_sam(user);
382 /************************************************************
383 Free the SAM_ACCOUNT and the member pointers.
384 ***********************************************************/
386 NTSTATUS pdb_free_sam(SAM_ACCOUNT **user)
389 DEBUG(0,("pdb_free_sam: SAM_ACCOUNT was NULL\n"));
391 smb_panic("NULL pointer passed to pdb_free_sam\n");
393 return NT_STATUS_UNSUCCESSFUL;
396 pdb_free_sam_contents(*user);
398 if ((*user)->free_fn) {
399 (*user)->free_fn(user);
406 /**********************************************************
407 Encode the account control bits into a string.
408 length = length of string to encode into (including terminating
409 null). length *MUST BE MORE THAN 2* !
410 **********************************************************/
412 char *pdb_encode_acct_ctrl(uint16 acct_ctrl, size_t length)
414 static fstring acct_str;
419 if (acct_ctrl & ACB_PWNOTREQ ) acct_str[i++] = 'N';
420 if (acct_ctrl & ACB_DISABLED ) acct_str[i++] = 'D';
421 if (acct_ctrl & ACB_HOMDIRREQ) acct_str[i++] = 'H';
422 if (acct_ctrl & ACB_TEMPDUP ) acct_str[i++] = 'T';
423 if (acct_ctrl & ACB_NORMAL ) acct_str[i++] = 'U';
424 if (acct_ctrl & ACB_MNS ) acct_str[i++] = 'M';
425 if (acct_ctrl & ACB_WSTRUST ) acct_str[i++] = 'W';
426 if (acct_ctrl & ACB_SVRTRUST ) acct_str[i++] = 'S';
427 if (acct_ctrl & ACB_AUTOLOCK ) acct_str[i++] = 'L';
428 if (acct_ctrl & ACB_PWNOEXP ) acct_str[i++] = 'X';
429 if (acct_ctrl & ACB_DOMTRUST ) acct_str[i++] = 'I';
431 for ( ; i < length - 2 ; i++ )
436 acct_str[i++] = '\0';
441 /**********************************************************
442 Decode the account control bits from a string.
443 **********************************************************/
445 uint16 pdb_decode_acct_ctrl(const char *p)
447 uint16 acct_ctrl = 0;
448 BOOL finished = False;
451 * Check if the account type bits have been encoded after the
452 * NT password (in the form [NDHTUWSLXI]).
458 for (p++; *p && !finished; p++) {
460 case 'N': { acct_ctrl |= ACB_PWNOTREQ ; break; /* 'N'o password. */ }
461 case 'D': { acct_ctrl |= ACB_DISABLED ; break; /* 'D'isabled. */ }
462 case 'H': { acct_ctrl |= ACB_HOMDIRREQ; break; /* 'H'omedir required. */ }
463 case 'T': { acct_ctrl |= ACB_TEMPDUP ; break; /* 'T'emp account. */ }
464 case 'U': { acct_ctrl |= ACB_NORMAL ; break; /* 'U'ser account (normal). */ }
465 case 'M': { acct_ctrl |= ACB_MNS ; break; /* 'M'NS logon user account. What is this ? */ }
466 case 'W': { acct_ctrl |= ACB_WSTRUST ; break; /* 'W'orkstation account. */ }
467 case 'S': { acct_ctrl |= ACB_SVRTRUST ; break; /* 'S'erver account. */ }
468 case 'L': { acct_ctrl |= ACB_AUTOLOCK ; break; /* 'L'ocked account. */ }
469 case 'X': { acct_ctrl |= ACB_PWNOEXP ; break; /* No 'X'piry on password */ }
470 case 'I': { acct_ctrl |= ACB_DOMTRUST ; break; /* 'I'nterdomain trust account. */ }
476 default: { finished = True; }
483 /*************************************************************
484 Routine to set 32 hex password characters from a 16 byte array.
485 **************************************************************/
487 void pdb_sethexpwd(char *p, const unsigned char *pwd, uint16 acct_ctrl)
491 for (i = 0; i < 16; i++)
492 slprintf(&p[i*2], 3, "%02X", pwd[i]);
494 if (acct_ctrl & ACB_PWNOTREQ)
495 safe_strcpy(p, "NO PASSWORDXXXXXXXXXXXXXXXXXXXXX", 33);
497 safe_strcpy(p, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 33);
501 /*************************************************************
502 Routine to get the 32 hex characters and turn them
503 into a 16 byte array.
504 **************************************************************/
506 BOOL pdb_gethexpwd(const char *p, unsigned char *pwd)
509 unsigned char lonybble, hinybble;
510 const char *hexchars = "0123456789ABCDEF";
516 for (i = 0; i < 32; i += 2) {
517 hinybble = toupper(p[i]);
518 lonybble = toupper(p[i + 1]);
520 p1 = strchr(hexchars, hinybble);
521 p2 = strchr(hexchars, lonybble);
526 hinybble = PTR_DIFF(p1, hexchars);
527 lonybble = PTR_DIFF(p2, hexchars);
529 pwd[i / 2] = (hinybble << 4) | lonybble;
534 /*******************************************************************
535 Converts NT user RID to a UNIX uid.
536 ********************************************************************/
538 static int algorithmic_rid_base(void)
540 static int rid_offset = 0;
545 rid_offset = lp_algorithmic_rid_base();
547 if (rid_offset < BASE_RID) {
548 /* Try to prevent admin foot-shooting, we can't put algorithmic
549 rids below 1000, that's the 'well known RIDs' on NT */
550 DEBUG(0, ("'algorithmic rid base' must be equal to or above %ld\n", BASE_RID));
551 rid_offset = BASE_RID;
553 if (rid_offset & 1) {
554 DEBUG(0, ("algorithmic rid base must be even\n"));
561 uid_t fallback_pdb_user_rid_to_uid(uint32 user_rid)
563 int rid_offset = algorithmic_rid_base();
564 return (uid_t)(((user_rid & (~USER_RID_TYPE))- rid_offset)/RID_MULTIPLIER);
568 /*******************************************************************
569 converts UNIX uid to an NT User RID.
570 ********************************************************************/
572 uint32 fallback_pdb_uid_to_user_rid(uid_t uid)
574 int rid_offset = algorithmic_rid_base();
575 return (((((uint32)uid)*RID_MULTIPLIER) + rid_offset) | USER_RID_TYPE);
578 /*******************************************************************
579 Converts NT group RID to a UNIX gid.
580 ********************************************************************/
582 gid_t pdb_group_rid_to_gid(uint32 group_rid)
584 int rid_offset = algorithmic_rid_base();
585 return (gid_t)(((group_rid & (~GROUP_RID_TYPE))- rid_offset)/RID_MULTIPLIER);
588 /*******************************************************************
589 converts NT Group RID to a UNIX uid.
591 warning: you must not call that function only
592 you must do a call to the group mapping first.
593 there is not anymore a direct link between the gid and the rid.
594 ********************************************************************/
596 uint32 pdb_gid_to_group_rid(gid_t gid)
598 int rid_offset = algorithmic_rid_base();
599 return (((((uint32)gid)*RID_MULTIPLIER) + rid_offset) | GROUP_RID_TYPE);
602 /*******************************************************************
603 Decides if a RID is a well known RID.
604 ********************************************************************/
606 static BOOL pdb_rid_is_well_known(uint32 rid)
608 /* Not using rid_offset here, becouse this is the actual
609 NT fixed value (1000) */
611 return (rid < BASE_RID);
614 /*******************************************************************
615 Decides if a RID is a user or group RID.
616 ********************************************************************/
618 BOOL pdb_rid_is_user(uint32 rid)
620 /* lkcl i understand that NT attaches an enumeration to a RID
621 * such that it can be identified as either a user, group etc
622 * type. there are 5 such categories, and they are documented.
624 /* However, they are not in the RID, just somthing you can query
625 seperatly. Sorry luke :-) */
627 if(pdb_rid_is_well_known(rid)) {
629 * The only well known user RIDs are DOMAIN_USER_RID_ADMIN
630 * and DOMAIN_USER_RID_GUEST.
632 if(rid == DOMAIN_USER_RID_ADMIN || rid == DOMAIN_USER_RID_GUEST)
634 } else if((rid & RID_TYPE_MASK) == USER_RID_TYPE) {
640 /*******************************************************************
641 Convert a rid into a name. Used in the lookup SID rpc.
642 ********************************************************************/
644 BOOL local_lookup_sid(DOM_SID *sid, char *name, enum SID_NAME_USE *psid_name_use)
647 SAM_ACCOUNT *sam_account = NULL;
650 if (!sid_peek_check_rid(get_global_sam_sid(), sid, &rid)){
651 DEBUG(0,("local_sid_to_gid: sid_peek_check_rid return False! SID: %s\n",
652 sid_string_static(&map.sid)));
655 *psid_name_use = SID_NAME_UNKNOWN;
657 DEBUG(5,("local_lookup_sid: looking up RID %u.\n", (unsigned int)rid));
659 if (rid == DOMAIN_USER_RID_ADMIN) {
660 const char **admin_list = lp_admin_users(-1);
661 *psid_name_use = SID_NAME_USER;
663 const char *p = *admin_list;
664 if(!next_token(&p, name, NULL, sizeof(fstring)))
665 fstrcpy(name, "Administrator");
667 fstrcpy(name, "Administrator");
673 * Don't try to convert the rid to a name if
674 * running in appliance mode
677 if (lp_hide_local_users())
680 if (!NT_STATUS_IS_OK(pdb_init_sam(&sam_account))) {
684 /* see if the passdb can help us with the name of the user */
685 if (pdb_getsampwsid(sam_account, sid)) {
686 fstrcpy(name, pdb_get_username(sam_account));
687 *psid_name_use = SID_NAME_USER;
689 pdb_free_sam(&sam_account);
694 pdb_free_sam(&sam_account);
696 if (pdb_getgrsid(&map, *sid, MAPPING_WITHOUT_PRIV)) {
697 if (map.gid!=(gid_t)-1) {
698 DEBUG(5,("local_lookup_sid: mapped group %s to gid %u\n", map.nt_name, (unsigned int)map.gid));
700 DEBUG(5,("local_lookup_sid: mapped group %s to no unix gid. Returning name.\n", map.nt_name));
703 fstrcpy(name, map.nt_name);
704 *psid_name_use = map.sid_name_use;
708 if (pdb_rid_is_user(rid)) {
711 DEBUG(5, ("assuming RID %u is a user\n", (unsigned)rid));
713 uid = fallback_pdb_user_rid_to_uid(rid);
714 slprintf(name, sizeof(fstring)-1, "unix_user.%u", (unsigned int)uid);
716 return False; /* Indicates that this user was 'not mapped' */
721 DEBUG(5, ("assuming RID %u is a group\n", (unsigned)rid));
723 gid = pdb_group_rid_to_gid(rid);
726 *psid_name_use = SID_NAME_ALIAS;
728 DEBUG(5,("local_lookup_sid: looking up gid %u %s\n", (unsigned int)gid,
729 gr ? "succeeded" : "failed" ));
732 slprintf(name, sizeof(fstring)-1, "unix_group.%u", (unsigned int)gid);
733 return False; /* Indicates that this group was 'not mapped' */
736 fstrcpy( name, gr->gr_name);
738 DEBUG(5,("local_lookup_sid: found group %s for rid %u\n", name,
739 (unsigned int)rid ));
744 /*******************************************************************
745 Convert a name into a SID. Used in the lookup name rpc.
746 ********************************************************************/
748 BOOL local_lookup_name(const char *c_user, DOM_SID *psid, enum SID_NAME_USE *psid_name_use)
750 extern DOM_SID global_sid_World_Domain;
753 SAM_ACCOUNT *sam_account = NULL;
757 *psid_name_use = SID_NAME_UNKNOWN;
760 * user may be quoted a const string, and map_username and
761 * friends can modify it. Make a modifiable copy. JRA.
764 fstrcpy(user, c_user);
766 sid_copy(&local_sid, get_global_sam_sid());
769 * Special case for MACHINE\Everyone. Map to the world_sid.
772 if(strequal(user, "Everyone")) {
773 sid_copy( psid, &global_sid_World_Domain);
774 sid_append_rid(psid, 0);
775 *psid_name_use = SID_NAME_ALIAS;
780 * Don't lookup local unix users if running in appliance mode
782 if (lp_hide_local_users())
785 (void)map_username(user);
787 if (!NT_STATUS_IS_OK(pdb_init_sam(&sam_account))) {
791 if (pdb_getsampwnam(sam_account, user)) {
792 sid_copy(psid, pdb_get_user_sid(sam_account));
793 *psid_name_use = SID_NAME_USER;
795 pdb_free_sam(&sam_account);
799 pdb_free_sam(&sam_account);
802 * Maybe it was a group ?
805 /* check if it's a mapped group */
806 if (pdb_getgrnam(&map, user, MAPPING_WITHOUT_PRIV)) {
807 /* yes it's a mapped group */
808 sid_copy(&local_sid, &map.sid);
809 *psid_name_use = map.sid_name_use;
811 /* it's not a mapped group */
812 grp = getgrnam(user);
817 *check if it's mapped, if it is reply it doesn't exist
819 * that's to prevent this case:
821 * unix group ug is mapped to nt group ng
822 * someone does a lookup on ug
823 * we must not reply as it doesn't "exist" anymore
824 * for NT. For NT only ng exists.
828 if (pdb_getgrgid(&map, grp->gr_gid, MAPPING_WITHOUT_PRIV)){
832 sid_append_rid( &local_sid, pdb_gid_to_group_rid(grp->gr_gid));
833 *psid_name_use = SID_NAME_ALIAS;
836 sid_copy( psid, &local_sid);
841 /****************************************************************************
842 Convert a uid to SID - locally.
843 ****************************************************************************/
845 DOM_SID *local_uid_to_sid(DOM_SID *psid, uid_t uid)
848 SAM_ACCOUNT *sam_user = NULL;
849 fstring str; /* sid string buffer */
851 sid_copy(psid, get_global_sam_sid());
853 if((pass = getpwuid_alloc(uid))) {
855 if (NT_STATUS_IS_ERR(pdb_init_sam(&sam_user))) {
860 if (pdb_getsampwnam(sam_user, pass->pw_name)) {
861 sid_copy(psid, pdb_get_user_sid(sam_user));
863 sid_append_rid(psid, fallback_pdb_uid_to_user_rid(uid));
866 DEBUG(10,("local_uid_to_sid: uid %u -> SID (%s) (%s).\n",
867 (unsigned)uid, sid_to_string( str, psid),
871 pdb_free_sam(&sam_user);
874 sid_append_rid(psid, fallback_pdb_uid_to_user_rid(uid));
876 DEBUG(10,("local_uid_to_sid: uid %u -> SID (%s) (unknown user).\n",
877 (unsigned)uid, sid_to_string( str, psid)));
883 /****************************************************************************
884 Convert a SID to uid - locally.
885 ****************************************************************************/
887 BOOL local_sid_to_uid(uid_t *puid, const DOM_SID *psid, enum SID_NAME_USE *name_type)
890 SAM_ACCOUNT *sam_user = NULL;
892 *name_type = SID_NAME_UNKNOWN;
894 if (NT_STATUS_IS_ERR(pdb_init_sam(&sam_user)))
897 if (pdb_getsampwsid(sam_user, psid)) {
899 if (!IS_SAM_SET(sam_user,PDB_UID)&&!IS_SAM_CHANGED(sam_user,PDB_UID)) {
900 pdb_free_sam(&sam_user);
904 *puid = pdb_get_uid(sam_user);
906 DEBUG(10,("local_sid_to_uid: SID %s -> uid (%u) (%s).\n", sid_to_string( str, psid),
907 (unsigned int)*puid, pdb_get_username(sam_user)));
908 pdb_free_sam(&sam_user);
915 pdb_free_sam(&sam_user);
917 if (pdb_getgrsid(&map, *psid, MAPPING_WITHOUT_PRIV)) {
918 DEBUG(3, ("local_sid_to_uid: SID '%s' is a group, not a user... \n", sid_to_string(str, psid)));
919 /* It's a group, not a user... */
923 sid_copy(&dom_sid, psid);
924 if (!sid_peek_check_rid(get_global_sam_sid(), psid, &rid)) {
925 DEBUG(3, ("sid_peek_rid failed - sid '%s' is not in our domain\n", sid_to_string(str, psid)));
929 if (!pdb_rid_is_user(rid)) {
930 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)));
934 *puid = fallback_pdb_user_rid_to_uid(rid);
936 DEBUG(5,("local_sid_to_uid: SID %s algorithmicly mapped to %ld mapped becouse SID was not found in passdb.\n",
937 sid_to_string(str, psid), (signed long int)(*puid)));
940 *name_type = SID_NAME_USER;
945 /****************************************************************************
946 Convert a gid to SID - locally.
947 ****************************************************************************/
949 DOM_SID *local_gid_to_sid(DOM_SID *psid, gid_t gid)
953 sid_copy(psid, get_global_sam_sid());
955 if (pdb_getgrgid(&map, gid, MAPPING_WITHOUT_PRIV)) {
956 sid_copy(psid, &map.sid);
959 sid_append_rid(psid, pdb_gid_to_group_rid(gid));
965 /****************************************************************************
966 Convert a SID to gid - locally.
967 ****************************************************************************/
969 BOOL local_sid_to_gid(gid_t *pgid, const DOM_SID *psid, enum SID_NAME_USE *name_type)
974 *name_type = SID_NAME_UNKNOWN;
977 * We can only convert to a gid if this is our local
978 * Domain SID (ie. we are the controling authority).
980 * Or in the Builtin SID too. JFM, 11/30/2001
983 if (pdb_getgrsid(&map, *psid, MAPPING_WITHOUT_PRIV)) {
985 /* the SID is in the mapping table but not mapped */
986 if (map.gid==(gid_t)-1)
990 *name_type = map.sid_name_use;
991 DEBUG(10,("local_sid_to_gid: mapped SID %s (%s) -> gid (%u).\n",
992 sid_to_string( str, psid),
993 map.nt_name, (unsigned int)*pgid));
997 SAM_ACCOUNT *sam_user = NULL;
998 if (NT_STATUS_IS_ERR(pdb_init_sam(&sam_user)))
1001 if (pdb_getsampwsid(sam_user, psid)) {
1003 pdb_free_sam(&sam_user);
1006 pdb_free_sam(&sam_user);
1008 if (!sid_peek_check_rid(get_global_sam_sid(), psid, &rid)) {
1009 DEBUG(3, ("sid_peek_rid failed - sid '%s' is not in our domain\n", sid_to_string(str, psid)));
1013 if (pdb_rid_is_user(rid))
1016 *pgid = pdb_group_rid_to_gid(rid);
1017 *name_type = SID_NAME_ALIAS;
1018 DEBUG(10,("local_sid_to_gid: SID %s -> gid (%u).\n", sid_to_string( str, psid),
1019 (unsigned int)*pgid));
1025 /*************************************************************
1026 Change a password entry in the local smbpasswd file.
1028 It is currently being called by SWAT and by smbpasswd.
1031 *************************************************************/
1033 BOOL local_password_change(const char *user_name, int local_flags,
1034 const char *new_passwd,
1035 char *err_str, size_t err_str_len,
1036 char *msg_str, size_t msg_str_len)
1038 SAM_ACCOUNT *sam_pass=NULL;
1044 /* Get the smb passwd entry for this user */
1045 pdb_init_sam(&sam_pass);
1046 if(!pdb_getsampwnam(sam_pass, user_name)) {
1047 pdb_free_sam(&sam_pass);
1049 if ((local_flags & LOCAL_ADD_USER) || (local_flags & LOCAL_DELETE_USER)) {
1050 /* Might not exist in /etc/passwd */
1051 if (!NT_STATUS_IS_OK(pdb_init_sam_new(&sam_pass, user_name))) {
1052 slprintf(err_str, err_str_len-1, "Failed initialise SAM_ACCOUNT for user %s.\n", user_name);
1056 slprintf(err_str, err_str_len-1,"Failed to find entry for user %s.\n", user_name);
1060 /* the entry already existed */
1061 local_flags &= ~LOCAL_ADD_USER;
1064 /* the 'other' acb bits not being changed here */
1065 other_acb = (pdb_get_acct_ctrl(sam_pass) & (!(ACB_WSTRUST|ACB_DOMTRUST|ACB_SVRTRUST|ACB_NORMAL)));
1066 if (local_flags & LOCAL_TRUST_ACCOUNT) {
1067 if (!pdb_set_acct_ctrl(sam_pass, ACB_WSTRUST | other_acb, PDB_CHANGED) ) {
1068 slprintf(err_str, err_str_len - 1, "Failed to set 'trusted workstation account' flags for user %s.\n", user_name);
1069 pdb_free_sam(&sam_pass);
1072 } else if (local_flags & LOCAL_INTERDOM_ACCOUNT) {
1073 if (!pdb_set_acct_ctrl(sam_pass, ACB_DOMTRUST | other_acb, PDB_CHANGED)) {
1074 slprintf(err_str, err_str_len - 1, "Failed to set 'domain trust account' flags for user %s.\n", user_name);
1075 pdb_free_sam(&sam_pass);
1079 if (!pdb_set_acct_ctrl(sam_pass, ACB_NORMAL | other_acb, PDB_CHANGED)) {
1080 slprintf(err_str, err_str_len - 1, "Failed to set 'normal account' flags for user %s.\n", user_name);
1081 pdb_free_sam(&sam_pass);
1087 * We are root - just write the new password
1088 * and the valid last change time.
1091 if (local_flags & LOCAL_DISABLE_USER) {
1092 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)|ACB_DISABLED, PDB_CHANGED)) {
1093 slprintf(err_str, err_str_len-1, "Failed to set 'disabled' flag for user %s.\n", user_name);
1094 pdb_free_sam(&sam_pass);
1097 } else if (local_flags & LOCAL_ENABLE_USER) {
1098 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_DISABLED), PDB_CHANGED)) {
1099 slprintf(err_str, err_str_len-1, "Failed to unset 'disabled' flag for user %s.\n", user_name);
1100 pdb_free_sam(&sam_pass);
1105 if (local_flags & LOCAL_SET_NO_PASSWORD) {
1106 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)|ACB_PWNOTREQ, PDB_CHANGED)) {
1107 slprintf(err_str, err_str_len-1, "Failed to set 'no password required' flag for user %s.\n", user_name);
1108 pdb_free_sam(&sam_pass);
1111 } else if (local_flags & LOCAL_SET_PASSWORD) {
1113 * If we're dealing with setting a completely empty user account
1114 * ie. One with a password of 'XXXX', but not set disabled (like
1115 * an account created from scratch) then if the old password was
1116 * 'XX's then getsmbpwent will have set the ACB_DISABLED flag.
1117 * We remove that as we're giving this user their first password
1118 * and the decision hasn't really been made to disable them (ie.
1119 * don't create them disabled). JRA.
1121 if ((pdb_get_lanman_passwd(sam_pass)==NULL) && (pdb_get_acct_ctrl(sam_pass)&ACB_DISABLED)) {
1122 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_DISABLED), PDB_CHANGED)) {
1123 slprintf(err_str, err_str_len-1, "Failed to unset 'disabled' flag for user %s.\n", user_name);
1124 pdb_free_sam(&sam_pass);
1128 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_PWNOTREQ), PDB_CHANGED)) {
1129 slprintf(err_str, err_str_len-1, "Failed to unset 'no password required' flag for user %s.\n", user_name);
1130 pdb_free_sam(&sam_pass);
1134 if (!pdb_set_plaintext_passwd (sam_pass, new_passwd)) {
1135 slprintf(err_str, err_str_len-1, "Failed to set password for user %s.\n", user_name);
1136 pdb_free_sam(&sam_pass);
1141 if (local_flags & LOCAL_ADD_USER) {
1142 if (pdb_add_sam_account(sam_pass)) {
1143 slprintf(msg_str, msg_str_len-1, "Added user %s.\n", user_name);
1144 pdb_free_sam(&sam_pass);
1147 slprintf(err_str, err_str_len-1, "Failed to add entry for user %s.\n", user_name);
1148 pdb_free_sam(&sam_pass);
1151 } else if (local_flags & LOCAL_DELETE_USER) {
1152 if (!pdb_delete_sam_account(sam_pass)) {
1153 slprintf(err_str,err_str_len-1, "Failed to delete entry for user %s.\n", user_name);
1154 pdb_free_sam(&sam_pass);
1157 slprintf(msg_str, msg_str_len-1, "Deleted user %s.\n", user_name);
1159 if(!pdb_update_sam_account(sam_pass)) {
1160 slprintf(err_str, err_str_len-1, "Failed to modify entry for user %s.\n", user_name);
1161 pdb_free_sam(&sam_pass);
1164 if(local_flags & LOCAL_DISABLE_USER)
1165 slprintf(msg_str, msg_str_len-1, "Disabled user %s.\n", user_name);
1166 else if (local_flags & LOCAL_ENABLE_USER)
1167 slprintf(msg_str, msg_str_len-1, "Enabled user %s.\n", user_name);
1168 else if (local_flags & LOCAL_SET_NO_PASSWORD)
1169 slprintf(msg_str, msg_str_len-1, "User %s password set to none.\n", user_name);
1172 pdb_free_sam(&sam_pass);