2 Unix SMB/Netbios implementation.
4 Password and authentication handling
5 Copyright (C) Jeremy Allison 1996-1998
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.
26 extern int DEBUGLEVEL;
29 * This is set on startup - it defines the SID for this
30 * machine, and therefore the SAM database for which it is
34 extern DOM_SID global_sam_sid;
36 struct passdb_ops *pdb_ops;
38 static void* pdb_handle = NULL;
40 /***************************************************************
41 Initialize the password db operations.
42 ***************************************************************/
43 BOOL initialize_password_db(BOOL reload)
46 char* modulename = lp_passdb_module_path();
48 /* This function is unfinished right now, so just
49 ignore the details and always return True. It is here
50 only as a placeholder --jerry */
53 /* load another module? */
54 if (reload && pdb_handle)
56 sys_dlclose (pdb_handle);
60 /* do we have a module defined or use the default? */
61 if (strlen (modulename) != 0)
63 if ((pdb_handle=sys_dlopen (modulename, RTLD_LAZY)) == NULL)
65 DEBUG(0,("initialize_password_db: ERROR - Unable to open passdb module \"%s\"!\n%s\n",
66 modulename, dlerror()));
69 DEBUG(1,("initialize_password_db: passdb module \"%s\" loaded successfully\n", modulename));
72 /* either no module name defined or the one that was failed
73 to open. Let's try the default */
74 if (pdb_handle == NULL)
76 if ((pdb_handle=sys_dlopen ("libpdbfile.so", RTLD_LAZY)) == NULL)
78 DEBUG(0,("initialize_password_db: ERROR - Unable to open \"libpdbfile.so\" passdb module! No user authentication possible!\n%s\n",
83 DEBUG(1,("initialize_password_db: passdb module \"libpdbfile.so\" loaded successfully\n"));
87 return (pdb_handle != NULL);
90 /*************************************************************
91 initialises a struct sam_disp_info.
92 **************************************************************/
93 static void pdb_init_dispinfo(struct sam_disp_info *user)
100 /*************************************************************
101 initialises a struct sam_passwd.
102 ************************************************************/
103 void pdb_init_sam(SAM_ACCOUNT *user)
110 user->mem_ctx = talloc_init();
111 DEBUG(10, ("pdb_init_sam: obtained a talloc context of 0x%x\n", user->mem_ctx));
113 user->logon_time = (time_t)0;
114 user->logoff_time = (time_t)-1;
115 user->kickoff_time = (time_t)-1;
116 user->pass_last_set_time = (time_t)-1;
117 user->pass_can_change_time = (time_t)-1;
118 user->pass_must_change_time = (time_t)-1;
120 user->unknown_3 = 0x00ffffff; /* don't know */
121 user->logon_divs = 168; /* hours per week */
122 user->hours_len = 21; /* 21 times 8 bits = 168 */
123 memset(user->hours, 0xff, user->hours_len); /* available at all hours */
124 user->unknown_5 = 0x00000000; /* don't know */
125 user->unknown_6 = 0x000004ec; /* don't know */
128 /************************************************************
129 free all pointer members and then reinit the SAM_ACCOUNT
130 ***********************************************************/
131 void pdb_clear_sam(SAM_ACCOUNT *user)
136 /* free upany memory used */
137 DEBUG(10, ("pdb_clear_sam: releasing memory. talloc context is 0x%x\n",user->mem_ctx));
138 talloc_destroy (user->mem_ctx);
146 /*************************************************************************
147 Routine to return the next entry in the sam passwd list.
148 *************************************************************************/
149 struct sam_disp_info *pdb_sam_to_dispinfo(SAM_ACCOUNT *user)
151 static struct sam_disp_info disp_info;
156 pdb_init_dispinfo(&disp_info);
158 disp_info.smb_name = user->username;
159 disp_info.full_name = user->full_name;
160 disp_info.user_rid = user->user_rid;
166 /**********************************************************
167 Encode the account control bits into a string.
168 length = length of string to encode into (including terminating
169 null). length *MUST BE MORE THAN 2* !
170 **********************************************************/
171 char *pdb_encode_acct_ctrl(uint16 acct_ctrl, size_t length)
173 static fstring acct_str;
178 if (acct_ctrl & ACB_PWNOTREQ ) acct_str[i++] = 'N';
179 if (acct_ctrl & ACB_DISABLED ) acct_str[i++] = 'D';
180 if (acct_ctrl & ACB_HOMDIRREQ) acct_str[i++] = 'H';
181 if (acct_ctrl & ACB_TEMPDUP ) acct_str[i++] = 'T';
182 if (acct_ctrl & ACB_NORMAL ) acct_str[i++] = 'U';
183 if (acct_ctrl & ACB_MNS ) acct_str[i++] = 'M';
184 if (acct_ctrl & ACB_WSTRUST ) acct_str[i++] = 'W';
185 if (acct_ctrl & ACB_SVRTRUST ) acct_str[i++] = 'S';
186 if (acct_ctrl & ACB_AUTOLOCK ) acct_str[i++] = 'L';
187 if (acct_ctrl & ACB_PWNOEXP ) acct_str[i++] = 'X';
188 if (acct_ctrl & ACB_DOMTRUST ) acct_str[i++] = 'I';
190 for ( ; i < length - 2 ; i++ ) { acct_str[i] = ' '; }
194 acct_str[i++] = '\0';
199 /**********************************************************
200 Decode the account control bits from a string.
202 this function breaks coding standards minimum line width of 80 chars.
203 reason: vertical line-up code clarity - all case statements fit into
204 15 lines, which is more important.
205 **********************************************************/
207 uint16 pdb_decode_acct_ctrl(const char *p)
209 uint16 acct_ctrl = 0;
210 BOOL finished = False;
213 * Check if the account type bits have been encoded after the
214 * NT password (in the form [NDHTUWSLXI]).
217 if (*p != '[') return 0;
219 for (p++; *p && !finished; p++)
223 case 'N': { acct_ctrl |= ACB_PWNOTREQ ; break; /* 'N'o password. */ }
224 case 'D': { acct_ctrl |= ACB_DISABLED ; break; /* 'D'isabled. */ }
225 case 'H': { acct_ctrl |= ACB_HOMDIRREQ; break; /* 'H'omedir required. */ }
226 case 'T': { acct_ctrl |= ACB_TEMPDUP ; break; /* 'T'emp account. */ }
227 case 'U': { acct_ctrl |= ACB_NORMAL ; break; /* 'U'ser account (normal). */ }
228 case 'M': { acct_ctrl |= ACB_MNS ; break; /* 'M'NS logon user account. What is this ? */ }
229 case 'W': { acct_ctrl |= ACB_WSTRUST ; break; /* 'W'orkstation account. */ }
230 case 'S': { acct_ctrl |= ACB_SVRTRUST ; break; /* 'S'erver account. */ }
231 case 'L': { acct_ctrl |= ACB_AUTOLOCK ; break; /* 'L'ocked account. */ }
232 case 'X': { acct_ctrl |= ACB_PWNOEXP ; break; /* No 'X'piry on password */ }
233 case 'I': { acct_ctrl |= ACB_DOMTRUST ; break; /* 'I'nterdomain trust account. */ }
239 default: { finished = True; }
246 /*************************************************************
247 Routine to set 32 hex password characters from a 16 byte array.
248 **************************************************************/
249 void pdb_sethexpwd(char *p, unsigned char *pwd, uint16 acct_ctrl)
253 for (i = 0; i < 16; i++)
254 slprintf(&p[i*2], 3, "%02X", pwd[i]);
256 if (acct_ctrl & ACB_PWNOTREQ)
257 safe_strcpy(p, "NO PASSWORDXXXXXXXXXXXXXXXXXXXXX", 33);
259 safe_strcpy(p, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 33);
263 /*************************************************************
264 Routine to get the 32 hex characters and turn them
265 into a 16 byte array.
266 **************************************************************/
267 BOOL pdb_gethexpwd(char *p, unsigned char *pwd)
270 unsigned char lonybble, hinybble;
271 char *hexchars = "0123456789ABCDEF";
274 if (!p) return (False);
276 for (i = 0; i < 32; i += 2)
278 hinybble = toupper(p[i]);
279 lonybble = toupper(p[i + 1]);
281 p1 = strchr(hexchars, hinybble);
282 p2 = strchr(hexchars, lonybble);
289 hinybble = PTR_DIFF(p1, hexchars);
290 lonybble = PTR_DIFF(p2, hexchars);
292 pwd[i / 2] = (hinybble << 4) | lonybble;
297 /*******************************************************************
298 Group and User RID username mapping function
299 ********************************************************************/
300 BOOL pdb_name_to_rid(char *user_name, uint32 *u_rid, uint32 *g_rid)
302 struct passwd *pw = Get_Pwnam(user_name, False);
304 if (u_rid == NULL || g_rid == NULL || user_name == NULL)
311 DEBUG(1,("Username %s is invalid on this system\n", user_name));
315 if (user_in_list(user_name, lp_domain_guest_users()))
317 *u_rid = DOMAIN_USER_RID_GUEST;
319 else if (user_in_list(user_name, lp_domain_admin_users()))
321 *u_rid = DOMAIN_USER_RID_ADMIN;
325 /* turn the unix UID into a Domain RID. this is what the posix
326 sub-system does (adds 1000 to the uid) */
327 *u_rid = pdb_uid_to_user_rid(pw->pw_uid);
330 /* absolutely no idea what to do about the unix GID to Domain RID mapping */
331 *g_rid = pdb_gid_to_group_rid(pw->pw_gid);
336 /*******************************************************************
337 Converts NT user RID to a UNIX uid.
338 ********************************************************************/
339 uid_t pdb_user_rid_to_uid(uint32 user_rid)
341 return (uid_t)(((user_rid & (~USER_RID_TYPE))- 1000)/RID_MULTIPLIER);
344 /*******************************************************************
345 Converts NT user RID to a UNIX gid.
346 ********************************************************************/
348 gid_t pdb_user_rid_to_gid(uint32 user_rid)
350 return (uid_t)(((user_rid & (~GROUP_RID_TYPE))- 1000)/RID_MULTIPLIER);
353 /*******************************************************************
354 converts UNIX uid to an NT User RID.
355 ********************************************************************/
357 uint32 pdb_uid_to_user_rid(uid_t uid)
359 return (((((uint32)uid)*RID_MULTIPLIER) + 1000) | USER_RID_TYPE);
362 /*******************************************************************
363 converts NT Group RID to a UNIX uid.
364 ********************************************************************/
366 uint32 pdb_gid_to_group_rid(gid_t gid)
368 return (((((uint32)gid)*RID_MULTIPLIER) + 1000) | GROUP_RID_TYPE);
371 /*******************************************************************
372 Decides if a RID is a well known RID.
373 ********************************************************************/
375 static BOOL pdb_rid_is_well_known(uint32 rid)
380 /*******************************************************************
381 Decides if a RID is a user or group RID.
382 ********************************************************************/
383 BOOL pdb_rid_is_user(uint32 rid)
385 /* lkcl i understand that NT attaches an enumeration to a RID
386 * such that it can be identified as either a user, group etc
387 * type. there are 5 such categories, and they are documented.
389 if(pdb_rid_is_well_known(rid)) {
391 * The only well known user RIDs are DOMAIN_USER_RID_ADMIN
392 * and DOMAIN_USER_RID_GUEST.
394 if(rid == DOMAIN_USER_RID_ADMIN || rid == DOMAIN_USER_RID_GUEST)
396 } else if((rid & RID_TYPE_MASK) == USER_RID_TYPE) {
402 /*******************************************************************
403 Convert a rid into a name. Used in the lookup SID rpc.
404 ********************************************************************/
405 BOOL local_lookup_rid(uint32 rid, char *name, enum SID_NAME_USE *psid_name_use)
408 BOOL is_user = pdb_rid_is_user(rid);
410 DEBUG(5,("local_lookup_rid: looking up %s RID %u.\n", is_user ? "user" :
411 "group", (unsigned int)rid));
414 if(rid == DOMAIN_USER_RID_ADMIN) {
416 char *p = admin_users;
417 pstrcpy( admin_users, lp_domain_admin_users());
418 if(!next_token(&p, name, NULL, sizeof(fstring)))
419 fstrcpy(name, "Administrator");
420 } else if (rid == DOMAIN_USER_RID_GUEST) {
422 char *p = guest_users;
423 pstrcpy( guest_users, lp_domain_guest_users());
424 if(!next_token(&p, name, NULL, sizeof(fstring)))
425 fstrcpy(name, "Guest");
431 * Don't try to convert the rid to a name if
432 * running in appliance mode
434 if (lp_hide_local_users())
437 uid = pdb_user_rid_to_uid(rid);
438 pass = sys_getpwuid(uid);
440 *psid_name_use = SID_NAME_USER;
442 DEBUG(5,("local_lookup_rid: looking up uid %u %s\n", (unsigned int)uid,
443 pass ? "succeeded" : "failed" ));
446 slprintf(name, sizeof(fstring)-1, "unix_user.%u", (unsigned int)uid);
450 fstrcpy(name, pass->pw_name);
452 DEBUG(5,("local_lookup_rid: found user %s for rid %u\n", name,
453 (unsigned int)rid ));
461 * Don't try to convert the rid to a name if running
465 if (lp_hide_local_users())
468 gid = pdb_user_rid_to_gid(rid);
471 *psid_name_use = SID_NAME_ALIAS;
473 DEBUG(5,("local_local_rid: looking up gid %u %s\n", (unsigned int)gid,
474 gr ? "succeeded" : "failed" ));
477 slprintf(name, sizeof(fstring)-1, "unix_group.%u", (unsigned int)gid);
481 fstrcpy( name, gr->gr_name);
483 DEBUG(5,("local_lookup_rid: found group %s for rid %u\n", name,
484 (unsigned int)rid ));
490 /*******************************************************************
491 Convert a name into a SID. Used in the lookup name rpc.
492 ********************************************************************/
494 BOOL local_lookup_name(const char *c_domain, const char *c_user, DOM_SID *psid, enum SID_NAME_USE *psid_name_use)
496 extern DOM_SID global_sid_World_Domain;
497 struct passwd *pass = NULL;
503 * domain and user may be quoted const strings, and map_username and
504 * friends can modify them. Make a modifiable copy. JRA.
507 fstrcpy(domain, c_domain);
508 fstrcpy(user, c_user);
510 sid_copy(&local_sid, &global_sam_sid);
513 * Special case for MACHINE\Everyone. Map to the world_sid.
516 if(strequal(user, "Everyone")) {
517 sid_copy( psid, &global_sid_World_Domain);
518 sid_append_rid(psid, 0);
519 *psid_name_use = SID_NAME_ALIAS;
524 * Don't lookup local unix users if running in appliance mode
526 if (lp_hide_local_users())
529 (void)map_username(user);
531 if(!(pass = Get_Pwnam(user, True))) {
533 * Maybe it was a group ?
535 struct group *grp = getgrnam(user);
540 sid_append_rid( &local_sid, pdb_gid_to_group_rid(grp->gr_gid));
541 *psid_name_use = SID_NAME_ALIAS;
544 sid_append_rid( &local_sid, pdb_uid_to_user_rid(pass->pw_uid));
545 *psid_name_use = SID_NAME_USER;
548 sid_copy( psid, &local_sid);
553 /****************************************************************************
554 Convert a uid to SID - locally.
555 ****************************************************************************/
556 DOM_SID *local_uid_to_sid(DOM_SID *psid, uid_t uid)
558 extern DOM_SID global_sam_sid;
560 sid_copy(psid, &global_sam_sid);
561 sid_append_rid(psid, pdb_uid_to_user_rid(uid));
567 /****************************************************************************
568 Convert a SID to uid - locally.
569 ****************************************************************************/
570 BOOL local_sid_to_uid(uid_t *puid, DOM_SID *psid, enum SID_NAME_USE *name_type)
572 extern DOM_SID global_sam_sid;
579 *name_type = SID_NAME_UNKNOWN;
581 sid_copy(&dom_sid, psid);
582 sid_split_rid(&dom_sid, &rid);
584 if (!pdb_rid_is_user(rid))
588 * We can only convert to a uid if this is our local
589 * Domain SID (ie. we are the controling authority).
591 if (!sid_equal(&global_sam_sid, &dom_sid))
594 *puid = pdb_user_rid_to_uid(rid);
597 * Ensure this uid really does exist.
599 if(!(pass = sys_getpwuid(*puid)))
602 DEBUG(10,("local_sid_to_uid: SID %s -> uid (%u) (%s).\n", sid_to_string( str, psid),
603 (unsigned int)*puid, pass->pw_name ));
608 /****************************************************************************
609 Convert a gid to SID - locally.
610 ****************************************************************************/
611 DOM_SID *local_gid_to_sid(DOM_SID *psid, gid_t gid)
613 extern DOM_SID global_sam_sid;
615 sid_copy(psid, &global_sam_sid);
616 sid_append_rid(psid, pdb_gid_to_group_rid(gid));
621 /****************************************************************************
622 Convert a SID to gid - locally.
623 ****************************************************************************/
624 BOOL local_sid_to_gid(gid_t *pgid, DOM_SID *psid, enum SID_NAME_USE *name_type)
626 extern DOM_SID global_sam_sid;
632 *name_type = SID_NAME_UNKNOWN;
634 sid_copy(&dom_sid, psid);
635 sid_split_rid(&dom_sid, &rid);
638 * We can only convert to a gid if this is our local
639 * Domain SID (ie. we are the controling authority).
642 if (!sid_equal(&global_sam_sid, &dom_sid))
645 if (pdb_rid_is_user(rid))
648 *pgid = pdb_user_rid_to_gid(rid);
651 * Ensure this gid really does exist.
654 if(!(grp = getgrgid(*pgid)))
657 DEBUG(10,("local_sid_to_gid: SID %s -> gid (%u) (%s).\n", sid_to_string( str, psid),
658 (unsigned int)*pgid, grp->gr_name ));
663 static void select_name(fstring *string, char **name, const UNISTR2 *from)
665 if (from->buffer != 0)
667 unistr2_to_ascii(*string, from, sizeof(*string));
672 /*************************************************************
673 copies a SAM_USER_INFO_23 to a SAM_ACCOUNT
674 **************************************************************/
675 void copy_id23_to_sam_passwd(SAM_ACCOUNT *to, SAM_USER_INFO_23 *from)
677 static fstring smb_name;
678 static fstring full_name;
679 static fstring home_dir;
680 static fstring dir_drive;
681 static fstring logon_script;
682 static fstring profile_path;
683 static fstring acct_desc;
684 static fstring workstations;
685 static fstring unknown_str;
686 static fstring munged_dial;
688 if (from == NULL || to == NULL)
691 to->logon_time = nt_time_to_unix(&from->logon_time);
692 to->logoff_time = nt_time_to_unix(&from->logoff_time);
693 to->kickoff_time = nt_time_to_unix(&from->kickoff_time);
694 to->pass_last_set_time = nt_time_to_unix(&from->pass_last_set_time);
695 to->pass_can_change_time = nt_time_to_unix(&from->pass_can_change_time);
696 to->pass_must_change_time = nt_time_to_unix(&from->pass_must_change_time);
698 select_name(&smb_name , &to->username , &from->uni_user_name );
699 select_name(&full_name , &to->full_name , &from->uni_full_name );
700 select_name(&home_dir , &to->home_dir , &from->uni_home_dir );
701 select_name(&dir_drive , &to->dir_drive , &from->uni_dir_drive );
702 select_name(&logon_script, &to->logon_script, &from->uni_logon_script);
703 select_name(&profile_path, &to->profile_path, &from->uni_profile_path);
704 select_name(&acct_desc , &to->acct_desc , &from->uni_acct_desc );
705 select_name(&workstations, &to->workstations, &from->uni_workstations);
706 select_name(&unknown_str , &to->unknown_str , &from->uni_unknown_str );
707 select_name(&munged_dial , &to->munged_dial , &from->uni_munged_dial );
709 to->user_rid = from->user_rid;
710 to->group_rid = from->group_rid;
712 to->acct_ctrl = from->acb_info;
713 to->unknown_3 = from->unknown_3;
715 to->logon_divs = from->logon_divs;
716 to->hours_len = from->logon_hrs.len;
717 memcpy(to->hours, from->logon_hrs.hours, MAX_HOURS_LEN);
719 to->unknown_5 = from->unknown_5;
720 to->unknown_6 = from->unknown_6;
723 /*************************************************************
725 **************************************************************/
726 void copy_id21_to_sam_passwd(SAM_ACCOUNT *to, SAM_USER_INFO_21 *from)
728 static fstring smb_name;
729 static fstring full_name;
730 static fstring home_dir;
731 static fstring dir_drive;
732 static fstring logon_script;
733 static fstring profile_path;
734 static fstring acct_desc;
735 static fstring workstations;
736 static fstring unknown_str;
737 static fstring munged_dial;
739 if (from == NULL || to == NULL)
742 to->logon_time = nt_time_to_unix(&from->logon_time);
743 to->logoff_time = nt_time_to_unix(&from->logoff_time);
744 to->kickoff_time = nt_time_to_unix(&from->kickoff_time);
745 to->pass_last_set_time = nt_time_to_unix(&from->pass_last_set_time);
746 to->pass_can_change_time = nt_time_to_unix(&from->pass_can_change_time);
747 to->pass_must_change_time = nt_time_to_unix(&from->pass_must_change_time);
749 select_name(&smb_name , &to->username , &from->uni_user_name );
750 select_name(&full_name , &to->full_name , &from->uni_full_name );
751 select_name(&home_dir , &to->home_dir , &from->uni_home_dir );
752 select_name(&dir_drive , &to->dir_drive , &from->uni_dir_drive );
753 select_name(&logon_script, &to->logon_script, &from->uni_logon_script);
754 select_name(&profile_path, &to->profile_path, &from->uni_profile_path);
755 select_name(&acct_desc , &to->acct_desc , &from->uni_acct_desc );
756 select_name(&workstations, &to->workstations, &from->uni_workstations);
757 select_name(&unknown_str , &to->unknown_str , &from->uni_unknown_str );
758 select_name(&munged_dial , &to->munged_dial , &from->uni_munged_dial );
760 to->user_rid = from->user_rid;
761 to->group_rid = from->group_rid;
763 /* FIXME!! Do we need to copy the passwords here as well?
764 I don't know. Need to figure this out --jerry */
766 to->acct_ctrl = from->acb_info;
767 to->unknown_3 = from->unknown_3;
769 to->logon_divs = from->logon_divs;
770 to->hours_len = from->logon_hrs.len;
771 memcpy(to->hours, from->logon_hrs.hours, MAX_HOURS_LEN);
773 to->unknown_5 = from->unknown_5;
774 to->unknown_6 = from->unknown_6;
778 /*************************************************************
780 **************************************************************/
781 void copy_sam_passwd(SAM_ACCOUNT *to, const SAM_ACCOUNT *from)
785 if (!from || !to) return;
789 /* copy all non-pointers */
790 memcpy(to, from, sizeof(*from));
792 if (from->username) {
793 len=strlen(from->username)+1;
794 to->username = talloc(to->mem_ctx, len);
795 StrnCpy (to->username, from->username, len-1);
798 if (from->full_name) {
799 len=strlen(from->full_name)+1;
800 to->full_name = talloc(to->mem_ctx, len);
801 StrnCpy (to->full_name, from->full_name, len-1);
804 if (from->nt_username) {
805 len=strlen(from->nt_username)+1;
806 to->nt_username = talloc(to->mem_ctx, len);
807 StrnCpy (to->nt_username, from->nt_username, len-1);
810 if (from->profile_path) {
811 len=strlen(from->profile_path)+1;
812 to->profile_path = talloc(to->mem_ctx, len);
813 StrnCpy (to->profile_path, from->profile_path, len-1);
816 if (from->logon_script) {
817 len=strlen(from->logon_script)+1;
818 to->logon_script = talloc(to->mem_ctx, len);
819 StrnCpy (to->logon_script, from->logon_script, len-1);
822 if (from->home_dir) {
823 len=strlen(from->home_dir)+1;
824 to->home_dir = talloc(to->mem_ctx, len);
825 StrnCpy (to->home_dir, from->home_dir, len-1);
828 if (from->dir_drive) {
829 len=strlen(from->dir_drive)+1;
830 to->dir_drive = talloc(to->mem_ctx, len);
831 StrnCpy (to->dir_drive, from->dir_drive, len-1);
834 if (from->workstations) {
835 len=strlen(from->workstations)+1;
836 to->workstations = talloc(to->mem_ctx, len);
837 StrnCpy (to->workstations, from->workstations, len-1);
840 if (from->acct_desc) {
841 len=strlen(from->acct_desc)+1;
842 to->acct_desc = talloc(to->mem_ctx, len);
843 StrnCpy (to->acct_desc, from->acct_desc, len-1);
846 if (from->munged_dial) {
847 len=strlen(from->munged_dial)+1;
848 to->munged_dial = talloc(to->mem_ctx, len);
849 StrnCpy (to->munged_dial, from->munged_dial, len);
852 if (from->unknown_str) {
853 len=strlen(from->unknown_str)+1;
854 to->unknown_str = talloc(to->mem_ctx, len);
855 StrnCpy (to->unknown_str, from->unknown_str, len-1);
860 to->nt_pw = talloc(to->mem_ctx, 16);
861 memcpy (to->nt_pw, from->nt_pw, 16);
865 to->lm_pw = talloc(to->mem_ctx, 16);
866 memcpy (to->lm_pw, from->lm_pw, 16);
872 /*************************************************************
873 change a password entry in the local smbpasswd file
875 FIXME!! The function needs to be abstracted into the
876 passdb interface or something. It is currently being called
877 by _api_samr_create_user() in rpc_server/srv_samr.c
880 *************************************************************/
882 BOOL local_password_change(char *user_name, int local_flags,
884 char *err_str, size_t err_str_len,
885 char *msg_str, size_t msg_str_len)
887 struct passwd *pwd = NULL;
888 SAM_ACCOUNT *sam_pass;
889 SAM_ACCOUNT new_sam_acct;
891 uchar new_nt_p16[16];
896 if (local_flags & LOCAL_ADD_USER) {
899 * Check for a local account - if we're adding only.
902 if(!(pwd = sys_getpwnam(user_name))) {
903 slprintf(err_str, err_str_len - 1, "User %s does not \
904 exist in system password file (usually /etc/passwd). Cannot add \
905 account without a valid local system user.\n", user_name);
910 /* Calculate the MD4 hash (NT compatible) of the new password. */
911 nt_lm_owf_gen(new_passwd, new_nt_p16, new_p16);
913 /* Get the smb passwd entry for this user */
914 sam_pass = pdb_getsampwnam(user_name);
915 if (sam_pass == NULL)
917 if(!(local_flags & LOCAL_ADD_USER))
919 slprintf(err_str, err_str_len-1,"Failed to find entry for user %s.\n", user_name);
923 /* create the SAM_ACCOUNT struct and call pdb_add_sam_account.
924 Because the new_sam_pwd only exists in the scope of this function
925 we will not allocate memory for members */
926 pdb_init_sam (&new_sam_acct);
927 pdb_set_username (&new_sam_acct, user_name);
928 pdb_set_fullname (&new_sam_acct, pwd->pw_gecos);
929 pdb_set_uid (&new_sam_acct, pwd->pw_uid);
930 pdb_set_gid (&new_sam_acct, pwd->pw_gid);
931 pdb_set_pass_last_set_time(&new_sam_acct, time(NULL));
932 pdb_set_profile_path (&new_sam_acct, lp_logon_path());
933 pdb_set_homedir (&new_sam_acct, lp_logon_home());
934 pdb_set_dir_drive (&new_sam_acct, lp_logon_drive());
935 pdb_set_logon_script (&new_sam_acct, lp_logon_script());
937 /* set account flags */
938 pdb_set_acct_ctrl(&new_sam_acct,((local_flags & LOCAL_TRUST_ACCOUNT) ? ACB_WSTRUST : ACB_NORMAL) );
939 if (local_flags & LOCAL_DISABLE_USER)
941 pdb_set_acct_ctrl (&new_sam_acct, pdb_get_acct_ctrl(&new_sam_acct)|ACB_DISABLED);
943 if (local_flags & LOCAL_SET_NO_PASSWORD)
945 pdb_set_acct_ctrl (&new_sam_acct, pdb_get_acct_ctrl(&new_sam_acct)|ACB_PWNOTREQ);
949 /* set the passwords here. if we get to here it means
950 we have a valid, active account */
951 pdb_set_lanman_passwd (&new_sam_acct, new_p16);
952 pdb_set_nt_passwd (&new_sam_acct, new_nt_p16);
956 if (pdb_add_sam_account(&new_sam_acct))
958 slprintf(msg_str, msg_str_len-1, "Added user %s.\n", user_name);
959 pdb_clear_sam (&new_sam_acct);
964 slprintf(err_str, err_str_len-1, "Failed to add entry for user %s.\n", user_name);
970 /* the entry already existed */
971 local_flags &= ~LOCAL_ADD_USER;
975 * We are root - just write the new password
976 * and the valid last change time.
979 if(local_flags & LOCAL_DISABLE_USER)
981 pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)|ACB_DISABLED);
983 else if (local_flags & LOCAL_ENABLE_USER)
985 if(pdb_get_lanman_passwd(sam_pass) == NULL)
987 pdb_set_lanman_passwd (sam_pass, new_p16);
988 pdb_set_nt_passwd (sam_pass, new_nt_p16);
990 pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_DISABLED));
991 } else if (local_flags & LOCAL_SET_NO_PASSWORD)
993 pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)|ACB_PWNOTREQ);
995 /* This is needed to preserve ACB_PWNOTREQ in mod_smbfilepwd_entry */
996 pdb_set_lanman_passwd (sam_pass, NULL);
997 pdb_set_nt_passwd (sam_pass, NULL);
1002 * If we're dealing with setting a completely empty user account
1003 * ie. One with a password of 'XXXX', but not set disabled (like
1004 * an account created from scratch) then if the old password was
1005 * 'XX's then getsmbpwent will have set the ACB_DISABLED flag.
1006 * We remove that as we're giving this user their first password
1007 * and the decision hasn't really been made to disable them (ie.
1008 * don't create them disabled). JRA.
1010 if ((pdb_get_lanman_passwd(sam_pass)==NULL) && (pdb_get_acct_ctrl(sam_pass)&ACB_DISABLED))
1011 pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_DISABLED));
1012 pdb_set_acct_ctrl (sam_pass, pdb_get_acct_ctrl(sam_pass)&(~ACB_PWNOTREQ));
1013 pdb_set_lanman_passwd (sam_pass, new_p16);
1014 pdb_set_nt_passwd (sam_pass, new_nt_p16);
1017 if(local_flags & LOCAL_DELETE_USER)
1019 if (!pdb_delete_sam_account(user_name))
1021 slprintf(err_str,err_str_len-1, "Failed to delete entry for user %s.\n", user_name);
1024 slprintf(msg_str, msg_str_len-1, "Deleted user %s.\n", user_name);
1028 if(!pdb_update_sam_account(sam_pass, True))
1030 slprintf(err_str, err_str_len-1, "Failed to modify entry for user %s.\n", user_name);
1033 if(local_flags & LOCAL_DISABLE_USER)
1034 slprintf(msg_str, msg_str_len-1, "Disabled user %s.\n", user_name);
1035 else if (local_flags & LOCAL_ENABLE_USER)
1036 slprintf(msg_str, msg_str_len-1, "Enabled user %s.\n", user_name);
1037 else if (local_flags & LOCAL_SET_NO_PASSWORD)
1038 slprintf(msg_str, msg_str_len-1, "User %s password set to none.\n", user_name);
1045 /*********************************************************************
1046 collection of get...() functions for SAM_ACCOUNT_INFO
1047 ********************************************************************/
1048 uint16 pdb_get_acct_ctrl (SAM_ACCOUNT *sampass)
1051 return (sampass->acct_ctrl);
1053 return (ACB_DISABLED);
1056 time_t pdb_get_logon_time (SAM_ACCOUNT *sampass)
1059 return (sampass->logon_time);
1064 time_t pdb_get_logoff_time (SAM_ACCOUNT *sampass)
1067 return (sampass->logoff_time);
1072 time_t pdb_get_kickoff_time (SAM_ACCOUNT *sampass)
1075 return (sampass->kickoff_time);
1080 time_t pdb_get_pass_last_set_time (SAM_ACCOUNT *sampass)
1083 return (sampass->pass_last_set_time);
1088 time_t pdb_get_pass_can_change_time (SAM_ACCOUNT *sampass)
1091 return (sampass->pass_can_change_time);
1096 time_t pdb_get_pass_must_change_time (SAM_ACCOUNT *sampass)
1099 return (sampass->pass_must_change_time);
1104 uint16 pdb_get_logon_divs (SAM_ACCOUNT *sampass)
1107 return (sampass->logon_divs);
1112 uint32 pdb_get_hours_len (SAM_ACCOUNT *sampass)
1115 return (sampass->hours_len);
1120 uint8* pdb_get_hours (SAM_ACCOUNT *sampass)
1123 return (sampass->hours);
1128 uint8* pdb_get_nt_passwd (SAM_ACCOUNT *sampass)
1131 return (sampass->nt_pw);
1136 uint8* pdb_get_lanman_passwd (SAM_ACCOUNT *sampass)
1139 return (sampass->lm_pw);
1145 uint32 pdb_get_user_rid (SAM_ACCOUNT *sampass)
1148 return (sampass->user_rid);
1153 uint32 pdb_get_group_rid (SAM_ACCOUNT *sampass)
1156 return (sampass->group_rid);
1161 uid_t pdb_get_uid (SAM_ACCOUNT *sampass)
1164 return (sampass->uid);
1169 gid_t pdb_get_gid (SAM_ACCOUNT *sampass)
1172 return (sampass->gid);
1177 char* pdb_get_username (SAM_ACCOUNT *sampass)
1180 return (sampass->username);
1185 char* pdb_get_domain (SAM_ACCOUNT *sampass)
1188 return (sampass->domain);
1193 char* pdb_get_nt_username (SAM_ACCOUNT *sampass)
1196 return (sampass->nt_username);
1201 char* pdb_get_fullname (SAM_ACCOUNT *sampass)
1204 return (sampass->full_name);
1209 char* pdb_get_homedir (SAM_ACCOUNT *sampass)
1212 return (sampass->home_dir);
1217 char* pdb_get_dirdrive (SAM_ACCOUNT *sampass)
1220 return (sampass->dir_drive);
1225 char* pdb_get_logon_script (SAM_ACCOUNT *sampass)
1228 return (sampass->logon_script);
1233 char* pdb_get_profile_path (SAM_ACCOUNT *sampass)
1236 return (sampass->profile_path);
1241 char* pdb_get_acct_desc (SAM_ACCOUNT *sampass)
1244 return (sampass->acct_desc);
1249 char* pdb_get_workstations (SAM_ACCOUNT *sampass)
1252 return (sampass->workstations);
1257 char* pdb_get_munged_dial (SAM_ACCOUNT *sampass)
1260 return (sampass->munged_dial);
1265 uint32 pdb_get_unknown3 (SAM_ACCOUNT *sampass)
1268 return (sampass->unknown_3);
1273 uint32 pdb_get_unknown5 (SAM_ACCOUNT *sampass)
1276 return (sampass->unknown_5);
1281 uint32 pdb_get_unknown6 (SAM_ACCOUNT *sampass)
1284 return (sampass->unknown_6);
1289 /*********************************************************************
1290 collection of set...() functions for SAM_ACCOUNT_INFO
1291 ********************************************************************/
1292 BOOL pdb_set_acct_ctrl (SAM_ACCOUNT *sampass, uint16 flags)
1299 sampass->acct_ctrl = flags;
1306 BOOL pdb_set_logon_time (SAM_ACCOUNT *sampass, time_t mytime)
1311 sampass->logon_time = mytime;
1315 BOOL pdb_set_logoff_time (SAM_ACCOUNT *sampass, time_t mytime)
1320 sampass->logoff_time = mytime;
1324 BOOL pdb_set_kickoff_time (SAM_ACCOUNT *sampass, time_t mytime)
1329 sampass->kickoff_time = mytime;
1333 BOOL pdb_set_pass_can_change_time (SAM_ACCOUNT *sampass, time_t mytime)
1338 sampass->pass_can_change_time = mytime;
1342 BOOL pdb_set_pass_must_change_time (SAM_ACCOUNT *sampass, time_t mytime)
1347 sampass->pass_must_change_time = mytime;
1351 BOOL pdb_set_pass_last_set_time (SAM_ACCOUNT *sampass, time_t mytime)
1356 sampass->pass_last_set_time = mytime;
1360 BOOL pdb_set_hours_len (SAM_ACCOUNT *sampass, uint32 len)
1365 sampass->hours_len = len;
1369 BOOL pdb_set_logons_divs (SAM_ACCOUNT *sampass, uint16 hours)
1374 sampass->logon_divs = hours;
1378 BOOL pdb_set_uid (SAM_ACCOUNT *sampass, uid_t uid)
1387 BOOL pdb_set_gid (SAM_ACCOUNT *sampass, gid_t gid)
1396 BOOL pdb_set_user_rid (SAM_ACCOUNT *sampass, uint32 rid)
1401 sampass->user_rid = rid;
1405 BOOL pdb_set_group_rid (SAM_ACCOUNT *sampass, uint32 grid)
1410 sampass->group_rid = grid;
1414 BOOL pdb_set_username (SAM_ACCOUNT *sampass, char *username)
1418 if (!sampass || !sampass->mem_ctx) return False;
1422 sampass->username = NULL;
1426 len = strlen(username)+1;
1427 sampass->username = (char*)talloc(sampass->mem_ctx, len);
1429 if (sampass->username == NULL )
1431 DEBUG (0,("pdb_set_username: ERROR - Unable to talloc memory for [%s]\n", username));
1435 StrnCpy (sampass->username, username, len-1);
1440 BOOL pdb_set_domain (SAM_ACCOUNT *sampass, char *domain)
1444 if (!sampass || !sampass->mem_ctx) return False;
1448 sampass->domain = NULL;
1452 len = strlen(domain)+1;
1453 sampass->domain = talloc (sampass->mem_ctx, len);
1455 if (sampass->domain == NULL )
1457 DEBUG (0,("pdb_set_domain: ERROR - Unable to talloc memory for [%s]\n", domain));
1461 StrnCpy (sampass->domain, domain, len-1);
1466 BOOL pdb_set_nt_username (SAM_ACCOUNT *sampass, char *nt_username)
1470 if (!sampass || !sampass->mem_ctx) return False;
1474 sampass->nt_username = NULL;
1478 len = strlen(nt_username)+1;
1479 sampass->nt_username = talloc (sampass->mem_ctx, len);
1481 if (sampass->nt_username == NULL )
1483 DEBUG (0,("pdb_set_nt_username: ERROR - Unable to talloc memory for [%s]\n", nt_username));
1487 StrnCpy (sampass->nt_username, nt_username, len-1);
1492 BOOL pdb_set_fullname (SAM_ACCOUNT *sampass, char *fullname)
1496 if (!sampass || !sampass->mem_ctx) return False;
1500 sampass->full_name = NULL;
1504 len = strlen(fullname)+1;
1505 sampass->full_name = talloc (sampass->mem_ctx, len);
1507 if (sampass->full_name == NULL )
1509 DEBUG (0,("pdb_set_fullname: ERROR - Unable to talloc memory for [%s]\n", fullname));
1513 StrnCpy (sampass->full_name, fullname, len-1);
1518 BOOL pdb_set_logon_script (SAM_ACCOUNT *sampass, char *logon_script)
1522 if (!sampass || !sampass->mem_ctx) return False;
1526 sampass->logon_script = NULL;
1530 len = strlen(logon_script)+1;
1531 sampass->logon_script = talloc (sampass->mem_ctx, len);
1533 if (sampass->logon_script == NULL )
1535 DEBUG (0,("pdb_set_logon_script: ERROR - Unable to talloc memory for [%s]\n", logon_script));
1539 StrnCpy (sampass->logon_script, logon_script, len-1);
1544 BOOL pdb_set_profile_path (SAM_ACCOUNT *sampass, char *profile_path)
1548 if (!sampass || !sampass->mem_ctx) return False;
1552 sampass->profile_path = NULL;
1556 len = strlen(profile_path)+1;
1557 sampass->profile_path = talloc (sampass->mem_ctx, len);
1559 if (!sampass->profile_path)
1561 DEBUG (0,("pdb_set_profile_path: ERROR - Unable to talloc memory for [%s]\n", profile_path));
1565 StrnCpy (sampass->profile_path, profile_path, len-1);
1570 BOOL pdb_set_dir_drive (SAM_ACCOUNT *sampass, char *dir_drive)
1574 if (!sampass || !sampass->mem_ctx) return False;
1578 sampass->dir_drive = NULL;
1582 len = strlen(dir_drive)+1;
1583 sampass->dir_drive = talloc (sampass->mem_ctx, len);
1585 if (sampass->dir_drive == NULL )
1587 DEBUG (0,("pdb_set_dir_drive: ERROR - Unable to talloc memory for [%s]\n", dir_drive));
1591 StrnCpy (sampass->dir_drive, dir_drive, len-1);
1596 BOOL pdb_set_homedir (SAM_ACCOUNT *sampass, char *homedir)
1600 if (!sampass || !sampass->mem_ctx) return False;
1604 sampass->home_dir = NULL;
1608 len = strlen(homedir)+1;
1609 sampass->home_dir = talloc (sampass->mem_ctx, len);
1611 if (sampass->home_dir == NULL )
1613 DEBUG (0,("pdb_set_homedir: ERROR - Unable to talloc memory for [%s]\n", homedir));
1617 StrnCpy (sampass->home_dir, homedir, len-1);
1622 BOOL pdb_set_acct_desc (SAM_ACCOUNT *sampass, char *acct_desc)
1626 if (!sampass || !sampass->mem_ctx) return False;
1630 sampass->acct_desc = NULL;
1634 len = strlen(acct_desc)+1;
1635 sampass->acct_desc = talloc (sampass->mem_ctx, len);
1637 if (sampass->acct_desc == NULL )
1639 DEBUG (0,("pdb_set_acct_desc: ERROR - Unable to talloc memory for [%s]\n", acct_desc));
1643 StrnCpy (sampass->acct_desc, acct_desc, len-1);
1648 BOOL pdb_set_workstations (SAM_ACCOUNT *sampass, char *workstations)
1652 if (!sampass || !sampass->mem_ctx) return False;
1656 sampass->workstations = NULL;
1660 len = strlen(workstations)+1;
1661 sampass->workstations = talloc (sampass->mem_ctx, len);
1663 if (sampass->workstations == NULL )
1665 DEBUG (0,("pdb_set_workstations: ERROR - Unable to talloc memory for [%s]\n", workstations));
1669 StrnCpy (sampass->workstations, workstations, len-1);
1674 BOOL pdb_set_munged_dial (SAM_ACCOUNT *sampass, char *munged_dial)
1678 if (!sampass || !sampass->mem_ctx) return False;
1682 sampass->munged_dial = NULL;
1686 len = strlen(munged_dial)+1;
1687 sampass->munged_dial = talloc (sampass->mem_ctx, len);
1689 if (sampass->munged_dial == NULL )
1691 DEBUG (0,("pdb_set_munged_dial: ERROR - Unable to talloc memory for [%s]\n", munged_dial));
1695 StrnCpy (sampass->munged_dial, munged_dial, len-1);
1700 BOOL pdb_set_nt_passwd (SAM_ACCOUNT *sampass, uint8 *pwd)
1702 if (!sampass || !sampass->mem_ctx) return False;
1706 sampass->nt_pw = NULL;
1710 sampass->nt_pw = talloc (sampass->mem_ctx, 16);
1712 if (sampass->nt_pw == NULL )
1714 DEBUG (0,("pdb_set_nt_passwd: ERROR - Unable to talloc memory for [%s]\n", pwd));
1718 memcpy (sampass->nt_pw, pwd, 16);
1723 BOOL pdb_set_lanman_passwd (SAM_ACCOUNT *sampass, uint8 *pwd)
1725 if (!sampass || !sampass->mem_ctx) return False;
1729 sampass->lm_pw = NULL;
1733 sampass->lm_pw = talloc (sampass->mem_ctx, 16);
1735 if (sampass->lm_pw == NULL )
1737 DEBUG (0,("pdb_set_lanman_passwd: ERROR - Unable to talloc memory for [%s]\n", pwd));
1741 memcpy (sampass->lm_pw, pwd, 16);