2 Unix SMB/Netbios implementation.
4 Password and authentication handling
5 Copyright (C) Jeremy Allison 1996-2001
6 Copyright (C) Luke Kenneth Casson Leighton 1996-1998
7 Copyright (C) Gerald (Jerry) Carter 2000-2001
8 Copyright (C) Andrew Bartlett 2001-2002
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 * This is set on startup - it defines the SID for this
29 * machine, and therefore the SAM database for which it is
33 extern DOM_SID global_sam_sid;
35 struct passdb_ops *pdb_ops;
38 static void* pdb_handle = NULL;
41 /***************************************************************
42 Initialize the password db operations.
43 ***************************************************************/
45 BOOL initialize_password_db(BOOL reload)
48 * This function is unfinished right now, so just
49 * ignore the details and always return True. It
50 * is here only as a placeholder --jerry
57 /************************************************************
58 Fill the SAM_ACCOUNT with default values.
59 ***********************************************************/
61 static void pdb_fill_default_sam(SAM_ACCOUNT *user)
63 ZERO_STRUCT(user->private); /* Don't touch the talloc context */
65 /* Don't change these timestamp settings without a good reason.
66 They are important for NT member server compatibility. */
68 user->private.init_flag = FLAG_SAM_UNINIT;
69 user->private.uid = user->private.gid = -1;
71 user->private.logon_time = (time_t)0;
72 user->private.pass_last_set_time = (time_t)0;
73 user->private.pass_can_change_time = (time_t)0;
74 user->private.logoff_time =
75 user->private.kickoff_time =
76 user->private.pass_must_change_time = get_time_t_max();
77 user->private.unknown_3 = 0x00ffffff; /* don't know */
78 user->private.logon_divs = 168; /* hours per week */
79 user->private.hours_len = 21; /* 21 times 8 bits = 168 */
80 memset(user->private.hours, 0xff, user->private.hours_len); /* available at all hours */
81 user->private.unknown_5 = 0x00000000; /* don't know */
82 user->private.unknown_6 = 0x000004ec; /* don't know */
85 static void destroy_pdb_talloc(SAM_ACCOUNT **user)
88 talloc_destroy((*user)->mem_ctx);
94 /**********************************************************************
95 Alloc memory and initialises a struct sam_passwd on supplied mem_ctx.
96 ***********************************************************************/
98 NTSTATUS pdb_init_sam_talloc(TALLOC_CTX *mem_ctx, SAM_ACCOUNT **user)
101 DEBUG(0,("pdb_init_sam: SAM_ACCOUNT was non NULL\n"));
103 smb_panic("NULL pointer passed to pdb_init_sam\n");
105 return NT_STATUS_UNSUCCESSFUL;
109 DEBUG(0,("pdb_init_sam_talloc: mem_ctx was NULL!\n"));
110 return NT_STATUS_UNSUCCESSFUL;
113 *user=(SAM_ACCOUNT *)talloc(mem_ctx, sizeof(SAM_ACCOUNT));
116 DEBUG(0,("pdb_init_sam: error while allocating memory\n"));
117 return NT_STATUS_NO_MEMORY;
120 (*user)->mem_ctx = mem_ctx;
122 (*user)->free_fn = NULL;
124 pdb_fill_default_sam(*user);
130 /*************************************************************
131 Alloc memory and initialises a struct sam_passwd.
132 ************************************************************/
134 NTSTATUS pdb_init_sam(SAM_ACCOUNT **user)
139 mem_ctx = talloc_init_named("passdb internal SAM_ACCOUNT allocation");
142 DEBUG(0,("pdb_init_sam: error while doing talloc_init()\n"));
143 return NT_STATUS_NO_MEMORY;
146 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_talloc(mem_ctx, user))) {
147 talloc_destroy(mem_ctx);
151 (*user)->free_fn = destroy_pdb_talloc;
157 /*************************************************************
158 Initialises a struct sam_passwd with sane values.
159 ************************************************************/
161 NTSTATUS pdb_init_sam_pw(SAM_ACCOUNT **new_sam_acct, const struct passwd *pwd)
170 return NT_STATUS_UNSUCCESSFUL;
173 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam(new_sam_acct))) {
178 pdb_set_username(*new_sam_acct, pwd->pw_name);
179 pdb_set_fullname(*new_sam_acct, pwd->pw_gecos);
181 pdb_set_uid(*new_sam_acct, pwd->pw_uid);
182 pdb_set_gid(*new_sam_acct, pwd->pw_gid);
184 pdb_set_user_rid(*new_sam_acct, pdb_uid_to_user_rid(pwd->pw_uid));
186 /* call the mapping code here */
187 if(get_group_map_from_gid(pwd->pw_gid, &map, MAPPING_WITHOUT_PRIV)) {
188 sid_peek_rid(&map.sid, &rid);
191 rid=pdb_gid_to_group_rid(pwd->pw_gid);
194 pdb_set_group_rid(*new_sam_acct, rid);
196 pstrcpy(str, lp_logon_path());
197 standard_sub_advanced(-1, pwd->pw_name, "", pwd->pw_gid, pwd->pw_name, str);
198 pdb_set_profile_path(*new_sam_acct, str, False);
200 pstrcpy(str, lp_logon_home());
201 standard_sub_advanced(-1, pwd->pw_name, "", pwd->pw_gid, pwd->pw_name, str);
202 pdb_set_homedir(*new_sam_acct, str, False);
204 pstrcpy(str, lp_logon_drive());
205 standard_sub_advanced(-1, pwd->pw_name, "", pwd->pw_gid, pwd->pw_name, str);
206 pdb_set_dir_drive(*new_sam_acct, str, False);
208 pstrcpy(str, lp_logon_script());
209 standard_sub_advanced(-1, pwd->pw_name, "", pwd->pw_gid, pwd->pw_name, str);
210 pdb_set_logon_script(*new_sam_acct, str, False);
217 * Free the contets of the SAM_ACCOUNT, but not the structure.
219 * Also wipes the LM and NT hashes from memory.
221 * @param user SAM_ACCOUNT to free members of.
224 static void pdb_free_sam_contents(SAM_ACCOUNT *user)
226 /* As we start mallocing more strings this is where
227 we should free them. */
229 data_blob_clear_free(&(user->private.lm_pw));
230 data_blob_clear_free(&(user->private.nt_pw));
234 /************************************************************
235 Reset the SAM_ACCOUNT and free the NT/LM hashes.
236 ***********************************************************/
238 NTSTATUS pdb_reset_sam(SAM_ACCOUNT *user)
241 DEBUG(0,("pdb_reset_sam: SAM_ACCOUNT was NULL\n"));
243 smb_panic("NULL pointer passed to pdb_free_sam\n");
245 return NT_STATUS_UNSUCCESSFUL;
248 pdb_free_sam_contents(user);
250 pdb_fill_default_sam(user);
256 /************************************************************
257 Free the SAM_ACCOUNT and the member pointers.
258 ***********************************************************/
260 NTSTATUS pdb_free_sam(SAM_ACCOUNT **user)
263 DEBUG(0,("pdb_free_sam: SAM_ACCOUNT was NULL\n"));
265 smb_panic("NULL pointer passed to pdb_free_sam\n");
267 return NT_STATUS_UNSUCCESSFUL;
270 pdb_free_sam_contents(*user);
272 if ((*user)->free_fn) {
273 (*user)->free_fn(user);
280 /**********************************************************
281 Encode the account control bits into a string.
282 length = length of string to encode into (including terminating
283 null). length *MUST BE MORE THAN 2* !
284 **********************************************************/
286 char *pdb_encode_acct_ctrl(uint16 acct_ctrl, size_t length)
288 static fstring acct_str;
293 if (acct_ctrl & ACB_PWNOTREQ ) acct_str[i++] = 'N';
294 if (acct_ctrl & ACB_DISABLED ) acct_str[i++] = 'D';
295 if (acct_ctrl & ACB_HOMDIRREQ) acct_str[i++] = 'H';
296 if (acct_ctrl & ACB_TEMPDUP ) acct_str[i++] = 'T';
297 if (acct_ctrl & ACB_NORMAL ) acct_str[i++] = 'U';
298 if (acct_ctrl & ACB_MNS ) acct_str[i++] = 'M';
299 if (acct_ctrl & ACB_WSTRUST ) acct_str[i++] = 'W';
300 if (acct_ctrl & ACB_SVRTRUST ) acct_str[i++] = 'S';
301 if (acct_ctrl & ACB_AUTOLOCK ) acct_str[i++] = 'L';
302 if (acct_ctrl & ACB_PWNOEXP ) acct_str[i++] = 'X';
303 if (acct_ctrl & ACB_DOMTRUST ) acct_str[i++] = 'I';
305 for ( ; i < length - 2 ; i++ )
310 acct_str[i++] = '\0';
315 /**********************************************************
316 Decode the account control bits from a string.
317 **********************************************************/
319 uint16 pdb_decode_acct_ctrl(const char *p)
321 uint16 acct_ctrl = 0;
322 BOOL finished = False;
325 * Check if the account type bits have been encoded after the
326 * NT password (in the form [NDHTUWSLXI]).
332 for (p++; *p && !finished; p++) {
334 case 'N': { acct_ctrl |= ACB_PWNOTREQ ; break; /* 'N'o password. */ }
335 case 'D': { acct_ctrl |= ACB_DISABLED ; break; /* 'D'isabled. */ }
336 case 'H': { acct_ctrl |= ACB_HOMDIRREQ; break; /* 'H'omedir required. */ }
337 case 'T': { acct_ctrl |= ACB_TEMPDUP ; break; /* 'T'emp account. */ }
338 case 'U': { acct_ctrl |= ACB_NORMAL ; break; /* 'U'ser account (normal). */ }
339 case 'M': { acct_ctrl |= ACB_MNS ; break; /* 'M'NS logon user account. What is this ? */ }
340 case 'W': { acct_ctrl |= ACB_WSTRUST ; break; /* 'W'orkstation account. */ }
341 case 'S': { acct_ctrl |= ACB_SVRTRUST ; break; /* 'S'erver account. */ }
342 case 'L': { acct_ctrl |= ACB_AUTOLOCK ; break; /* 'L'ocked account. */ }
343 case 'X': { acct_ctrl |= ACB_PWNOEXP ; break; /* No 'X'piry on password */ }
344 case 'I': { acct_ctrl |= ACB_DOMTRUST ; break; /* 'I'nterdomain trust account. */ }
350 default: { finished = True; }
357 /*************************************************************
358 Routine to set 32 hex password characters from a 16 byte array.
359 **************************************************************/
361 void pdb_sethexpwd(char *p, const unsigned char *pwd, uint16 acct_ctrl)
365 for (i = 0; i < 16; i++)
366 slprintf(&p[i*2], 3, "%02X", pwd[i]);
368 if (acct_ctrl & ACB_PWNOTREQ)
369 safe_strcpy(p, "NO PASSWORDXXXXXXXXXXXXXXXXXXXXX", 33);
371 safe_strcpy(p, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 33);
375 /*************************************************************
376 Routine to get the 32 hex characters and turn them
377 into a 16 byte array.
378 **************************************************************/
380 BOOL pdb_gethexpwd(const char *p, unsigned char *pwd)
383 unsigned char lonybble, hinybble;
384 char *hexchars = "0123456789ABCDEF";
390 for (i = 0; i < 32; i += 2) {
391 hinybble = toupper(p[i]);
392 lonybble = toupper(p[i + 1]);
394 p1 = strchr(hexchars, hinybble);
395 p2 = strchr(hexchars, lonybble);
400 hinybble = PTR_DIFF(p1, hexchars);
401 lonybble = PTR_DIFF(p2, hexchars);
403 pwd[i / 2] = (hinybble << 4) | lonybble;
408 /*******************************************************************
409 Group and User RID username mapping function
410 ********************************************************************/
412 BOOL pdb_name_to_rid(const char *user_name, uint32 *u_rid, uint32 *g_rid)
415 struct passwd *pw = Get_Pwnam(user_name);
417 if (u_rid == NULL || g_rid == NULL || user_name == NULL)
421 DEBUG(1,("Username %s is invalid on this system\n", user_name));
425 /* turn the unix UID into a Domain RID. this is what the posix
426 sub-system does (adds 1000 to the uid) */
427 *u_rid = pdb_uid_to_user_rid(pw->pw_uid);
429 /* absolutely no idea what to do about the unix GID to Domain RID mapping */
431 if (get_group_map_from_gid(pw->pw_gid, &map, MAPPING_WITHOUT_PRIV)) {
432 sid_peek_rid(&map.sid, g_rid);
434 *g_rid = pdb_gid_to_group_rid(pw->pw_gid);
439 /*******************************************************************
440 Converts NT user RID to a UNIX uid.
441 ********************************************************************/
443 uid_t pdb_user_rid_to_uid(uint32 user_rid)
445 return (uid_t)(((user_rid & (~USER_RID_TYPE))- 1000)/RID_MULTIPLIER);
449 /*******************************************************************
450 Converts NT group RID to a UNIX gid.
451 ********************************************************************/
453 gid_t pdb_group_rid_to_gid(uint32 group_rid)
455 return (gid_t)(((group_rid & (~GROUP_RID_TYPE))- 1000)/RID_MULTIPLIER);
458 /*******************************************************************
459 converts UNIX uid to an NT User RID.
460 ********************************************************************/
462 uint32 pdb_uid_to_user_rid(uid_t uid)
464 return (((((uint32)uid)*RID_MULTIPLIER) + 1000) | USER_RID_TYPE);
467 /*******************************************************************
468 converts NT Group RID to a UNIX uid.
470 warning: you must not call that function only
471 you must do a call to the group mapping first.
472 there is not anymore a direct link between the gid and the rid.
473 ********************************************************************/
475 uint32 pdb_gid_to_group_rid(gid_t gid)
477 return (((((uint32)gid)*RID_MULTIPLIER) + 1000) | 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)
489 /*******************************************************************
490 Decides if a RID is a user or group RID.
491 ********************************************************************/
493 BOOL pdb_rid_is_user(uint32 rid)
495 /* lkcl i understand that NT attaches an enumeration to a RID
496 * such that it can be identified as either a user, group etc
497 * type. there are 5 such categories, and they are documented.
499 if(pdb_rid_is_well_known(rid)) {
501 * The only well known user RIDs are DOMAIN_USER_RID_ADMIN
502 * and DOMAIN_USER_RID_GUEST.
504 if(rid == DOMAIN_USER_RID_ADMIN || rid == DOMAIN_USER_RID_GUEST)
506 } else if((rid & RID_TYPE_MASK) == USER_RID_TYPE) {
512 /*******************************************************************
513 Convert a rid into a name. Used in the lookup SID rpc.
514 ********************************************************************/
516 BOOL local_lookup_sid(DOM_SID *sid, char *name, enum SID_NAME_USE *psid_name_use)
521 sid_peek_rid(sid, &rid);
522 is_user = pdb_rid_is_user(rid);
523 *psid_name_use = SID_NAME_UNKNOWN;
525 DEBUG(5,("local_lookup_sid: looking up %s RID %u.\n", is_user ? "user" :
526 "group", (unsigned int)rid));
529 if(rid == DOMAIN_USER_RID_ADMIN) {
531 char *p = admin_users;
532 *psid_name_use = SID_NAME_USER;
533 if(!next_token(&p, name, NULL, sizeof(fstring)))
534 fstrcpy(name, "Administrator");
535 } else if (rid == DOMAIN_USER_RID_GUEST) {
537 char *p = guest_users;
538 *psid_name_use = SID_NAME_USER;
539 if(!next_token(&p, name, NULL, sizeof(fstring)))
540 fstrcpy(name, "Guest");
546 * Don't try to convert the rid to a name if
547 * running in appliance mode
549 if (lp_hide_local_users())
552 uid = pdb_user_rid_to_uid(rid);
553 pass = sys_getpwuid(uid);
555 *psid_name_use = SID_NAME_USER;
557 DEBUG(5,("local_lookup_sid: looking up uid %u %s\n", (unsigned int)uid,
558 pass ? "succeeded" : "failed" ));
561 slprintf(name, sizeof(fstring)-1, "unix_user.%u", (unsigned int)uid);
565 fstrcpy(name, pass->pw_name);
567 DEBUG(5,("local_lookup_sid: found user %s for rid %u\n", name,
568 (unsigned int)rid ));
577 * Don't try to convert the rid to a name if running
581 if (lp_hide_local_users())
584 /* check if it's a mapped group */
585 if (get_group_map_from_sid(*sid, &map, MAPPING_WITHOUT_PRIV)) {
587 DEBUG(5,("local_lookup_sid: mapped group %s to gid %u\n", map.nt_name, (unsigned int)map.gid));
588 fstrcpy(name, map.nt_name);
589 *psid_name_use = map.sid_name_use;
594 gid = pdb_group_rid_to_gid(rid);
597 *psid_name_use = SID_NAME_ALIAS;
599 DEBUG(5,("local_lookup_sid: looking up gid %u %s\n", (unsigned int)gid,
600 gr ? "succeeded" : "failed" ));
603 slprintf(name, sizeof(fstring)-1, "unix_group.%u", (unsigned int)gid);
607 fstrcpy( name, gr->gr_name);
609 DEBUG(5,("local_lookup_sid: found group %s for rid %u\n", name,
610 (unsigned int)rid ));
616 /*******************************************************************
617 Convert a name into a SID. Used in the lookup name rpc.
618 ********************************************************************/
620 BOOL local_lookup_name(const char *c_domain, const char *c_user, DOM_SID *psid, enum SID_NAME_USE *psid_name_use)
622 extern DOM_SID global_sid_World_Domain;
623 struct passwd *pass = NULL;
628 *psid_name_use = SID_NAME_UNKNOWN;
631 * domain and user may be quoted const strings, and map_username and
632 * friends can modify them. Make a modifiable copy. JRA.
635 fstrcpy(domain, c_domain);
636 fstrcpy(user, c_user);
638 sid_copy(&local_sid, &global_sam_sid);
641 * Special case for MACHINE\Everyone. Map to the world_sid.
644 if(strequal(user, "Everyone")) {
645 sid_copy( psid, &global_sid_World_Domain);
646 sid_append_rid(psid, 0);
647 *psid_name_use = SID_NAME_ALIAS;
652 * Don't lookup local unix users if running in appliance mode
654 if (lp_hide_local_users())
657 (void)map_username(user);
659 if((pass = Get_Pwnam(user))) {
660 sid_append_rid( &local_sid, pdb_uid_to_user_rid(pass->pw_uid));
661 *psid_name_use = SID_NAME_USER;
664 * Maybe it was a group ?
669 /* check if it's a mapped group */
670 if (get_group_map_from_ntname(user, &map, MAPPING_WITHOUT_PRIV)) {
672 /* yes it's a mapped group to a valid unix group */
673 sid_copy(&local_sid, &map.sid);
674 *psid_name_use = map.sid_name_use;
677 /* it's a correct name but not mapped so it points to nothing*/
680 /* it's not a mapped group */
681 grp = getgrnam(user);
686 *check if it's mapped, if it is reply it doesn't exist
688 * that's to prevent this case:
690 * unix group ug is mapped to nt group ng
691 * someone does a lookup on ug
692 * we must not reply as it doesn't "exist" anymore
693 * for NT. For NT only ng exists.
697 if(get_group_map_from_gid(grp->gr_gid, &map, MAPPING_WITHOUT_PRIV)){
701 sid_append_rid( &local_sid, pdb_gid_to_group_rid(grp->gr_gid));
702 *psid_name_use = SID_NAME_ALIAS;
706 sid_copy( psid, &local_sid);
711 /****************************************************************************
712 Convert a uid to SID - locally.
713 ****************************************************************************/
715 DOM_SID *local_uid_to_sid(DOM_SID *psid, uid_t uid)
717 extern DOM_SID global_sam_sid;
719 sid_copy(psid, &global_sam_sid);
720 sid_append_rid(psid, pdb_uid_to_user_rid(uid));
725 /****************************************************************************
726 Convert a SID to uid - locally.
727 ****************************************************************************/
729 BOOL local_sid_to_uid(uid_t *puid, DOM_SID *psid, enum SID_NAME_USE *name_type)
731 extern DOM_SID global_sam_sid;
738 *name_type = SID_NAME_UNKNOWN;
740 sid_copy(&dom_sid, psid);
741 sid_split_rid(&dom_sid, &rid);
743 if (!pdb_rid_is_user(rid))
747 * We can only convert to a uid if this is our local
748 * Domain SID (ie. we are the controling authority).
750 if (!sid_equal(&global_sam_sid, &dom_sid))
753 *puid = pdb_user_rid_to_uid(rid);
756 * Ensure this uid really does exist.
758 if(!(pass = sys_getpwuid(*puid)))
761 DEBUG(10,("local_sid_to_uid: SID %s -> uid (%u) (%s).\n", sid_to_string( str, psid),
762 (unsigned int)*puid, pass->pw_name ));
764 *name_type = SID_NAME_USER;
769 /****************************************************************************
770 Convert a gid to SID - locally.
771 ****************************************************************************/
773 DOM_SID *local_gid_to_sid(DOM_SID *psid, gid_t gid)
775 extern DOM_SID global_sam_sid;
778 sid_copy(psid, &global_sam_sid);
780 if (get_group_map_from_gid(gid, &map, MAPPING_WITHOUT_PRIV)) {
781 sid_copy(psid, &map.sid);
784 sid_append_rid(psid, pdb_gid_to_group_rid(gid));
790 /****************************************************************************
791 Convert a SID to gid - locally.
792 ****************************************************************************/
794 BOOL local_sid_to_gid(gid_t *pgid, DOM_SID *psid, enum SID_NAME_USE *name_type)
796 extern DOM_SID global_sam_sid;
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 gid if this is our local
810 * Domain SID (ie. we are the controling authority).
812 * Or in the Builtin SID too. JFM, 11/30/2001
815 if (!sid_equal(&global_sam_sid, &dom_sid))
818 if (pdb_rid_is_user(rid))
821 if (get_group_map_from_sid(*psid, &map, MAPPING_WITHOUT_PRIV)) {
823 /* the SID is in the mapping table but not mapped */
827 sid_peek_rid(&map.sid, &rid);
829 *name_type = map.sid_name_use;
831 *pgid = pdb_group_rid_to_gid(rid);
832 *name_type = SID_NAME_ALIAS;
836 * Ensure this gid really does exist.
839 if(!(grp = getgrgid(*pgid)))
842 DEBUG(10,("local_sid_to_gid: SID %s -> gid (%u) (%s).\n", sid_to_string( str, psid),
843 (unsigned int)*pgid, grp->gr_name ));
849 * Quick hack to do an easy ucs2 -> mulitbyte conversion
850 * @return static buffer containing the converted string
853 static char *pdb_convert(const UNISTR2 *from)
855 static pstring convert_buffer;
858 return convert_buffer;
861 unistr2_to_ascii(convert_buffer, from, sizeof(pstring));
862 return convert_buffer;
865 /*************************************************************
866 Copies a SAM_USER_INFO_23 to a SAM_ACCOUNT
867 **************************************************************/
869 void copy_id23_to_sam_passwd(SAM_ACCOUNT *to, SAM_USER_INFO_23 *from)
872 if (from == NULL || to == NULL)
875 pdb_set_logon_time(to,nt_time_to_unix(&from->logon_time));
876 pdb_set_logoff_time(to,nt_time_to_unix(&from->logoff_time));
877 pdb_set_kickoff_time(to, nt_time_to_unix(&from->kickoff_time));
878 pdb_set_pass_last_set_time(to, nt_time_to_unix(&from->pass_last_set_time));
879 pdb_set_pass_can_change_time(to, nt_time_to_unix(&from->pass_can_change_time));
880 pdb_set_pass_must_change_time(to, nt_time_to_unix(&from->pass_must_change_time));
882 pdb_set_username(to , pdb_convert(&from->uni_user_name ));
883 pdb_set_fullname(to , pdb_convert(&from->uni_full_name ));
884 pdb_set_homedir(to , pdb_convert(&from->uni_home_dir ), True);
885 pdb_set_dir_drive(to , pdb_convert(&from->uni_dir_drive ), True);
886 pdb_set_logon_script(to , pdb_convert(&from->uni_logon_script), True);
887 pdb_set_profile_path(to , pdb_convert(&from->uni_profile_path), True);
888 pdb_set_acct_desc(to , pdb_convert(&from->uni_acct_desc ));
889 pdb_set_workstations(to , pdb_convert(&from->uni_workstations));
890 pdb_set_unknown_str(to , pdb_convert(&from->uni_unknown_str ));
891 pdb_set_munged_dial(to , pdb_convert(&from->uni_munged_dial ));
894 pdb_set_user_rid(to, from->user_rid);
896 pdb_set_group_rid(to, from->group_rid);
898 pdb_set_acct_ctrl(to, from->acb_info);
899 pdb_set_unknown_3(to, from->unknown_3);
901 pdb_set_logon_divs(to, from->logon_divs);
902 pdb_set_hours_len(to, from->logon_hrs.len);
903 pdb_set_hours(to, from->logon_hrs.hours);
905 pdb_set_unknown_5(to, from->unknown_5);
906 pdb_set_unknown_6(to, from->unknown_6);
910 /*************************************************************
912 **************************************************************/
914 void copy_id21_to_sam_passwd(SAM_ACCOUNT *to, SAM_USER_INFO_21 *from)
916 if (from == NULL || to == NULL)
919 pdb_set_logon_time(to,nt_time_to_unix(&from->logon_time));
920 pdb_set_logoff_time(to,nt_time_to_unix(&from->logoff_time));
921 pdb_set_kickoff_time(to, nt_time_to_unix(&from->kickoff_time));
922 pdb_set_pass_last_set_time(to, nt_time_to_unix(&from->pass_last_set_time));
923 pdb_set_pass_can_change_time(to, nt_time_to_unix(&from->pass_can_change_time));
924 pdb_set_pass_must_change_time(to, nt_time_to_unix(&from->pass_must_change_time));
926 pdb_set_username(to , pdb_convert(&from->uni_user_name ));
927 pdb_set_fullname(to , pdb_convert(&from->uni_full_name ));
928 pdb_set_homedir(to , pdb_convert(&from->uni_home_dir ), True);
929 pdb_set_dir_drive(to , pdb_convert(&from->uni_dir_drive ), True);
930 pdb_set_logon_script(to , pdb_convert(&from->uni_logon_script), True);
931 pdb_set_profile_path(to , pdb_convert(&from->uni_profile_path), True);
932 pdb_set_acct_desc(to , pdb_convert(&from->uni_acct_desc ));
933 pdb_set_workstations(to , pdb_convert(&from->uni_workstations));
934 pdb_set_unknown_str(to , pdb_convert(&from->uni_unknown_str ));
935 pdb_set_munged_dial(to , pdb_convert(&from->uni_munged_dial ));
938 pdb_set_user_rid(to, from->user_rid);
940 pdb_set_group_rid(to, from->group_rid);
942 /* FIXME!! Do we need to copy the passwords here as well?
943 I don't know. Need to figure this out --jerry */
945 /* Passwords dealt with in caller --abartlet */
947 pdb_set_acct_ctrl(to, from->acb_info);
948 pdb_set_unknown_3(to, from->unknown_3);
950 pdb_set_logon_divs(to, from->logon_divs);
951 pdb_set_hours_len(to, from->logon_hrs.len);
952 pdb_set_hours(to, from->logon_hrs.hours);
954 pdb_set_unknown_5(to, from->unknown_5);
955 pdb_set_unknown_6(to, from->unknown_6);
959 /*************************************************************
960 Change a password entry in the local smbpasswd file.
962 FIXME!! The function needs to be abstracted into the
963 passdb interface or something. It is currently being called
964 by _api_samr_create_user() in rpc_server/srv_samr.c,
965 in SWAT and by smbpasswd/pdbedit.
968 *************************************************************/
970 BOOL local_password_change(const char *user_name, int local_flags,
971 const char *new_passwd,
972 char *err_str, size_t err_str_len,
973 char *msg_str, size_t msg_str_len)
975 struct passwd *pwd = NULL;
976 SAM_ACCOUNT *sam_pass=NULL;
981 /* Get the smb passwd entry for this user */
982 pdb_init_sam(&sam_pass);
983 if(!pdb_getsampwnam(sam_pass, user_name)) {
984 pdb_free_sam(&sam_pass);
986 if (local_flags & LOCAL_ADD_USER) {
988 * Check for a local account - if we're adding only.
991 if(!(pwd = sys_getpwnam(user_name))) {
992 slprintf(err_str, err_str_len - 1, "User %s does not \
993 exist in system password file (usually /etc/passwd). Cannot add \
994 account without a valid local system user.\n", user_name);
998 slprintf(err_str, err_str_len-1,"Failed to find entry for user %s.\n", user_name);
1002 if (!NT_STATUS_IS_OK(pdb_init_sam_pw(&sam_pass, pwd))){
1003 slprintf(err_str, err_str_len-1, "Failed initialise SAM_ACCOUNT for user %s.\n", user_name);
1008 if (local_flags & LOCAL_TRUST_ACCOUNT) {
1009 if (!pdb_set_acct_ctrl(sam_pass, ACB_WSTRUST)) {
1010 slprintf(err_str, err_str_len - 1, "Failed to set 'trusted workstation account' flags for user %s.\n", user_name);
1011 pdb_free_sam(&sam_pass);
1014 } else if (local_flags & LOCAL_INTERDOM_ACCOUNT) {
1015 if (!pdb_set_acct_ctrl(sam_pass, ACB_DOMTRUST)) {
1016 slprintf(err_str, err_str_len - 1, "Failed to set 'domain trust account' flags for user %s.\n", user_name);
1017 pdb_free_sam(&sam_pass);
1021 if (!pdb_set_acct_ctrl(sam_pass, ACB_NORMAL)) {
1022 slprintf(err_str, err_str_len - 1, "Failed to set 'normal account' flags for user %s.\n", user_name);
1023 pdb_free_sam(&sam_pass);
1029 /* the entry already existed */
1030 local_flags &= ~LOCAL_ADD_USER;
1034 * We are root - just write the new password
1035 * and the valid last change time.
1038 if (local_flags & LOCAL_DISABLE_USER) {
1039 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)|ACB_DISABLED)) {
1040 slprintf(err_str, err_str_len-1, "Failed to set 'disabled' flag for user %s.\n", user_name);
1041 pdb_free_sam(&sam_pass);
1044 } else if (local_flags & LOCAL_ENABLE_USER) {
1045 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_DISABLED))) {
1046 slprintf(err_str, err_str_len-1, "Failed to unset 'disabled' flag for user %s.\n", user_name);
1047 pdb_free_sam(&sam_pass);
1052 if (local_flags & LOCAL_SET_NO_PASSWORD) {
1053 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)|ACB_PWNOTREQ)) {
1054 slprintf(err_str, err_str_len-1, "Failed to set 'no password required' flag for user %s.\n", user_name);
1055 pdb_free_sam(&sam_pass);
1058 } else if (local_flags & LOCAL_SET_PASSWORD) {
1060 * If we're dealing with setting a completely empty user account
1061 * ie. One with a password of 'XXXX', but not set disabled (like
1062 * an account created from scratch) then if the old password was
1063 * 'XX's then getsmbpwent will have set the ACB_DISABLED flag.
1064 * We remove that as we're giving this user their first password
1065 * and the decision hasn't really been made to disable them (ie.
1066 * don't create them disabled). JRA.
1068 if ((pdb_get_lanman_passwd(sam_pass)==NULL) && (pdb_get_acct_ctrl(sam_pass)&ACB_DISABLED)) {
1069 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_DISABLED))) {
1070 slprintf(err_str, err_str_len-1, "Failed to unset 'disabled' flag for user %s.\n", user_name);
1071 pdb_free_sam(&sam_pass);
1075 if (!pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_PWNOTREQ))) {
1076 slprintf(err_str, err_str_len-1, "Failed to unset 'no password required' flag for user %s.\n", user_name);
1077 pdb_free_sam(&sam_pass);
1081 if (!pdb_set_plaintext_passwd (sam_pass, new_passwd)) {
1082 slprintf(err_str, err_str_len-1, "Failed to set password for user %s.\n", user_name);
1083 pdb_free_sam(&sam_pass);
1088 if (local_flags & LOCAL_ADD_USER) {
1089 if (pdb_add_sam_account(sam_pass)) {
1090 slprintf(msg_str, msg_str_len-1, "Added user %s.\n", user_name);
1091 pdb_free_sam(&sam_pass);
1094 slprintf(err_str, err_str_len-1, "Failed to add entry for user %s.\n", user_name);
1095 pdb_free_sam(&sam_pass);
1098 } else if (local_flags & LOCAL_DELETE_USER) {
1099 if (!pdb_delete_sam_account(user_name)) {
1100 slprintf(err_str,err_str_len-1, "Failed to delete entry for user %s.\n", user_name);
1101 pdb_free_sam(&sam_pass);
1104 slprintf(msg_str, msg_str_len-1, "Deleted user %s.\n", user_name);
1106 if(!pdb_update_sam_account(sam_pass, True)) {
1107 slprintf(err_str, err_str_len-1, "Failed to modify entry for user %s.\n", user_name);
1108 pdb_free_sam(&sam_pass);
1111 if(local_flags & LOCAL_DISABLE_USER)
1112 slprintf(msg_str, msg_str_len-1, "Disabled user %s.\n", user_name);
1113 else if (local_flags & LOCAL_ENABLE_USER)
1114 slprintf(msg_str, msg_str_len-1, "Enabled user %s.\n", user_name);
1115 else if (local_flags & LOCAL_SET_NO_PASSWORD)
1116 slprintf(msg_str, msg_str_len-1, "User %s password set to none.\n", user_name);
1119 pdb_free_sam(&sam_pass);
1123 /***************************************************************************
1124 Search by uid. Wrapper around pdb_getsampwnam()
1125 **************************************************************************/
1127 BOOL pdb_getsampwuid (SAM_ACCOUNT* user, uid_t uid)
1133 DEBUG(0,("pdb_getsampwuid: SAM_ACCOUNT is NULL.\n"));
1138 * Never trust the uid in the passdb. Lookup the username first
1139 * and then lokup the user by name in the sam.
1142 if ((pw=sys_getpwuid(uid)) == NULL) {
1143 DEBUG(0,("pdb_getsampwuid: getpwuid(%d) return NULL. User does not exist in Unix accounts!\n", uid));
1147 fstrcpy (name, pw->pw_name);
1149 return pdb_getsampwnam (user, name);