2010-05-18 14:06:23 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1997-07-08 20:54:44 +04:00
Samba utility functions
1998-01-22 16:27:43 +03:00
Copyright ( C ) Andrew Tridgell 1992 - 1998
2004-01-26 12:03:09 +03:00
Copyright ( C ) Andrew Bartlett 2001 - 2004
2010-04-16 16:16:40 +04:00
1997-07-08 20:54:44 +04:00
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
1997-07-08 20:54:44 +04:00
( at your option ) any later version .
2010-04-16 16:16:40 +04:00
1997-07-08 20:54:44 +04:00
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
2010-04-16 16:16:40 +04:00
1997-07-08 20:54:44 +04:00
You should have received a copy of the GNU General Public License
2007-07-10 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
1997-07-08 20:54:44 +04:00
*/
2004-01-26 12:03:09 +03:00
/* These comments regard the code to change the user's unix password: */
1996-05-04 11:50:46 +04:00
/* fork a child process to exec passwd and write to its
2001-05-03 03:32:09 +04:00
* tty to change a users password . This is running as the
* user who is attempting to change the password .
*/
1996-05-04 11:50:46 +04:00
2007-11-13 02:02:50 +03:00
/*
1996-05-04 11:50:46 +04:00
* This code was copied / borrowed and stolen from various sources .
* The primary source was the poppasswd . c from the authors of POPMail . This software
* was included as a client to change passwords using the ' passwd ' program
* on the remote machine .
*
* This code has been hacked by Bob Nance ( nance @ niehs . nih . gov ) and Evan Patterson
* ( patters2 @ niehs . nih . gov ) at the National Institute of Environmental Health Sciences
* and rights to modify , distribute or incorporate this change to the CAP suite or
* using it for any other reason are granted , so long as this disclaimer is left intact .
*/
/*
This code was hacked considerably for inclusion in Samba , primarily
by Andrew . Tridgell @ anu . edu . au . The biggest change was the addition
of the " password chat " option , which allows the easy runtime
specification of the expected sequence of events to change a
password .
*/
# include "includes.h"
2011-02-25 18:48:10 +03:00
# include "system/terminal.h"
2011-02-25 19:14:22 +03:00
# include "system/passwd.h"
2011-02-26 01:20:06 +03:00
# include "system/filesys.h"
2009-03-16 13:27:58 +03:00
# include "../libcli/auth/libcli_auth.h"
2010-05-18 02:16:40 +04:00
# include "../lib/crypto/arcfour.h"
2011-02-08 16:17:14 +03:00
# include "rpc_server/samr/srv_samr_util.h"
2011-03-18 20:58:37 +03:00
# include "passdb.h"
2011-03-24 14:08:15 +03:00
# include "auth.h"
1996-05-04 11:50:46 +04:00
2011-02-22 21:45:24 +03:00
# ifndef ALLOW_CHANGE_PASSWORD
# if (defined(HAVE_TERMIOS_H) && defined(HAVE_DUP2) && defined(HAVE_SETSID))
# define ALLOW_CHANGE_PASSWORD 1
# endif
# endif
1998-07-29 07:08:05 +04:00
# if ALLOW_CHANGE_PASSWORD
1996-05-04 11:50:46 +04:00
static int findpty ( char * * slave )
{
2007-12-17 05:02:32 +03:00
int master = - 1 ;
char * line = NULL ;
2012-03-28 06:22:03 +04:00
DIR * dirp = NULL ;
2003-03-18 01:56:13 +03:00
const char * dpname ;
2000-03-22 00:08:07 +03:00
2007-12-17 05:02:32 +03:00
* slave = NULL ;
1998-07-29 07:08:05 +04:00
# if defined(HAVE_GRANTPT)
2000-03-22 00:08:07 +03:00
/* Try to open /dev/ptmx. If that fails, fall through to old method. */
2013-01-11 13:31:42 +04:00
master = open ( " /dev/ptmx " , O_RDWR , 0 ) ;
if ( master > = 0 ) {
2000-03-22 00:08:07 +03:00
grantpt ( master ) ;
unlockpt ( master ) ;
2007-12-17 05:02:32 +03:00
line = ( char * ) ptsname ( master ) ;
if ( line ) {
* slave = SMB_STRDUP ( line ) ;
}
if ( * slave = = NULL ) {
2000-03-22 00:08:07 +03:00
DEBUG ( 0 ,
( " findpty: Unable to create master/slave pty pair. \n " ) ) ;
/* Stop fd leak on error. */
close ( master ) ;
return - 1 ;
2007-12-17 05:02:32 +03:00
} else {
2000-03-22 00:08:07 +03:00
DEBUG ( 10 ,
( " findpty: Allocated slave pty %s \n " , * slave ) ) ;
return ( master ) ;
}
}
1999-12-13 16:27:58 +03:00
# endif /* HAVE_GRANTPT */
2007-12-17 05:02:32 +03:00
line = SMB_STRDUP ( " /dev/ptyXX " ) ;
if ( ! line ) {
return ( - 1 ) ;
}
2000-03-22 00:08:07 +03:00
2012-03-28 06:01:19 +04:00
dirp = opendir ( " /dev " ) ;
2007-12-17 05:02:32 +03:00
if ( ! dirp ) {
SAFE_FREE ( line ) ;
2000-03-22 00:08:07 +03:00
return ( - 1 ) ;
2007-12-17 05:02:32 +03:00
}
while ( ( dpname = readdirname ( dirp ) ) ! = NULL ) {
if ( strncmp ( dpname , " pty " , 3 ) = = 0 & & strlen ( dpname ) = = 5 ) {
2000-03-22 00:08:07 +03:00
DEBUG ( 3 ,
( " pty: try to open %s, line was %s \n " , dpname ,
line ) ) ;
line [ 8 ] = dpname [ 3 ] ;
line [ 9 ] = dpname [ 4 ] ;
2012-03-28 05:48:00 +04:00
if ( ( master = open ( line , O_RDWR , 0 ) ) > = 0 ) {
2000-03-22 00:08:07 +03:00
DEBUG ( 3 , ( " pty: opened %s \n " , line ) ) ;
line [ 5 ] = ' t ' ;
* slave = line ;
2012-03-28 06:08:27 +04:00
closedir ( dirp ) ;
2000-03-22 00:08:07 +03:00
return ( master ) ;
}
}
}
2012-03-28 06:08:27 +04:00
closedir ( dirp ) ;
2007-12-17 05:02:32 +03:00
SAFE_FREE ( line ) ;
2000-03-22 00:08:07 +03:00
return ( - 1 ) ;
1996-05-04 11:50:46 +04:00
}
2001-10-29 10:28:32 +03:00
static int dochild ( int master , const char * slavedev , const struct passwd * pass ,
2007-10-19 04:40:25 +04:00
const char * passwordprogram , bool as_root )
1996-05-04 11:50:46 +04:00
{
2000-03-22 00:08:07 +03:00
int slave ;
struct termios stermios ;
gid_t gid ;
uid_t uid ;
2007-12-15 04:02:50 +03:00
char * const eptrs [ 1 ] = { NULL } ;
2000-03-22 00:08:07 +03:00
if ( pass = = NULL )
{
DEBUG ( 0 ,
2001-10-29 10:28:32 +03:00
( " dochild: user doesn't exist in the UNIX password database. \n " ) ) ;
2000-03-22 00:08:07 +03:00
return False ;
}
gid = pass - > pw_gid ;
uid = pass - > pw_uid ;
gain_root_privilege ( ) ;
/* Start new session - gets rid of controlling terminal. */
if ( setsid ( ) < 0 )
{
DEBUG ( 3 ,
( " Weirdness, couldn't let go of controlling terminal \n " ) ) ;
return ( False ) ;
}
/* Open slave pty and acquire as new controlling terminal. */
2012-03-28 05:48:00 +04:00
if ( ( slave = open ( slavedev , O_RDWR , 0 ) ) < 0 )
2000-03-22 00:08:07 +03:00
{
DEBUG ( 3 , ( " More weirdness, could not open %s \n " , slavedev ) ) ;
return ( False ) ;
}
2008-04-11 13:34:33 +04:00
# if defined(TIOCSCTTY) && !defined(SUNOS5)
/*
* On patched Solaris 10 TIOCSCTTY is defined but seems not to work ,
* see the discussion under
* https : //bugzilla.samba.org/show_bug.cgi?id=5366.
*/
2007-12-17 21:44:09 +03:00
if ( ioctl ( slave , TIOCSCTTY , 0 ) < 0 )
{
DEBUG ( 3 , ( " Error in ioctl call for slave pty \n " ) ) ;
/* return(False); */
}
# elif defined(I_PUSH) && defined(I_FIND)
2006-01-14 23:25:51 +03:00
if ( ioctl ( slave , I_FIND , " ptem " ) = = 0 ) {
ioctl ( slave , I_PUSH , " ptem " ) ;
}
if ( ioctl ( slave , I_FIND , " ldterm " ) = = 0 ) {
ioctl ( slave , I_PUSH , " ldterm " ) ;
}
2000-03-22 00:08:07 +03:00
# endif
/* Close master. */
close ( master ) ;
/* Make slave stdin/out/err of child. */
2008-11-01 05:58:14 +03:00
if ( dup2 ( slave , STDIN_FILENO ) ! = STDIN_FILENO )
2000-03-22 00:08:07 +03:00
{
DEBUG ( 3 , ( " Could not re-direct stdin \n " ) ) ;
return ( False ) ;
}
2008-11-01 05:58:14 +03:00
if ( dup2 ( slave , STDOUT_FILENO ) ! = STDOUT_FILENO )
2000-03-22 00:08:07 +03:00
{
DEBUG ( 3 , ( " Could not re-direct stdout \n " ) ) ;
return ( False ) ;
}
2008-11-01 05:58:14 +03:00
if ( dup2 ( slave , STDERR_FILENO ) ! = STDERR_FILENO )
2000-03-22 00:08:07 +03:00
{
DEBUG ( 3 , ( " Could not re-direct stderr \n " ) ) ;
return ( False ) ;
}
if ( slave > 2 )
close ( slave ) ;
/* Set proper terminal attributes - no echo, canonical input processing,
no map NL to CR / NL on output . */
if ( tcgetattr ( 0 , & stermios ) < 0 )
{
DEBUG ( 3 ,
( " could not read default terminal attributes on pty \n " ) ) ;
return ( False ) ;
}
stermios . c_lflag & = ~ ( ECHO | ECHOE | ECHOK | ECHONL ) ;
stermios . c_lflag | = ICANON ;
2002-08-17 19:27:10 +04:00
# ifdef ONLCR
2010-05-18 14:06:23 +04:00
stermios . c_oflag & = ~ ( ONLCR ) ;
2002-08-17 19:27:10 +04:00
# endif
2000-03-22 00:08:07 +03:00
if ( tcsetattr ( 0 , TCSANOW , & stermios ) < 0 )
{
DEBUG ( 3 , ( " could not set attributes of pty \n " ) ) ;
return ( False ) ;
}
/* make us completely into the right uid */
if ( ! as_root )
{
become_user_permanently ( uid , gid ) ;
}
DEBUG ( 10 ,
( " Invoking '%s' as password change program. \n " ,
passwordprogram ) ) ;
/* execl() password-change application */
2007-12-15 04:02:50 +03:00
if ( execle ( " /bin/sh " , " sh " , " -c " , passwordprogram , NULL , eptrs ) < 0 )
2000-03-22 00:08:07 +03:00
{
DEBUG ( 3 , ( " Bad status returned from %s \n " , passwordprogram ) ) ;
return ( False ) ;
}
return ( True ) ;
1996-05-04 11:50:46 +04:00
}
1999-12-13 16:27:58 +03:00
static int expect ( int master , char * issue , char * expected )
1996-05-04 11:50:46 +04:00
{
2007-11-13 02:02:50 +03:00
char buffer [ 1024 ] ;
2008-01-26 01:41:48 +03:00
int attempts , timeout , nread ;
size_t len ;
2007-10-19 04:40:25 +04:00
bool match = False ;
1996-05-04 11:50:46 +04:00
2001-10-03 01:58:09 +04:00
for ( attempts = 0 ; attempts < 2 ; attempts + + ) {
2008-04-19 15:08:14 +04:00
NTSTATUS status ;
2001-10-03 01:58:09 +04:00
if ( ! strequal ( issue , " . " ) ) {
1999-12-13 16:27:58 +03:00
if ( lp_passwd_chat_debug ( ) )
DEBUG ( 100 , ( " expect: sending [%s] \n " , issue ) ) ;
1996-05-04 11:50:46 +04:00
2006-05-25 19:43:12 +04:00
if ( ( len = sys_write ( master , issue , strlen ( issue ) ) ) ! = strlen ( issue ) ) {
2008-01-26 01:41:48 +03:00
DEBUG ( 2 , ( " expect: (short) write returned %d \n " ,
( int ) len ) ) ;
2001-10-03 01:58:09 +04:00
return False ;
}
1999-12-13 16:27:58 +03:00
}
1996-05-04 11:50:46 +04:00
1999-12-13 16:27:58 +03:00
if ( strequal ( expected , " . " ) )
return True ;
2003-11-24 23:18:47 +03:00
/* Initial timeout. */
timeout = lp_passwd_chat_timeout ( ) * 1000 ;
1999-12-13 16:27:58 +03:00
nread = 0 ;
buffer [ nread ] = 0 ;
2008-01-26 01:41:48 +03:00
while ( True ) {
2009-09-07 08:38:50 +04:00
status = read_fd_with_timeout (
2008-01-26 01:41:48 +03:00
master , buffer + nread , 1 ,
sizeof ( buffer ) - nread - 1 ,
timeout , & len ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2010-08-15 17:23:47 +04:00
DEBUG ( 2 , ( " expect: read error %s \n " ,
nt_errstr ( status ) ) ) ;
2008-01-26 01:41:48 +03:00
break ;
}
1999-12-13 16:27:58 +03:00
nread + = len ;
buffer [ nread ] = 0 ;
2001-10-03 01:58:09 +04:00
{
/* Eat leading/trailing whitespace before match. */
2007-11-12 08:46:52 +03:00
char * str = SMB_STRDUP ( buffer ) ;
if ( ! str ) {
DEBUG ( 2 , ( " expect: ENOMEM \n " ) ) ;
return False ;
}
trim_char ( str , ' ' , ' ' ) ;
2001-10-03 01:58:09 +04:00
2005-12-16 04:41:12 +03:00
if ( ( match = unix_wild_match ( expected , str ) ) = = True ) {
2003-11-24 23:18:47 +03:00
/* Now data has started to return, lower timeout. */
timeout = lp_passwd_chat_timeout ( ) * 100 ;
}
2007-11-12 08:46:52 +03:00
SAFE_FREE ( str ) ;
2001-10-03 01:58:09 +04:00
}
1999-12-13 16:27:58 +03:00
}
if ( lp_passwd_chat_debug ( ) )
2001-10-03 01:58:09 +04:00
DEBUG ( 100 , ( " expect: expected [%s] received [%s] match %s \n " ,
expected , buffer , match ? " yes " : " no " ) ) ;
1999-12-13 16:27:58 +03:00
if ( match )
break ;
2008-04-19 15:08:14 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 2 , ( " expect: %s \n " , nt_errstr ( status ) ) ) ;
1999-12-13 16:27:58 +03:00
return False ;
}
}
2001-10-03 01:58:09 +04:00
DEBUG ( 10 , ( " expect: returning %s \n " , match ? " True " : " False " ) ) ;
1999-12-13 16:27:58 +03:00
return match ;
1996-05-04 11:50:46 +04:00
}
static void pwd_sub ( char * buf )
{
2000-03-22 00:08:07 +03:00
all_string_sub ( buf , " \\ n " , " \n " , 0 ) ;
all_string_sub ( buf , " \\ r " , " \r " , 0 ) ;
all_string_sub ( buf , " \\ s " , " " , 0 ) ;
all_string_sub ( buf , " \\ t " , " \t " , 0 ) ;
1996-05-04 11:50:46 +04:00
}
2002-11-13 02:20:50 +03:00
static int talktochild ( int master , const char * seq )
1996-05-04 11:50:46 +04:00
{
2007-12-08 04:32:32 +03:00
TALLOC_CTX * frame = talloc_stackframe ( ) ;
1999-12-13 16:27:58 +03:00
int count = 0 ;
2007-12-08 04:32:32 +03:00
char * issue ;
char * expected ;
1996-05-04 11:50:46 +04:00
2007-12-08 04:32:32 +03:00
issue = talloc_strdup ( frame , " . " ) ;
if ( ! issue ) {
TALLOC_FREE ( frame ) ;
return false ;
}
1996-05-04 11:50:46 +04:00
2007-12-08 04:32:32 +03:00
while ( next_token_talloc ( frame , & seq , & expected , NULL ) ) {
1999-12-13 16:27:58 +03:00
pwd_sub ( expected ) ;
count + + ;
1996-05-04 11:50:46 +04:00
2007-12-08 04:32:32 +03:00
if ( ! expect ( master , issue , expected ) ) {
2000-03-22 00:08:07 +03:00
DEBUG ( 3 , ( " Response %d incorrect \n " , count ) ) ;
2007-12-08 04:32:32 +03:00
TALLOC_FREE ( frame ) ;
return false ;
1999-12-13 16:27:58 +03:00
}
1996-05-04 11:50:46 +04:00
2007-12-08 04:32:32 +03:00
if ( ! next_token_talloc ( frame , & seq , & issue , NULL ) ) {
issue = talloc_strdup ( frame , " . " ) ;
if ( ! issue ) {
TALLOC_FREE ( frame ) ;
return false ;
}
}
1999-12-13 16:27:58 +03:00
pwd_sub ( issue ) ;
}
2007-12-08 04:32:32 +03:00
2001-12-05 01:24:17 +03:00
if ( ! strequal ( issue , " . " ) ) {
/* we have one final issue to send */
2007-12-08 04:32:32 +03:00
expected = talloc_strdup ( frame , " . " ) ;
if ( ! expected ) {
TALLOC_FREE ( frame ) ;
return false ;
}
if ( ! expect ( master , issue , expected ) ) {
TALLOC_FREE ( frame ) ;
2001-12-05 01:24:17 +03:00
return False ;
2007-12-08 04:32:32 +03:00
}
2001-12-05 01:24:17 +03:00
}
2007-12-08 04:32:32 +03:00
TALLOC_FREE ( frame ) ;
1999-12-13 16:27:58 +03:00
return ( count > 0 ) ;
1996-05-04 11:50:46 +04:00
}
2007-10-19 04:40:25 +04:00
static bool chat_with_program ( char * passwordprogram , const struct passwd * pass ,
char * chatsequence , bool as_root )
1996-05-04 11:50:46 +04:00
{
2007-12-17 05:02:32 +03:00
char * slavedev = NULL ;
2000-03-22 00:08:07 +03:00
int master ;
pid_t pid , wpid ;
int wstat ;
2007-10-19 04:40:25 +04:00
bool chstat = False ;
2000-03-22 00:08:07 +03:00
2003-09-04 22:02:17 +04:00
if ( pass = = NULL ) {
DEBUG ( 0 , ( " chat_with_program: user doesn't exist in the UNIX password database. \n " ) ) ;
2001-10-29 10:28:32 +03:00
return False ;
}
2000-03-22 00:08:07 +03:00
/* allocate a pseudo-terminal device */
2003-09-04 22:02:17 +04:00
if ( ( master = findpty ( & slavedev ) ) < 0 ) {
2004-01-07 01:34:06 +03:00
DEBUG ( 3 , ( " chat_with_program: Cannot Allocate pty for password change: %s \n " , pass - > pw_name ) ) ;
2000-03-22 00:08:07 +03:00
return ( False ) ;
}
1998-03-27 22:59:14 +03:00
1999-12-13 16:27:58 +03:00
/*
2000-03-22 00:08:07 +03:00
* We need to temporarily stop CatchChild from eating
* SIGCLD signals as it also eats the exit status code . JRA .
1999-12-13 16:27:58 +03:00
*/
2000-03-22 00:08:07 +03:00
CatchChildLeaveStatus ( ) ;
2012-03-24 23:17:08 +04:00
if ( ( pid = fork ( ) ) < 0 ) {
2004-01-07 01:34:06 +03:00
DEBUG ( 3 , ( " chat_with_program: Cannot fork() child for password change: %s \n " , pass - > pw_name ) ) ;
2007-12-17 05:02:32 +03:00
SAFE_FREE ( slavedev ) ;
2000-03-22 00:08:07 +03:00
close ( master ) ;
CatchChild ( ) ;
return ( False ) ;
}
/* we now have a pty */
2003-09-04 22:02:17 +04:00
if ( pid > 0 ) { /* This is the parent process */
2007-12-17 05:02:32 +03:00
/* Don't need this anymore in parent. */
SAFE_FREE ( slavedev ) ;
2003-09-04 22:02:17 +04:00
if ( ( chstat = talktochild ( master , chatsequence ) ) = = False ) {
2004-01-07 01:34:06 +03:00
DEBUG ( 3 , ( " chat_with_program: Child failed to change password: %s \n " , pass - > pw_name ) ) ;
2000-03-22 00:08:07 +03:00
kill ( pid , SIGKILL ) ; /* be sure to end this process */
}
2003-09-04 22:02:17 +04:00
while ( ( wpid = sys_waitpid ( pid , & wstat , 0 ) ) < 0 ) {
if ( errno = = EINTR ) {
2000-03-22 00:08:07 +03:00
errno = 0 ;
continue ;
}
break ;
}
2003-09-04 22:02:17 +04:00
if ( wpid < 0 ) {
2004-01-07 01:34:06 +03:00
DEBUG ( 3 , ( " chat_with_program: The process is no longer waiting! \n \n " ) ) ;
2000-03-22 00:08:07 +03:00
close ( master ) ;
CatchChild ( ) ;
return ( False ) ;
}
/*
* Go back to ignoring children .
*/
CatchChild ( ) ;
close ( master ) ;
2003-09-04 22:02:17 +04:00
if ( pid ! = wpid ) {
2004-01-07 01:34:06 +03:00
DEBUG ( 3 , ( " chat_with_program: We were waiting for the wrong process ID \n " ) ) ;
2000-03-22 00:08:07 +03:00
return ( False ) ;
}
2004-01-07 01:34:06 +03:00
if ( WIFEXITED ( wstat ) & & ( WEXITSTATUS ( wstat ) ! = 0 ) ) {
DEBUG ( 3 , ( " chat_with_program: The process exited with status %d \
while we were waiting \ n " , WEXITSTATUS(wstat)));
2000-03-22 00:08:07 +03:00
return ( False ) ;
}
2004-01-07 01:34:06 +03:00
# if defined(WIFSIGNALLED) && defined(WTERMSIG)
else if ( WIFSIGNALLED ( wstat ) ) {
DEBUG ( 3 , ( " chat_with_program: The process was killed by signal %d \
while we were waiting \ n " , WTERMSIG(wstat)));
2000-03-22 00:08:07 +03:00
return ( False ) ;
}
2004-01-07 01:34:06 +03:00
# endif
2003-09-04 22:02:17 +04:00
} else {
2000-03-22 00:08:07 +03:00
/* CHILD */
1998-07-27 22:50:45 +04:00
2000-03-22 00:08:07 +03:00
/*
2006-03-23 02:49:09 +03:00
* Lose any elevated privileges .
2000-03-22 00:08:07 +03:00
*/
2006-03-21 05:56:49 +03:00
drop_effective_capability ( KERNEL_OPLOCK_CAPABILITY ) ;
2006-03-23 02:49:09 +03:00
drop_effective_capability ( DMAPI_ACCESS_CAPABILITY ) ;
2000-03-22 00:08:07 +03:00
/* make sure it doesn't freeze */
alarm ( 20 ) ;
if ( as_root )
2000-06-23 09:57:48 +04:00
become_root ( ) ;
2004-01-07 01:34:06 +03:00
DEBUG ( 3 , ( " chat_with_program: Dochild for user %s (uid=%d,gid=%d) (as_root = %s) \n " , pass - > pw_name ,
2003-09-04 22:02:17 +04:00
( int ) getuid ( ) , ( int ) getgid ( ) , BOOLSTR ( as_root ) ) ) ;
chstat = dochild ( master , slavedev , pass , passwordprogram , as_root ) ;
2000-03-22 00:08:07 +03:00
2000-06-23 09:57:48 +04:00
if ( as_root )
unbecome_root ( ) ;
2000-03-22 00:08:07 +03:00
/*
* The child should never return from dochild ( ) . . . .
*/
2003-09-04 22:02:17 +04:00
DEBUG ( 0 , ( " chat_with_program: Error: dochild() returned %d \n " , chstat ) ) ;
2000-03-22 00:08:07 +03:00
exit ( 1 ) ;
}
if ( chstat )
2004-01-07 01:34:06 +03:00
DEBUG ( 3 , ( " chat_with_program: Password change %ssuccessful for user %s \n " ,
2001-10-29 10:28:32 +03:00
( chstat ? " " : " un " ) , pass - > pw_name ) ) ;
2000-03-22 00:08:07 +03:00
return ( chstat ) ;
1996-05-04 11:50:46 +04:00
}
2010-08-18 21:51:45 +04:00
bool chgpasswd ( const char * name , const char * rhost , const struct passwd * pass ,
2007-10-19 04:40:25 +04:00
const char * oldpass , const char * newpass , bool as_root )
1996-05-04 11:50:46 +04:00
{
2007-11-13 02:02:50 +03:00
char * passwordprogram = NULL ;
char * chatsequence = NULL ;
2000-03-22 00:08:07 +03:00
size_t i ;
size_t len ;
2007-11-13 02:02:50 +03:00
TALLOC_CTX * ctx = talloc_tos ( ) ;
1996-05-04 11:50:46 +04:00
2003-01-04 11:45:17 +03:00
if ( ! oldpass ) {
oldpass = " " ;
}
2004-01-07 01:34:06 +03:00
DEBUG ( 3 , ( " chgpasswd: Password change (as_root=%s) for user: %s \n " , BOOLSTR ( as_root ) , name ) ) ;
1996-05-04 11:50:46 +04:00
2004-10-11 04:32:31 +04:00
# ifdef DEBUG_PASSWORD
2004-01-07 01:34:06 +03:00
DEBUG ( 100 , ( " chgpasswd: Passwords: old=%s new=%s \n " , oldpass , newpass ) ) ;
1996-05-04 11:50:46 +04:00
# endif
2000-03-22 00:08:07 +03:00
/* Take the passed information and test it for minimum criteria */
1998-03-27 22:59:14 +03:00
2000-03-22 00:08:07 +03:00
/* Password is same as old password */
2001-05-03 03:32:09 +04:00
if ( strcmp ( oldpass , newpass ) = = 0 ) {
/* don't allow same password */
2004-01-07 01:34:06 +03:00
DEBUG ( 2 , ( " chgpasswd: Password Change: %s, New password is same as old \n " , name ) ) ; /* log the attempt */
2000-03-22 00:08:07 +03:00
return ( False ) ; /* inform the user */
}
2007-11-13 02:02:50 +03:00
/*
2000-03-22 00:08:07 +03:00
* Check the old and new passwords don ' t contain any control
* characters .
*/
len = strlen ( oldpass ) ;
2001-05-03 03:32:09 +04:00
for ( i = 0 ; i < len ; i + + ) {
if ( iscntrl ( ( int ) oldpass [ i ] ) ) {
2004-01-07 01:34:06 +03:00
DEBUG ( 0 , ( " chgpasswd: oldpass contains control characters (disallowed). \n " ) ) ;
2000-03-22 00:08:07 +03:00
return False ;
}
}
len = strlen ( newpass ) ;
2001-05-03 03:32:09 +04:00
for ( i = 0 ; i < len ; i + + ) {
if ( iscntrl ( ( int ) newpass [ i ] ) ) {
2004-01-07 01:34:06 +03:00
DEBUG ( 0 , ( " chgpasswd: newpass contains control characters (disallowed). \n " ) ) ;
2000-03-22 00:08:07 +03:00
return False ;
}
}
2007-11-13 02:02:50 +03:00
2001-05-03 03:32:09 +04:00
# ifdef WITH_PAM
if ( lp_pam_password_change ( ) ) {
2007-10-19 04:40:25 +04:00
bool ret ;
2007-12-15 04:02:50 +03:00
# ifdef HAVE_SETLOCALE
2007-12-16 09:28:38 +03:00
const char * prevlocale = setlocale ( LC_ALL , " C " ) ;
2007-12-15 04:02:50 +03:00
# endif
2001-05-03 03:32:09 +04:00
if ( as_root )
become_root ( ) ;
2001-10-29 10:28:32 +03:00
if ( pass ) {
2010-08-18 21:51:45 +04:00
ret = smb_pam_passchange ( pass - > pw_name , rhost ,
oldpass , newpass ) ;
2001-10-29 10:28:32 +03:00
} else {
2010-08-18 21:51:45 +04:00
ret = smb_pam_passchange ( name , rhost , oldpass ,
newpass ) ;
2001-10-29 10:28:32 +03:00
}
2007-11-13 02:02:50 +03:00
2001-05-03 03:32:09 +04:00
if ( as_root )
unbecome_root ( ) ;
2007-12-15 04:02:50 +03:00
# ifdef HAVE_SETLOCALE
2007-12-16 09:28:38 +03:00
setlocale ( LC_ALL , prevlocale ) ;
2007-12-15 04:02:50 +03:00
# endif
2001-05-03 03:32:09 +04:00
return ret ;
}
# endif
2001-10-29 10:28:32 +03:00
/* A non-PAM password change just doen't make sense without a valid local user */
2003-09-04 22:02:17 +04:00
if ( pass = = NULL ) {
DEBUG ( 0 , ( " chgpasswd: user %s doesn't exist in the UNIX password database. \n " , name ) ) ;
2007-11-13 02:02:50 +03:00
return false ;
2001-10-29 10:28:32 +03:00
}
2012-07-18 09:37:23 +04:00
passwordprogram = lp_passwd_program ( ctx ) ;
2007-11-13 02:02:50 +03:00
if ( ! passwordprogram | | ! * passwordprogram ) {
2001-10-03 01:58:09 +04:00
DEBUG ( 2 , ( " chgpasswd: Null password program - no password changing \n " ) ) ;
2007-11-13 02:02:50 +03:00
return false ;
}
2012-07-18 09:37:23 +04:00
chatsequence = lp_passwd_chat ( ctx ) ;
2007-11-13 02:02:50 +03:00
if ( ! chatsequence | | ! * chatsequence ) {
DEBUG ( 2 , ( " chgpasswd: Null chat sequence - no password changing \n " ) ) ;
return false ;
2001-05-03 03:32:09 +04:00
}
2001-10-03 01:58:09 +04:00
if ( as_root ) {
/* The password program *must* contain the user name to work. Fail if not. */
2004-03-09 03:17:14 +03:00
if ( strstr_m ( passwordprogram , " %u " ) = = NULL ) {
2001-10-03 01:58:09 +04:00
DEBUG ( 0 , ( " chgpasswd: Running as root the 'passwd program' parameter *MUST* contain \
the string % % u , and the given string % s does not . \ n " , passwordprogram ));
2007-11-13 02:02:50 +03:00
return false ;
2001-10-03 01:58:09 +04:00
}
}
2007-11-13 02:02:50 +03:00
passwordprogram = talloc_string_sub ( ctx , passwordprogram , " %u " , name ) ;
if ( ! passwordprogram ) {
return false ;
}
2000-03-22 00:08:07 +03:00
/* note that we do NOT substitute the %o and %n in the password program
as this would open up a security hole where the user could use
a new password containing shell escape characters */
2007-11-13 02:02:50 +03:00
chatsequence = talloc_string_sub ( ctx , chatsequence , " %u " , name ) ;
if ( ! chatsequence ) {
return false ;
}
chatsequence = talloc_all_string_sub ( ctx ,
chatsequence ,
" %o " ,
oldpass ) ;
if ( ! chatsequence ) {
return false ;
}
chatsequence = talloc_all_string_sub ( ctx ,
chatsequence ,
" %n " ,
newpass ) ;
return chat_with_program ( passwordprogram ,
pass ,
chatsequence ,
as_root ) ;
1996-05-04 11:50:46 +04:00
}
1998-05-14 05:30:40 +04:00
# else /* ALLOW_CHANGE_PASSWORD */
2001-05-01 01:05:58 +04:00
2010-05-18 14:06:23 +04:00
bool chgpasswd ( const char * name , const struct passwd * pass ,
2007-10-19 04:40:25 +04:00
const char * oldpass , const char * newpass , bool as_root )
1996-05-04 11:50:46 +04:00
{
2004-01-26 12:03:09 +03:00
DEBUG ( 0 , ( " chgpasswd: Unix Password changing not compiled in (user=%s) \n " , name ) ) ;
2000-03-22 00:08:07 +03:00
return ( False ) ;
1996-05-04 11:50:46 +04:00
}
1998-05-14 05:30:40 +04:00
# endif /* ALLOW_CHANGE_PASSWORD */
1998-01-17 10:08:21 +03:00
1998-10-19 21:32:10 +04:00
/***********************************************************
2007-11-13 02:02:50 +03:00
Decrypt and verify a user password change .
1998-03-18 22:07:53 +03:00
2007-11-13 02:02:50 +03:00
The 516 byte long buffers are encrypted with the old NT and
old LM passwords , and if the NT passwords are present , both
2004-01-26 12:03:09 +03:00
buffers contain a unicode string .
1998-03-18 22:07:53 +03:00
2004-01-26 12:03:09 +03:00
After decrypting the buffers , check the password is correct by
matching the old hashed passwords with the passwords in the passdb .
2007-11-13 02:02:50 +03:00
1998-10-19 21:32:10 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-07 00:48:46 +03:00
2003-01-04 11:45:17 +03:00
static NTSTATUS check_oem_password ( const char * user ,
2007-11-13 02:02:50 +03:00
uchar password_encrypted_with_lm_hash [ 516 ] ,
2004-01-26 12:03:09 +03:00
const uchar old_lm_hash_encrypted [ 16 ] ,
2007-11-13 02:02:50 +03:00
uchar password_encrypted_with_nt_hash [ 516 ] ,
2004-01-26 12:03:09 +03:00
const uchar old_nt_hash_encrypted [ 16 ] ,
2009-05-08 01:22:26 +04:00
struct samu * sampass ,
2007-11-13 02:02:50 +03:00
char * * pp_new_passwd )
1998-10-19 21:32:10 +04:00
{
2009-01-08 10:47:57 +03:00
uchar null_pw [ 16 ] ;
uchar null_ntpw [ 16 ] ;
2005-03-22 21:07:58 +03:00
uint8 * password_encrypted ;
const uint8 * encryption_key ;
This commit is number 4 of 4.
In particular this commit focuses on:
Actually adding the 'const' to the passdb interface, and the flow-on changes.
Also kill off the 'disp_info' stuff, as its no longer used.
While these changes have been mildly tested, and are pretty small, any
assistance in this is appreciated.
----
These changes introduces a large dose of 'const' to the Samba tree.
There are a number of good reasons to do this:
- I want to allow the SAM_ACCOUNT structure to move from wasteful
pstrings and fstrings to allocated strings. We can't do that if
people are modifying these outputs, as they may well make
assumptions about getting pstrings and fstrings
- I want --with-pam_smbpass to compile with a slightly sane
volume of warnings, currently its pretty bad, even in 2.2
where is compiles at all.
- Tridge assures me that he no longer opposes 'const religion'
based on the ability to #define const the problem away.
- Changed Get_Pwnam(x,y) into two variants (so that the const
parameter can work correctly): - Get_Pwnam(const x) and
Get_Pwnam_Modify(x).
- Reworked smbd/chgpasswd.c to work with these mods, passing
around a 'struct passwd' rather than the modified username
---
This finishes this line of commits off, your tree should now compile again :-)
Andrew Bartlett
(This used to be commit c95f5aeb9327347674589ae313b75bee3bf8e317)
2001-10-29 10:35:11 +03:00
const uint8 * lanman_pw , * nt_pw ;
2006-02-27 13:32:45 +03:00
uint32 acct_ctrl ;
2009-03-16 13:27:58 +03:00
size_t new_pw_len ;
2004-01-26 12:03:09 +03:00
uchar new_nt_hash [ 16 ] ;
uchar new_lm_hash [ 16 ] ;
2004-11-25 07:10:19 +03:00
uchar verifier [ 16 ] ;
1998-10-21 20:28:44 +04:00
char no_pw [ 2 ] ;
1998-10-21 20:58:34 +04:00
2007-10-19 04:40:25 +04:00
bool nt_pass_set = ( password_encrypted_with_nt_hash & & old_nt_hash_encrypted ) ;
bool lm_pass_set = ( password_encrypted_with_lm_hash & & old_lm_hash_encrypted ) ;
1998-10-19 21:32:10 +04:00
2000-11-14 02:03:34 +03:00
acct_ctrl = pdb_get_acct_ctrl ( sampass ) ;
2009-05-08 01:54:58 +04:00
#if 0
/* I am convinced this check here is wrong, it is valid to
* change a password of a user that has a disabled account - gd */
2007-11-13 02:02:50 +03:00
2001-05-04 19:44:27 +04:00
if ( acct_ctrl & ACB_DISABLED ) {
2004-01-26 12:03:09 +03:00
DEBUG ( 2 , ( " check_lanman_password: account %s disabled. \n " , user ) ) ;
2003-01-04 11:45:17 +03:00
return NT_STATUS_ACCOUNT_DISABLED ;
1998-10-19 21:32:10 +04:00
}
2009-05-08 01:54:58 +04:00
# endif
2004-11-25 07:10:19 +03:00
if ( ( acct_ctrl & ACB_PWNOTREQ ) & & lp_null_passwords ( ) ) {
2004-01-26 12:03:09 +03:00
/* construct a null password (in case one is needed */
no_pw [ 0 ] = 0 ;
no_pw [ 1 ] = 0 ;
nt_lm_owf_gen ( no_pw , null_ntpw , null_pw ) ;
lanman_pw = null_pw ;
nt_pw = null_pw ;
1998-10-19 21:32:10 +04:00
2004-01-26 12:03:09 +03:00
} else {
/* save pointers to passwords so we don't have to keep looking them up */
if ( lp_lanman_auth ( ) ) {
lanman_pw = pdb_get_lanman_passwd ( sampass ) ;
} else {
lanman_pw = NULL ;
1998-10-19 21:32:10 +04:00
}
2004-01-26 12:03:09 +03:00
nt_pw = pdb_get_nt_passwd ( sampass ) ;
1998-10-19 21:32:10 +04:00
}
2004-01-26 12:03:09 +03:00
if ( nt_pw & & nt_pass_set ) {
/* IDEAL Case: passwords are in unicode, and we can
2007-11-13 02:02:50 +03:00
* read use the password encrypted with the NT hash
2004-01-26 12:03:09 +03:00
*/
password_encrypted = password_encrypted_with_nt_hash ;
encryption_key = nt_pw ;
} else if ( lanman_pw & & lm_pass_set ) {
/* password may still be in unicode, but use LM hash version */
password_encrypted = password_encrypted_with_lm_hash ;
encryption_key = lanman_pw ;
} else if ( nt_pass_set ) {
2010-05-18 14:06:23 +04:00
DEBUG ( 1 , ( " NT password change supplied for user %s, but we have no NT password to check it with \n " ,
2004-01-26 12:03:09 +03:00
user ) ) ;
2007-11-13 02:02:50 +03:00
return NT_STATUS_WRONG_PASSWORD ;
2004-01-26 12:03:09 +03:00
} else if ( lm_pass_set ) {
2004-12-23 05:16:57 +03:00
if ( lp_lanman_auth ( ) ) {
2010-05-18 14:06:23 +04:00
DEBUG ( 1 , ( " LM password change supplied for user %s, but we have no LanMan password to check it with \n " ,
2004-12-23 05:16:57 +03:00
user ) ) ;
} else {
2010-05-18 14:06:23 +04:00
DEBUG ( 1 , ( " LM password change supplied for user %s, but we have disabled LanMan authentication \n " ,
2004-12-23 05:16:57 +03:00
user ) ) ;
}
2004-01-26 12:03:09 +03:00
return NT_STATUS_WRONG_PASSWORD ;
} else {
2010-05-18 14:06:23 +04:00
DEBUG ( 1 , ( " password change requested for user %s, but no password supplied! \n " ,
2004-01-26 12:03:09 +03:00
user ) ) ;
return NT_STATUS_WRONG_PASSWORD ;
1998-10-19 21:32:10 +04:00
}
2007-11-13 02:02:50 +03:00
/*
* Decrypt the password with the key
1999-12-13 16:27:58 +03:00
*/
2009-03-16 13:27:58 +03:00
arcfour_crypt ( password_encrypted , encryption_key , 516 ) ;
1999-12-13 16:27:58 +03:00
2007-11-13 02:02:50 +03:00
if ( ! decode_pw_buffer ( talloc_tos ( ) ,
password_encrypted ,
pp_new_passwd ,
& new_pw_len ,
2009-03-16 13:27:58 +03:00
nt_pass_set ? CH_UTF16 : CH_DOS ) ) {
2003-01-04 11:45:17 +03:00
return NT_STATUS_WRONG_PASSWORD ;
1998-10-19 21:32:10 +04:00
}
/*
* To ensure we got the correct new password , hash it and
* use it as a key to test the passed old password .
*/
2004-01-26 12:03:09 +03:00
if ( nt_pass_set ) {
/* NT passwords, verify the NT hash. */
2007-11-13 02:02:50 +03:00
2004-01-26 12:03:09 +03:00
/* Calculate the MD4 hash (NT compatible) of the password */
memset ( new_nt_hash , ' \0 ' , 16 ) ;
2007-11-13 02:02:50 +03:00
E_md4hash ( * pp_new_passwd , new_nt_hash ) ;
2004-01-26 12:03:09 +03:00
if ( nt_pw ) {
/*
2004-11-25 07:10:19 +03:00
* check the NT verifier
2004-01-26 12:03:09 +03:00
*/
2004-11-25 07:10:19 +03:00
E_old_pw_hash ( new_nt_hash , nt_pw , verifier ) ;
if ( memcmp ( verifier , old_nt_hash_encrypted , 16 ) ) {
2010-04-23 09:06:03 +04:00
DEBUG ( 0 , ( " check_oem_password: old nt "
" password doesn't match. \n " ) ) ;
2004-01-26 12:03:09 +03:00
return NT_STATUS_WRONG_PASSWORD ;
}
2007-11-13 02:02:50 +03:00
2004-01-26 12:03:09 +03:00
/* We could check the LM password here, but there is
* little point , we already know the password is
* correct , and the LM password might not even be
* present . */
/* Further, LM hash generation algorithms
* differ with charset , so we could
* incorrectly fail a perfectly valid password
* change */
# ifdef DEBUG_PASSWORD
DEBUG ( 100 ,
2007-11-13 02:02:50 +03:00
( " check_oem_password: password %s ok \n " , * pp_new_passwd ) ) ;
2004-01-26 12:03:09 +03:00
# endif
return NT_STATUS_OK ;
}
2007-11-13 02:02:50 +03:00
2004-01-26 12:03:09 +03:00
if ( lanman_pw ) {
/*
2004-11-25 07:10:19 +03:00
* check the lm verifier
2004-01-26 12:03:09 +03:00
*/
2004-11-25 07:10:19 +03:00
E_old_pw_hash ( new_nt_hash , lanman_pw , verifier ) ;
if ( memcmp ( verifier , old_lm_hash_encrypted , 16 ) ) {
2004-01-26 12:03:09 +03:00
DEBUG ( 0 , ( " check_oem_password: old lm password doesn't match. \n " ) ) ;
return NT_STATUS_WRONG_PASSWORD ;
}
# ifdef DEBUG_PASSWORD
DEBUG ( 100 ,
2007-11-13 02:02:50 +03:00
( " check_oem_password: password %s ok \n " , * pp_new_passwd ) ) ;
2004-01-26 12:03:09 +03:00
# endif
return NT_STATUS_OK ;
}
}
if ( lanman_pw & & lm_pass_set ) {
2007-11-13 02:02:50 +03:00
E_deshash ( * pp_new_passwd , new_lm_hash ) ;
1998-10-19 21:32:10 +04:00
/*
2004-11-25 07:10:19 +03:00
* check the lm verifier
1998-10-19 21:32:10 +04:00
*/
2004-11-25 07:10:19 +03:00
E_old_pw_hash ( new_lm_hash , lanman_pw , verifier ) ;
if ( memcmp ( verifier , old_lm_hash_encrypted , 16 ) ) {
2000-11-14 02:03:34 +03:00
DEBUG ( 0 , ( " check_oem_password: old lm password doesn't match. \n " ) ) ;
2003-01-04 11:45:17 +03:00
return NT_STATUS_WRONG_PASSWORD ;
1998-10-19 21:32:10 +04:00
}
2007-11-13 02:02:50 +03:00
1998-10-19 21:32:10 +04:00
# ifdef DEBUG_PASSWORD
2000-03-22 00:08:07 +03:00
DEBUG ( 100 ,
2007-11-13 02:02:50 +03:00
( " check_oem_password: password %s ok \n " , * pp_new_passwd ) ) ;
1998-10-19 21:32:10 +04:00
# endif
2003-01-04 11:45:17 +03:00
return NT_STATUS_OK ;
1998-10-19 21:32:10 +04:00
}
2004-01-26 12:03:09 +03:00
/* should not be reached */
return NT_STATUS_WRONG_PASSWORD ;
1998-03-18 22:07:53 +03:00
}
2010-05-07 21:26:06 +04:00
static bool password_in_history ( uint8_t nt_pw [ NT_HASH_LEN ] ,
uint32_t pw_history_len ,
const uint8_t * pw_history )
2009-12-14 22:54:33 +03:00
{
static const uint8_t zero_md5_nt_pw [ SALTED_MD5_HASH_LEN ] = { 0 , } ;
int i ;
dump_data ( 100 , nt_pw , NT_HASH_LEN ) ;
dump_data ( 100 , pw_history , PW_HISTORY_ENTRY_LEN * pw_history_len ) ;
for ( i = 0 ; i < pw_history_len ; i + + ) {
uint8_t new_nt_pw_salted_md5_hash [ SALTED_MD5_HASH_LEN ] ;
const uint8_t * current_salt ;
const uint8_t * old_nt_pw_salted_md5_hash ;
current_salt = & pw_history [ i * PW_HISTORY_ENTRY_LEN ] ;
old_nt_pw_salted_md5_hash = current_salt + PW_HISTORY_SALT_LEN ;
if ( memcmp ( zero_md5_nt_pw , old_nt_pw_salted_md5_hash ,
SALTED_MD5_HASH_LEN ) = = 0 ) {
/* Ignore zero valued entries. */
continue ;
}
2010-01-05 18:58:30 +03:00
if ( memcmp ( zero_md5_nt_pw , current_salt ,
PW_HISTORY_SALT_LEN ) = = 0 )
{
/*
* New format : zero salt and then plain nt hash .
* Directly compare the hashes .
*/
if ( memcmp ( nt_pw , old_nt_pw_salted_md5_hash ,
SALTED_MD5_HASH_LEN ) = = 0 )
{
return true ;
}
} else {
/*
* Old format : md5sum of salted nt hash .
* Create salted version of new pw to compare .
*/
E_md5hash ( current_salt , nt_pw , new_nt_pw_salted_md5_hash ) ;
if ( memcmp ( new_nt_pw_salted_md5_hash ,
old_nt_pw_salted_md5_hash ,
SALTED_MD5_HASH_LEN ) = = 0 ) {
return true ;
}
2009-12-14 22:54:33 +03:00
}
}
return false ;
}
2004-07-08 02:46:51 +04:00
/***********************************************************
This routine takes the given password and checks it against
the password history . Returns True if this password has been
found in the history list .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
static bool check_passwd_history ( struct samu * sampass , const char * plaintext )
2004-07-08 02:46:51 +04:00
{
uchar new_nt_p16 [ NT_HASH_LEN ] ;
const uint8 * nt_pw ;
const uint8 * pwhistory ;
2005-03-19 21:21:39 +03:00
uint32 pwHisLen , curr_pwHisLen ;
2004-07-08 02:46:51 +04:00
2009-07-14 01:53:49 +04:00
pdb_get_account_policy ( PDB_POLICY_PASSWORD_HISTORY , & pwHisLen ) ;
2004-07-08 02:46:51 +04:00
if ( pwHisLen = = 0 ) {
return False ;
}
pwhistory = pdb_get_pw_history ( sampass , & curr_pwHisLen ) ;
if ( ! pwhistory | | curr_pwHisLen = = 0 ) {
return False ;
}
/* Only examine the minimum of the current history len and
the stored history len . Avoids race conditions . */
pwHisLen = MIN ( pwHisLen , curr_pwHisLen ) ;
nt_pw = pdb_get_nt_passwd ( sampass ) ;
E_md4hash ( plaintext , new_nt_p16 ) ;
if ( ! memcmp ( nt_pw , new_nt_p16 , NT_HASH_LEN ) ) {
DEBUG ( 10 , ( " check_passwd_history: proposed new password for user %s is the same as the current password ! \n " ,
pdb_get_username ( sampass ) ) ) ;
return True ;
}
2009-12-14 22:54:33 +03:00
if ( password_in_history ( new_nt_p16 , pwHisLen , pwhistory ) ) {
DEBUG ( 1 , ( " check_passwd_history: proposed new password for "
" user %s found in history list ! \n " ,
pdb_get_username ( sampass ) ) ) ;
return true ;
2004-07-08 02:46:51 +04:00
}
2009-12-14 22:54:33 +03:00
return false ;
2004-07-08 02:46:51 +04:00
}
2009-11-10 14:48:52 +03:00
/***********************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
NTSTATUS check_password_complexity ( const char * username ,
const char * password ,
enum samPwdChangeReason * samr_reject_reason )
{
TALLOC_CTX * tosctx = talloc_tos ( ) ;
2010-04-16 17:00:54 +04:00
int check_ret ;
char * cmd ;
2009-11-10 14:48:52 +03:00
/* Use external script to check password complexity */
2012-07-18 09:37:23 +04:00
if ( ( lp_check_password_script ( tosctx ) = = NULL )
| | ( * ( lp_check_password_script ( tosctx ) ) = = ' \0 ' ) ) {
2010-04-16 17:00:54 +04:00
return NT_STATUS_OK ;
}
2009-11-10 14:48:52 +03:00
2012-07-18 09:37:23 +04:00
cmd = talloc_string_sub ( tosctx , lp_check_password_script ( tosctx ) , " %u " ,
2010-04-16 17:00:54 +04:00
username ) ;
if ( ! cmd ) {
return NT_STATUS_PASSWORD_RESTRICTION ;
}
2009-11-10 14:48:52 +03:00
2010-04-16 17:00:54 +04:00
check_ret = smbrunsecret ( cmd , password ) ;
DEBUG ( 5 , ( " check_password_complexity: check password script (%s) "
" returned [%d] \n " , cmd , check_ret ) ) ;
TALLOC_FREE ( cmd ) ;
if ( check_ret ! = 0 ) {
DEBUG ( 1 , ( " check_password_complexity: "
" check password script said new password is not good "
" enough! \n " ) ) ;
if ( samr_reject_reason ) {
* samr_reject_reason = SAM_PWD_CHANGE_NOT_COMPLEX ;
2009-11-10 14:48:52 +03:00
}
2010-04-16 17:00:54 +04:00
return NT_STATUS_PASSWORD_RESTRICTION ;
2009-11-10 14:48:52 +03:00
}
return NT_STATUS_OK ;
}
1998-03-18 22:07:53 +03:00
/***********************************************************
Code to change the oem password . Changes both the lanman
2003-01-04 11:45:17 +03:00
and NT hashes . Old_passwd is almost always NULL .
2003-07-31 05:33:44 +04:00
NOTE this function is designed to be called as root . Check the old password
is correct before calling . JRA .
1998-03-18 22:07:53 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2010-08-18 21:51:45 +04:00
static NTSTATUS change_oem_password ( struct samu * hnd , const char * rhost ,
char * old_passwd , char * new_passwd ,
bool as_root ,
enum samPwdChangeReason * samr_reject_reason )
1998-03-18 22:07:53 +03:00
{
2006-10-03 21:14:18 +04:00
uint32 min_len ;
2007-07-03 17:07:56 +04:00
uint32 refuse ;
2009-05-09 19:26:14 +04:00
TALLOC_CTX * tosctx = talloc_tos ( ) ;
2004-07-08 02:46:51 +04:00
struct passwd * pass = NULL ;
const char * username = pdb_get_username ( hnd ) ;
time_t can_change_time = pdb_get_pass_can_change_time ( hnd ) ;
2009-11-10 14:48:52 +03:00
NTSTATUS status ;
2003-01-04 11:45:17 +03:00
2006-02-11 02:41:41 +03:00
if ( samr_reject_reason ) {
2009-09-26 00:44:00 +04:00
* samr_reject_reason = SAM_PWD_CHANGE_NO_ERROR ;
2006-02-11 02:41:41 +03:00
}
2006-10-03 21:14:18 +04:00
/* check to see if the secdesc has previously been set to disallow */
if ( ! pdb_get_pass_can_change ( hnd ) ) {
2006-10-05 13:38:49 +04:00
DEBUG ( 1 , ( " user %s does not have permissions to change password \n " , username ) ) ;
2006-10-03 21:14:18 +04:00
if ( samr_reject_reason ) {
2009-09-26 00:44:00 +04:00
* samr_reject_reason = SAM_PWD_CHANGE_NO_ERROR ;
2006-02-02 19:38:37 +03:00
}
2006-10-03 21:14:18 +04:00
return NT_STATUS_ACCOUNT_RESTRICTION ;
}
2007-07-03 17:07:56 +04:00
/* check to see if it is a Machine account and if the policy
* denies machines to change the password . *
* Should we deny also SRVTRUST and / or DOMSTRUST ? . SSS . */
if ( pdb_get_acct_ctrl ( hnd ) & ACB_WSTRUST ) {
2009-07-14 01:53:49 +04:00
if ( pdb_get_account_policy ( PDB_POLICY_REFUSE_MACHINE_PW_CHANGE , & refuse ) & & refuse ) {
2007-07-03 17:07:56 +04:00
DEBUG ( 1 , ( " Machine %s cannot change password now, "
" denied by Refuse Machine Password Change policy \n " ,
username ) ) ;
if ( samr_reject_reason ) {
2009-09-26 00:44:00 +04:00
* samr_reject_reason = SAM_PWD_CHANGE_NO_ERROR ;
2007-07-03 17:07:56 +04:00
}
return NT_STATUS_ACCOUNT_RESTRICTION ;
}
}
2009-12-14 19:51:39 +03:00
/* removed calculation here, because passdb now calculates
2006-10-03 21:14:18 +04:00
based on policy . jmcd */
if ( ( can_change_time ! = 0 ) & & ( time ( NULL ) < can_change_time ) ) {
DEBUG ( 1 , ( " user %s cannot change password now, must "
" wait until %s \n " , username ,
2009-05-09 19:26:14 +04:00
http_timestring ( tosctx , can_change_time ) ) ) ;
2006-10-03 21:14:18 +04:00
if ( samr_reject_reason ) {
2009-09-26 00:44:00 +04:00
* samr_reject_reason = SAM_PWD_CHANGE_NO_ERROR ;
2006-02-02 19:38:37 +03:00
}
2006-10-03 21:14:18 +04:00
return NT_STATUS_ACCOUNT_RESTRICTION ;
2003-01-04 11:45:17 +03:00
}
2009-07-14 01:53:49 +04:00
if ( pdb_get_account_policy ( PDB_POLICY_MIN_PASSWORD_LEN , & min_len ) & & ( str_charnum ( new_passwd ) < min_len ) ) {
2010-05-18 14:06:23 +04:00
DEBUG ( 1 , ( " user %s cannot change password - password too short \n " ,
2004-07-08 02:46:51 +04:00
username ) ) ;
2003-01-04 11:45:17 +03:00
DEBUGADD ( 1 , ( " account policy min password len = %d \n " , min_len ) ) ;
2006-02-11 02:09:00 +03:00
if ( samr_reject_reason ) {
2009-09-26 00:44:00 +04:00
* samr_reject_reason = SAM_PWD_CHANGE_PASSWORD_TOO_SHORT ;
2006-02-11 02:09:00 +03:00
}
2003-01-04 11:45:17 +03:00
return NT_STATUS_PASSWORD_RESTRICTION ;
/* return NT_STATUS_PWD_TOO_SHORT; */
}
2004-07-08 02:46:51 +04:00
if ( check_passwd_history ( hnd , new_passwd ) ) {
2006-02-11 02:09:00 +03:00
if ( samr_reject_reason ) {
2009-09-26 00:44:00 +04:00
* samr_reject_reason = SAM_PWD_CHANGE_PWD_IN_HISTORY ;
2006-02-11 02:09:00 +03:00
}
2004-07-08 02:46:51 +04:00
return NT_STATUS_PASSWORD_RESTRICTION ;
}
2009-05-09 19:26:14 +04:00
pass = Get_Pwnam_alloc ( tosctx , username ) ;
2004-02-02 03:08:35 +03:00
if ( ! pass ) {
2006-02-13 20:08:25 +03:00
DEBUG ( 1 , ( " change_oem_password: Username %s does not exist in system !?! \n " , username ) ) ;
2004-07-08 02:46:51 +04:00
return NT_STATUS_ACCESS_DENIED ;
2004-02-02 03:08:35 +03:00
}
2009-11-10 14:48:52 +03:00
status = check_password_complexity ( username , new_passwd , samr_reject_reason ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
TALLOC_FREE ( pass ) ;
return status ;
2004-09-14 04:21:11 +04:00
}
2003-01-04 11:45:17 +03:00
/*
* If unix password sync was requested , attempt to change
* the / etc / passwd database first . Return failure if this cannot
* be done .
*
2003-05-14 04:46:43 +04:00
* This occurs before the oem change , because we don ' t want to
2003-01-04 11:45:17 +03:00
* update it if chgpasswd failed .
*
2003-05-14 04:46:43 +04:00
* Conditional on lp_unix_password_sync ( ) because we don ' t want
2003-01-04 11:45:17 +03:00
* to touch the unix db unless we have admin permission .
*/
2010-04-16 16:16:40 +04:00
2003-05-12 22:12:31 +04:00
if ( lp_unix_password_sync ( ) & &
2010-08-18 21:51:45 +04:00
! chgpasswd ( username , rhost , pass , old_passwd , new_passwd ,
as_root ) ) {
2007-12-19 17:02:59 +03:00
TALLOC_FREE ( pass ) ;
2003-01-04 11:45:17 +03:00
return NT_STATUS_ACCESS_DENIED ;
}
1998-03-18 22:07:53 +03:00
2007-12-19 17:02:59 +03:00
TALLOC_FREE ( pass ) ;
2001-09-29 17:08:26 +04:00
if ( ! pdb_set_plaintext_passwd ( hnd , new_passwd ) ) {
2003-01-04 11:45:17 +03:00
return NT_STATUS_ACCESS_DENIED ;
2001-09-29 17:08:26 +04:00
}
1998-03-18 22:07:53 +03:00
2000-03-22 00:08:07 +03:00
/* Now write it into the file. */
2006-02-04 01:19:41 +03:00
return pdb_update_sam_account ( hnd ) ;
1998-03-18 22:07:53 +03:00
}
2010-05-07 21:26:06 +04:00
/***********************************************************
Code to check and change the OEM hashed password .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2010-08-18 21:51:45 +04:00
NTSTATUS pass_oem_change ( char * user , const char * rhost ,
2010-05-07 21:26:06 +04:00
uchar password_encrypted_with_lm_hash [ 516 ] ,
const uchar old_lm_hash_encrypted [ 16 ] ,
uchar password_encrypted_with_nt_hash [ 516 ] ,
const uchar old_nt_hash_encrypted [ 16 ] ,
enum samPwdChangeReason * reject_reason )
{
char * new_passwd = NULL ;
struct samu * sampass = NULL ;
NTSTATUS nt_status ;
bool ret = false ;
if ( ! ( sampass = samu_new ( NULL ) ) ) {
return NT_STATUS_NO_MEMORY ;
}
become_root ( ) ;
ret = pdb_getsampwnam ( sampass , user ) ;
unbecome_root ( ) ;
if ( ret = = false ) {
DEBUG ( 0 , ( " pass_oem_change: getsmbpwnam returned NULL \n " ) ) ;
TALLOC_FREE ( sampass ) ;
return NT_STATUS_NO_SUCH_USER ;
}
nt_status = check_oem_password ( user ,
password_encrypted_with_lm_hash ,
old_lm_hash_encrypted ,
password_encrypted_with_nt_hash ,
old_nt_hash_encrypted ,
sampass ,
& new_passwd ) ;
if ( ! NT_STATUS_IS_OK ( nt_status ) ) {
TALLOC_FREE ( sampass ) ;
return nt_status ;
}
/* We've already checked the old password here.... */
become_root ( ) ;
2010-08-18 21:51:45 +04:00
nt_status = change_oem_password ( sampass , rhost , NULL , new_passwd ,
True , reject_reason ) ;
2010-05-07 21:26:06 +04:00
unbecome_root ( ) ;
memset ( new_passwd , 0 , strlen ( new_passwd ) ) ;
TALLOC_FREE ( sampass ) ;
return nt_status ;
}