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(), False
);
148 pdb_set_homedir(*new_sam_acct
, lp_logon_home(), False
);
149 pdb_set_dir_drive(*new_sam_acct
, lp_logon_drive(), False
);
150 pdb_set_logon_script(*new_sam_acct
, lp_logon_script(), False
);
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
;
832 /*************************************************************
833 Copies a SAM_ACCOUNT.
834 FIXME!!!! This is broken as SAM_ACCOUNT contains two
836 **************************************************************/
838 void copy_sam_passwd(SAM_ACCOUNT
*to
, const SAM_ACCOUNT
*from
)
843 memcpy(to
, from
, sizeof(SAM_ACCOUNT
));
849 /*************************************************************
850 Change a password entry in the local smbpasswd file.
852 FIXME!! The function needs to be abstracted into the
853 passdb interface or something. It is currently being called
854 by _api_samr_create_user() in rpc_server/srv_samr.c
857 *************************************************************/
859 BOOL
local_password_change(char *user_name
, int local_flags
,
861 char *err_str
, size_t err_str_len
,
862 char *msg_str
, size_t msg_str_len
)
864 struct passwd
*pwd
= NULL
;
865 SAM_ACCOUNT
*sam_pass
=NULL
;
870 if (local_flags
& LOCAL_ADD_USER
) {
873 * Check for a local account - if we're adding only.
876 if(!(pwd
= sys_getpwnam(user_name
))) {
877 slprintf(err_str
, err_str_len
- 1, "User %s does not \
878 exist in system password file (usually /etc/passwd). Cannot add \
879 account without a valid local system user.\n", user_name
);
884 /* Get the smb passwd entry for this user */
885 pdb_init_sam(&sam_pass
);
886 if(!pdb_getsampwnam(sam_pass
, user_name
)) {
887 pdb_free_sam(sam_pass
);
889 if(!(local_flags
& LOCAL_ADD_USER
)) {
890 slprintf(err_str
, err_str_len
-1,"Failed to find entry for user %s.\n", user_name
);
895 if (!pdb_init_sam_pw(&sam_pass
, pwd
)) {
899 /* Set account flags. Note that the default is non-expiring accounts */
900 pdb_set_acct_ctrl(sam_pass
,(local_flags
& LOCAL_TRUST_ACCOUNT
) ? ACB_WSTRUST
: ACB_NORMAL
|ACB_PWNOEXP
);
902 if (local_flags
& LOCAL_DISABLE_USER
)
903 pdb_set_acct_ctrl (sam_pass
, pdb_get_acct_ctrl(sam_pass
)|ACB_DISABLED
);
905 if (local_flags
& LOCAL_SET_NO_PASSWORD
) {
906 pdb_set_acct_ctrl (sam_pass
, pdb_get_acct_ctrl(sam_pass
)|ACB_PWNOTREQ
);
908 /* set the passwords here. if we get to here it means
909 we have a valid, active account */
910 if (!pdb_set_plaintext_passwd (sam_pass
, new_passwd
)) {
911 pdb_free_sam(sam_pass
);
916 /* Remember to set the "last changed time". */
917 pdb_set_pass_last_set_time(sam_pass
, time(NULL
));
919 if (pdb_add_sam_account(sam_pass
)) {
920 slprintf(msg_str
, msg_str_len
-1, "Added user %s.\n", user_name
);
921 pdb_free_sam(sam_pass
);
924 slprintf(err_str
, err_str_len
-1, "Failed to add entry for user %s.\n", user_name
);
925 pdb_free_sam(sam_pass
);
929 /* the entry already existed */
930 local_flags
&= ~LOCAL_ADD_USER
;
934 * We are root - just write the new password
935 * and the valid last change time.
938 if(local_flags
& LOCAL_DISABLE_USER
) {
939 pdb_set_acct_ctrl (sam_pass
, pdb_get_acct_ctrl(sam_pass
)|ACB_DISABLED
);
940 } else if (local_flags
& LOCAL_ENABLE_USER
) {
941 if(pdb_get_lanman_passwd(sam_pass
) == NULL
) {
942 if (!pdb_set_plaintext_passwd (sam_pass
, new_passwd
)) {
943 pdb_free_sam(sam_pass
);
947 pdb_set_acct_ctrl (sam_pass
, pdb_get_acct_ctrl(sam_pass
)&(~ACB_DISABLED
));
948 } else if (local_flags
& LOCAL_SET_NO_PASSWORD
) {
949 pdb_set_acct_ctrl (sam_pass
, pdb_get_acct_ctrl(sam_pass
)|ACB_PWNOTREQ
);
950 /* This is needed to preserve ACB_PWNOTREQ in mod_smbfilepwd_entry */
951 if (!pdb_set_lanman_passwd (sam_pass
, NULL
)) {
952 pdb_free_sam(sam_pass
);
955 if (!pdb_set_nt_passwd(sam_pass
, NULL
)) {
956 pdb_free_sam(sam_pass
);
961 * If we're dealing with setting a completely empty user account
962 * ie. One with a password of 'XXXX', but not set disabled (like
963 * an account created from scratch) then if the old password was
964 * 'XX's then getsmbpwent will have set the ACB_DISABLED flag.
965 * We remove that as we're giving this user their first password
966 * and the decision hasn't really been made to disable them (ie.
967 * don't create them disabled). JRA.
969 if ((pdb_get_lanman_passwd(sam_pass
)==NULL
) && (pdb_get_acct_ctrl(sam_pass
)&ACB_DISABLED
))
970 pdb_set_acct_ctrl (sam_pass
, pdb_get_acct_ctrl(sam_pass
)&(~ACB_DISABLED
));
971 pdb_set_acct_ctrl (sam_pass
, pdb_get_acct_ctrl(sam_pass
)&(~ACB_PWNOTREQ
));
972 if (!pdb_set_plaintext_passwd (sam_pass
, new_passwd
)) {
973 pdb_free_sam(sam_pass
);
978 if(local_flags
& LOCAL_DELETE_USER
) {
979 if (!pdb_delete_sam_account(user_name
)) {
980 slprintf(err_str
,err_str_len
-1, "Failed to delete entry for user %s.\n", user_name
);
981 pdb_free_sam(sam_pass
);
984 slprintf(msg_str
, msg_str_len
-1, "Deleted user %s.\n", user_name
);
986 if(!pdb_update_sam_account(sam_pass
, True
)) {
987 slprintf(err_str
, err_str_len
-1, "Failed to modify entry for user %s.\n", user_name
);
988 pdb_free_sam(sam_pass
);
991 if(local_flags
& LOCAL_DISABLE_USER
)
992 slprintf(msg_str
, msg_str_len
-1, "Disabled user %s.\n", user_name
);
993 else if (local_flags
& LOCAL_ENABLE_USER
)
994 slprintf(msg_str
, msg_str_len
-1, "Enabled user %s.\n", user_name
);
995 else if (local_flags
& LOCAL_SET_NO_PASSWORD
)
996 slprintf(msg_str
, msg_str_len
-1, "User %s password set to none.\n", user_name
);
999 pdb_free_sam(sam_pass
);
1003 /*********************************************************************
1004 Collection of get...() functions for SAM_ACCOUNT_INFO.
1005 ********************************************************************/
1007 uint16
pdb_get_acct_ctrl (SAM_ACCOUNT
*sampass
)
1010 return (sampass
->acct_ctrl
);
1012 return (ACB_DISABLED
);
1015 time_t pdb_get_logon_time (SAM_ACCOUNT
*sampass
)
1018 return (sampass
->logon_time
);
1023 time_t pdb_get_logoff_time (SAM_ACCOUNT
*sampass
)
1026 return (sampass
->logoff_time
);
1031 time_t pdb_get_kickoff_time (SAM_ACCOUNT
*sampass
)
1034 return (sampass
->kickoff_time
);
1039 time_t pdb_get_pass_last_set_time (SAM_ACCOUNT
*sampass
)
1042 return (sampass
->pass_last_set_time
);
1047 time_t pdb_get_pass_can_change_time (SAM_ACCOUNT
*sampass
)
1050 return (sampass
->pass_can_change_time
);
1055 time_t pdb_get_pass_must_change_time (SAM_ACCOUNT
*sampass
)
1058 return (sampass
->pass_must_change_time
);
1063 uint16
pdb_get_logon_divs (SAM_ACCOUNT
*sampass
)
1066 return (sampass
->logon_divs
);
1071 uint32
pdb_get_hours_len (SAM_ACCOUNT
*sampass
)
1074 return (sampass
->hours_len
);
1079 uint8
* pdb_get_hours (SAM_ACCOUNT
*sampass
)
1082 return (sampass
->hours
);
1087 uint8
* pdb_get_nt_passwd (SAM_ACCOUNT
*sampass
)
1090 return (sampass
->nt_pw
);
1095 uint8
* pdb_get_lanman_passwd (SAM_ACCOUNT
*sampass
)
1098 return (sampass
->lm_pw
);
1103 uint32
pdb_get_user_rid (SAM_ACCOUNT
*sampass
)
1106 return (sampass
->user_rid
);
1111 uint32
pdb_get_group_rid (SAM_ACCOUNT
*sampass
)
1114 return (sampass
->group_rid
);
1119 uid_t
pdb_get_uid (SAM_ACCOUNT
*sampass
)
1122 return (sampass
->uid
);
1127 gid_t
pdb_get_gid (SAM_ACCOUNT
*sampass
)
1130 return (sampass
->gid
);
1135 char* pdb_get_username (SAM_ACCOUNT
*sampass
)
1138 return (sampass
->username
);
1143 char* pdb_get_domain (SAM_ACCOUNT
*sampass
)
1146 return (sampass
->domain
);
1151 char* pdb_get_nt_username (SAM_ACCOUNT
*sampass
)
1154 return (sampass
->nt_username
);
1159 char* pdb_get_fullname (SAM_ACCOUNT
*sampass
)
1162 return (sampass
->full_name
);
1167 char* pdb_get_homedir (SAM_ACCOUNT
*sampass
)
1170 return (sampass
->home_dir
);
1175 char* pdb_get_dirdrive (SAM_ACCOUNT
*sampass
)
1178 return (sampass
->dir_drive
);
1183 char* pdb_get_logon_script (SAM_ACCOUNT
*sampass
)
1186 return (sampass
->logon_script
);
1191 char* pdb_get_profile_path (SAM_ACCOUNT
*sampass
)
1194 return (sampass
->profile_path
);
1199 char* pdb_get_acct_desc (SAM_ACCOUNT
*sampass
)
1202 return (sampass
->acct_desc
);
1207 char* pdb_get_workstations (SAM_ACCOUNT
*sampass
)
1210 return (sampass
->workstations
);
1215 char* pdb_get_munged_dial (SAM_ACCOUNT
*sampass
)
1218 return (sampass
->munged_dial
);
1223 uint32
pdb_get_unknown3 (SAM_ACCOUNT
*sampass
)
1226 return (sampass
->unknown_3
);
1231 uint32
pdb_get_unknown5 (SAM_ACCOUNT
*sampass
)
1234 return (sampass
->unknown_5
);
1239 uint32
pdb_get_unknown6 (SAM_ACCOUNT
*sampass
)
1242 return (sampass
->unknown_6
);
1247 /*********************************************************************
1248 Collection of set...() functions for SAM_ACCOUNT_INFO.
1249 ********************************************************************/
1251 BOOL
pdb_set_acct_ctrl (SAM_ACCOUNT
*sampass
, uint16 flags
)
1257 sampass
->acct_ctrl
= flags
;
1264 BOOL
pdb_set_logon_time (SAM_ACCOUNT
*sampass
, time_t mytime
)
1269 sampass
->logon_time
= mytime
;
1273 BOOL
pdb_set_logoff_time (SAM_ACCOUNT
*sampass
, time_t mytime
)
1278 sampass
->logoff_time
= mytime
;
1282 BOOL
pdb_set_kickoff_time (SAM_ACCOUNT
*sampass
, time_t mytime
)
1287 sampass
->kickoff_time
= mytime
;
1291 BOOL
pdb_set_pass_can_change_time (SAM_ACCOUNT
*sampass
, time_t mytime
)
1296 sampass
->pass_can_change_time
= mytime
;
1300 BOOL
pdb_set_pass_must_change_time (SAM_ACCOUNT
*sampass
, time_t mytime
)
1305 sampass
->pass_must_change_time
= mytime
;
1309 BOOL
pdb_set_pass_last_set_time (SAM_ACCOUNT
*sampass
, time_t mytime
)
1314 sampass
->pass_last_set_time
= mytime
;
1318 BOOL
pdb_set_hours_len (SAM_ACCOUNT
*sampass
, uint32 len
)
1323 sampass
->hours_len
= len
;
1327 BOOL
pdb_set_logon_divs (SAM_ACCOUNT
*sampass
, uint16 hours
)
1332 sampass
->logon_divs
= hours
;
1336 BOOL
pdb_set_init_flag (SAM_ACCOUNT
*sampass
, uint32 flag
)
1341 sampass
->init_flag
|= flag
;
1346 BOOL
pdb_set_uid (SAM_ACCOUNT
*sampass
, uid_t uid
)
1352 sampass
->init_flag
|= FLAG_SAM_UID
;
1357 BOOL
pdb_set_gid (SAM_ACCOUNT
*sampass
, gid_t gid
)
1363 sampass
->init_flag
|= FLAG_SAM_GID
;
1368 BOOL
pdb_set_user_rid (SAM_ACCOUNT
*sampass
, uint32 rid
)
1373 sampass
->user_rid
= rid
;
1377 BOOL
pdb_set_group_rid (SAM_ACCOUNT
*sampass
, uint32 grid
)
1382 sampass
->group_rid
= grid
;
1386 /*********************************************************************
1387 Set the user's UNIX name.
1388 ********************************************************************/
1390 BOOL
pdb_set_username(SAM_ACCOUNT
*sampass
, char *username
)
1394 *sampass
->username
= '\0';
1398 StrnCpy (sampass
->username
, username
, strlen(username
));
1403 /*********************************************************************
1404 Set the domain name.
1405 ********************************************************************/
1407 BOOL
pdb_set_domain(SAM_ACCOUNT
*sampass
, char *domain
)
1411 *sampass
->domain
= '\0';
1415 StrnCpy (sampass
->domain
, domain
, strlen(domain
));
1420 /*********************************************************************
1421 Set the user's NT name.
1422 ********************************************************************/
1424 BOOL
pdb_set_nt_username(SAM_ACCOUNT
*sampass
, char *nt_username
)
1428 *sampass
->nt_username
= '\0';
1432 StrnCpy (sampass
->nt_username
, nt_username
, strlen(nt_username
));
1437 /*********************************************************************
1438 Set the user's full name.
1439 ********************************************************************/
1441 BOOL
pdb_set_fullname(SAM_ACCOUNT
*sampass
, char *fullname
)
1445 *sampass
->full_name
= '\0';
1449 StrnCpy (sampass
->full_name
, fullname
, strlen(fullname
));
1454 /*********************************************************************
1455 Set the user's logon script.
1456 ********************************************************************/
1458 BOOL
pdb_set_logon_script(SAM_ACCOUNT
*sampass
, char *logon_script
, BOOL store
)
1462 *sampass
->logon_script
= '\0';
1466 StrnCpy (sampass
->logon_script
, logon_script
, strlen(logon_script
));
1469 pdb_set_init_flag(sampass
, FLAG_SAM_LOGONSCRIPT
);
1474 /*********************************************************************
1475 Set the user's profile path.
1476 ********************************************************************/
1478 BOOL
pdb_set_profile_path (SAM_ACCOUNT
*sampass
, char *profile_path
, BOOL store
)
1482 *sampass
->profile_path
= '\0';
1486 StrnCpy (sampass
->profile_path
, profile_path
, strlen(profile_path
));
1489 pdb_set_init_flag(sampass
, FLAG_SAM_PROFILE
);
1494 /*********************************************************************
1495 Set the user's directory drive.
1496 ********************************************************************/
1498 BOOL
pdb_set_dir_drive (SAM_ACCOUNT
*sampass
, char *dir_drive
, BOOL store
)
1502 *sampass
->dir_drive
= '\0';
1506 StrnCpy (sampass
->dir_drive
, dir_drive
, strlen(dir_drive
));
1509 pdb_set_init_flag(sampass
, FLAG_SAM_DRIVE
);
1514 /*********************************************************************
1515 Set the user's home directory.
1516 ********************************************************************/
1518 BOOL
pdb_set_homedir (SAM_ACCOUNT
*sampass
, char *homedir
, BOOL store
)
1522 *sampass
->home_dir
= '\0';
1526 StrnCpy (sampass
->home_dir
, homedir
, strlen(homedir
));
1529 pdb_set_init_flag(sampass
, FLAG_SAM_SMBHOME
);
1534 /*********************************************************************
1535 Set the user's account description.
1536 ********************************************************************/
1538 BOOL
pdb_set_acct_desc (SAM_ACCOUNT
*sampass
, char *acct_desc
)
1542 *sampass
->acct_desc
= '\0';
1546 StrnCpy (sampass
->acct_desc
, acct_desc
, strlen(acct_desc
));
1551 /*********************************************************************
1552 Set the user's workstation allowed list.
1553 ********************************************************************/
1555 BOOL
pdb_set_workstations (SAM_ACCOUNT
*sampass
, char *workstations
)
1559 *sampass
->workstations
= '\0';
1563 StrnCpy (sampass
->workstations
, workstations
, strlen(workstations
));
1568 /*********************************************************************
1569 Set the user's dial string.
1570 ********************************************************************/
1572 BOOL
pdb_set_munged_dial (SAM_ACCOUNT
*sampass
, char *munged_dial
)
1576 *sampass
->munged_dial
= '\0';
1580 StrnCpy (sampass
->munged_dial
, munged_dial
, strlen(munged_dial
));
1585 /*********************************************************************
1586 Set the user's NT hash.
1587 ********************************************************************/
1589 BOOL
pdb_set_nt_passwd (SAM_ACCOUNT
*sampass
, uint8
*pwd
)
1594 /* Remember to set the "last changed time". */
1595 pdb_set_pass_last_set_time(sampass
, time(NULL
));
1598 /* Allow setting to NULL */
1599 SAFE_FREE(sampass
->nt_pw
);
1603 if (sampass
->nt_pw
!=NULL
)
1604 DEBUG(4,("pdb_set_nt_passwd: NT hash non NULL overwritting ?\n"));
1606 sampass
->nt_pw
=(unsigned char *)malloc(sizeof(unsigned char)*16);
1608 if (sampass
->nt_pw
==NULL
)
1611 memcpy (sampass
->nt_pw
, pwd
, 16);
1616 /*********************************************************************
1617 Set the user's LM hash.
1618 ********************************************************************/
1620 BOOL
pdb_set_lanman_passwd (SAM_ACCOUNT
*sampass
, uint8
*pwd
)
1625 /* Remember to set the "last changed time". */
1626 pdb_set_pass_last_set_time(sampass
, time(NULL
));
1629 /* Allow setting to NULL */
1630 SAFE_FREE(sampass
->lm_pw
);
1634 if (sampass
->lm_pw
!=NULL
)
1635 DEBUG(4,("pdb_set_lanman_passwd: LM hash non NULL overwritting ?\n"));
1637 sampass
->lm_pw
=(unsigned char *)malloc(sizeof(unsigned char)*16);
1639 if (sampass
->lm_pw
==NULL
)
1642 memcpy (sampass
->lm_pw
, pwd
, 16);
1647 /*********************************************************************
1648 Set the user's PLAINTEXT password. Used as an interface to the above.
1649 ********************************************************************/
1651 BOOL
pdb_set_plaintext_passwd (SAM_ACCOUNT
*sampass
, char *plaintext
)
1653 uchar new_lanman_p16
[16];
1654 uchar new_nt_p16
[16];
1656 if (!sampass
|| !plaintext
)
1659 nt_lm_owf_gen (plaintext
, new_nt_p16
, new_lanman_p16
);
1661 if (!pdb_set_nt_passwd (sampass
, new_nt_p16
))
1664 if (!pdb_set_lanman_passwd (sampass
, new_lanman_p16
))
1670 BOOL
pdb_set_unknown_3 (SAM_ACCOUNT
*sampass
, uint32 unkn
)
1675 sampass
->unknown_3
= unkn
;
1679 BOOL
pdb_set_unknown_5 (SAM_ACCOUNT
*sampass
, uint32 unkn
)
1684 sampass
->unknown_5
= unkn
;
1688 BOOL
pdb_set_unknown_6 (SAM_ACCOUNT
*sampass
, uint32 unkn
)
1693 sampass
->unknown_6
= unkn
;
1697 BOOL
pdb_set_hours (SAM_ACCOUNT
*sampass
, uint8
*hours
)
1703 memset ((char *)sampass
->hours
, 0, MAX_HOURS_LEN
);
1707 memcpy (sampass
->hours
, hours
, MAX_HOURS_LEN
);
1712 /***************************************************************************
1713 Search by uid. Wrapper around pdb_getsampwnam()
1714 **************************************************************************/
1716 BOOL
pdb_getsampwuid (SAM_ACCOUNT
* user
, uid_t uid
)
1722 DEBUG(0,("pdb_getsampwuid: SAM_ACCOUNT is NULL.\n"));
1727 * Never trust the uid in the passdb. Lookup the username first
1728 * and then lokup the user by name in the sam.
1731 if ((pw
=sys_getpwuid(uid
)) == NULL
) {
1732 DEBUG(0,("pdb_getsampwuid: getpwuid(%d) return NULL. User does not exist in Unix accounts!\n", uid
));
1736 fstrcpy (name
, pw
->pw_name
);
1738 return pdb_getsampwnam (user
, name
);