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
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 struct passdb_ops
*pdb_ops
;
37 static void* pdb_handle
= NULL
;
40 /***************************************************************
41 Initialize the password db operations.
42 ***************************************************************/
44 BOOL
initialize_password_db(BOOL reload
)
47 * This function is unfinished right now, so just
48 * ignore the details and always return True. It
49 * is here only as a placeholder --jerry
55 /*************************************************************
56 Initialises a struct sam_disp_info.
57 **************************************************************/
59 static void pdb_init_dispinfo(struct sam_disp_info
*user
)
67 /************************************************************
68 Fill the SAM_ACCOUNT with default values.
69 ***********************************************************/
71 static BOOL
pdb_fill_default_sam(SAM_ACCOUNT
*user
)
74 DEBUG(0,("pdb_fill_default_sam: SAM_ACCOUNT was NULL\n"));
80 user
->init_flag
= FLAG_SAM_UNINIT
;
81 user
->uid
= user
->gid
= -1;
82 user
->logon_time
= (time_t)0;
83 user
->pass_last_set_time
= (time_t)0;
84 user
->pass_can_change_time
= (time_t)0;
87 user
->pass_must_change_time
= get_time_t_max(); /* Password never expires. */
89 user
->unknown_3
= 0x00ffffff; /* don't know */
90 user
->logon_divs
= 168; /* hours per week */
91 user
->hours_len
= 21; /* 21 times 8 bits = 168 */
92 memset(user
->hours
, 0xff, user
->hours_len
); /* available at all hours */
93 user
->unknown_5
= 0x00000000; /* don't know */
94 user
->unknown_6
= 0x000004ec; /* don't know */
99 /*************************************************************
100 Alloc memory and initialises a struct sam_passwd.
101 ************************************************************/
103 BOOL
pdb_init_sam(SAM_ACCOUNT
**user
)
106 DEBUG(0,("pdb_init_sam: SAM_ACCOUNT was non NULL\n"));
108 smb_panic("NULL pointer passed to pdb_init_sam\n");
113 *user
=(SAM_ACCOUNT
*)malloc(sizeof(SAM_ACCOUNT
));
116 DEBUG(0,("pdb_init_sam: error while allocating memory\n"));
120 pdb_fill_default_sam(*user
);
126 /*************************************************************
127 Initialises a struct sam_passwd with sane values.
128 ************************************************************/
130 BOOL
pdb_init_sam_pw(SAM_ACCOUNT
**new_sam_acct
, struct passwd
*pwd
)
137 if (!pdb_init_sam(new_sam_acct
)) {
143 pdb_set_username(*new_sam_acct
, pwd
->pw_name
);
144 pdb_set_fullname(*new_sam_acct
, pwd
->pw_gecos
);
145 pdb_set_uid(*new_sam_acct
, pwd
->pw_uid
);
146 pdb_set_gid(*new_sam_acct
, pwd
->pw_gid
);
147 pdb_set_profile_path(*new_sam_acct
, lp_logon_path());
148 pdb_set_homedir(*new_sam_acct
, lp_logon_home());
149 pdb_set_dir_drive(*new_sam_acct
, lp_logon_drive());
150 pdb_set_logon_script(*new_sam_acct
, lp_logon_script());
155 /************************************************************
156 Free the NT/LM hashes only.
157 ***********************************************************/
159 static BOOL
pdb_free_sam_contents(SAM_ACCOUNT
*user
)
162 DEBUG(0,("pdb_free_sam_contents: SAM_ACCOUNT was NULL\n"));
164 smb_panic("NULL pointer passed to pdb_free_sam\n");
169 /* As we start mallocing more strings this is where
170 we should free them. */
172 SAFE_FREE(user
->nt_pw
);
173 SAFE_FREE(user
->lm_pw
);
179 /************************************************************
180 Reset the SAM_ACCOUNT and free the NT/LM hashes.
181 - note: they are not zero'ed out however.
182 ***********************************************************/
184 BOOL
pdb_reset_sam(SAM_ACCOUNT
*user
)
187 DEBUG(0,("pdb_reset_sam: SAM_ACCOUNT was NULL\n"));
191 if (!pdb_free_sam_contents(user
)) {
195 if (!pdb_fill_default_sam(user
)) {
203 /************************************************************
204 Free the SAM_ACCOUNT and the NT/LM hashes.
205 ***********************************************************/
207 BOOL
pdb_free_sam(SAM_ACCOUNT
*user
)
210 DEBUG(0,("pdb_free_sam: SAM_ACCOUNT was NULL\n"));
212 smb_panic("NULL pointer passed to pdb_free_sam\n");
217 if (!pdb_free_sam_contents(user
)) {
227 /*************************************************************************
228 Routine to return the next entry in the sam passwd list.
229 *************************************************************************/
231 struct sam_disp_info
*pdb_sam_to_dispinfo(SAM_ACCOUNT
*user
)
233 static struct sam_disp_info disp_info
;
238 pdb_init_dispinfo(&disp_info
);
240 disp_info
.smb_name
= user
->username
;
241 disp_info
.full_name
= user
->full_name
;
242 disp_info
.user_rid
= user
->user_rid
;
247 /**********************************************************
248 Encode the account control bits into a string.
249 length = length of string to encode into (including terminating
250 null). length *MUST BE MORE THAN 2* !
251 **********************************************************/
253 char *pdb_encode_acct_ctrl(uint16 acct_ctrl
, size_t length
)
255 static fstring acct_str
;
260 if (acct_ctrl
& ACB_PWNOTREQ
) acct_str
[i
++] = 'N';
261 if (acct_ctrl
& ACB_DISABLED
) acct_str
[i
++] = 'D';
262 if (acct_ctrl
& ACB_HOMDIRREQ
) acct_str
[i
++] = 'H';
263 if (acct_ctrl
& ACB_TEMPDUP
) acct_str
[i
++] = 'T';
264 if (acct_ctrl
& ACB_NORMAL
) acct_str
[i
++] = 'U';
265 if (acct_ctrl
& ACB_MNS
) acct_str
[i
++] = 'M';
266 if (acct_ctrl
& ACB_WSTRUST
) acct_str
[i
++] = 'W';
267 if (acct_ctrl
& ACB_SVRTRUST
) acct_str
[i
++] = 'S';
268 if (acct_ctrl
& ACB_AUTOLOCK
) acct_str
[i
++] = 'L';
269 if (acct_ctrl
& ACB_PWNOEXP
) acct_str
[i
++] = 'X';
270 if (acct_ctrl
& ACB_DOMTRUST
) acct_str
[i
++] = 'I';
272 for ( ; i
< length
- 2 ; i
++ )
277 acct_str
[i
++] = '\0';
282 /**********************************************************
283 Decode the account control bits from a string.
284 **********************************************************/
286 uint16
pdb_decode_acct_ctrl(const char *p
)
288 uint16 acct_ctrl
= 0;
289 BOOL finished
= False
;
292 * Check if the account type bits have been encoded after the
293 * NT password (in the form [NDHTUWSLXI]).
299 for (p
++; *p
&& !finished
; p
++) {
301 case 'N': { acct_ctrl
|= ACB_PWNOTREQ
; break; /* 'N'o password. */ }
302 case 'D': { acct_ctrl
|= ACB_DISABLED
; break; /* 'D'isabled. */ }
303 case 'H': { acct_ctrl
|= ACB_HOMDIRREQ
; break; /* 'H'omedir required. */ }
304 case 'T': { acct_ctrl
|= ACB_TEMPDUP
; break; /* 'T'emp account. */ }
305 case 'U': { acct_ctrl
|= ACB_NORMAL
; break; /* 'U'ser account (normal). */ }
306 case 'M': { acct_ctrl
|= ACB_MNS
; break; /* 'M'NS logon user account. What is this ? */ }
307 case 'W': { acct_ctrl
|= ACB_WSTRUST
; break; /* 'W'orkstation account. */ }
308 case 'S': { acct_ctrl
|= ACB_SVRTRUST
; break; /* 'S'erver account. */ }
309 case 'L': { acct_ctrl
|= ACB_AUTOLOCK
; break; /* 'L'ocked account. */ }
310 case 'X': { acct_ctrl
|= ACB_PWNOEXP
; break; /* No 'X'piry on password */ }
311 case 'I': { acct_ctrl
|= ACB_DOMTRUST
; break; /* 'I'nterdomain trust account. */ }
317 default: { finished
= True
; }
324 /*************************************************************
325 Routine to set 32 hex password characters from a 16 byte array.
326 **************************************************************/
328 void pdb_sethexpwd(char *p
, unsigned char *pwd
, uint16 acct_ctrl
)
332 for (i
= 0; i
< 16; i
++)
333 slprintf(&p
[i
*2], 3, "%02X", pwd
[i
]);
335 if (acct_ctrl
& ACB_PWNOTREQ
)
336 safe_strcpy(p
, "NO PASSWORDXXXXXXXXXXXXXXXXXXXXX", 33);
338 safe_strcpy(p
, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 33);
342 /*************************************************************
343 Routine to get the 32 hex characters and turn them
344 into a 16 byte array.
345 **************************************************************/
347 BOOL
pdb_gethexpwd(char *p
, unsigned char *pwd
)
350 unsigned char lonybble
, hinybble
;
351 char *hexchars
= "0123456789ABCDEF";
357 for (i
= 0; i
< 32; i
+= 2) {
358 hinybble
= toupper(p
[i
]);
359 lonybble
= toupper(p
[i
+ 1]);
361 p1
= strchr(hexchars
, hinybble
);
362 p2
= strchr(hexchars
, lonybble
);
367 hinybble
= PTR_DIFF(p1
, hexchars
);
368 lonybble
= PTR_DIFF(p2
, hexchars
);
370 pwd
[i
/ 2] = (hinybble
<< 4) | lonybble
;
375 /*******************************************************************
376 Group and User RID username mapping function
377 ********************************************************************/
379 BOOL
pdb_name_to_rid(char *user_name
, uint32
*u_rid
, uint32
*g_rid
)
381 struct passwd
*pw
= Get_Pwnam(user_name
, False
);
383 if (u_rid
== NULL
|| g_rid
== NULL
|| user_name
== NULL
)
387 DEBUG(1,("Username %s is invalid on this system\n", user_name
));
391 /* turn the unix UID into a Domain RID. this is what the posix
392 sub-system does (adds 1000 to the uid) */
393 *u_rid
= pdb_uid_to_user_rid(pw
->pw_uid
);
395 /* absolutely no idea what to do about the unix GID to Domain RID mapping */
396 *g_rid
= pdb_gid_to_group_rid(pw
->pw_gid
);
401 /*******************************************************************
402 Converts NT user RID to a UNIX uid.
403 ********************************************************************/
405 uid_t
pdb_user_rid_to_uid(uint32 user_rid
)
407 return (uid_t
)(((user_rid
& (~USER_RID_TYPE
))- 1000)/RID_MULTIPLIER
);
410 /*******************************************************************
411 Converts NT user RID to a UNIX gid.
412 ********************************************************************/
414 gid_t
pdb_user_rid_to_gid(uint32 user_rid
)
416 return (uid_t
)(((user_rid
& (~GROUP_RID_TYPE
))- 1000)/RID_MULTIPLIER
);
419 /*******************************************************************
420 converts UNIX uid to an NT User RID.
421 ********************************************************************/
423 uint32
pdb_uid_to_user_rid(uid_t uid
)
425 return (((((uint32
)uid
)*RID_MULTIPLIER
) + 1000) | USER_RID_TYPE
);
428 /*******************************************************************
429 converts NT Group RID to a UNIX uid.
430 ********************************************************************/
432 uint32
pdb_gid_to_group_rid(gid_t gid
)
434 return (((((uint32
)gid
)*RID_MULTIPLIER
) + 1000) | GROUP_RID_TYPE
);
437 /*******************************************************************
438 Decides if a RID is a well known RID.
439 ********************************************************************/
441 static BOOL
pdb_rid_is_well_known(uint32 rid
)
446 /*******************************************************************
447 Decides if a RID is a user or group RID.
448 ********************************************************************/
450 BOOL
pdb_rid_is_user(uint32 rid
)
452 /* lkcl i understand that NT attaches an enumeration to a RID
453 * such that it can be identified as either a user, group etc
454 * type. there are 5 such categories, and they are documented.
456 if(pdb_rid_is_well_known(rid
)) {
458 * The only well known user RIDs are DOMAIN_USER_RID_ADMIN
459 * and DOMAIN_USER_RID_GUEST.
461 if(rid
== DOMAIN_USER_RID_ADMIN
|| rid
== DOMAIN_USER_RID_GUEST
)
463 } else if((rid
& RID_TYPE_MASK
) == USER_RID_TYPE
) {
469 /*******************************************************************
470 Convert a rid into a name. Used in the lookup SID rpc.
471 ********************************************************************/
473 BOOL
local_lookup_rid(uint32 rid
, char *name
, enum SID_NAME_USE
*psid_name_use
)
475 BOOL is_user
= pdb_rid_is_user(rid
);
477 *psid_name_use
= SID_NAME_UNKNOWN
;
479 DEBUG(5,("local_lookup_rid: looking up %s RID %u.\n", is_user
? "user" :
480 "group", (unsigned int)rid
));
483 if(rid
== DOMAIN_USER_RID_ADMIN
) {
485 char *p
= admin_users
;
486 *psid_name_use
= SID_NAME_USER
;
487 if(!next_token(&p
, name
, NULL
, sizeof(fstring
)))
488 fstrcpy(name
, "Administrator");
489 } else if (rid
== DOMAIN_USER_RID_GUEST
) {
491 char *p
= guest_users
;
492 *psid_name_use
= SID_NAME_USER
;
493 if(!next_token(&p
, name
, NULL
, sizeof(fstring
)))
494 fstrcpy(name
, "Guest");
500 * Don't try to convert the rid to a name if
501 * running in appliance mode
503 if (lp_hide_local_users())
506 uid
= pdb_user_rid_to_uid(rid
);
507 pass
= sys_getpwuid(uid
);
509 *psid_name_use
= SID_NAME_USER
;
511 DEBUG(5,("local_lookup_rid: looking up uid %u %s\n", (unsigned int)uid
,
512 pass
? "succeeded" : "failed" ));
515 slprintf(name
, sizeof(fstring
)-1, "unix_user.%u", (unsigned int)uid
);
519 fstrcpy(name
, pass
->pw_name
);
521 DEBUG(5,("local_lookup_rid: found user %s for rid %u\n", name
,
522 (unsigned int)rid
));
530 * Don't try to convert the rid to a name if running
534 if (lp_hide_local_users())
537 gid
= pdb_user_rid_to_gid(rid
);
540 *psid_name_use
= SID_NAME_ALIAS
;
542 DEBUG(5,("local_local_rid: looking up gid %u %s\n", (unsigned int)gid
,
543 gr
? "succeeded" : "failed" ));
546 slprintf(name
, sizeof(fstring
)-1, "unix_group.%u", (unsigned int)gid
);
550 fstrcpy( name
, gr
->gr_name
);
552 DEBUG(5,("local_lookup_rid: found group %s for rid %u\n", name
,
553 (unsigned int)rid
));
559 /*******************************************************************
560 Convert a name into a SID. Used in the lookup name rpc.
561 ********************************************************************/
563 BOOL
local_lookup_name(const char *c_domain
, const char *c_user
, DOM_SID
*psid
, enum SID_NAME_USE
*psid_name_use
)
565 extern DOM_SID global_sid_World_Domain
;
566 struct passwd
*pass
= NULL
;
571 *psid_name_use
= SID_NAME_UNKNOWN
;
574 * domain and user may be quoted const strings, and map_username and
575 * friends can modify them. Make a modifiable copy. JRA.
578 fstrcpy(domain
, c_domain
);
579 fstrcpy(user
, c_user
);
581 sid_copy(&local_sid
, &global_sam_sid
);
584 * Special case for MACHINE\Everyone. Map to the world_sid.
587 if(strequal(user
, "Everyone")) {
588 sid_copy( psid
, &global_sid_World_Domain
);
589 sid_append_rid(psid
, 0);
590 *psid_name_use
= SID_NAME_ALIAS
;
595 * Don't lookup local unix users if running in appliance mode
597 if (lp_hide_local_users())
600 (void)map_username(user
);
602 if((pass
= Get_Pwnam(user
, False
))) {
603 sid_append_rid( &local_sid
, pdb_uid_to_user_rid(pass
->pw_uid
));
604 *psid_name_use
= SID_NAME_USER
;
607 * Maybe it was a group ?
609 struct group
*grp
= getgrnam(user
);
614 sid_append_rid( &local_sid
, pdb_gid_to_group_rid(grp
->gr_gid
));
615 *psid_name_use
= SID_NAME_ALIAS
;
618 sid_copy( psid
, &local_sid
);
623 /****************************************************************************
624 Convert a uid to SID - locally.
625 ****************************************************************************/
627 DOM_SID
*local_uid_to_sid(DOM_SID
*psid
, uid_t uid
)
629 extern DOM_SID global_sam_sid
;
631 sid_copy(psid
, &global_sam_sid
);
632 sid_append_rid(psid
, pdb_uid_to_user_rid(uid
));
637 /****************************************************************************
638 Convert a SID to uid - locally.
639 ****************************************************************************/
641 BOOL
local_sid_to_uid(uid_t
*puid
, DOM_SID
*psid
, enum SID_NAME_USE
*name_type
)
643 extern DOM_SID global_sam_sid
;
650 *name_type
= SID_NAME_UNKNOWN
;
652 sid_copy(&dom_sid
, psid
);
653 sid_split_rid(&dom_sid
, &rid
);
655 if (!pdb_rid_is_user(rid
))
659 * We can only convert to a uid if this is our local
660 * Domain SID (ie. we are the controling authority).
662 if (!sid_equal(&global_sam_sid
, &dom_sid
))
665 *puid
= pdb_user_rid_to_uid(rid
);
668 * Ensure this uid really does exist.
670 if(!(pass
= sys_getpwuid(*puid
)))
673 DEBUG(10,("local_sid_to_uid: SID %s -> uid (%u) (%s).\n", sid_to_string( str
, psid
),
674 (unsigned int)*puid
, pass
->pw_name
));
676 *name_type
= SID_NAME_USER
;
680 /****************************************************************************
681 Convert a gid to SID - locally.
682 ****************************************************************************/
684 DOM_SID
*local_gid_to_sid(DOM_SID
*psid
, gid_t gid
)
686 extern DOM_SID global_sam_sid
;
688 sid_copy(psid
, &global_sam_sid
);
689 sid_append_rid(psid
, pdb_gid_to_group_rid(gid
));
694 /****************************************************************************
695 Convert a SID to gid - locally.
696 ****************************************************************************/
698 BOOL
local_sid_to_gid(gid_t
*pgid
, DOM_SID
*psid
, enum SID_NAME_USE
*name_type
)
700 extern DOM_SID global_sam_sid
;
706 *name_type
= SID_NAME_UNKNOWN
;
708 sid_copy(&dom_sid
, psid
);
709 sid_split_rid(&dom_sid
, &rid
);
712 * We can only convert to a gid if this is our local
713 * Domain SID (ie. we are the controling authority).
716 if (!sid_equal(&global_sam_sid
, &dom_sid
))
719 if (pdb_rid_is_user(rid
))
722 *pgid
= pdb_user_rid_to_gid(rid
);
725 * Ensure this gid really does exist.
728 if(!(grp
= getgrgid(*pgid
)))
731 *name_type
= SID_NAME_ALIAS
;
733 DEBUG(10,("local_sid_to_gid: SID %s -> gid (%u) (%s).\n", sid_to_string( str
, psid
),
734 (unsigned int)*pgid
, grp
->gr_name
));
739 static void select_name(pstring string
, const UNISTR2
*from
)
741 if (from
->buffer
!= 0)
742 unistr2_to_ascii(string
, from
, sizeof(pstring
));
745 /*************************************************************
746 Copies a SAM_USER_INFO_23 to a SAM_ACCOUNT
747 **************************************************************/
749 void copy_id23_to_sam_passwd(SAM_ACCOUNT
*to
, SAM_USER_INFO_23
*from
)
752 if (from
== NULL
|| to
== NULL
)
755 to
->logon_time
= nt_time_to_unix(&from
->logon_time
);
756 to
->logoff_time
= nt_time_to_unix(&from
->logoff_time
);
757 to
->kickoff_time
= nt_time_to_unix(&from
->kickoff_time
);
758 to
->pass_last_set_time
= nt_time_to_unix(&from
->pass_last_set_time
);
759 to
->pass_can_change_time
= nt_time_to_unix(&from
->pass_can_change_time
);
760 to
->pass_must_change_time
= nt_time_to_unix(&from
->pass_must_change_time
);
762 select_name(to
->username
, &from
->uni_user_name
);
763 select_name(to
->full_name
, &from
->uni_full_name
);
764 select_name(to
->home_dir
, &from
->uni_home_dir
);
765 select_name(to
->dir_drive
, &from
->uni_dir_drive
);
766 select_name(to
->logon_script
, &from
->uni_logon_script
);
767 select_name(to
->profile_path
, &from
->uni_profile_path
);
768 select_name(to
->acct_desc
, &from
->uni_acct_desc
);
769 select_name(to
->workstations
, &from
->uni_workstations
);
770 select_name(to
->unknown_str
, &from
->uni_unknown_str
);
771 select_name(to
->munged_dial
, &from
->uni_munged_dial
);
773 to
->user_rid
= from
->user_rid
;
774 to
->group_rid
= from
->group_rid
;
776 to
->acct_ctrl
= from
->acb_info
;
777 to
->unknown_3
= from
->unknown_3
;
779 to
->logon_divs
= from
->logon_divs
;
780 to
->hours_len
= from
->logon_hrs
.len
;
781 memcpy(to
->hours
, from
->logon_hrs
.hours
, MAX_HOURS_LEN
);
783 to
->unknown_5
= from
->unknown_5
;
784 to
->unknown_6
= from
->unknown_6
;
787 /*************************************************************
789 **************************************************************/
791 void copy_id21_to_sam_passwd(SAM_ACCOUNT
*to
, SAM_USER_INFO_21
*from
)
793 if (from
== NULL
|| to
== NULL
)
796 to
->logon_time
= nt_time_to_unix(&from
->logon_time
);
797 to
->logoff_time
= nt_time_to_unix(&from
->logoff_time
);
798 to
->kickoff_time
= nt_time_to_unix(&from
->kickoff_time
);
799 to
->pass_last_set_time
= nt_time_to_unix(&from
->pass_last_set_time
);
800 to
->pass_can_change_time
= nt_time_to_unix(&from
->pass_can_change_time
);
801 to
->pass_must_change_time
= nt_time_to_unix(&from
->pass_must_change_time
);
803 select_name(to
->username
, &from
->uni_user_name
);
804 select_name(to
->full_name
, &from
->uni_full_name
);
805 select_name(to
->home_dir
, &from
->uni_home_dir
);
806 select_name(to
->dir_drive
, &from
->uni_dir_drive
);
807 select_name(to
->logon_script
, &from
->uni_logon_script
);
808 select_name(to
->profile_path
, &from
->uni_profile_path
);
809 select_name(to
->acct_desc
, &from
->uni_acct_desc
);
810 select_name(to
->workstations
, &from
->uni_workstations
);
811 select_name(to
->unknown_str
, &from
->uni_unknown_str
);
812 select_name(to
->munged_dial
, &from
->uni_munged_dial
);
814 to
->user_rid
= from
->user_rid
;
815 to
->group_rid
= from
->group_rid
;
817 /* FIXME!! Do we need to copy the passwords here as well?
818 I don't know. Need to figure this out --jerry */
820 to
->acct_ctrl
= from
->acb_info
;
821 to
->unknown_3
= from
->unknown_3
;
823 to
->logon_divs
= from
->logon_divs
;
824 to
->hours_len
= from
->logon_hrs
.len
;
825 memcpy(to
->hours
, from
->logon_hrs
.hours
, MAX_HOURS_LEN
);
827 to
->unknown_5
= from
->unknown_5
;
828 to
->unknown_6
= from
->unknown_6
;
831 /*************************************************************
832 Copies a SAM_ACCOUNT.
833 **************************************************************/
835 void copy_sam_passwd(SAM_ACCOUNT
*to
, const SAM_ACCOUNT
*from
)
840 memcpy(to
, from
, sizeof(SAM_ACCOUNT
));
843 /*************************************************************
844 Change a password entry in the local smbpasswd file.
846 FIXME!! The function needs to be abstracted into the
847 passdb interface or something. It is currently being called
848 by _api_samr_create_user() in rpc_server/srv_samr.c
851 *************************************************************/
853 BOOL
local_password_change(char *user_name
, int local_flags
,
855 char *err_str
, size_t err_str_len
,
856 char *msg_str
, size_t msg_str_len
)
858 struct passwd
*pwd
= NULL
;
859 SAM_ACCOUNT
*sam_pass
=NULL
;
864 if (local_flags
& LOCAL_ADD_USER
) {
867 * Check for a local account - if we're adding only.
870 if(!(pwd
= sys_getpwnam(user_name
))) {
871 slprintf(err_str
, err_str_len
- 1, "User %s does not \
872 exist in system password file (usually /etc/passwd). Cannot add \
873 account without a valid local system user.\n", user_name
);
878 /* Get the smb passwd entry for this user */
879 pdb_init_sam(&sam_pass
);
880 if(!pdb_getsampwnam(sam_pass
, user_name
)) {
881 pdb_free_sam(sam_pass
);
883 if(!(local_flags
& LOCAL_ADD_USER
)) {
884 slprintf(err_str
, err_str_len
-1,"Failed to find entry for user %s.\n", user_name
);
889 if (!pdb_init_sam_pw(&sam_pass
, pwd
)) {
893 /* Set account flags. Note that the default is non-expiring accounts */
894 pdb_set_acct_ctrl(sam_pass
,(local_flags
& LOCAL_TRUST_ACCOUNT
) ? ACB_WSTRUST
: ACB_NORMAL
|ACB_PWNOEXP
);
896 if (local_flags
& LOCAL_DISABLE_USER
)
897 pdb_set_acct_ctrl (sam_pass
, pdb_get_acct_ctrl(sam_pass
)|ACB_DISABLED
);
899 if (local_flags
& LOCAL_SET_NO_PASSWORD
) {
900 pdb_set_acct_ctrl (sam_pass
, pdb_get_acct_ctrl(sam_pass
)|ACB_PWNOTREQ
);
902 /* set the passwords here. if we get to here it means
903 we have a valid, active account */
904 if (!pdb_set_plaintext_passwd (sam_pass
, new_passwd
)) {
905 pdb_free_sam(sam_pass
);
910 if (pdb_add_sam_account(sam_pass
)) {
911 slprintf(msg_str
, msg_str_len
-1, "Added user %s.\n", user_name
);
912 pdb_free_sam(sam_pass
);
915 slprintf(err_str
, err_str_len
-1, "Failed to add entry for user %s.\n", user_name
);
916 pdb_free_sam(sam_pass
);
920 /* the entry already existed */
921 local_flags
&= ~LOCAL_ADD_USER
;
925 * We are root - just write the new password
926 * and the valid last change time.
929 if(local_flags
& LOCAL_DISABLE_USER
) {
930 pdb_set_acct_ctrl (sam_pass
, pdb_get_acct_ctrl(sam_pass
)|ACB_DISABLED
);
931 } else if (local_flags
& LOCAL_ENABLE_USER
) {
932 if(pdb_get_lanman_passwd(sam_pass
) == NULL
) {
933 if (!pdb_set_plaintext_passwd (sam_pass
, new_passwd
)) {
934 pdb_free_sam(sam_pass
);
938 pdb_set_acct_ctrl (sam_pass
, pdb_get_acct_ctrl(sam_pass
)&(~ACB_DISABLED
));
939 } else if (local_flags
& LOCAL_SET_NO_PASSWORD
) {
940 pdb_set_acct_ctrl (sam_pass
, pdb_get_acct_ctrl(sam_pass
)|ACB_PWNOTREQ
);
942 /* This is needed to preserve ACB_PWNOTREQ in mod_smbfilepwd_entry */
943 if (!pdb_set_lanman_passwd (sam_pass
, NULL
)) {
944 pdb_free_sam(sam_pass
);
947 if (!pdb_set_nt_passwd(sam_pass
, NULL
)) {
948 pdb_free_sam(sam_pass
);
953 * If we're dealing with setting a completely empty user account
954 * ie. One with a password of 'XXXX', but not set disabled (like
955 * an account created from scratch) then if the old password was
956 * 'XX's then getsmbpwent will have set the ACB_DISABLED flag.
957 * We remove that as we're giving this user their first password
958 * and the decision hasn't really been made to disable them (ie.
959 * don't create them disabled). JRA.
961 if ((pdb_get_lanman_passwd(sam_pass
)==NULL
) && (pdb_get_acct_ctrl(sam_pass
)&ACB_DISABLED
))
962 pdb_set_acct_ctrl (sam_pass
, pdb_get_acct_ctrl(sam_pass
)&(~ACB_DISABLED
));
963 pdb_set_acct_ctrl (sam_pass
, pdb_get_acct_ctrl(sam_pass
)&(~ACB_PWNOTREQ
));
964 if (!pdb_set_plaintext_passwd (sam_pass
, new_passwd
)) {
965 pdb_free_sam(sam_pass
);
970 if(local_flags
& LOCAL_DELETE_USER
) {
971 if (!pdb_delete_sam_account(user_name
)) {
972 slprintf(err_str
,err_str_len
-1, "Failed to delete entry for user %s.\n", user_name
);
973 pdb_free_sam(sam_pass
);
976 slprintf(msg_str
, msg_str_len
-1, "Deleted user %s.\n", user_name
);
978 if(!pdb_update_sam_account(sam_pass
, True
)) {
979 slprintf(err_str
, err_str_len
-1, "Failed to modify entry for user %s.\n", user_name
);
980 pdb_free_sam(sam_pass
);
983 if(local_flags
& LOCAL_DISABLE_USER
)
984 slprintf(msg_str
, msg_str_len
-1, "Disabled user %s.\n", user_name
);
985 else if (local_flags
& LOCAL_ENABLE_USER
)
986 slprintf(msg_str
, msg_str_len
-1, "Enabled user %s.\n", user_name
);
987 else if (local_flags
& LOCAL_SET_NO_PASSWORD
)
988 slprintf(msg_str
, msg_str_len
-1, "User %s password set to none.\n", user_name
);
991 pdb_free_sam(sam_pass
);
995 /*********************************************************************
996 Collection of get...() functions for SAM_ACCOUNT_INFO.
997 ********************************************************************/
999 uint16
pdb_get_acct_ctrl (SAM_ACCOUNT
*sampass
)
1002 return (sampass
->acct_ctrl
);
1004 return (ACB_DISABLED
);
1007 time_t pdb_get_logon_time (SAM_ACCOUNT
*sampass
)
1010 return (sampass
->logon_time
);
1015 time_t pdb_get_logoff_time (SAM_ACCOUNT
*sampass
)
1018 return (sampass
->logoff_time
);
1023 time_t pdb_get_kickoff_time (SAM_ACCOUNT
*sampass
)
1026 return (sampass
->kickoff_time
);
1031 time_t pdb_get_pass_last_set_time (SAM_ACCOUNT
*sampass
)
1034 return (sampass
->pass_last_set_time
);
1039 time_t pdb_get_pass_can_change_time (SAM_ACCOUNT
*sampass
)
1042 return (sampass
->pass_can_change_time
);
1047 time_t pdb_get_pass_must_change_time (SAM_ACCOUNT
*sampass
)
1050 return (sampass
->pass_must_change_time
);
1055 uint16
pdb_get_logon_divs (SAM_ACCOUNT
*sampass
)
1058 return (sampass
->logon_divs
);
1063 uint32
pdb_get_hours_len (SAM_ACCOUNT
*sampass
)
1066 return (sampass
->hours_len
);
1071 uint8
* pdb_get_hours (SAM_ACCOUNT
*sampass
)
1074 return (sampass
->hours
);
1079 uint8
* pdb_get_nt_passwd (SAM_ACCOUNT
*sampass
)
1082 return (sampass
->nt_pw
);
1087 uint8
* pdb_get_lanman_passwd (SAM_ACCOUNT
*sampass
)
1090 return (sampass
->lm_pw
);
1095 uint32
pdb_get_user_rid (SAM_ACCOUNT
*sampass
)
1098 return (sampass
->user_rid
);
1103 uint32
pdb_get_group_rid (SAM_ACCOUNT
*sampass
)
1106 return (sampass
->group_rid
);
1111 uid_t
pdb_get_uid (SAM_ACCOUNT
*sampass
)
1114 return (sampass
->uid
);
1119 gid_t
pdb_get_gid (SAM_ACCOUNT
*sampass
)
1122 return (sampass
->gid
);
1127 char* pdb_get_username (SAM_ACCOUNT
*sampass
)
1130 return (sampass
->username
);
1135 char* pdb_get_domain (SAM_ACCOUNT
*sampass
)
1138 return (sampass
->domain
);
1143 char* pdb_get_nt_username (SAM_ACCOUNT
*sampass
)
1146 return (sampass
->nt_username
);
1151 char* pdb_get_fullname (SAM_ACCOUNT
*sampass
)
1154 return (sampass
->full_name
);
1159 char* pdb_get_homedir (SAM_ACCOUNT
*sampass
)
1162 return (sampass
->home_dir
);
1167 char* pdb_get_dirdrive (SAM_ACCOUNT
*sampass
)
1170 return (sampass
->dir_drive
);
1175 char* pdb_get_logon_script (SAM_ACCOUNT
*sampass
)
1178 return (sampass
->logon_script
);
1183 char* pdb_get_profile_path (SAM_ACCOUNT
*sampass
)
1186 return (sampass
->profile_path
);
1191 char* pdb_get_acct_desc (SAM_ACCOUNT
*sampass
)
1194 return (sampass
->acct_desc
);
1199 char* pdb_get_workstations (SAM_ACCOUNT
*sampass
)
1202 return (sampass
->workstations
);
1207 char* pdb_get_munged_dial (SAM_ACCOUNT
*sampass
)
1210 return (sampass
->munged_dial
);
1215 uint32
pdb_get_unknown3 (SAM_ACCOUNT
*sampass
)
1218 return (sampass
->unknown_3
);
1223 uint32
pdb_get_unknown5 (SAM_ACCOUNT
*sampass
)
1226 return (sampass
->unknown_5
);
1231 uint32
pdb_get_unknown6 (SAM_ACCOUNT
*sampass
)
1234 return (sampass
->unknown_6
);
1239 /*********************************************************************
1240 Collection of set...() functions for SAM_ACCOUNT_INFO.
1241 ********************************************************************/
1243 BOOL
pdb_set_acct_ctrl (SAM_ACCOUNT
*sampass
, uint16 flags
)
1249 sampass
->acct_ctrl
= flags
;
1256 BOOL
pdb_set_logon_time (SAM_ACCOUNT
*sampass
, time_t mytime
)
1261 sampass
->logon_time
= mytime
;
1265 BOOL
pdb_set_logoff_time (SAM_ACCOUNT
*sampass
, time_t mytime
)
1270 sampass
->logoff_time
= mytime
;
1274 BOOL
pdb_set_kickoff_time (SAM_ACCOUNT
*sampass
, time_t mytime
)
1279 sampass
->kickoff_time
= mytime
;
1283 BOOL
pdb_set_pass_can_change_time (SAM_ACCOUNT
*sampass
, time_t mytime
)
1288 sampass
->pass_can_change_time
= mytime
;
1292 BOOL
pdb_set_pass_must_change_time (SAM_ACCOUNT
*sampass
, time_t mytime
)
1297 sampass
->pass_must_change_time
= mytime
;
1301 BOOL
pdb_set_pass_last_set_time (SAM_ACCOUNT
*sampass
, time_t mytime
)
1306 sampass
->pass_last_set_time
= mytime
;
1310 BOOL
pdb_set_hours_len (SAM_ACCOUNT
*sampass
, uint32 len
)
1315 sampass
->hours_len
= len
;
1319 BOOL
pdb_set_logons_divs (SAM_ACCOUNT
*sampass
, uint16 hours
)
1324 sampass
->logon_divs
= hours
;
1328 BOOL
pdb_set_uid (SAM_ACCOUNT
*sampass
, uid_t uid
)
1334 sampass
->init_flag
|= FLAG_SAM_UID
;
1339 BOOL
pdb_set_gid (SAM_ACCOUNT
*sampass
, gid_t gid
)
1345 sampass
->init_flag
|= FLAG_SAM_GID
;
1350 BOOL
pdb_set_user_rid (SAM_ACCOUNT
*sampass
, uint32 rid
)
1355 sampass
->user_rid
= rid
;
1359 BOOL
pdb_set_group_rid (SAM_ACCOUNT
*sampass
, uint32 grid
)
1364 sampass
->group_rid
= grid
;
1368 /*********************************************************************
1369 Set the user's UNIX name.
1370 ********************************************************************/
1372 BOOL
pdb_set_username(SAM_ACCOUNT
*sampass
, char *username
)
1376 *sampass
->username
= '\0';
1380 StrnCpy (sampass
->username
, username
, strlen(username
));
1385 /*********************************************************************
1386 Set the domain name.
1387 ********************************************************************/
1389 BOOL
pdb_set_domain(SAM_ACCOUNT
*sampass
, char *domain
)
1393 *sampass
->domain
= '\0';
1397 StrnCpy (sampass
->domain
, domain
, strlen(domain
));
1402 /*********************************************************************
1403 Set the user's NT name.
1404 ********************************************************************/
1406 BOOL
pdb_set_nt_username(SAM_ACCOUNT
*sampass
, char *nt_username
)
1410 *sampass
->nt_username
= '\0';
1414 StrnCpy (sampass
->nt_username
, nt_username
, strlen(nt_username
));
1419 /*********************************************************************
1420 Set the user's full name.
1421 ********************************************************************/
1423 BOOL
pdb_set_fullname(SAM_ACCOUNT
*sampass
, char *fullname
)
1427 *sampass
->full_name
= '\0';
1431 StrnCpy (sampass
->full_name
, fullname
, strlen(fullname
));
1436 /*********************************************************************
1437 Set the user's logon script.
1438 ********************************************************************/
1440 BOOL
pdb_set_logon_script(SAM_ACCOUNT
*sampass
, char *logon_script
)
1444 *sampass
->logon_script
= '\0';
1448 StrnCpy (sampass
->logon_script
, logon_script
, strlen(logon_script
));
1453 /*********************************************************************
1454 Set the user's profile path.
1455 ********************************************************************/
1457 BOOL
pdb_set_profile_path (SAM_ACCOUNT
*sampass
, char *profile_path
)
1461 *sampass
->profile_path
= '\0';
1465 StrnCpy (sampass
->profile_path
, profile_path
, strlen(profile_path
));
1470 /*********************************************************************
1471 Set the user's directory drive.
1472 ********************************************************************/
1474 BOOL
pdb_set_dir_drive (SAM_ACCOUNT
*sampass
, char *dir_drive
)
1478 *sampass
->dir_drive
= '\0';
1482 StrnCpy (sampass
->dir_drive
, dir_drive
, strlen(dir_drive
));
1487 /*********************************************************************
1488 Set the user's home directory.
1489 ********************************************************************/
1491 BOOL
pdb_set_homedir (SAM_ACCOUNT
*sampass
, char *homedir
)
1495 *sampass
->home_dir
= '\0';
1499 StrnCpy (sampass
->home_dir
, homedir
, strlen(homedir
));
1504 /*********************************************************************
1505 Set the user's account description.
1506 ********************************************************************/
1508 BOOL
pdb_set_acct_desc (SAM_ACCOUNT
*sampass
, char *acct_desc
)
1512 *sampass
->acct_desc
= '\0';
1516 StrnCpy (sampass
->acct_desc
, acct_desc
, strlen(acct_desc
));
1521 /*********************************************************************
1522 Set the user's workstation allowed list.
1523 ********************************************************************/
1525 BOOL
pdb_set_workstations (SAM_ACCOUNT
*sampass
, char *workstations
)
1529 *sampass
->workstations
= '\0';
1533 StrnCpy (sampass
->workstations
, workstations
, strlen(workstations
));
1538 /*********************************************************************
1539 Set the user's dial string.
1540 ********************************************************************/
1542 BOOL
pdb_set_munged_dial (SAM_ACCOUNT
*sampass
, char *munged_dial
)
1546 *sampass
->munged_dial
= '\0';
1550 StrnCpy (sampass
->munged_dial
, munged_dial
, strlen(munged_dial
));
1555 /*********************************************************************
1556 Set the user's NT hash.
1557 ********************************************************************/
1559 BOOL
pdb_set_nt_passwd (SAM_ACCOUNT
*sampass
, uint8
*pwd
)
1565 /* Allow setting to NULL */
1566 SAFE_FREE(sampass
->nt_pw
);
1570 if (sampass
->nt_pw
!=NULL
)
1571 DEBUG(4,("pdb_set_nt_passwd: NT hash non NULL overwritting ?\n"));
1573 sampass
->nt_pw
=(unsigned char *)malloc(sizeof(unsigned char)*16);
1575 if (sampass
->nt_pw
==NULL
)
1578 memcpy (sampass
->nt_pw
, pwd
, 16);
1583 /*********************************************************************
1584 Set the user's LM hash.
1585 ********************************************************************/
1587 BOOL
pdb_set_lanman_passwd (SAM_ACCOUNT
*sampass
, uint8
*pwd
)
1593 /* Allow setting to NULL */
1594 SAFE_FREE(sampass
->lm_pw
);
1598 if (sampass
->lm_pw
!=NULL
)
1599 DEBUG(4,("pdb_set_lanman_passwd: LM hash non NULL overwritting ?\n"));
1601 sampass
->lm_pw
=(unsigned char *)malloc(sizeof(unsigned char)*16);
1603 if (sampass
->lm_pw
==NULL
)
1606 memcpy (sampass
->lm_pw
, pwd
, 16);
1611 /*********************************************************************
1612 Set the user's PLAINTEXT password. Used as an interface to the above.
1613 ********************************************************************/
1615 BOOL
pdb_set_plaintext_passwd (SAM_ACCOUNT
*sampass
, char *plaintext
)
1617 uchar new_lanman_p16
[16];
1618 uchar new_nt_p16
[16];
1620 if (!sampass
|| !plaintext
)
1623 nt_lm_owf_gen (plaintext
, new_nt_p16
, new_lanman_p16
);
1625 if (!pdb_set_nt_passwd (sampass
, new_nt_p16
))
1628 if (!pdb_set_lanman_passwd (sampass
, new_lanman_p16
))
1634 BOOL
pdb_set_unknown_3 (SAM_ACCOUNT
*sampass
, uint32 unkn
)
1639 sampass
->unknown_3
= unkn
;
1643 BOOL
pdb_set_unknown_5 (SAM_ACCOUNT
*sampass
, uint32 unkn
)
1648 sampass
->unknown_5
= unkn
;
1652 BOOL
pdb_set_unknown_6 (SAM_ACCOUNT
*sampass
, uint32 unkn
)
1657 sampass
->unknown_6
= unkn
;
1661 BOOL
pdb_set_hours (SAM_ACCOUNT
*sampass
, uint8
*hours
)
1667 memset ((char *)sampass
->hours
, 0, MAX_HOURS_LEN
);
1671 memcpy (sampass
->hours
, hours
, MAX_HOURS_LEN
);