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
* Free Software Foundation ; either version 2 of the License , or ( at your
* 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
* this program ; if not , write to the Free Software Foundation , Inc . , 675
2001-06-29 12:04:12 +04:00
* Mass Ave , Cambridge , MA 0213 9 , USA . */
1996-05-04 11:50:46 +04:00
# include "includes.h"
2002-01-08 00:32:22 +03: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 */
2004-07-15 04:03:14 +04:00
static BOOL got_username = False ;
2002-04-02 10:18:13 +04:00
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 " ) ;
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 ;
2002-07-15 14:35:28 +04:00
pstring configfile ;
pstrcpy ( configfile , 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 ' ;
2002-08-17 18:45:04 +04:00
while ( ( ch = getopt ( argc , argv , " c:axdehminjr:sw:R:D:U:L " ) ) ! = EOF ) {
2002-04-02 10:18:13 +04:00
switch ( ch ) {
case ' L ' :
2002-07-15 14:35:28 +04:00
local_flags | = LOCAL_AM_ROOT ;
2002-04-02 10:18:13 +04:00
break ;
case ' c ' :
pstrcpy ( configfile , optarg ) ;
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 ;
}
case ' h ' :
default :
usage ( ) ;
}
}
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 ( ) ;
}
2002-07-15 14:35:28 +04:00
if ( ! lp_load ( configfile , True , False , False ) ) {
fprintf ( stderr , " Can't load %s - run testparm to debug it \n " ,
dyn_CONFIGFILE ) ;
exit ( 1 ) ;
}
return local_flags ;
2002-04-02 10:18:13 +04:00
}
1998-11-04 03:57:00 +03:00
/*************************************************************
Utility function to prompt for passwords from stdin . Each
password entered must end with a newline .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static char * stdin_new_passwd ( void )
{
2002-09-25 19:19:00 +04:00
static fstring new_pw ;
1998-11-12 09:12:19 +03:00
size_t len ;
2002-09-25 19:19:00 +04:00
ZERO_ARRAY ( new_pw ) ;
1998-11-12 09:12:19 +03:00
/*
* if no error is reported from fgets ( ) and string at least contains
* the newline that ends the password , then replace the newline with
* a null terminator .
*/
2002-09-25 19:19:00 +04:00
if ( fgets ( new_pw , sizeof ( new_pw ) , stdin ) ! = NULL ) {
if ( ( len = strlen ( new_pw ) ) > 0 ) {
if ( new_pw [ len - 1 ] = = ' \n ' )
new_pw [ len - 1 ] = 0 ;
1998-11-12 09:12:19 +03:00
}
}
2002-09-25 19:19:00 +04:00
return ( new_pw ) ;
1998-11-04 03:57:00 +03:00
}
1998-11-12 09:12:19 +03:00
1998-11-04 03:57:00 +03:00
/*************************************************************
Utility function to get passwords via tty or stdin
Used if the ' - s ' option is set to silently get passwords
to enable scripting .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-03 11:28:12 +03:00
static char * get_pass ( const char * prompt , BOOL stdin_get )
1998-11-04 03:57:00 +03:00
{
1998-11-12 09:12:19 +03:00
char * p ;
if ( stdin_get ) {
p = stdin_new_passwd ( ) ;
} else {
p = getpass ( prompt ) ;
}
2001-11-20 09:38:09 +03:00
return smb_xstrdup ( p ) ;
1998-11-04 03:57:00 +03:00
}
1998-09-25 02:33:13 +04:00
/*************************************************************
Utility function to prompt for new password .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-11-12 09:12:19 +03: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
2002-09-25 19:19:00 +04:00
static BOOL 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
{
1998-11-13 02:49:32 +03:00
BOOL ret ;
pstring err_str ;
pstring msg_str ;
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 */
return False ;
}
2002-09-25 19:19:00 +04:00
ret = remote_password_change ( remote_mach , username ,
old_passwd , new_pw , err_str , sizeof ( err_str ) ) ;
1999-12-13 16:27:58 +03:00
if ( * err_str )
1998-11-13 02:49:32 +03:00
fprintf ( stderr , err_str ) ;
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 ,
1999-12-13 16:27:58 +03:00
err_str , sizeof ( err_str ) , msg_str , sizeof ( msg_str ) ) ;
1998-11-13 02:49:32 +03:00
1999-12-13 16:27:58 +03:00
if ( * msg_str )
1998-11-13 02:49:32 +03:00
printf ( msg_str ) ;
1999-12-13 16:27:58 +03:00
if ( * err_str )
1998-11-13 02:49:32 +03:00
fprintf ( stderr , err_str ) ;
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
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static BOOL store_ldap_admin_pw ( char * pw )
{
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 ) {
2001-12-13 21:09:29 +03:00
printf ( " Setting stored password for \" %s \" in secrets.tdb \n " ,
lp_ldap_admin_dn ( ) ) ;
if ( ! store_ldap_admin_pw ( ldap_secret ) )
DEBUG ( 0 , ( " ERROR: Failed to store the ldap admin password! \n " ) ) ;
goto done ;
}
2002-04-02 10:18:13 +04:00
2004-02-26 01:01:02 +03:00
/* Ensure passdb startup(). */
if ( ! initialize_password_db ( False ) ) {
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 ( ) ;
}
2002-07-15 14:35:28 +04:00
if ( ! user_name [ 0 ] & & ( pwd = getpwuid_alloc ( geteuid ( ) ) ) ) {
2001-06-29 12:04:12 +04:00
fstrcpy ( user_name , pwd - > pw_name ) ;
2002-07-15 14:35:28 +04:00
passwd_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 ) {
SAM_ACCOUNT * sampass = NULL ;
BOOL ret ;
pdb_init_sam ( & sampass ) ;
ret = pdb_getsampwnam ( sampass , user_name ) ;
2004-11-19 23:55:54 +03:00
if ( ( ret ) & &
( pdb_get_lanman_passwd ( sampass ) = = NULL ) ) {
2001-12-05 14:00:26 +03:00
local_flags | = LOCAL_SET_PASSWORD ;
}
pdb_free_sam ( & 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
2001-03-11 03:32:10 +03:00
if ( ! 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 ) ) ) {
2001-05-04 19:44:27 +04:00
SAM_ACCOUNT * sampass = NULL ;
BOOL ret ;
pdb_init_sam ( & sampass ) ;
ret = pdb_getsampwnam ( sampass , user_name ) ;
1999-12-13 16:27:58 +03:00
printf ( " Password changed for user %s. " , user_name ) ;
2001-05-04 19:44:27 +04:00
if ( ( ret ! = False ) & & ( pdb_get_acct_ctrl ( sampass ) & ACB_DISABLED ) )
1999-12-13 16:27:58 +03:00
printf ( " User has disabled flag set. " ) ;
2001-05-04 19:44:27 +04:00
if ( ( ret ! = False ) & & ( pdb_get_acct_ctrl ( sampass ) & ACB_PWNOTREQ ) )
1999-12-13 16:27:58 +03:00
printf ( " User has no password flag set. " ) ;
printf ( " \n " ) ;
2001-09-29 17:08:26 +04:00
pdb_free_sam ( & 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 ] ) {
2002-07-15 14:35:28 +04:00
pwd = getpwuid_alloc ( 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 ) ;
2002-07-15 14:35:28 +04:00
passwd_free ( & pwd ) ;
1998-11-12 09:12:19 +03:00
} else {
2001-11-20 09:38:09 +03:00
fprintf ( stderr , " smbpasswd: you don't exist - go away \n " ) ;
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
}
2002-12-12 23:50:31 +03:00
if ( ! 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 )
{
2002-07-15 14:35:28 +04:00
int local_flags = 0 ;
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 ;
}
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
}
2002-07-15 14:35:28 +04:00
return process_nonroot ( local_flags ) ;
1996-05-04 11:50:46 +04:00
}