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 * This is set on startup - it defines the SID for this
28 * machine, and therefore the SAM database for which it is
32 extern DOM_SID global_sam_sid;
34 /************************************************************
35 Fill the SAM_ACCOUNT with default values.
36 ***********************************************************/
38 static void pdb_fill_default_sam(SAM_ACCOUNT *user)
40 ZERO_STRUCT(user->private); /* Don't touch the talloc context */
42 /* Don't change these timestamp settings without a good reason.
43 They are important for NT member server compatibility. */
45 user->private.init_flag = FLAG_SAM_UNINIT;
46 user->private.uid = user->private.gid = -1;
48 user->private.logon_time = (time_t)0;
49 user->private.pass_last_set_time = (time_t)0;
50 user->private.pass_can_change_time = (time_t)0;
51 user->private.logoff_time =
52 user->private.kickoff_time =
53 user->private.pass_must_change_time = get_time_t_max();
54 user->private.unknown_3 = 0x00ffffff; /* don't know */
55 user->private.logon_divs = 168; /* hours per week */
56 user->private.hours_len = 21; /* 21 times 8 bits = 168 */
57 memset(user->private.hours, 0xff, user->private.hours_len); /* available at all hours */
58 user->private.unknown_5 = 0x00000000; /* don't know */
59 user->private.unknown_6 = 0x000004ec; /* don't know */
61 /* Some parts of samba strlen their pdb_get...() returns,
62 so this keeps the interface unchanged for now. */
64 user->private.username = "";
65 user->private.domain = "";
66 user->private.nt_username = "";
67 user->private.full_name = "";
68 user->private.home_dir = "";
69 user->private.logon_script = "";
70 user->private.profile_path = "";
71 user->private.acct_desc = "";
72 user->private.workstations = "";
73 user->private.unknown_str = "";
74 user->private.munged_dial = "";
77 static void destroy_pdb_talloc(SAM_ACCOUNT **user)
80 talloc_destroy((*user)->mem_ctx);
86 /**********************************************************************
87 Alloc memory and initialises a struct sam_passwd on supplied mem_ctx.
88 ***********************************************************************/
90 NTSTATUS pdb_init_sam_talloc(TALLOC_CTX *mem_ctx, SAM_ACCOUNT **user)
93 DEBUG(0,("pdb_init_sam: SAM_ACCOUNT was non NULL\n"));
95 smb_panic("non-NULL pointer passed to pdb_init_sam\n");
97 return NT_STATUS_UNSUCCESSFUL;
101 DEBUG(0,("pdb_init_sam_talloc: mem_ctx was NULL!\n"));
102 return NT_STATUS_UNSUCCESSFUL;
105 *user=(SAM_ACCOUNT *)talloc(mem_ctx, sizeof(SAM_ACCOUNT));
108 DEBUG(0,("pdb_init_sam: error while allocating memory\n"));
109 return NT_STATUS_NO_MEMORY;
112 (*user)->mem_ctx = mem_ctx;
114 (*user)->free_fn = NULL;
116 pdb_fill_default_sam(*user);
122 /*************************************************************
123 Alloc memory and initialises a struct sam_passwd.
124 ************************************************************/
126 NTSTATUS pdb_init_sam(SAM_ACCOUNT **user)
131 mem_ctx = talloc_init_named("passdb internal SAM_ACCOUNT allocation");
134 DEBUG(0,("pdb_init_sam: error while doing talloc_init()\n"));
135 return NT_STATUS_NO_MEMORY;
138 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_talloc(mem_ctx, user))) {
139 talloc_destroy(mem_ctx);
143 (*user)->free_fn = destroy_pdb_talloc;
149 /*************************************************************
150 Initialises a struct sam_passwd with sane values.
151 ************************************************************/
153 NTSTATUS pdb_init_sam_pw(SAM_ACCOUNT **new_sam_acct, const struct passwd *pwd)
162 return NT_STATUS_UNSUCCESSFUL;
165 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam(new_sam_acct))) {
170 pdb_set_username(*new_sam_acct, pwd->pw_name);
171 pdb_set_fullname(*new_sam_acct, pwd->pw_gecos);
173 pdb_set_uid(*new_sam_acct, pwd->pw_uid);
174 pdb_set_gid(*new_sam_acct, pwd->pw_gid);
176 /* let the backends set the rid!!
177 pdb_set_user_rid(*new_sam_acct, pdb_uid_to_user_rid(pwd->pw_uid));
180 /* call the mapping code here */
181 if(get_group_map_from_gid(pwd->pw_gid, &map, MAPPING_WITHOUT_PRIV)) {
182 sid_peek_rid(&map.sid, &rid);
185 rid=pdb_gid_to_group_rid(pwd->pw_gid);
188 pdb_set_group_rid(*new_sam_acct, rid);
190 pstrcpy(str, lp_logon_path());
191 standard_sub_advanced(-1, pwd->pw_name, "", pwd->pw_gid, pwd->pw_name, str);
192 pdb_set_profile_path(*new_sam_acct, str, False);
194 pstrcpy(str, lp_logon_home());
195 standard_sub_advanced(-1, pwd->pw_name, "", pwd->pw_gid, pwd->pw_name, str);
196 pdb_set_homedir(*new_sam_acct, str, False);
198 pstrcpy(str, lp_logon_drive());
199 standard_sub_advanced(-1, pwd->pw_name, "", pwd->pw_gid, pwd->pw_name, str);
200 pdb_set_dir_drive(*new_sam_acct, str, False);
202 pstrcpy(str, lp_logon_script());
203 standard_sub_advanced(-1, pwd->pw_name, "", pwd->pw_gid, pwd->pw_name, str);
204 pdb_set_logon_script(*new_sam_acct, str, False);
211 * Free the contets of the SAM_ACCOUNT, but not the structure.
213 * Also wipes the LM and NT hashes from memory.
215 * @param user SAM_ACCOUNT to free members of.
218 static void pdb_free_sam_contents(SAM_ACCOUNT *user)
220 /* As we start mallocing more strings this is where
221 we should free them. */
223 data_blob_clear_free(&(user->private.lm_pw));
224 data_blob_clear_free(&(user->private.nt_pw));
228 /************************************************************
229 Reset the SAM_ACCOUNT and free the NT/LM hashes.
230 ***********************************************************/
232 NTSTATUS pdb_reset_sam(SAM_ACCOUNT *user)
235 DEBUG(0,("pdb_reset_sam: SAM_ACCOUNT was NULL\n"));
237 smb_panic("NULL pointer passed to pdb_free_sam\n");
239 return NT_STATUS_UNSUCCESSFUL;
242 pdb_free_sam_contents(user);
244 pdb_fill_default_sam(user);
250 /************************************************************
251 Free the SAM_ACCOUNT and the member pointers.
252 ***********************************************************/
254 NTSTATUS pdb_free_sam(SAM_ACCOUNT **user)
257 DEBUG(0,("pdb_free_sam: SAM_ACCOUNT was NULL\n"));
259 smb_panic("NULL pointer passed to pdb_free_sam\n");
261 return NT_STATUS_UNSUCCESSFUL;
264 pdb_free_sam_contents(*user);
266 if ((*user)->free_fn) {
267 (*user)->free_fn(user);
274 /**********************************************************
275 Encode the account control bits into a string.
276 length = length of string to encode into (including terminating
277 null). length *MUST BE MORE THAN 2* !
278 **********************************************************/
280 char *pdb_encode_acct_ctrl(uint16 acct_ctrl, size_t length)
282 static fstring acct_str;
287 if (acct_ctrl & ACB_PWNOTREQ ) acct_str[i++] = 'N';
288 if (acct_ctrl & ACB_DISABLED ) acct_str[i++] = 'D';
289 if (acct_ctrl & ACB_HOMDIRREQ) acct_str[i++] = 'H';
290 if (acct_ctrl & ACB_TEMPDUP ) acct_str[i++] = 'T';
291 if (acct_ctrl & ACB_NORMAL ) acct_str[i++] = 'U';
292 if (acct_ctrl & ACB_MNS ) acct_str[i++] = 'M';
293 if (acct_ctrl & ACB_WSTRUST ) acct_str[i++] = 'W';
294 if (acct_ctrl & ACB_SVRTRUST ) acct_str[i++] = 'S';
295 if (acct_ctrl & ACB_AUTOLOCK ) acct_str[i++] = 'L';
296 if (acct_ctrl & ACB_PWNOEXP ) acct_str[i++] = 'X';
297 if (acct_ctrl & ACB_DOMTRUST ) acct_str[i++] = 'I';
299 for ( ; i < length - 2 ; i++ )
304 acct_str[i++] = '\0';
309 /**********************************************************
310 Decode the account control bits from a string.
311 **********************************************************/
313 uint16 pdb_decode_acct_ctrl(const char *p)
315 uint16 acct_ctrl = 0;
316 BOOL finished = False;
319 * Check if the account type bits have been encoded after the
320 * NT password (in the form [NDHTUWSLXI]).
326 for (p++; *p && !finished; p++) {
328 case 'N': { acct_ctrl |= ACB_PWNOTREQ ; break; /* 'N'o password. */ }
329 case 'D': { acct_ctrl |= ACB_DISABLED ; break; /* 'D'isabled. */ }
330 case 'H': { acct_ctrl |= ACB_HOMDIRREQ; break; /* 'H'omedir required. */ }
331 case 'T': { acct_ctrl |= ACB_TEMPDUP ; break; /* 'T'emp account. */ }
332 case 'U': { acct_ctrl |= ACB_NORMAL ; break; /* 'U'ser account (normal). */ }
333 case 'M': { acct_ctrl |= ACB_MNS ; break; /* 'M'NS logon user account. What is this ? */ }
334 case 'W': { acct_ctrl |= ACB_WSTRUST ; break; /* 'W'orkstation account. */ }
335 case 'S': { acct_ctrl |= ACB_SVRTRUST ; break; /* 'S'erver account. */ }
336 case 'L': { acct_ctrl |= ACB_AUTOLOCK ; break; /* 'L'ocked account. */ }
337 case 'X': { acct_ctrl |= ACB_PWNOEXP ; break; /* No 'X'piry on password */ }
338 case 'I': { acct_ctrl |= ACB_DOMTRUST ; break; /* 'I'nterdomain trust account. */ }
344 default: { finished = True; }
351 /*************************************************************
352 Routine to set 32 hex password characters from a 16 byte array.
353 **************************************************************/
355 void pdb_sethexpwd(char *p, const unsigned char *pwd, uint16 acct_ctrl)
359 for (i = 0; i < 16; i++)
360 slprintf(&p[i*2], 3, "%02X", pwd[i]);
362 if (acct_ctrl & ACB_PWNOTREQ)
363 safe_strcpy(p, "NO PASSWORDXXXXXXXXXXXXXXXXXXXXX", 33);
365 safe_strcpy(p, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 33);
369 /*************************************************************
370 Routine to get the 32 hex characters and turn them
371 into a 16 byte array.
372 **************************************************************/
374 BOOL pdb_gethexpwd(const char *p, unsigned char *pwd)
377 unsigned char lonybble, hinybble;
378 char *hexchars = "0123456789ABCDEF";
384 for (i = 0; i < 32; i += 2) {
385 hinybble = toupper(p[i]);
386 lonybble = toupper(p[i + 1]);
388 p1 = strchr(hexchars, hinybble);
389 p2 = strchr(hexchars, lonybble);
394 hinybble = PTR_DIFF(p1, hexchars);
395 lonybble = PTR_DIFF(p2, hexchars);
397 pwd[i / 2] = (hinybble << 4) | lonybble;
402 #if 0 /* seem it is not used by anyone */
403 /*******************************************************************
404 Group and User RID username mapping function
405 ********************************************************************/
407 BOOL pdb_name_to_rid(const char *user_name, uint32 *u_rid, uint32 *g_rid)
410 struct passwd *pw = Get_Pwnam(user_name);
412 if (u_rid == NULL || g_rid == NULL || user_name == NULL)
416 DEBUG(1,("Username %s is invalid on this system\n", user_name));
420 /* turn the unix UID into a Domain RID. this is what the posix
421 sub-system does (adds 1000 to the uid) */
422 *u_rid = fallback_pdb_uid_to_user_rid(pw->pw_uid);
424 /* absolutely no idea what to do about the unix GID to Domain RID mapping */
426 if (get_group_map_from_gid(pw->pw_gid, &map, MAPPING_WITHOUT_PRIV)) {
427 sid_peek_rid(&map.sid, g_rid);
429 *g_rid = pdb_gid_to_group_rid(pw->pw_gid);
433 #endif /* seem it is not used by anyone */
435 /*******************************************************************
436 Converts NT user RID to a UNIX uid.
437 ********************************************************************/
439 uid_t fallback_pdb_user_rid_to_uid(uint32 user_rid)
441 int rid_offset = lp_algorithmic_rid_base();
442 return (uid_t)(((user_rid & (~USER_RID_TYPE))- rid_offset)/RID_MULTIPLIER);
446 /*******************************************************************
447 converts UNIX uid to an NT User RID.
448 ********************************************************************/
450 uint32 fallback_pdb_uid_to_user_rid(uid_t uid)
452 int rid_offset = lp_algorithmic_rid_base();
453 return (((((uint32)uid)*RID_MULTIPLIER) + rid_offset) | USER_RID_TYPE);
456 /*******************************************************************
457 Converts NT group RID to a UNIX gid.
458 ********************************************************************/
460 gid_t pdb_group_rid_to_gid(uint32 group_rid)
462 int rid_offset = lp_algorithmic_rid_base();
463 return (gid_t)(((group_rid & (~GROUP_RID_TYPE))- rid_offset)/RID_MULTIPLIER);
466 /*******************************************************************
467 converts NT Group RID to a UNIX uid.
469 warning: you must not call that function only
470 you must do a call to the group mapping first.
471 there is not anymore a direct link between the gid and the rid.
472 ********************************************************************/
474 uint32 pdb_gid_to_group_rid(gid_t gid)
476 int rid_offset = lp_algorithmic_rid_base();
477 return (((((uint32)gid)*RID_MULTIPLIER) + rid_offset) | GROUP_RID_TYPE);
480 /*******************************************************************
481 Decides if a RID is a well known RID.
482 ********************************************************************/
484 static BOOL pdb_rid_is_well_known(uint32 rid)
486 /* Not using rid_offset here, becouse this is the actual
487 NT fixed value (1000) */
489 return (rid < BASE_RID);
492 /*******************************************************************
493 Decides if a RID is a user or group RID.
494 ********************************************************************/
496 BOOL pdb_rid_is_user(uint32 rid)
498 /* lkcl i understand that NT attaches an enumeration to a RID
499 * such that it can be identified as either a user, group etc
500 * type. there are 5 such categories, and they are documented.
502 if(pdb_rid_is_well_known(rid)) {
504 * The only well known user RIDs are DOMAIN_USER_RID_ADMIN
505 * and DOMAIN_USER_RID_GUEST.
507 if(rid == DOMAIN_USER_RID_ADMIN || rid == DOMAIN_USER_RID_GUEST)
509 } else if((rid & RID_TYPE_MASK) == USER_RID_TYPE) {
515 /*******************************************************************
516 Convert a rid into a name. Used in the lookup SID rpc.
517 ********************************************************************/
519 BOOL local_lookup_sid(DOM_SID *sid, char *name, enum SID_NAME_USE *psid_name_use)
523 SAM_ACCOUNT *sam_account = NULL;
529 sid_peek_rid(sid, &rid);
530 *psid_name_use = SID_NAME_UNKNOWN;
532 DEBUG(5,("local_lookup_sid: looking up RID %u.\n", (unsigned int)rid));
534 if (rid == DOMAIN_USER_RID_ADMIN) {
535 char **admin_list = lp_admin_users(-1);
536 *psid_name_use = SID_NAME_USER;
538 char *p = *admin_list;
539 if(!next_token(&p, name, NULL, sizeof(fstring)))
540 fstrcpy(name, "Administrator");
542 fstrcpy(name, "Administrator");
546 } else if (rid == DOMAIN_USER_RID_GUEST) {
547 char *p = lp_guestaccount();
548 *psid_name_use = SID_NAME_USER;
549 if(!next_token(&p, name, NULL, sizeof(fstring)))
550 fstrcpy(name, "Guest");
556 * Don't try to convert the rid to a name if
557 * running in appliance mode
560 if (lp_hide_local_users())
563 if (!NT_STATUS_IS_OK(pdb_init_sam(&sam_account))) {
567 if (pdb_getsampwrid(sam_account, rid)) {
568 fstrcpy(name, pdb_get_username(sam_account));
569 *psid_name_use = SID_NAME_USER;
571 pdb_free_sam(&sam_account);
576 pdb_free_sam(&sam_account);
578 if (get_group_map_from_sid(*sid, &map, MAPPING_WITHOUT_PRIV)) {
580 DEBUG(5,("local_lookup_sid: mapped group %s to gid %u\n", map.nt_name, (unsigned int)map.gid));
581 fstrcpy(name, map.nt_name);
582 *psid_name_use = map.sid_name_use;
587 is_user = pdb_rid_is_user(rid);
589 DEBUG(5, ("assuming RID %u is a %s\n", (unsigned)rid, is_user ? "user" : "group"));
591 if (pdb_rid_is_user(rid)) {
592 uid = fallback_pdb_user_rid_to_uid(rid);
593 pass = getpwuid_alloc(uid);
595 *psid_name_use = SID_NAME_USER;
597 DEBUG(5,("local_lookup_sid: looking up uid %u %s\n", (unsigned int)uid,
598 pass ? "succeeded" : "failed" ));
601 slprintf(name, sizeof(fstring)-1, "unix_user.%u", (unsigned int)uid);
605 fstrcpy(name, pass->pw_name);
607 DEBUG(5,("local_lookup_sid: found user %s for rid %u\n", name,
608 (unsigned int)rid ));
616 gid = pdb_group_rid_to_gid(rid);
619 *psid_name_use = SID_NAME_ALIAS;
621 DEBUG(5,("local_lookup_sid: looking up gid %u %s\n", (unsigned int)gid,
622 gr ? "succeeded" : "failed" ));
625 slprintf(name, sizeof(fstring)-1, "unix_group.%u", (unsigned int)gid);
629 fstrcpy( name, gr->gr_name);
631 DEBUG(5,("local_lookup_sid: found group %s for rid %u\n", name,
632 (unsigned int)rid ));
637 /*******************************************************************
638 Convert a name into a SID. Used in the lookup name rpc.
639 ********************************************************************/
641 BOOL local_lookup_name(const char *c_user, DOM_SID *psid, enum SID_NAME_USE *psid_name_use)
643 extern DOM_SID global_sid_World_Domain;
644 struct passwd *pass = NULL;
647 SAM_ACCOUNT *sam_account = NULL;
649 *psid_name_use = SID_NAME_UNKNOWN;
652 * user may be quoted a const string, and map_username and
653 * friends can modify it. Make a modifiable copy. JRA.
656 fstrcpy(user, c_user);
658 sid_copy(&local_sid, &global_sam_sid);
661 * Special case for MACHINE\Everyone. Map to the world_sid.
664 if(strequal(user, "Everyone")) {
665 sid_copy( psid, &global_sid_World_Domain);
666 sid_append_rid(psid, 0);
667 *psid_name_use = SID_NAME_ALIAS;
672 * Don't lookup local unix users if running in appliance mode
674 if (lp_hide_local_users())
677 (void)map_username(user);
679 if (!NT_STATUS_IS_OK(pdb_init_sam(&sam_account))) {
683 if (pdb_getsampwnam(sam_account, user)) {
684 sid_append_rid( &local_sid, pdb_get_user_rid(sam_account));
685 *psid_name_use = SID_NAME_USER;
687 sid_copy( psid, &local_sid);
688 pdb_free_sam(&sam_account);
692 pdb_free_sam(&sam_account);
694 if ((pass = Get_Pwnam(user))) {
695 sid_append_rid( &local_sid, fallback_pdb_uid_to_user_rid(pass->pw_uid));
696 *psid_name_use = SID_NAME_USER;
700 * Maybe it was a group ?
705 /* check if it's a mapped group */
706 if (get_group_map_from_ntname(user, &map, MAPPING_WITHOUT_PRIV)) {
708 /* yes it's a mapped group to a valid unix group */
709 sid_copy(&local_sid, &map.sid);
710 *psid_name_use = map.sid_name_use;
713 /* it's a correct name but not mapped so it points to nothing*/
716 /* it's not a mapped group */
717 grp = getgrnam(user);
722 *check if it's mapped, if it is reply it doesn't exist
724 * that's to prevent this case:
726 * unix group ug is mapped to nt group ng
727 * someone does a lookup on ug
728 * we must not reply as it doesn't "exist" anymore
729 * for NT. For NT only ng exists.
733 if(get_group_map_from_gid(grp->gr_gid, &map, MAPPING_WITHOUT_PRIV)){
737 sid_append_rid( &local_sid, pdb_gid_to_group_rid(grp->gr_gid));
738 *psid_name_use = SID_NAME_ALIAS;
742 sid_copy( psid, &local_sid);
747 /****************************************************************************
748 Convert a uid to SID - locally.
749 ****************************************************************************/
751 DOM_SID *local_uid_to_sid(DOM_SID *psid, uid_t uid)
753 extern DOM_SID global_sam_sid;
755 SAM_ACCOUNT *sam_user = NULL;
756 fstring str; /* sid string buffer */
758 sid_copy(psid, &global_sam_sid);
760 if((pass = getpwuid_alloc(uid))) {
762 if (NT_STATUS_IS_ERR(pdb_init_sam(&sam_user))) {
767 if (pdb_getsampwnam(sam_user, pass->pw_name)) {
768 sid_append_rid(psid, pdb_get_user_rid(sam_user));
770 sid_append_rid(psid, fallback_pdb_uid_to_user_rid(uid));
773 DEBUG(10,("local_uid_to_sid: uid %u -> SID (%s) (%s).\n",
774 (unsigned)uid, sid_to_string( str, psid),
778 pdb_free_sam(&sam_user);
781 sid_append_rid(psid, fallback_pdb_uid_to_user_rid(uid));
783 DEBUG(10,("local_uid_to_sid: uid %u -> SID (%s) (unknown user).\n",
784 (unsigned)uid, sid_to_string( str, psid)));
790 /****************************************************************************
791 Convert a SID to uid - locally.
792 ****************************************************************************/
794 BOOL local_sid_to_uid(uid_t *puid, DOM_SID *psid, enum SID_NAME_USE *name_type)
796 extern DOM_SID global_sam_sid;
801 SAM_ACCOUNT *sam_user = NULL;
803 *name_type = SID_NAME_UNKNOWN;
805 sid_copy(&dom_sid, psid);
806 sid_split_rid(&dom_sid, &rid);
809 * We can only convert to a uid if this is our local
810 * Domain SID (ie. we are the controling authority).
812 if (!sid_equal(&global_sam_sid, &dom_sid))
815 if (NT_STATUS_IS_ERR(pdb_init_sam(&sam_user)))
818 if (pdb_getsampwrid(sam_user, rid)) {
819 *puid = pdb_get_uid(sam_user);
821 pdb_free_sam(&sam_user);
824 DEBUG(10,("local_sid_to_uid: SID %s -> uid (%u) (%s).\n", sid_to_string( str, psid),
825 (unsigned int)*puid, pdb_get_username(sam_user)));
827 if ((pdb_rid_is_user(rid))) {
828 *puid = fallback_pdb_user_rid_to_uid(rid);
829 DEBUG(10,("local_sid_to_uid: SID %s -> uid (%u) (non-passdb user).\n", sid_to_string( str, psid),
830 (unsigned int)*puid));
832 DEBUG(5,("local_sid_to_uid: SID %s not mapped becouse RID isn't a user.\n", sid_to_string( str, psid)));
833 pdb_free_sam(&sam_user);
837 pdb_free_sam(&sam_user);
839 *name_type = SID_NAME_USER;
844 /****************************************************************************
845 Convert a gid to SID - locally.
846 ****************************************************************************/
848 DOM_SID *local_gid_to_sid(DOM_SID *psid, gid_t gid)
850 extern DOM_SID global_sam_sid;
853 sid_copy(psid, &global_sam_sid);
855 if (get_group_map_from_gid(gid, &map, MAPPING_WITHOUT_PRIV)) {
856 sid_copy(psid, &map.sid);
859 sid_append_rid(psid, pdb_gid_to_group_rid(gid));
865 /****************************************************************************
866 Convert a SID to gid - locally.
867 ****************************************************************************/
869 BOOL local_sid_to_gid(gid_t *pgid, DOM_SID *psid, enum SID_NAME_USE *name_type)
871 extern DOM_SID global_sam_sid;
877 *name_type = SID_NAME_UNKNOWN;
879 sid_copy(&dom_sid, psid);
880 sid_split_rid(&dom_sid, &rid);
883 * We can only convert to a gid if this is our local
884 * Domain SID (ie. we are the controling authority).
886 * Or in the Builtin SID too. JFM, 11/30/2001
889 if (!sid_equal(&global_sam_sid, &dom_sid))
892 if (get_group_map_from_sid(*psid, &map, MAPPING_WITHOUT_PRIV)) {
894 /* the SID is in the mapping table but not mapped */
898 sid_peek_rid(&map.sid, &rid);
900 *name_type = map.sid_name_use;
901 DEBUG(10,("local_sid_to_gid: mapped SID %s (%s) -> gid (%u).\n", sid_to_string( str, psid),
902 map.nt_name, (unsigned int)*pgid));
905 if (pdb_rid_is_user(rid))
908 *pgid = pdb_group_rid_to_gid(rid);
909 *name_type = SID_NAME_ALIAS;
910 DEBUG(10,("local_sid_to_gid: SID %s -> gid (%u).\n", sid_to_string( str, psid),
911 (unsigned int)*pgid));
918 * Quick hack to do an easy ucs2 -> mulitbyte conversion
919 * @return static buffer containing the converted string
922 static char *pdb_convert(const UNISTR2 *from)
924 static pstring convert_buffer;
927 return convert_buffer;
930 unistr2_to_ascii(convert_buffer, from, sizeof(pstring));
931 return convert_buffer;
934 /*************************************************************
935 Copies a SAM_USER_INFO_23 to a SAM_ACCOUNT
936 **************************************************************/
938 void copy_id23_to_sam_passwd(SAM_ACCOUNT *to, SAM_USER_INFO_23 *from)
941 if (from == NULL || to == NULL)
944 pdb_set_logon_time(to,nt_time_to_unix(&from->logon_time), True);
945 pdb_set_logoff_time(to,nt_time_to_unix(&from->logoff_time), True);
946 pdb_set_kickoff_time(to, nt_time_to_unix(&from->kickoff_time), True);
947 pdb_set_pass_can_change_time(to, nt_time_to_unix(&from->pass_can_change_time), True);
948 pdb_set_pass_must_change_time(to, nt_time_to_unix(&from->pass_must_change_time), True);
950 pdb_set_pass_last_set_time(to, nt_time_to_unix(&from->pass_last_set_time));
952 if (from->uni_user_name.buffer)
953 pdb_set_username(to , pdb_convert(&from->uni_user_name ));
954 if (from->uni_full_name.buffer)
955 pdb_set_fullname(to , pdb_convert(&from->uni_full_name ));
956 if (from->uni_home_dir.buffer)
957 pdb_set_homedir(to , pdb_convert(&from->uni_home_dir ), True);
958 if (from->uni_dir_drive.buffer)
959 pdb_set_dir_drive(to , pdb_convert(&from->uni_dir_drive ), True);
960 if (from->uni_logon_script.buffer)
961 pdb_set_logon_script(to , pdb_convert(&from->uni_logon_script), True);
962 if (from->uni_profile_path.buffer)
963 pdb_set_profile_path(to , pdb_convert(&from->uni_profile_path), True);
964 if (from->uni_acct_desc.buffer)
965 pdb_set_acct_desc(to , pdb_convert(&from->uni_acct_desc ));
966 if (from->uni_workstations.buffer)
967 pdb_set_workstations(to , pdb_convert(&from->uni_workstations));
968 if (from->uni_unknown_str.buffer)
969 pdb_set_unknown_str(to , pdb_convert(&from->uni_unknown_str ));
970 if (from->uni_munged_dial.buffer)
971 pdb_set_munged_dial(to , pdb_convert(&from->uni_munged_dial ));
974 pdb_set_user_rid(to, from->user_rid);
976 pdb_set_group_rid(to, from->group_rid);
978 pdb_set_acct_ctrl(to, from->acb_info);
979 pdb_set_unknown_3(to, from->unknown_3);
981 pdb_set_logon_divs(to, from->logon_divs);
982 pdb_set_hours_len(to, from->logon_hrs.len);
983 pdb_set_hours(to, from->logon_hrs.hours);
985 pdb_set_unknown_5(to, from->unknown_5);
986 pdb_set_unknown_6(to, from->unknown_6);
990 /*************************************************************
992 **************************************************************/
994 void copy_id21_to_sam_passwd(SAM_ACCOUNT *to, SAM_USER_INFO_21 *from)
996 if (from == NULL || to == NULL)
999 pdb_set_logon_time(to,nt_time_to_unix(&from->logon_time), True);
1000 pdb_set_logoff_time(to,nt_time_to_unix(&from->logoff_time), True);
1001 pdb_set_kickoff_time(to, nt_time_to_unix(&from->kickoff_time), True);
1002 pdb_set_pass_can_change_time(to, nt_time_to_unix(&from->pass_can_change_time), True);
1003 pdb_set_pass_must_change_time(to, nt_time_to_unix(&from->pass_must_change_time), True);
1005 pdb_set_pass_last_set_time(to, nt_time_to_unix(&from->pass_last_set_time));
1007 if (from->uni_user_name.buffer)
1008 pdb_set_username(to , pdb_convert(&from->uni_user_name ));
1009 if (from->uni_full_name.buffer)
1010 pdb_set_fullname(to , pdb_convert(&from->uni_full_name ));
1011 if (from->uni_home_dir.buffer)
1012 pdb_set_homedir(to , pdb_convert(&from->uni_home_dir ), True);
1013 if (from->uni_dir_drive.buffer)
1014 pdb_set_dir_drive(to , pdb_convert(&from->uni_dir_drive ), True);
1015 if (from->uni_logon_script.buffer)
1016 pdb_set_logon_script(to , pdb_convert(&from->uni_logon_script), True);
1017 if (from->uni_profile_path.buffer)
1018 pdb_set_profile_path(to , pdb_convert(&from->uni_profile_path), True);
1019 if (from->uni_acct_desc.buffer)
1020 pdb_set_acct_desc(to , pdb_convert(&from->uni_acct_desc ));
1021 if (from->uni_workstations.buffer)
1022 pdb_set_workstations(to , pdb_convert(&from->uni_workstations));
1023 if (from->uni_unknown_str.buffer)
1024 pdb_set_unknown_str(to , pdb_convert(&from->uni_unknown_str ));
1025 if (from->uni_munged_dial.buffer)
1026 pdb_set_munged_dial(to , pdb_convert(&from->uni_munged_dial ));
1029 pdb_set_user_rid(to, from->user_rid);
1030 if (from->group_rid)
1031 pdb_set_group_rid(to, from->group_rid);
1033 /* FIXME!! Do we need to copy the passwords here as well?
1034 I don't know. Need to figure this out --jerry */
1036 /* Passwords dealt with in caller --abartlet */
1038 pdb_set_acct_ctrl(to, from->acb_info);
1039 pdb_set_unknown_3(to, from->unknown_3);
1041 pdb_set_logon_divs(to, from->logon_divs);
1042 pdb_set_hours_len(to, from->logon_hrs.len);
1043 pdb_set_hours(to, from->logon_hrs.hours);
1045 pdb_set_unknown_5(to, from->unknown_5);
1046 pdb_set_unknown_6(to, from->unknown_6);
1050 /*************************************************************
1051 Change a password entry in the local smbpasswd file.
1053 FIXME!! The function needs to be abstracted into the
1054 passdb interface or something. It is currently being called
1055 by _api_samr_create_user() in rpc_server/srv_samr.c,
1056 in SWAT and by smbpasswd/pdbedit.
1059 *************************************************************/
1061 BOOL local_password_change(const char *user_name, int local_flags,
1062 const char *new_passwd,
1063 char *err_str, size_t err_str_len,
1064 char *msg_str, size_t msg_str_len)
1066 struct passwd *pwd = NULL;
1067 SAM_ACCOUNT *sam_pass=NULL;
1072 /* Get the smb passwd entry for this user */
1073 pdb_init_sam(&sam_pass);
1074 if(!pdb_getsampwnam(sam_pass, user_name)) {
1075 pdb_free_sam(&sam_pass);
1077 if (local_flags & LOCAL_ADD_USER) {
1078 pwd = getpwnam_alloc(user_name);
1079 } else if (local_flags & LOCAL_DELETE_USER) {
1080 /* Might not exist in /etc/passwd */
1082 slprintf(err_str, err_str_len-1,"Failed to find entry for user %s.\n", user_name);
1087 /* Local user found, so init from this */
1088 if (!NT_STATUS_IS_OK(pdb_init_sam_pw(&sam_pass, pwd))){
1089 slprintf(err_str, err_str_len-1, "Failed initialise SAM_ACCOUNT for user %s.\n", user_name);
1096 if (!NT_STATUS_IS_OK(pdb_init_sam(&sam_pass))){
1097 slprintf(err_str, err_str_len-1, "Failed initialise SAM_ACCOUNT for user %s.\n", user_name);
1101 if (!pdb_set_username(sam_pass, user_name)) {
1102 slprintf(err_str, err_str_len - 1, "Failed to set username for user %s.\n", user_name);
1103 pdb_free_sam(&sam_pass);
1107 if (local_flags & LOCAL_TRUST_ACCOUNT) {
1108 if (!pdb_set_acct_ctrl(sam_pass, ACB_WSTRUST)) {
1109 slprintf(err_str, err_str_len - 1, "Failed to set 'trusted workstation account' flags for user %s.\n", user_name);
1110 pdb_free_sam(&sam_pass);
1113 } else if (local_flags & LOCAL_INTERDOM_ACCOUNT) {
1114 if (!pdb_set_acct_ctrl(sam_pass, ACB_DOMTRUST)) {
1115 slprintf(err_str, err_str_len - 1, "Failed to set 'domain trust account' flags for user %s.\n", user_name);
1116 pdb_free_sam(&sam_pass);
1120 if (!pdb_set_acct_ctrl(sam_pass, ACB_NORMAL)) {
1121 slprintf(err_str, err_str_len - 1, "Failed to set 'normal account' flags for user %s.\n", user_name);
1122 pdb_free_sam(&sam_pass);
1128 /* the entry already existed */
1129 local_flags &= ~LOCAL_ADD_USER;
1133 * We are root - just write the new password
1134 * and the valid last change time.
1137 if (local_flags & LOCAL_DISABLE_USER) {
1138 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)|ACB_DISABLED)) {
1139 slprintf(err_str, err_str_len-1, "Failed to set 'disabled' flag for user %s.\n", user_name);
1140 pdb_free_sam(&sam_pass);
1143 } else if (local_flags & LOCAL_ENABLE_USER) {
1144 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_DISABLED))) {
1145 slprintf(err_str, err_str_len-1, "Failed to unset 'disabled' flag for user %s.\n", user_name);
1146 pdb_free_sam(&sam_pass);
1151 if (local_flags & LOCAL_SET_NO_PASSWORD) {
1152 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)|ACB_PWNOTREQ)) {
1153 slprintf(err_str, err_str_len-1, "Failed to set 'no password required' flag for user %s.\n", user_name);
1154 pdb_free_sam(&sam_pass);
1157 } else if (local_flags & LOCAL_SET_PASSWORD) {
1159 * If we're dealing with setting a completely empty user account
1160 * ie. One with a password of 'XXXX', but not set disabled (like
1161 * an account created from scratch) then if the old password was
1162 * 'XX's then getsmbpwent will have set the ACB_DISABLED flag.
1163 * We remove that as we're giving this user their first password
1164 * and the decision hasn't really been made to disable them (ie.
1165 * don't create them disabled). JRA.
1167 if ((pdb_get_lanman_passwd(sam_pass)==NULL) && (pdb_get_acct_ctrl(sam_pass)&ACB_DISABLED)) {
1168 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_DISABLED))) {
1169 slprintf(err_str, err_str_len-1, "Failed to unset 'disabled' flag for user %s.\n", user_name);
1170 pdb_free_sam(&sam_pass);
1174 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_PWNOTREQ))) {
1175 slprintf(err_str, err_str_len-1, "Failed to unset 'no password required' flag for user %s.\n", user_name);
1176 pdb_free_sam(&sam_pass);
1180 if (!pdb_set_plaintext_passwd (sam_pass, new_passwd)) {
1181 slprintf(err_str, err_str_len-1, "Failed to set password for user %s.\n", user_name);
1182 pdb_free_sam(&sam_pass);
1187 if (local_flags & LOCAL_ADD_USER) {
1188 if (pdb_add_sam_account(sam_pass)) {
1189 slprintf(msg_str, msg_str_len-1, "Added user %s.\n", user_name);
1190 pdb_free_sam(&sam_pass);
1193 slprintf(err_str, err_str_len-1, "Failed to add entry for user %s.\n", user_name);
1194 pdb_free_sam(&sam_pass);
1197 } else if (local_flags & LOCAL_DELETE_USER) {
1198 if (!pdb_delete_sam_account(sam_pass)) {
1199 slprintf(err_str,err_str_len-1, "Failed to delete entry for user %s.\n", user_name);
1200 pdb_free_sam(&sam_pass);
1203 slprintf(msg_str, msg_str_len-1, "Deleted user %s.\n", user_name);
1205 if(!pdb_update_sam_account(sam_pass)) {
1206 slprintf(err_str, err_str_len-1, "Failed to modify entry for user %s.\n", user_name);
1207 pdb_free_sam(&sam_pass);
1210 if(local_flags & LOCAL_DISABLE_USER)
1211 slprintf(msg_str, msg_str_len-1, "Disabled user %s.\n", user_name);
1212 else if (local_flags & LOCAL_ENABLE_USER)
1213 slprintf(msg_str, msg_str_len-1, "Enabled user %s.\n", user_name);
1214 else if (local_flags & LOCAL_SET_NO_PASSWORD)
1215 slprintf(msg_str, msg_str_len-1, "User %s password set to none.\n", user_name);
1218 pdb_free_sam(&sam_pass);