1996-05-04 11:50:46 +04:00
/*
2002-01-30 09:08:46 +03:00
* Unix SMB / CIFS implementation .
2001-06-29 12:04:12 +04:00
* Copyright ( C ) Jeremy Allison 1995 - 1998
* Copyright ( C ) Tim Potter 2001
1996-05-04 11:50:46 +04:00
*
2001-06-29 12:04:12 +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 the
2007-07-09 23:25:36 +04:00
* Free Software Foundation ; either version 3 of the License , or ( at your
2001-06-29 12:04:12 +04:00
* option ) any later version .
1996-05-04 11:50:46 +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 .
*
* You should have received a copy of the GNU General Public License along with
2007-07-10 09:23:25 +04:00
* this program ; if not , see < http : //www.gnu.org/licenses/>. */
1996-05-04 11:50:46 +04:00
# include "includes.h"
2007-10-19 04:40:25 +04:00
extern bool AllowDebugChange ;
1998-04-30 02:27:26 +04:00
1998-11-19 01:04:24 +03:00
/*
* Next two lines needed for SunOS and don ' t
* hurt anything else . . .
*/
extern char * optarg ;
extern int optind ;
1998-04-25 05:12:08 +04:00
2002-04-02 10:18:13 +04:00
/* forced running in root-mode */
2007-10-19 04:40:25 +04:00
static bool got_username = False ;
static bool stdin_passwd_get = False ;
2004-07-15 04:03:14 +04:00
static fstring user_name ;
2002-04-02 10:18:13 +04:00
static char * new_passwd = NULL ;
2003-01-03 11:28:12 +03:00
static const char * remote_machine = NULL ;
2001-06-25 05:15:17 +04:00
2002-04-02 10:18:13 +04:00
static fstring ldap_secret ;
2004-02-02 03:08:35 +03:00
2002-04-02 10:18:13 +04:00
/*********************************************************
Print command usage on stderr and die .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-11-12 09:12:19 +03:00
static void usage ( void )
1996-05-04 11:50:46 +04:00
{
2001-11-19 11:12:06 +03:00
printf ( " When run by root: \n " ) ;
2004-07-15 04:03:14 +04:00
printf ( " smbpasswd [options] [username] \n " ) ;
2001-11-19 11:12:06 +03:00
printf ( " otherwise: \n " ) ;
2004-07-15 04:03:14 +04:00
printf ( " smbpasswd [options] \n \n " ) ;
2001-11-19 11:12:06 +03:00
1998-11-12 09:12:19 +03:00
printf ( " options: \n " ) ;
2002-04-03 20:44:15 +04:00
printf ( " -L local mode (must be first option) \n " ) ;
printf ( " -h print this usage message \n " ) ;
1998-11-12 09:12:19 +03:00
printf ( " -s use stdin for password prompt \n " ) ;
2002-04-03 20:44:15 +04:00
printf ( " -c smb.conf file Use the given path to the smb.conf file \n " ) ;
1998-11-12 09:12:19 +03:00
printf ( " -D LEVEL debug level \n " ) ;
printf ( " -r MACHINE remote machine \n " ) ;
2002-04-03 20:44:15 +04:00
printf ( " -U USER remote username \n " ) ;
1998-11-12 09:12:19 +03:00
2001-11-19 11:12:06 +03:00
printf ( " extra options when run by root or in local mode: \n " ) ;
printf ( " -a add user \n " ) ;
printf ( " -d disable user \n " ) ;
printf ( " -e enable user \n " ) ;
2001-12-14 17:35:38 +03:00
printf ( " -i interdomain trust account \n " ) ;
2002-04-03 20:44:15 +04:00
printf ( " -m machine trust account \n " ) ;
printf ( " -n set no password \n " ) ;
2006-05-27 03:06:56 +04:00
printf ( " -W use stdin ldap admin password \n " ) ;
2003-09-15 18:13:35 +04:00
printf ( " -w PASSWORD ldap admin password \n " ) ;
2002-04-03 20:44:15 +04:00
printf ( " -x delete user \n " ) ;
printf ( " -R ORDER name resolve order \n " ) ;
2001-11-19 11:12:06 +03:00
1996-05-04 11:50:46 +04:00
exit ( 1 ) ;
}
1998-11-12 09:12:19 +03:00
static void set_line_buffering ( FILE * f )
1998-04-30 02:27:26 +04:00
{
1998-11-12 09:12:19 +03:00
setvbuf ( f , NULL , _IOLBF , 0 ) ;
1998-04-30 02:27:26 +04:00
}
2002-04-02 10:18:13 +04:00
/*******************************************************************
Process command line options
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-07-15 04:03:14 +04:00
2002-07-15 14:35:28 +04:00
static int process_options ( int argc , char * * argv , int local_flags )
2002-04-02 10:18:13 +04:00
{
int ch ;
2007-12-10 22:30:37 +03:00
const char * configfile = get_dyn_CONFIGFILE ( ) ;
2002-04-02 10:18:13 +04:00
2002-07-15 14:35:28 +04:00
local_flags | = LOCAL_SET_PASSWORD ;
2002-04-02 10:18:13 +04:00
ZERO_STRUCT ( user_name ) ;
user_name [ 0 ] = ' \0 ' ;
2006-01-11 15:09:30 +03:00
while ( ( ch = getopt ( argc , argv , " c:axdehminjr:sw:R:D:U:LW " ) ) ! = EOF ) {
2002-04-02 10:18:13 +04:00
switch ( ch ) {
case ' L ' :
2007-12-13 05:38:52 +03:00
# if !defined(NSS_WRAPPER)
2007-12-13 04:26:49 +03:00
if ( getuid ( ) ! = 0 ) {
fprintf ( stderr , " smbpasswd -L can only be used by root. \n " ) ;
exit ( 1 ) ;
}
2007-12-13 04:56:28 +03:00
# endif
2002-07-15 14:35:28 +04:00
local_flags | = LOCAL_AM_ROOT ;
2002-04-02 10:18:13 +04:00
break ;
case ' c ' :
2007-12-04 05:48:41 +03:00
configfile = optarg ;
2002-04-02 10:18:13 +04:00
break ;
case ' a ' :
local_flags | = LOCAL_ADD_USER ;
break ;
case ' x ' :
local_flags | = LOCAL_DELETE_USER ;
local_flags & = ~ LOCAL_SET_PASSWORD ;
break ;
case ' d ' :
local_flags | = LOCAL_DISABLE_USER ;
local_flags & = ~ LOCAL_SET_PASSWORD ;
break ;
case ' e ' :
local_flags | = LOCAL_ENABLE_USER ;
local_flags & = ~ LOCAL_SET_PASSWORD ;
break ;
case ' m ' :
local_flags | = LOCAL_TRUST_ACCOUNT ;
break ;
case ' i ' :
local_flags | = LOCAL_INTERDOM_ACCOUNT ;
break ;
case ' j ' :
2002-11-09 02:08:59 +03:00
d_printf ( " See 'net join' for this functionality \n " ) ;
2002-04-02 10:18:13 +04:00
exit ( 1 ) ;
break ;
case ' n ' :
local_flags | = LOCAL_SET_NO_PASSWORD ;
2002-07-15 14:35:28 +04:00
local_flags & = ~ LOCAL_SET_PASSWORD ;
2002-04-02 10:18:13 +04:00
new_passwd = smb_xstrdup ( " NO PASSWORD " ) ;
break ;
case ' r ' :
remote_machine = optarg ;
break ;
case ' s ' :
set_line_buffering ( stdin ) ;
set_line_buffering ( stdout ) ;
set_line_buffering ( stderr ) ;
stdin_passwd_get = True ;
break ;
case ' w ' :
local_flags | = LOCAL_SET_LDAP_ADMIN_PW ;
fstrcpy ( ldap_secret , optarg ) ;
break ;
case ' R ' :
lp_set_name_resolve_order ( optarg ) ;
break ;
case ' D ' :
DEBUGLEVEL = atoi ( optarg ) ;
break ;
case ' U ' : {
got_username = True ;
fstrcpy ( user_name , optarg ) ;
break ;
2006-01-11 15:09:30 +03:00
case ' W ' :
local_flags | = LOCAL_SET_LDAP_ADMIN_PW ;
* ldap_secret = ' \0 ' ;
break ;
2002-04-02 10:18:13 +04:00
}
case ' h ' :
default :
usage ( ) ;
}
}
2007-12-04 05:48:41 +03:00
2002-04-02 10:18:13 +04:00
argc - = optind ;
argv + = optind ;
switch ( argc ) {
case 0 :
if ( ! got_username )
fstrcpy ( user_name , " " ) ;
break ;
case 1 :
2002-07-15 14:35:28 +04:00
if ( ! ( local_flags & LOCAL_AM_ROOT ) ) {
2004-07-15 04:03:14 +04:00
usage ( ) ;
2002-07-15 14:35:28 +04:00
} else {
if ( got_username ) {
usage ( ) ;
} else {
fstrcpy ( user_name , argv [ 0 ] ) ;
}
2002-04-02 10:18:13 +04:00
}
break ;
default :
usage ( ) ;
}
2006-01-29 01:53:04 +03:00
if ( ! lp_load ( configfile , True , False , False , True ) ) {
2002-07-15 14:35:28 +04:00
fprintf ( stderr , " Can't load %s - run testparm to debug it \n " ,
2007-04-04 10:03:54 +04:00
configfile ) ;
2002-07-15 14:35:28 +04:00
exit ( 1 ) ;
}
return local_flags ;
2002-04-02 10:18:13 +04:00
}
1998-09-25 02:33:13 +04:00
/*************************************************************
Utility function to prompt for new password .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
static char * prompt_for_new_password ( bool stdin_get )
1998-09-25 02:33:13 +04:00
{
1998-11-12 09:12:19 +03:00
char * p ;
2002-09-25 19:19:00 +04:00
fstring new_pw ;
1998-09-25 02:33:13 +04:00
2002-09-25 19:19:00 +04:00
ZERO_ARRAY ( new_pw ) ;
1998-11-04 03:57:00 +03:00
1998-11-12 09:12:19 +03:00
p = get_pass ( " New SMB password: " , stdin_get ) ;
1998-09-25 02:33:13 +04:00
2002-09-25 19:19:00 +04:00
fstrcpy ( new_pw , p ) ;
2001-09-17 15:48:29 +04:00
SAFE_FREE ( p ) ;
1998-09-25 02:33:13 +04:00
1998-11-12 09:12:19 +03:00
p = get_pass ( " Retype new SMB password: " , stdin_get ) ;
1998-09-25 02:33:13 +04:00
2002-09-25 19:19:00 +04:00
if ( strcmp ( p , new_pw ) ) {
1998-11-12 09:12:19 +03:00
fprintf ( stderr , " Mismatch - password unchanged. \n " ) ;
2002-09-25 19:19:00 +04:00
ZERO_ARRAY ( new_pw ) ;
2001-09-17 15:48:29 +04:00
SAFE_FREE ( p ) ;
1998-11-12 09:12:19 +03:00
return NULL ;
}
1998-09-25 02:33:13 +04:00
2000-07-26 07:55:17 +04:00
return p ;
1998-09-25 02:33:13 +04:00
}
1998-03-25 00:04:36 +03:00
1998-11-12 09:12:19 +03:00
/*************************************************************
2000-02-26 01:25:25 +03:00
Change a password either locally or remotely .
1998-11-12 09:12:19 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-02-26 01:25:25 +03:00
2006-02-04 01:19:41 +03:00
static NTSTATUS password_change ( const char * remote_mach , char * username ,
char * old_passwd , char * new_pw ,
int local_flags )
1998-11-12 09:12:19 +03:00
{
2006-02-04 01:19:41 +03:00
NTSTATUS ret ;
2007-11-22 04:42:52 +03:00
char * err_str = NULL ;
char * msg_str = NULL ;
1998-11-13 02:49:32 +03:00
2002-09-25 19:19:00 +04:00
if ( remote_mach ! = NULL ) {
2000-02-26 01:25:25 +03:00
if ( local_flags & ( LOCAL_ADD_USER | LOCAL_DELETE_USER | LOCAL_DISABLE_USER | LOCAL_ENABLE_USER |
2001-03-11 03:32:10 +03:00
LOCAL_TRUST_ACCOUNT | LOCAL_SET_NO_PASSWORD ) ) {
1998-11-12 09:12:19 +03:00
/* these things can't be done remotely yet */
2006-02-04 01:19:41 +03:00
return NT_STATUS_UNSUCCESSFUL ;
1998-11-12 09:12:19 +03:00
}
2002-09-25 19:19:00 +04:00
ret = remote_password_change ( remote_mach , username ,
2007-11-23 14:04:35 +03:00
old_passwd , new_pw , & err_str ) ;
if ( err_str ! = NULL )
2005-04-09 02:58:07 +04:00
fprintf ( stderr , " %s " , err_str ) ;
2007-11-23 14:04:35 +03:00
SAFE_FREE ( err_str ) ;
1998-11-13 02:49:32 +03:00
return ret ;
1998-11-12 09:12:19 +03:00
}
2002-09-25 19:19:00 +04:00
ret = local_password_change ( username , local_flags , new_pw ,
2007-11-22 04:42:52 +03:00
& err_str , & msg_str ) ;
1998-11-13 02:49:32 +03:00
2007-11-22 04:42:52 +03:00
if ( msg_str )
2005-04-09 04:50:12 +04:00
printf ( " %s " , msg_str ) ;
2007-11-22 04:42:52 +03:00
if ( err_str )
2005-04-09 02:58:07 +04:00
fprintf ( stderr , " %s " , err_str ) ;
1998-11-13 02:49:32 +03:00
2007-11-22 04:42:52 +03:00
SAFE_FREE ( msg_str ) ;
SAFE_FREE ( err_str ) ;
1998-11-13 02:49:32 +03:00
return ret ;
1998-11-12 09:12:19 +03:00
}
2001-12-13 21:09:29 +03:00
/*******************************************************************
Store the LDAP admin password in secrets . tdb
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
static bool store_ldap_admin_pw ( char * pw )
2001-12-13 21:09:29 +03:00
{
if ( ! pw )
return False ;
if ( ! secrets_init ( ) )
return False ;
return secrets_store_ldap_pw ( lp_ldap_admin_dn ( ) , pw ) ;
}
1998-11-12 09:12:19 +03:00
2002-04-02 10:18:13 +04:00
1998-11-12 09:12:19 +03:00
/*************************************************************
2000-02-26 01:25:25 +03:00
Handle password changing for root .
1998-11-12 09:12:19 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-02-26 01:25:25 +03:00
2002-07-15 14:35:28 +04:00
static int process_root ( int local_flags )
1998-11-12 09:12:19 +03:00
{
struct passwd * pwd ;
2002-04-02 10:18:13 +04:00
int result = 0 ;
2002-07-15 14:35:28 +04:00
char * old_passwd = NULL ;
1998-11-12 09:12:19 +03:00
2004-02-23 23:12:31 +03:00
if ( local_flags & LOCAL_SET_LDAP_ADMIN_PW ) {
2006-01-26 03:14:25 +03:00
char * ldap_admin_dn = lp_ldap_admin_dn ( ) ;
if ( ! * ldap_admin_dn ) {
DEBUG ( 0 , ( " ERROR: 'ldap admin dn' not defined! Please check your smb.conf \n " ) ) ;
goto done ;
}
printf ( " Setting stored password for \" %s \" in secrets.tdb \n " , ldap_admin_dn ) ;
2006-01-11 15:09:30 +03:00
if ( ! * ldap_secret ) {
new_passwd = prompt_for_new_password ( stdin_passwd_get ) ;
fstrcpy ( ldap_secret , new_passwd ) ;
}
2006-01-26 03:14:25 +03:00
if ( ! store_ldap_admin_pw ( ldap_secret ) ) {
2001-12-13 21:09:29 +03:00
DEBUG ( 0 , ( " ERROR: Failed to store the ldap admin password! \n " ) ) ;
2006-01-26 03:14:25 +03:00
}
2001-12-13 21:09:29 +03:00
goto done ;
}
2002-04-02 10:18:13 +04:00
2004-02-26 01:01:02 +03:00
/* Ensure passdb startup(). */
2007-03-11 19:49:16 +03:00
if ( ! initialize_password_db ( False , NULL ) ) {
2004-02-26 01:01:02 +03:00
DEBUG ( 0 , ( " Failed to open passdb! \n " ) ) ;
exit ( 1 ) ;
}
2004-02-23 23:12:31 +03:00
/* Ensure we have a SAM sid. */
get_global_sam_sid ( ) ;
1998-11-12 09:12:19 +03:00
/*
2001-04-24 20:44:28 +04:00
* Ensure both add / delete user are not set
2000-02-26 01:25:25 +03:00
* Ensure add / delete user and either remote machine or join domain are
1998-11-12 09:12:19 +03:00
* not both set .
*/
2001-04-24 22:53:15 +04:00
if ( ( ( local_flags & ( LOCAL_ADD_USER | LOCAL_DELETE_USER ) ) = = ( LOCAL_ADD_USER | LOCAL_DELETE_USER ) ) | |
2001-04-24 20:44:28 +04:00
( ( local_flags & ( LOCAL_ADD_USER | LOCAL_DELETE_USER ) ) & &
2001-12-05 14:00:26 +03:00
( remote_machine ! = NULL ) ) ) {
1998-11-12 09:12:19 +03:00
usage ( ) ;
}
2001-01-12 02:21:17 +03:00
/* Only load interfaces if we are doing network operations. */
2001-12-05 14:00:26 +03:00
if ( remote_machine ) {
2001-01-12 02:21:17 +03:00
load_interfaces ( ) ;
}
2006-02-04 01:19:41 +03:00
if ( ! user_name [ 0 ] & & ( pwd = getpwuid_alloc ( NULL , geteuid ( ) ) ) ) {
2001-06-29 12:04:12 +04:00
fstrcpy ( user_name , pwd - > pw_name ) ;
2006-02-20 20:59:58 +03:00
TALLOC_FREE ( pwd ) ;
1998-11-12 09:12:19 +03:00
}
2001-06-29 12:04:12 +04:00
if ( ! user_name [ 0 ] ) {
1998-11-12 09:12:19 +03:00
fprintf ( stderr , " You must specify a username \n " ) ;
exit ( 1 ) ;
}
2000-02-26 01:25:25 +03:00
if ( local_flags & LOCAL_TRUST_ACCOUNT ) {
1998-11-12 09:12:19 +03:00
/* add the $ automatically */
static fstring buf ;
1998-11-16 23:19:57 +03:00
/*
* Remove any trailing ' $ ' before we
* generate the initial machine password .
*/
if ( user_name [ strlen ( user_name ) - 1 ] = = ' $ ' ) {
user_name [ strlen ( user_name ) - 1 ] = 0 ;
}
2000-02-26 01:25:25 +03:00
if ( local_flags & LOCAL_ADD_USER ) {
2001-09-17 15:48:29 +04:00
SAFE_FREE ( new_passwd ) ;
2001-11-20 09:38:09 +03:00
new_passwd = smb_xstrdup ( user_name ) ;
2003-07-03 23:11:31 +04:00
strlower_m ( new_passwd ) ;
1998-11-12 09:12:19 +03:00
}
1998-11-16 23:19:57 +03:00
/*
* Now ensure the username ends in ' $ ' for
* the machine add .
*/
1998-11-12 09:12:19 +03:00
slprintf ( buf , sizeof ( buf ) - 1 , " %s$ " , user_name ) ;
2001-06-29 12:04:12 +04:00
fstrcpy ( user_name , buf ) ;
2001-12-14 17:35:38 +03:00
} else if ( local_flags & LOCAL_INTERDOM_ACCOUNT ) {
static fstring buf ;
2003-07-15 20:07:50 +04:00
if ( ( local_flags & LOCAL_ADD_USER ) & & ( new_passwd = = NULL ) ) {
2001-12-14 17:35:38 +03:00
/*
* Prompt for trusting domain ' s account password
*/
new_passwd = prompt_for_new_password ( stdin_passwd_get ) ;
if ( ! new_passwd ) {
fprintf ( stderr , " Unable to get newpassword. \n " ) ;
exit ( 1 ) ;
}
}
2002-08-17 18:45:04 +04:00
/* prepare uppercased and '$' terminated username */
2001-12-14 17:35:38 +03:00
slprintf ( buf , sizeof ( buf ) - 1 , " %s$ " , user_name ) ;
fstrcpy ( user_name , buf ) ;
2002-08-17 18:45:04 +04:00
2001-12-05 14:00:26 +03:00
} else {
if ( remote_machine ! = NULL ) {
old_passwd = get_pass ( " Old SMB password: " , stdin_passwd_get ) ;
}
if ( ! ( local_flags & LOCAL_SET_PASSWORD ) ) {
/*
* If we are trying to enable a user , first we need to find out
* if they are using a modern version of the smbpasswd file that
* disables a user by just writing a flag into the file . If so
* then we can re - enable a user without prompting for a new
* password . If not ( ie . they have a no stored password in the
* smbpasswd file ) then we need to prompt for a new password .
*/
2001-05-04 19:44:27 +04:00
2001-12-05 14:00:26 +03:00
if ( local_flags & LOCAL_ENABLE_USER ) {
2006-02-20 23:09:36 +03:00
struct samu * sampass = NULL ;
2001-12-05 14:00:26 +03:00
2006-02-21 17:34:11 +03:00
sampass = samu_new ( NULL ) ;
2006-06-15 05:54:09 +04:00
if ( ! sampass ) {
fprintf ( stderr , " talloc fail for struct samu. \n " ) ;
exit ( 1 ) ;
}
if ( ! pdb_getsampwnam ( sampass , user_name ) ) {
fprintf ( stderr , " Failed to find user %s in passdb backend. \n " ,
user_name ) ;
exit ( 1 ) ;
}
if ( pdb_get_nt_passwd ( sampass ) = = NULL ) {
2001-12-05 14:00:26 +03:00
local_flags | = LOCAL_SET_PASSWORD ;
}
2006-02-20 23:09:36 +03:00
TALLOC_FREE ( sampass ) ;
1998-11-12 23:22:11 +03:00
}
}
2001-10-30 08:21:16 +03:00
2003-07-15 20:07:50 +04:00
if ( ( local_flags & LOCAL_SET_PASSWORD ) & & ( new_passwd = = NULL ) ) {
2001-12-05 14:00:26 +03:00
new_passwd = prompt_for_new_password ( stdin_passwd_get ) ;
if ( ! new_passwd ) {
fprintf ( stderr , " Unable to get new password. \n " ) ;
exit ( 1 ) ;
}
1999-12-13 16:27:58 +03:00
}
1999-03-19 08:00:39 +03:00
}
2001-12-05 14:00:26 +03:00
2006-02-04 01:19:41 +03:00
if ( ! NT_STATUS_IS_OK ( password_change ( remote_machine , user_name ,
old_passwd , new_passwd ,
local_flags ) ) ) {
2000-02-26 01:25:25 +03:00
fprintf ( stderr , " Failed to modify password entry for user %s \n " , user_name ) ;
2000-07-26 07:55:17 +04:00
result = 1 ;
goto done ;
1998-11-12 09:12:19 +03:00
}
2002-03-27 22:20:48 +03:00
if ( remote_machine ) {
printf ( " Password changed for user %s on %s. \n " , user_name , remote_machine ) ;
} else if ( ! ( local_flags & ( LOCAL_ADD_USER | LOCAL_DISABLE_USER | LOCAL_ENABLE_USER | LOCAL_DELETE_USER | LOCAL_SET_NO_PASSWORD | LOCAL_SET_PASSWORD ) ) ) {
2006-02-20 23:09:36 +03:00
struct samu * sampass = NULL ;
2001-05-04 19:44:27 +04:00
2006-02-21 17:34:11 +03:00
sampass = samu_new ( NULL ) ;
2006-06-15 14:06:09 +04:00
if ( ! sampass ) {
2006-06-15 05:54:09 +04:00
fprintf ( stderr , " talloc fail for struct samu. \n " ) ;
exit ( 1 ) ;
}
if ( ! pdb_getsampwnam ( sampass , user_name ) ) {
fprintf ( stderr , " Failed to find user %s in passdb backend. \n " ,
user_name ) ;
exit ( 1 ) ;
}
2001-05-04 19:44:27 +04:00
1999-12-13 16:27:58 +03:00
printf ( " Password changed for user %s. " , user_name ) ;
2006-06-15 05:54:09 +04:00
if ( pdb_get_acct_ctrl ( sampass ) & ACB_DISABLED ) {
1999-12-13 16:27:58 +03:00
printf ( " User has disabled flag set. " ) ;
2006-06-15 05:54:09 +04:00
}
if ( pdb_get_acct_ctrl ( sampass ) & ACB_PWNOTREQ ) {
1999-12-13 16:27:58 +03:00
printf ( " User has no password flag set. " ) ;
2006-06-15 05:54:09 +04:00
}
1999-12-13 16:27:58 +03:00
printf ( " \n " ) ;
2006-02-20 23:09:36 +03:00
TALLOC_FREE ( sampass ) ;
1998-11-12 09:12:19 +03:00
}
2000-07-26 07:55:17 +04:00
done :
2001-09-17 15:48:29 +04:00
SAFE_FREE ( new_passwd ) ;
2000-07-26 07:55:17 +04:00
return result ;
1998-11-12 09:12:19 +03:00
}
2002-04-02 10:18:13 +04:00
/*************************************************************
Handle password changing for non - root .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-07-15 14:35:28 +04:00
static int process_nonroot ( int local_flags )
1998-11-12 09:12:19 +03:00
{
struct passwd * pwd = NULL ;
2002-04-02 10:18:13 +04:00
int result = 0 ;
2002-12-12 23:50:31 +03:00
char * old_pw = NULL ;
char * new_pw = NULL ;
2002-07-15 14:35:28 +04:00
if ( local_flags & ~ ( LOCAL_AM_ROOT | LOCAL_SET_PASSWORD ) ) {
/* Extra flags that we can't honor non-root */
usage ( ) ;
}
1998-11-12 09:12:19 +03:00
2002-04-02 10:18:13 +04:00
if ( ! user_name [ 0 ] ) {
2006-02-04 01:19:41 +03:00
pwd = getpwuid_alloc ( NULL , getuid ( ) ) ;
1998-11-12 09:12:19 +03:00
if ( pwd ) {
2002-04-02 10:18:13 +04:00
fstrcpy ( user_name , pwd - > pw_name ) ;
2006-02-20 20:59:58 +03:00
TALLOC_FREE ( pwd ) ;
1998-11-12 09:12:19 +03:00
} else {
2005-04-09 02:58:07 +04:00
fprintf ( stderr , " smbpasswd: cannot lookup user name for uid %u \n " , ( unsigned int ) getuid ( ) ) ;
1998-11-12 09:12:19 +03:00
exit ( 1 ) ;
}
}
/*
* A non - root user is always setting a password
* via a remote machine ( even if that machine is
* localhost ) .
*/
2001-01-12 02:21:17 +03:00
load_interfaces ( ) ; /* Delayed from main() */
1998-11-12 09:12:19 +03:00
if ( remote_machine = = NULL ) {
remote_machine = " 127.0.0.1 " ;
}
if ( remote_machine ! = NULL ) {
2002-12-12 23:50:31 +03:00
old_pw = get_pass ( " Old SMB password: " , stdin_passwd_get ) ;
1998-11-12 09:12:19 +03:00
}
if ( ! new_passwd ) {
2002-12-12 23:50:31 +03:00
new_pw = prompt_for_new_password ( stdin_passwd_get ) ;
1998-11-12 09:12:19 +03:00
}
2002-12-12 23:50:31 +03:00
else
new_pw = smb_xstrdup ( new_passwd ) ;
1998-11-12 09:12:19 +03:00
2002-12-12 23:50:31 +03:00
if ( ! new_pw ) {
1999-12-13 16:27:58 +03:00
fprintf ( stderr , " Unable to get new password. \n " ) ;
exit ( 1 ) ;
1998-11-12 09:12:19 +03:00
}
2006-02-04 01:19:41 +03:00
if ( ! NT_STATUS_IS_OK ( password_change ( remote_machine , user_name , old_pw ,
new_pw , 0 ) ) ) {
1998-11-12 09:12:19 +03:00
fprintf ( stderr , " Failed to change password for %s \n " , user_name ) ;
2000-07-26 07:55:17 +04:00
result = 1 ;
goto done ;
1998-11-12 09:12:19 +03:00
}
printf ( " Password changed for user %s \n " , user_name ) ;
2000-07-26 07:55:17 +04:00
done :
2002-12-12 23:50:31 +03:00
SAFE_FREE ( old_pw ) ;
SAFE_FREE ( new_pw ) ;
2000-07-26 07:55:17 +04:00
return result ;
1998-11-12 09:12:19 +03:00
}
/*********************************************************
Start here .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int main ( int argc , char * * argv )
{
2007-09-04 09:39:06 +04:00
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2002-07-15 14:35:28 +04:00
int local_flags = 0 ;
2007-09-04 09:39:06 +04:00
int ret ;
2002-07-15 14:35:28 +04:00
2002-01-08 00:32:22 +03:00
AllowDebugChange = False ;
1999-12-13 16:27:58 +03:00
# if defined(HAVE_SET_AUTH_PARAMETERS)
set_auth_parameters ( argc , argv ) ;
# endif /* HAVE_SET_AUTH_PARAMETERS */
2002-07-15 14:35:28 +04:00
if ( getuid ( ) = = 0 ) {
local_flags = LOCAL_AM_ROOT ;
}
2005-12-29 00:10:11 +03:00
load_case_tables ( ) ;
2002-07-15 14:35:28 +04:00
local_flags = process_options ( argc , argv , local_flags ) ;
2002-04-02 10:18:13 +04:00
1998-11-12 09:12:19 +03:00
setup_logging ( " smbpasswd " , True ) ;
/*
* Set the machine NETBIOS name if not already
* set from the config file .
*/
2002-11-13 02:20:50 +03:00
if ( ! init_names ( ) )
return 1 ;
1998-11-12 09:12:19 +03:00
/* Check the effective uid - make sure we are not setuid */
2001-10-31 04:52:34 +03:00
if ( is_setuid_root ( ) ) {
1998-11-12 09:12:19 +03:00
fprintf ( stderr , " smbpasswd must *NOT* be setuid root. \n " ) ;
exit ( 1 ) ;
}
2002-07-15 14:35:28 +04:00
if ( local_flags & LOCAL_AM_ROOT ) {
2001-07-07 01:53:32 +04:00
secrets_init ( ) ;
2002-07-15 14:35:28 +04:00
return process_root ( local_flags ) ;
1998-11-12 09:12:19 +03:00
}
2007-09-04 09:39:06 +04:00
ret = process_nonroot ( local_flags ) ;
TALLOC_FREE ( frame ) ;
return ret ;
1996-05-04 11:50:46 +04:00
}