2008-05-08 11:14:16 +04:00
/*
Samba Unix / Linux SMB client library
Distributed SMB / CIFS Server Management Utility
2001-10-19 20:51:26 +04:00
Copyright ( C ) 2001 Steve French ( sfrench @ us . ibm . com )
Copyright ( C ) 2001 Jim McDonough ( jmcd @ us . ibm . com )
2001-11-24 16:26:01 +03:00
Copyright ( C ) 2001 Andrew Tridgell ( tridge @ samba . org )
2001-12-03 07:39:23 +03:00
Copyright ( C ) 2001 Andrew Bartlett ( abartlet @ samba . org )
2008-05-10 01:22:12 +04:00
Copyright ( C ) 2008 Kai Blin ( kai @ samba . org )
2001-10-19 20:51:26 +04:00
2001-11-24 16:26:01 +03:00
Originally written by Steve and Jim . Largely rewritten by tridge in
November 2001.
2001-10-19 20:51:26 +04:00
2001-12-03 07:39:23 +03:00
Reworked again by abartlet in December 2001
2008-05-10 01:22:12 +04:00
Another overhaul , moving functionality into plug - ins loaded on demand by Kai
in May 2008.
2001-10-19 20:51:26 +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
2001-10-19 20:51:26 +04:00
( at your option ) any later version .
2008-05-08 11:14:16 +04:00
2001-10-19 20:51:26 +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 .
2008-05-08 11:14:16 +04:00
2001-10-19 20:51:26 +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/>. */
2008-05-08 11:14:16 +04:00
2001-10-19 20:51:26 +04:00
/*****************************************************/
/* */
/* Distributed SMB/CIFS Server Management Utility */
/* */
/* The intent was to make the syntax similar */
/* to the NET utility (first developed in DOS */
/* with additional interesting & useful functions */
/* added in later SMB server network operating */
/* systems). */
/* */
/*****************************************************/
2001-11-25 03:18:11 +03:00
# include "includes.h"
2004-10-07 08:01:18 +04:00
# include "utils/net.h"
2001-10-19 20:51:26 +04:00
/***********************************************************************/
/* Beginning of internationalization section. Translatable constants */
/* should be kept in this area and referenced in the rest of the code. */
/* */
/* No functions, outside of Samba or LSB (Linux Standards Base) should */
/* be used (if possible). */
/***********************************************************************/
# define YES_STRING "Yes"
# define NO_STRING "No"
2008-05-08 11:14:16 +04:00
/***********************************************************************/
/* end of internationalization section */
/***********************************************************************/
2001-10-19 20:51:26 +04:00
2008-05-08 11:14:16 +04:00
uint32 get_sec_channel_type ( const char * param )
2003-04-21 18:09:03 +04:00
{
if ( ! ( param & & * param ) ) {
return get_default_sec_channel ( ) ;
} else {
2003-10-23 03:38:20 +04:00
if ( strequal ( param , " PDC " ) ) {
2003-04-21 18:09:03 +04:00
return SEC_CHAN_BDC ;
2003-10-23 03:38:20 +04:00
} else if ( strequal ( param , " BDC " ) ) {
2003-04-21 18:09:03 +04:00
return SEC_CHAN_BDC ;
2003-10-23 03:38:20 +04:00
} else if ( strequal ( param , " MEMBER " ) ) {
2003-04-21 18:09:03 +04:00
return SEC_CHAN_WKSTA ;
2008-05-08 11:14:16 +04:00
#if 0
2003-10-23 03:38:20 +04:00
} else if ( strequal ( param , " DOMAIN " ) ) {
2003-04-21 18:09:03 +04:00
return SEC_CHAN_DOMAIN ;
# endif
} else {
return get_default_sec_channel ( ) ;
}
}
}
2001-11-24 16:26:01 +03:00
/*
run a function from a function table . If not found then
2008-05-08 11:14:16 +04:00
call the specified usage function
2001-11-24 16:26:01 +03:00
*/
2008-05-10 01:22:12 +04:00
int net_run_function ( struct net_context * c , int argc , const char * * argv ,
struct functable * table ,
int ( * usage_fn ) ( struct net_context * c ,
int argc , const char * * argv ) )
2001-10-19 20:51:26 +04:00
{
2001-11-24 16:26:01 +03:00
int i ;
2008-05-08 11:14:16 +04:00
2001-12-03 07:39:23 +03:00
if ( argc < 1 ) {
d_printf ( " \n Usage: \n " ) ;
2008-05-10 01:22:12 +04:00
return usage_fn ( c , argc , argv ) ;
2001-12-03 07:39:23 +03:00
}
2001-11-24 16:26:01 +03:00
for ( i = 0 ; table [ i ] . funcname ; i + + ) {
2001-11-26 07:53:08 +03:00
if ( StrCaseCmp ( argv [ 0 ] , table [ i ] . funcname ) = = 0 )
2008-05-10 01:22:12 +04:00
return table [ i ] . fn ( c , argc - 1 , argv + 1 ) ;
2001-11-24 16:26:01 +03:00
}
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " No command: %s \n " , argv [ 0 ] ) ;
2008-05-10 01:22:12 +04:00
return usage_fn ( c , argc , argv ) ;
2001-10-19 20:51:26 +04:00
}
2006-02-04 01:19:41 +03:00
/*
* run a function from a function table .
*/
2008-05-10 01:22:12 +04:00
int net_run_function2 ( struct net_context * c , int argc , const char * * argv ,
const char * whoami , struct functable2 * table )
2006-02-04 01:19:41 +03:00
{
int i ;
if ( argc ! = 0 ) {
for ( i = 0 ; table [ i ] . funcname ; i + + ) {
if ( StrCaseCmp ( argv [ 0 ] , table [ i ] . funcname ) = = 0 )
2008-05-10 01:22:12 +04:00
return table [ i ] . fn ( c , argc - 1 , argv + 1 ) ;
2006-02-04 01:19:41 +03:00
}
}
for ( i = 0 ; table [ i ] . funcname ! = NULL ; i + + ) {
d_printf ( " %s %-15s %s \n " , whoami , table [ i ] . funcname ,
table [ i ] . helptext ) ;
}
return - 1 ;
}
2001-10-19 20:51:26 +04:00
/****************************************************************************
2007-10-25 01:16:54 +04:00
Connect to \ \ server \ service .
2001-10-19 20:51:26 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2006-10-20 11:44:09 +04:00
2008-05-10 01:22:12 +04:00
NTSTATUS connect_to_service ( struct net_context * c ,
struct cli_state * * cli_ctx ,
2007-10-25 01:16:54 +04:00
struct sockaddr_storage * server_ss ,
const char * server_name ,
const char * service_name ,
2004-08-10 18:27:17 +04:00
const char * service_type )
2001-11-24 16:26:01 +03:00
{
2001-12-03 10:42:18 +03:00
NTSTATUS nt_status ;
2001-11-24 16:26:01 +03:00
2008-05-10 01:22:12 +04:00
c - > opt_password = net_prompt_pass ( c , c - > opt_user_name ) ;
if ( ! c - > opt_password ) {
2007-07-19 17:07:22 +04:00
return NT_STATUS_NO_MEMORY ;
2001-11-24 16:26:01 +03:00
}
2006-10-20 11:44:09 +04:00
2008-05-10 01:22:12 +04:00
nt_status = cli_full_connection ( cli_ctx , NULL , server_name ,
server_ss , c - > opt_port ,
2007-10-25 01:16:54 +04:00
service_name , service_type ,
2008-05-10 01:22:12 +04:00
c - > opt_user_name , c - > opt_workgroup ,
c - > opt_password , 0 , Undefined , NULL ) ;
2008-01-05 11:23:35 +03:00
if ( ! NT_STATUS_IS_OK ( nt_status ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Could not connect to server %s \n " , server_name ) ;
2001-12-14 06:55:09 +03:00
/* Display a nicer message depending on the result */
2007-10-25 01:16:54 +04:00
if ( NT_STATUS_V ( nt_status ) = =
2001-12-14 06:55:09 +03:00
NT_STATUS_V ( NT_STATUS_LOGON_FAILURE ) )
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " The username or password was not correct. \n " ) ;
2004-01-21 17:36:56 +03:00
2007-10-25 01:16:54 +04:00
if ( NT_STATUS_V ( nt_status ) = =
2004-01-21 17:36:56 +03:00
NT_STATUS_V ( NT_STATUS_ACCOUNT_LOCKED_OUT ) )
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " The account was locked out. \n " ) ;
2004-01-21 17:36:56 +03:00
2007-10-25 01:16:54 +04:00
if ( NT_STATUS_V ( nt_status ) = =
2004-01-21 17:36:56 +03:00
NT_STATUS_V ( NT_STATUS_ACCOUNT_DISABLED ) )
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " The account was disabled. \n " ) ;
2002-03-01 05:56:35 +03:00
return nt_status ;
2001-11-24 16:26:01 +03:00
}
2008-01-05 11:23:35 +03:00
2008-05-10 01:22:12 +04:00
if ( c - > smb_encrypt ) {
nt_status = cli_force_encryption ( * cli_ctx ,
c - > opt_user_name ,
c - > opt_password ,
c - > opt_workgroup ) ;
2008-01-05 11:23:35 +03:00
if ( NT_STATUS_EQUAL ( nt_status , NT_STATUS_NOT_SUPPORTED ) ) {
d_printf ( " Encryption required and "
" server that doesn't support "
" UNIX extensions - failing connect \n " ) ;
} else if ( NT_STATUS_EQUAL ( nt_status , NT_STATUS_UNKNOWN_REVISION ) ) {
d_printf ( " Encryption required and "
" can't get UNIX CIFS extensions "
" version from server. \n " ) ;
} else if ( NT_STATUS_EQUAL ( nt_status , NT_STATUS_UNSUPPORTED_COMPRESSION ) ) {
d_printf ( " Encryption required and "
" share %s doesn't support "
" encryption. \n " , service_name ) ;
} else if ( ! NT_STATUS_IS_OK ( nt_status ) ) {
d_printf ( " Encryption required and "
" setup failed with error %s. \n " ,
nt_errstr ( nt_status ) ) ;
}
if ( ! NT_STATUS_IS_OK ( nt_status ) ) {
2008-05-10 01:22:12 +04:00
cli_shutdown ( * cli_ctx ) ;
* cli_ctx = NULL ;
2008-01-05 11:23:35 +03:00
}
}
return nt_status ;
2001-11-24 16:26:01 +03:00
}
2001-10-19 20:51:26 +04:00
2004-08-10 18:27:17 +04:00
/****************************************************************************
2007-10-25 01:16:54 +04:00
Connect to \ \ server \ ipc $ .
2004-08-10 18:27:17 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-25 01:16:54 +04:00
2008-05-10 01:22:12 +04:00
NTSTATUS connect_to_ipc ( struct net_context * c ,
struct cli_state * * cli_ctx ,
2007-10-25 01:16:54 +04:00
struct sockaddr_storage * server_ss ,
const char * server_name )
2004-08-10 18:27:17 +04:00
{
2008-05-10 01:22:12 +04:00
return connect_to_service ( c , cli_ctx , server_ss , server_name , " IPC$ " ,
" IPC " ) ;
2004-08-10 18:27:17 +04:00
}
2001-12-05 06:14:21 +03:00
/****************************************************************************
2007-10-25 01:16:54 +04:00
Connect to \ \ server \ ipc $ anonymously .
2001-12-05 06:14:21 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-25 01:16:54 +04:00
2008-05-10 01:22:12 +04:00
NTSTATUS connect_to_ipc_anonymous ( struct net_context * c ,
struct cli_state * * cli_ctx ,
2007-10-25 01:16:54 +04:00
struct sockaddr_storage * server_ss ,
const char * server_name )
2001-12-05 06:14:21 +03:00
{
NTSTATUS nt_status ;
2008-05-10 01:22:12 +04:00
nt_status = cli_full_connection ( cli_ctx , c - > opt_requester_name ,
server_name , server_ss , c - > opt_port ,
2007-10-25 01:16:54 +04:00
" IPC$ " , " IPC " ,
2001-12-05 06:14:21 +03:00
" " , " " ,
2003-07-31 03:49:29 +04:00
" " , 0 , Undefined , NULL ) ;
2007-10-25 01:16:54 +04:00
2001-12-05 06:14:21 +03:00
if ( NT_STATUS_IS_OK ( nt_status ) ) {
2002-03-01 05:56:35 +03:00
return nt_status ;
2001-12-05 06:14:21 +03:00
} else {
2002-04-04 20:53:07 +04:00
DEBUG ( 1 , ( " Cannot connect to server (anonymously). Error was %s \n " , nt_errstr ( nt_status ) ) ) ;
2002-03-01 05:56:35 +03:00
return nt_status ;
2001-12-05 06:14:21 +03:00
}
}
2006-10-20 11:44:09 +04:00
/****************************************************************************
Return malloced user @ realm for krb5 login .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static char * get_user_and_realm ( const char * username )
{
char * user_and_realm = NULL ;
2006-10-20 12:14:38 +04:00
if ( ! username ) {
return NULL ;
}
2006-10-20 11:44:09 +04:00
if ( strchr_m ( username , ' @ ' ) ) {
user_and_realm = SMB_STRDUP ( username ) ;
} else {
if ( asprintf ( & user_and_realm , " %s@%s " , username , lp_realm ( ) ) = = - 1 ) {
user_and_realm = NULL ;
}
}
return user_and_realm ;
}
2006-05-12 19:17:35 +04:00
/****************************************************************************
2007-10-25 01:16:54 +04:00
Connect to \ \ server \ ipc $ using KRB5 .
2006-05-12 19:17:35 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2006-10-20 12:14:38 +04:00
2008-05-10 01:22:12 +04:00
NTSTATUS connect_to_ipc_krb5 ( struct net_context * c ,
struct cli_state * * cli_ctx ,
2007-10-25 01:16:54 +04:00
struct sockaddr_storage * server_ss ,
const char * server_name )
2006-05-12 19:17:35 +04:00
{
NTSTATUS nt_status ;
2006-10-20 11:44:09 +04:00
char * user_and_realm = NULL ;
2008-05-10 01:22:12 +04:00
/* FIXME: Should get existing kerberos ticket if possible. */
c - > opt_password = net_prompt_pass ( c , c - > opt_user_name ) ;
if ( ! c - > opt_password ) {
2007-07-19 17:07:22 +04:00
return NT_STATUS_NO_MEMORY ;
2006-10-20 11:44:09 +04:00
}
2008-05-10 01:22:12 +04:00
user_and_realm = get_user_and_realm ( c - > opt_user_name ) ;
2006-10-20 11:44:09 +04:00
if ( ! user_and_realm ) {
return NT_STATUS_NO_MEMORY ;
}
2006-05-12 19:17:35 +04:00
2008-05-10 01:22:12 +04:00
nt_status = cli_full_connection ( cli_ctx , NULL , server_name ,
server_ss , c - > opt_port ,
2007-10-25 01:16:54 +04:00
" IPC$ " , " IPC " ,
2008-05-10 01:22:12 +04:00
user_and_realm , c - > opt_workgroup ,
c - > opt_password ,
CLI_FULL_CONNECTION_USE_KERBEROS ,
2006-05-12 19:17:35 +04:00
Undefined , NULL ) ;
2007-10-25 01:16:54 +04:00
2006-10-20 11:44:09 +04:00
SAFE_FREE ( user_and_realm ) ;
2008-01-05 11:23:35 +03:00
if ( ! NT_STATUS_IS_OK ( nt_status ) ) {
2006-05-12 19:17:35 +04:00
DEBUG ( 1 , ( " Cannot connect to server using kerberos. Error was %s \n " , nt_errstr ( nt_status ) ) ) ;
return nt_status ;
}
2008-01-05 11:23:35 +03:00
2008-05-10 01:22:12 +04:00
if ( c - > smb_encrypt ) {
nt_status = cli_cm_force_encryption ( * cli_ctx ,
2008-01-05 11:23:35 +03:00
user_and_realm ,
2008-05-10 01:22:12 +04:00
c - > opt_password ,
c - > opt_workgroup ,
2008-01-05 11:23:35 +03:00
" IPC$ " ) ;
if ( ! NT_STATUS_IS_OK ( nt_status ) ) {
2008-05-10 01:22:12 +04:00
cli_shutdown ( * cli_ctx ) ;
* cli_ctx = NULL ;
2008-01-05 11:23:35 +03:00
}
}
return nt_status ;
2006-05-12 19:17:35 +04:00
}
2004-08-10 18:27:17 +04:00
/**
2004-08-27 01:37:20 +04:00
* Connect a server and open a given pipe
2004-08-10 18:27:17 +04:00
*
2008-05-08 11:14:16 +04:00
* @ param cli_dst A cli_state
2004-08-10 18:27:17 +04:00
* @ param pipe The pipe to open
2004-08-21 00:13:05 +04:00
* @ param got_pipe boolean that stores if we got a pipe
2004-08-10 18:27:17 +04:00
*
* @ return Normal NTSTATUS return .
* */
2008-05-10 01:22:12 +04:00
NTSTATUS connect_dst_pipe ( struct net_context * c , struct cli_state * * cli_dst ,
struct rpc_pipe_client * * pp_pipe_hnd , int pipe_num )
2004-08-10 18:27:17 +04:00
{
NTSTATUS nt_status ;
2004-12-07 21:25:53 +03:00
char * server_name = SMB_STRDUP ( " 127.0.0.1 " ) ;
2004-08-10 18:27:17 +04:00
struct cli_state * cli_tmp = NULL ;
2005-09-30 21:13:37 +04:00
struct rpc_pipe_client * pipe_hnd = NULL ;
2004-08-10 18:27:17 +04:00
2006-03-09 23:42:01 +03:00
if ( server_name = = NULL ) {
return NT_STATUS_NO_MEMORY ;
}
2008-05-10 01:22:12 +04:00
if ( c - > opt_destination ) {
2006-03-09 23:42:01 +03:00
SAFE_FREE ( server_name ) ;
2008-05-10 01:22:12 +04:00
if ( ( server_name = SMB_STRDUP ( c - > opt_destination ) ) = = NULL ) {
2006-03-09 23:42:01 +03:00
return NT_STATUS_NO_MEMORY ;
}
}
2004-08-27 01:37:20 +04:00
/* make a connection to a named pipe */
2008-05-10 01:22:12 +04:00
nt_status = connect_to_ipc ( c , & cli_tmp , NULL , server_name ) ;
2005-09-30 21:13:37 +04:00
if ( ! NT_STATUS_IS_OK ( nt_status ) ) {
2006-03-09 23:42:01 +03:00
SAFE_FREE ( server_name ) ;
2004-08-10 18:27:17 +04:00
return nt_status ;
2005-09-30 21:13:37 +04:00
}
2004-08-10 18:27:17 +04:00
2005-09-30 21:13:37 +04:00
pipe_hnd = cli_rpc_pipe_open_noauth ( cli_tmp , pipe_num , & nt_status ) ;
if ( ! pipe_hnd ) {
2004-08-21 00:13:05 +04:00
DEBUG ( 0 , ( " couldn't not initialize pipe \n " ) ) ;
2004-08-10 18:27:17 +04:00
cli_shutdown ( cli_tmp ) ;
2006-03-09 23:42:01 +03:00
SAFE_FREE ( server_name ) ;
2005-09-30 21:13:37 +04:00
return nt_status ;
2004-08-10 18:27:17 +04:00
}
2004-08-27 01:37:20 +04:00
* cli_dst = cli_tmp ;
2005-09-30 21:13:37 +04:00
* pp_pipe_hnd = pipe_hnd ;
2006-03-09 23:42:01 +03:00
SAFE_FREE ( server_name ) ;
2004-08-10 18:27:17 +04:00
return nt_status ;
}
2003-08-20 02:47:10 +04:00
/****************************************************************************
2007-09-01 01:25:53 +04:00
Use the local machine account ( krb ) and password for this session .
2003-08-20 02:47:10 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2005-09-30 21:13:37 +04:00
2008-05-10 01:22:12 +04:00
int net_use_krb_machine_account ( struct net_context * c )
2003-08-20 02:47:10 +04:00
{
char * user_name = NULL ;
if ( ! secrets_init ( ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " ERROR: Unable to open secrets database \n " ) ;
2003-08-20 02:47:10 +04:00
exit ( 1 ) ;
}
2008-05-10 01:22:12 +04:00
c - > opt_password = secrets_fetch_machine_password (
c - > opt_target_workgroup , NULL , NULL ) ;
2003-08-20 02:47:10 +04:00
if ( asprintf ( & user_name , " %s$@%s " , global_myname ( ) , lp_realm ( ) ) = = - 1 ) {
return - 1 ;
}
2008-05-10 01:22:12 +04:00
c - > opt_user_name = user_name ;
2003-08-20 02:47:10 +04:00
return 0 ;
}
2007-08-29 23:55:13 +04:00
/****************************************************************************
Use the machine account name and password for this session .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-05-10 01:22:12 +04:00
int net_use_machine_account ( struct net_context * c )
2007-08-29 23:55:13 +04:00
{
char * user_name = NULL ;
2007-10-25 01:16:54 +04:00
2007-08-29 23:55:13 +04:00
if ( ! secrets_init ( ) ) {
d_fprintf ( stderr , " ERROR: Unable to open secrets database \n " ) ;
exit ( 1 ) ;
}
2008-05-10 01:22:12 +04:00
c - > opt_password = secrets_fetch_machine_password (
c - > opt_target_workgroup , NULL , NULL ) ;
2007-08-29 23:55:13 +04:00
if ( asprintf ( & user_name , " %s$ " , global_myname ( ) ) = = - 1 ) {
return - 1 ;
}
2008-05-10 01:22:12 +04:00
c - > opt_user_name = user_name ;
2007-08-29 23:55:13 +04:00
return 0 ;
}
2008-05-10 01:22:12 +04:00
bool net_find_server ( struct net_context * c ,
const char * domain ,
2007-10-25 01:16:54 +04:00
unsigned flags ,
struct sockaddr_storage * server_ss ,
char * * server_name )
2001-11-24 16:26:01 +03:00
{
2008-05-10 01:22:12 +04:00
const char * d = domain ? domain : c - > opt_target_workgroup ;
2001-12-04 07:48:01 +03:00
2008-05-10 01:22:12 +04:00
if ( c - > opt_host ) {
* server_name = SMB_STRDUP ( c - > opt_host ) ;
2007-10-25 01:16:54 +04:00
}
2001-12-04 07:48:01 +03:00
2008-05-10 01:22:12 +04:00
if ( c - > opt_have_ip ) {
* server_ss = c - > opt_dest_ip ;
2001-12-05 05:58:40 +03:00
if ( ! * server_name ) {
2007-10-25 01:16:54 +04:00
char addr [ INET6_ADDRSTRLEN ] ;
2008-05-10 01:22:12 +04:00
print_sockaddr ( addr , sizeof ( addr ) , & c - > opt_dest_ip ) ;
2007-10-25 01:16:54 +04:00
* server_name = SMB_STRDUP ( addr ) ;
2001-12-03 07:39:23 +03:00
}
2001-12-05 14:00:26 +03:00
} else if ( * server_name ) {
2001-12-03 07:39:23 +03:00
/* resolve the IP address */
2007-10-25 01:16:54 +04:00
if ( ! resolve_name ( * server_name , server_ss , 0x20 ) ) {
2001-12-03 10:42:18 +03:00
DEBUG ( 1 , ( " Unable to resolve server name \n " ) ) ;
2007-10-25 01:16:54 +04:00
return false ;
2001-12-03 07:39:23 +03:00
}
2001-12-04 07:48:01 +03:00
} else if ( flags & NET_FLAGS_PDC ) {
2007-10-25 01:16:54 +04:00
fstring dc_name ;
struct sockaddr_storage pdc_ss ;
2007-11-29 17:16:06 +03:00
if ( ! get_pdc_ip ( d , & pdc_ss ) ) {
2007-10-25 01:16:54 +04:00
DEBUG ( 1 , ( " Unable to resolve PDC server address \n " ) ) ;
return false ;
}
if ( is_zero_addr ( & pdc_ss ) ) {
return false ;
2001-11-24 16:26:01 +03:00
}
2007-10-25 01:16:54 +04:00
if ( ! name_status_find ( d , 0x1b , 0x20 , & pdc_ss , dc_name ) ) {
return False ;
}
* server_name = SMB_STRDUP ( dc_name ) ;
* server_ss = pdc_ss ;
2001-12-04 07:48:01 +03:00
} else if ( flags & NET_FLAGS_DMB ) {
2007-10-25 01:16:54 +04:00
struct sockaddr_storage msbrow_ss ;
char addr [ INET6_ADDRSTRLEN ] ;
2001-12-04 07:48:01 +03:00
/* if (!resolve_name(MSBROWSE, &msbrow_ip, 1)) */
2007-10-25 01:16:54 +04:00
if ( ! resolve_name ( d , & msbrow_ss , 0x1B ) ) {
2001-12-04 07:48:01 +03:00
DEBUG ( 1 , ( " Unable to resolve domain browser via name lookup \n " ) ) ;
2007-10-25 01:16:54 +04:00
return false ;
2001-12-04 07:48:01 +03:00
}
2007-10-25 01:16:54 +04:00
* server_ss = msbrow_ss ;
print_sockaddr ( addr , sizeof ( addr ) , server_ss ) ;
* server_name = SMB_STRDUP ( addr ) ;
2001-12-03 07:39:23 +03:00
} else if ( flags & NET_FLAGS_MASTER ) {
2007-10-25 01:16:54 +04:00
struct sockaddr_storage brow_ss ;
char addr [ INET6_ADDRSTRLEN ] ;
if ( ! resolve_name ( d , & brow_ss , 0x1D ) ) {
2001-11-24 16:26:01 +03:00
/* go looking for workgroups */
DEBUG ( 1 , ( " Unable to resolve master browser via name lookup \n " ) ) ;
2007-10-25 01:16:54 +04:00
return false ;
2001-11-24 16:26:01 +03:00
}
2007-10-25 01:16:54 +04:00
* server_ss = brow_ss ;
print_sockaddr ( addr , sizeof ( addr ) , server_ss ) ;
* server_name = SMB_STRDUP ( addr ) ;
2001-12-03 07:39:23 +03:00
} else if ( ! ( flags & NET_FLAGS_LOCALHOST_DEFAULT_INSANE ) ) {
2007-10-25 01:16:54 +04:00
if ( ! interpret_string_addr ( server_ss ,
" 127.0.0.1 " , AI_NUMERICHOST ) ) {
DEBUG ( 1 , ( " Unable to resolve 127.0.0.1 \n " ) ) ;
return false ;
}
2004-12-07 21:25:53 +03:00
* server_name = SMB_STRDUP ( " 127.0.0.1 " ) ;
2001-11-24 16:26:01 +03:00
}
2001-10-19 20:51:26 +04:00
2008-03-23 19:54:48 +03:00
if ( ! * server_name ) {
2001-11-24 16:26:01 +03:00
DEBUG ( 1 , ( " no server to connect to \n " ) ) ;
2001-12-05 05:58:40 +03:00
return False ;
2001-11-24 16:26:01 +03:00
}
2001-12-03 07:39:23 +03:00
2001-12-05 05:58:40 +03:00
return True ;
}
2007-10-25 01:16:54 +04:00
bool net_find_pdc ( struct sockaddr_storage * server_ss ,
fstring server_name ,
const char * domain_name )
2002-03-01 05:56:35 +03:00
{
2007-10-25 01:16:54 +04:00
if ( ! get_pdc_ip ( domain_name , server_ss ) ) {
return false ;
}
if ( is_zero_addr ( server_ss ) ) {
return false ;
}
if ( ! name_status_find ( domain_name , 0x1b , 0x20 , server_ss , server_name ) ) {
return false ;
}
return true ;
2002-03-01 05:56:35 +03:00
}
2008-05-10 01:22:12 +04:00
NTSTATUS net_make_ipc_connection ( struct net_context * c , unsigned flags ,
struct cli_state * * pcli )
2006-05-12 19:17:35 +04:00
{
2008-05-10 01:22:12 +04:00
return net_make_ipc_connection_ex ( c , NULL , NULL , NULL , flags , pcli ) ;
2006-05-12 19:17:35 +04:00
}
2002-03-01 05:56:35 +03:00
2008-05-10 01:22:12 +04:00
NTSTATUS net_make_ipc_connection_ex ( struct net_context * c , const char * domain ,
const char * server ,
struct sockaddr_storage * pss ,
unsigned flags , struct cli_state * * pcli )
2001-12-05 05:58:40 +03:00
{
char * server_name = NULL ;
2007-10-25 01:16:54 +04:00
struct sockaddr_storage server_ss ;
2002-03-01 05:56:35 +03:00
struct cli_state * cli = NULL ;
NTSTATUS nt_status ;
2001-12-05 05:58:40 +03:00
2007-10-25 01:16:54 +04:00
if ( ! server | | ! pss ) {
2008-05-10 01:22:12 +04:00
if ( ! net_find_server ( c , domain , flags , & server_ss ,
& server_name ) ) {
2006-05-12 19:17:35 +04:00
d_fprintf ( stderr , " Unable to find a suitable server \n " ) ;
2007-09-17 19:11:20 +04:00
nt_status = NT_STATUS_UNSUCCESSFUL ;
goto done ;
2006-05-12 19:17:35 +04:00
}
} else {
server_name = SMB_STRDUP ( server ) ;
2007-10-25 01:16:54 +04:00
server_ss = * pss ;
2001-12-05 05:58:40 +03:00
}
2001-12-05 06:14:21 +03:00
if ( flags & NET_FLAGS_ANONYMOUS ) {
2008-05-10 01:22:12 +04:00
nt_status = connect_to_ipc_anonymous ( c , & cli , & server_ss ,
server_name ) ;
2001-12-05 06:14:21 +03:00
} else {
2008-05-10 01:22:12 +04:00
nt_status = connect_to_ipc ( c , & cli , & server_ss ,
server_name ) ;
2001-12-05 06:14:21 +03:00
}
2002-10-01 17:10:57 +04:00
2006-02-08 07:03:47 +03:00
/* store the server in the affinity cache if it was a PDC */
2006-03-06 17:55:27 +03:00
if ( ( flags & NET_FLAGS_PDC ) & & NT_STATUS_IS_OK ( nt_status ) )
2006-02-08 07:03:47 +03:00
saf_store ( cli - > server_domain , cli - > desthost ) ;
2001-12-04 07:48:01 +03:00
SAFE_FREE ( server_name ) ;
2007-09-17 19:11:20 +04:00
if ( ! NT_STATUS_IS_OK ( nt_status ) ) {
2006-02-04 01:19:41 +03:00
d_fprintf ( stderr , " Connection failed: %s \n " ,
nt_errstr ( nt_status ) ) ;
2007-09-17 19:11:20 +04:00
cli = NULL ;
2002-10-01 17:10:57 +04:00
}
2007-09-17 19:11:20 +04:00
done :
if ( pcli ! = NULL ) {
* pcli = cli ;
}
return nt_status ;
2001-10-19 20:51:26 +04:00
}
2008-05-10 01:22:12 +04:00
static int net_user ( struct net_context * c , int argc , const char * * argv )
2002-04-04 20:53:07 +04:00
{
2008-05-10 01:22:12 +04:00
if ( net_ads_check ( c ) = = 0 )
return net_ads_user ( c , argc , argv ) ;
2002-04-04 20:53:07 +04:00
2002-04-05 05:36:28 +04:00
/* if server is not specified, default to PDC? */
2008-05-10 01:22:12 +04:00
if ( net_rpc_check ( c , NET_FLAGS_PDC ) )
return net_rpc_user ( c , argc , argv ) ;
2002-04-05 05:36:28 +04:00
2008-05-10 01:22:12 +04:00
return net_rap_user ( c , argc , argv ) ;
2002-04-04 20:53:07 +04:00
}
2008-05-10 01:22:12 +04:00
static int net_group ( struct net_context * c , int argc , const char * * argv )
2002-07-15 14:35:28 +04:00
{
2008-05-10 01:22:12 +04:00
if ( net_ads_check ( c ) = = 0 )
return net_ads_group ( c , argc , argv ) ;
2002-07-15 14:35:28 +04:00
2008-05-10 01:22:12 +04:00
if ( argc = = 0 & & net_rpc_check ( c , NET_FLAGS_PDC ) )
return net_rpc_group ( c , argc , argv ) ;
2002-07-15 14:35:28 +04:00
2008-05-10 01:22:12 +04:00
return net_rap_group ( c , argc , argv ) ;
2002-07-15 14:35:28 +04:00
}
2001-10-19 20:51:26 +04:00
2008-05-10 01:22:12 +04:00
static int net_join ( struct net_context * c , int argc , const char * * argv )
2002-03-16 01:04:53 +03:00
{
2008-05-10 01:22:12 +04:00
if ( net_ads_check_our_domain ( c ) = = 0 ) {
if ( net_ads_join ( c , argc , argv ) = = 0 )
2002-03-16 01:04:53 +03:00
return 0 ;
else
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " ADS join did not work, falling back to RPC... \n " ) ;
2002-03-16 01:04:53 +03:00
}
2008-05-10 01:22:12 +04:00
return net_rpc_join ( c , argc , argv ) ;
2002-03-16 01:04:53 +03:00
}
2008-05-10 01:22:12 +04:00
static int net_changetrustpw ( struct net_context * c , int argc , const char * * argv )
2003-04-15 02:27:09 +04:00
{
2008-05-10 01:22:12 +04:00
if ( net_ads_check_our_domain ( c ) = = 0 )
return net_ads_changetrustpw ( c , argc , argv ) ;
2003-04-15 02:27:09 +04:00
2008-05-10 01:22:12 +04:00
return net_rpc_changetrustpw ( c , argc , argv ) ;
2003-04-15 02:27:09 +04:00
}
2006-05-31 02:10:09 +04:00
static void set_line_buffering ( FILE * f )
{
setvbuf ( f , NULL , _IOLBF , 0 ) ;
}
2008-05-10 01:22:12 +04:00
static int net_changesecretpw ( struct net_context * c , int argc ,
const char * * argv )
2003-08-29 00:24:25 +04:00
{
char * trust_pw ;
uint32 sec_channel_type = SEC_CHAN_WKSTA ;
2008-05-10 01:22:12 +04:00
if ( c - > opt_force ) {
if ( c - > opt_stdin ) {
2006-05-31 02:10:09 +04:00
set_line_buffering ( stdin ) ;
set_line_buffering ( stdout ) ;
set_line_buffering ( stderr ) ;
}
2006-07-11 22:01:26 +04:00
2008-05-10 01:22:12 +04:00
trust_pw = get_pass ( " Enter machine password: " , c - > opt_stdin ) ;
2003-08-29 00:24:25 +04:00
2007-03-13 23:53:38 +03:00
if ( ! secrets_store_machine_password ( trust_pw , lp_workgroup ( ) , sec_channel_type ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Unable to write the machine account password in the secrets database " ) ;
2003-08-29 00:24:25 +04:00
return 1 ;
}
else {
d_printf ( " Modified trust account password in secrets database \n " ) ;
}
}
else {
d_printf ( " Machine account password change requires the -f flag. \n " ) ;
d_printf ( " Do NOT use this function unless you know what it does! \n " ) ;
d_printf ( " This function will change the ADS Domain member machine account password in the secrets.tdb file! \n " ) ;
}
return 0 ;
}
2008-05-10 01:22:12 +04:00
static int net_share ( struct net_context * c , int argc , const char * * argv )
2002-07-15 14:35:28 +04:00
{
2008-05-10 01:22:12 +04:00
if ( net_rpc_check ( c , 0 ) )
return net_rpc_share ( c , argc , argv ) ;
return net_rap_share ( c , argc , argv ) ;
2002-07-15 14:35:28 +04:00
}
2008-05-10 01:22:12 +04:00
static int net_file ( struct net_context * c , int argc , const char * * argv )
2002-07-15 14:35:28 +04:00
{
2008-05-10 01:22:12 +04:00
if ( net_rpc_check ( c , 0 ) )
return net_rpc_file ( c , argc , argv ) ;
return net_rap_file ( c , argc , argv ) ;
2002-07-15 14:35:28 +04:00
}
2002-09-25 19:19:00 +04:00
/*
Retrieve our local SID or the SID for the specified name
*/
2008-05-10 01:22:12 +04:00
static int net_getlocalsid ( struct net_context * c , int argc , const char * * argv )
2002-09-25 19:19:00 +04:00
{
DOM_SID sid ;
const char * name ;
fstring sid_str ;
if ( argc > = 1 ) {
name = argv [ 0 ] ;
}
else {
2002-11-13 02:20:50 +03:00
name = global_myname ( ) ;
2002-09-25 19:19:00 +04:00
}
2007-03-11 19:49:16 +03:00
if ( ! initialize_password_db ( False , NULL ) ) {
2004-02-26 01:01:02 +03:00
DEBUG ( 0 , ( " WARNING: Could not open passdb - local sid may not reflect passdb \n "
2007-10-03 16:55:45 +04:00
" backend knowledge (such as the sid stored in LDAP) \n " ) ) ;
2004-02-26 01:01:02 +03:00
}
2004-11-15 20:20:58 +03:00
/* first check to see if we can even access secrets, so we don't
panic when we can ' t . */
if ( ! secrets_init ( ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Unable to open secrets.tdb. Can't fetch domain SID for name: %s \n " , name ) ;
2004-11-15 20:20:58 +03:00
return 1 ;
}
2004-02-26 02:12:29 +03:00
/* Generate one, if it doesn't exist */
get_global_sam_sid ( ) ;
2002-09-25 19:19:00 +04:00
if ( ! secrets_fetch_domain_sid ( name , & sid ) ) {
2004-11-15 20:20:58 +03:00
DEBUG ( 0 , ( " Can't fetch domain SID for name: %s \n " , name ) ) ;
2002-09-25 19:19:00 +04:00
return 1 ;
}
2007-12-16 00:47:30 +03:00
sid_to_fstring ( sid_str , & sid ) ;
2002-09-25 19:19:00 +04:00
d_printf ( " SID for domain %s is: %s \n " , name , sid_str ) ;
return 0 ;
}
2008-05-10 01:22:12 +04:00
static int net_setlocalsid ( struct net_context * c , int argc , const char * * argv )
2002-09-25 19:19:00 +04:00
{
DOM_SID sid ;
if ( ( argc ! = 1 )
| | ( strncmp ( argv [ 0 ] , " S-1-5-21- " , strlen ( " S-1-5-21- " ) ) ! = 0 )
| | ( ! string_to_sid ( & sid , argv [ 0 ] ) )
| | ( sid . num_auths ! = 4 ) ) {
d_printf ( " usage: net setlocalsid S-1-5-21-x-y-z \n " ) ;
return 1 ;
}
2002-11-13 02:20:50 +03:00
if ( ! secrets_store_domain_sid ( global_myname ( ) , & sid ) ) {
2002-09-25 19:19:00 +04:00
DEBUG ( 0 , ( " Can't store domain SID as a pdc/bdc. \n " ) ) ;
return 1 ;
}
return 0 ;
}
2008-05-10 01:22:12 +04:00
static int net_setdomainsid ( struct net_context * c , int argc , const char * * argv )
2006-05-16 21:40:28 +04:00
{
DOM_SID sid ;
if ( ( argc ! = 1 )
| | ( strncmp ( argv [ 0 ] , " S-1-5-21- " , strlen ( " S-1-5-21- " ) ) ! = 0 )
| | ( ! string_to_sid ( & sid , argv [ 0 ] ) )
| | ( sid . num_auths ! = 4 ) ) {
d_printf ( " usage: net setdomainsid S-1-5-21-x-y-z \n " ) ;
return 1 ;
}
if ( ! secrets_store_domain_sid ( lp_workgroup ( ) , & sid ) ) {
DEBUG ( 0 , ( " Can't store domain SID. \n " ) ) ;
return 1 ;
}
return 0 ;
}
2008-05-10 01:22:12 +04:00
static int net_getdomainsid ( struct net_context * c , int argc , const char * * argv )
2002-09-25 19:19:00 +04:00
{
DOM_SID domain_sid ;
fstring sid_str ;
2007-12-06 15:54:45 +03:00
if ( argc > 0 ) {
d_printf ( " usage: net getdomainsid \n " ) ;
return 1 ;
}
2007-03-11 19:49:16 +03:00
if ( ! initialize_password_db ( False , NULL ) ) {
2007-12-06 15:54:45 +03:00
DEBUG ( 0 , ( " WARNING: Could not open passdb - domain SID may "
2007-12-03 12:45:47 +03:00
" not reflect passdb \n "
2007-12-06 15:54:45 +03:00
" backend knowledge (such as the SID stored in "
2007-12-03 12:45:47 +03:00
" LDAP) \n " ) ) ;
2007-10-03 16:55:45 +04:00
}
/* first check to see if we can even access secrets, so we don't
panic when we can ' t . */
if ( ! secrets_init ( ) ) {
d_fprintf ( stderr , " Unable to open secrets.tdb. Can't fetch domain "
" SID for name: %s \n " , get_global_sam_name ( ) ) ;
return 1 ;
2004-02-26 01:01:02 +03:00
}
2004-02-26 02:12:29 +03:00
/* Generate one, if it doesn't exist */
get_global_sam_sid ( ) ;
2002-11-13 02:20:50 +03:00
if ( ! secrets_fetch_domain_sid ( global_myname ( ) , & domain_sid ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Could not fetch local SID \n " ) ;
2002-09-25 19:19:00 +04:00
return 1 ;
}
2007-12-16 00:47:30 +03:00
sid_to_fstring ( sid_str , & domain_sid ) ;
2007-12-06 15:54:45 +03:00
d_printf ( " SID for local machine %s is: %s \n " , global_myname ( ) , sid_str ) ;
2002-09-25 19:19:00 +04:00
2008-05-10 01:22:12 +04:00
if ( ! secrets_fetch_domain_sid ( c - > opt_workgroup , & domain_sid ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Could not fetch domain SID \n " ) ;
2002-09-25 19:19:00 +04:00
return 1 ;
}
2007-12-16 00:47:30 +03:00
sid_to_fstring ( sid_str , & domain_sid ) ;
2008-05-10 01:22:12 +04:00
d_printf ( " SID for domain %s is: %s \n " , c - > opt_workgroup , sid_str ) ;
2002-09-25 19:19:00 +04:00
return 0 ;
}
2003-09-07 20:36:13 +04:00
# ifdef WITH_FAKE_KASERVER
2008-05-10 01:22:12 +04:00
int net_help_afs ( struct net_context * c , int argc , const char * * argv )
2003-09-07 20:36:13 +04:00
{
2005-02-10 20:38:49 +03:00
d_printf ( " net afs key filename \n "
2003-09-07 20:36:13 +04:00
" \t Imports a OpenAFS KeyFile into our secrets.tdb \n \n " ) ;
2005-02-10 20:38:49 +03:00
d_printf ( " net afs impersonate <user> <cell> \n "
" \t Creates a token for user@cell \n \n " ) ;
2003-09-07 20:36:13 +04:00
return - 1 ;
}
2008-05-10 01:22:12 +04:00
static int net_afs_key ( struct net_context * c , int argc , const char * * argv )
2003-09-07 20:36:13 +04:00
{
int fd ;
struct afs_keyfile keyfile ;
2003-09-23 18:52:21 +04:00
if ( argc ! = 2 ) {
2005-06-30 00:17:05 +04:00
d_printf ( " usage: 'net afs key <keyfile> cell' \n " ) ;
2003-09-07 20:36:13 +04:00
return - 1 ;
}
if ( ! secrets_init ( ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Could not open secrets.tdb \n " ) ;
2003-09-07 20:36:13 +04:00
return - 1 ;
}
if ( ( fd = open ( argv [ 0 ] , O_RDONLY , 0 ) ) < 0 ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Could not open %s \n " , argv [ 0 ] ) ;
2003-09-07 20:36:13 +04:00
return - 1 ;
}
if ( read ( fd , & keyfile , sizeof ( keyfile ) ) ! = sizeof ( keyfile ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Could not read keyfile \n " ) ;
2003-09-07 20:36:13 +04:00
return - 1 ;
}
2003-09-23 18:52:21 +04:00
if ( ! secrets_store_afs_keyfile ( argv [ 1 ] , & keyfile ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " Could not write keyfile to secrets.tdb \n " ) ;
2003-09-07 20:36:13 +04:00
return - 1 ;
}
return 0 ;
}
2008-05-10 01:22:12 +04:00
static int net_afs_impersonate ( struct net_context * c , int argc ,
const char * * argv )
2005-02-10 20:38:49 +03:00
{
char * token ;
if ( argc ! = 2 ) {
fprintf ( stderr , " Usage: net afs impersonate <user> <cell> \n " ) ;
exit ( 1 ) ;
}
token = afs_createtoken_str ( argv [ 0 ] , argv [ 1 ] ) ;
if ( token = = NULL ) {
fprintf ( stderr , " Could not create token \n " ) ;
exit ( 1 ) ;
}
if ( ! afs_settoken_str ( token ) ) {
fprintf ( stderr , " Could not set token into kernel \n " ) ;
exit ( 1 ) ;
}
printf ( " Success: %s@%s \n " , argv [ 0 ] , argv [ 1 ] ) ;
return 0 ;
}
2008-05-10 01:22:12 +04:00
static int net_afs ( struct net_context * c , int argc , const char * * argv )
2005-02-10 20:38:49 +03:00
{
struct functable func [ ] = {
{ " key " , net_afs_key } ,
{ " impersonate " , net_afs_impersonate } ,
{ " help " , net_help_afs } ,
{ NULL , NULL }
} ;
2008-05-10 01:22:12 +04:00
return net_run_function ( c , argc , argv , func , net_help_afs ) ;
2005-02-10 20:38:49 +03:00
}
2003-09-07 20:36:13 +04:00
# endif /* WITH_FAKE_KASERVER */
2007-10-19 04:40:25 +04:00
static bool search_maxrid ( struct pdb_search * search , const char * type ,
2005-04-10 19:26:37 +04:00
uint32 * max_rid )
2003-01-15 20:22:48 +03:00
{
2005-04-10 19:26:37 +04:00
struct samr_displayentry * entries ;
uint32 i , num_entries ;
2003-01-15 20:22:48 +03:00
2005-04-10 19:26:37 +04:00
if ( search = = NULL ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " get_maxrid: Could not search %s \n " , type ) ;
2005-04-10 19:26:37 +04:00
return False ;
2003-01-15 20:22:48 +03:00
}
2005-04-10 19:26:37 +04:00
num_entries = pdb_search_entries ( search , 0 , 0xffffffff , & entries ) ;
for ( i = 0 ; i < num_entries ; i + + )
* max_rid = MAX ( * max_rid , entries [ i ] . rid ) ;
pdb_search_destroy ( search ) ;
return True ;
}
2003-01-15 20:22:48 +03:00
2005-04-10 19:26:37 +04:00
static uint32 get_maxrid ( void )
{
uint32 max_rid = 0 ;
2003-01-15 20:22:48 +03:00
2005-04-10 19:26:37 +04:00
if ( ! search_maxrid ( pdb_search_users ( 0 ) , " users " , & max_rid ) )
return 0 ;
2003-01-15 20:22:48 +03:00
2005-04-10 19:26:37 +04:00
if ( ! search_maxrid ( pdb_search_groups ( ) , " groups " , & max_rid ) )
return 0 ;
2003-01-15 20:22:48 +03:00
2005-04-10 19:26:37 +04:00
if ( ! search_maxrid ( pdb_search_aliases ( get_global_sam_sid ( ) ) ,
" aliases " , & max_rid ) )
return 0 ;
2008-05-08 11:14:16 +04:00
2003-01-15 20:22:48 +03:00
return max_rid ;
}
2008-05-10 01:22:12 +04:00
static int net_maxrid ( struct net_context * c , int argc , const char * * argv )
2003-01-15 20:22:48 +03:00
{
uint32 rid ;
if ( argc ! = 0 ) {
2003-04-14 07:59:04 +04:00
DEBUG ( 0 , ( " usage: net maxrid \n " ) ) ;
2003-01-15 20:22:48 +03:00
return 1 ;
}
if ( ( rid = get_maxrid ( ) ) = = 0 ) {
DEBUG ( 0 , ( " can't get current maximum rid \n " ) ) ;
return 1 ;
}
d_printf ( " Currently used maximum rid: %d \n " , rid ) ;
return 0 ;
}
2007-07-19 17:07:22 +04:00
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-05-10 01:22:12 +04:00
const char * net_prompt_pass ( struct net_context * c , const char * user )
2007-07-19 17:07:22 +04:00
{
char * prompt = NULL ;
const char * pass = NULL ;
2008-05-10 01:22:12 +04:00
if ( c - > opt_password ) {
return c - > opt_password ;
2007-07-19 17:07:22 +04:00
}
2008-05-10 01:22:12 +04:00
if ( c - > opt_machine_pass ) {
2007-07-19 17:07:22 +04:00
return NULL ;
}
asprintf ( & prompt , " Enter %s's password: " , user ) ;
if ( ! prompt ) {
return NULL ;
}
pass = getpass ( prompt ) ;
SAFE_FREE ( prompt ) ;
return pass ;
}
2001-11-24 16:26:01 +03:00
/* main function table */
static struct functable net_func [ ] = {
2001-12-03 07:39:23 +03:00
{ " RPC " , net_rpc } ,
{ " RAP " , net_rap } ,
2001-11-25 03:18:11 +03:00
{ " ADS " , net_ads } ,
2001-12-11 01:25:21 +03:00
/* eventually these should auto-choose the transport ... */
2002-07-15 14:35:28 +04:00
{ " FILE " , net_file } ,
{ " SHARE " , net_share } ,
2001-12-11 01:25:21 +03:00
{ " SESSION " , net_rap_session } ,
{ " SERVER " , net_rap_server } ,
{ " DOMAIN " , net_rap_domain } ,
{ " PRINTQ " , net_rap_printq } ,
2002-04-04 20:53:07 +04:00
{ " USER " , net_user } ,
2002-07-15 14:35:28 +04:00
{ " GROUP " , net_group } ,
2003-04-19 09:53:55 +04:00
{ " GROUPMAP " , net_groupmap } ,
2006-02-04 01:19:41 +03:00
{ " SAM " , net_sam } ,
2001-12-11 01:25:21 +03:00
{ " VALIDATE " , net_rap_validate } ,
{ " GROUPMEMBER " , net_rap_groupmember } ,
{ " ADMIN " , net_rap_admin } ,
2008-05-08 11:14:16 +04:00
{ " SERVICE " , net_rap_service } ,
2001-12-11 01:25:21 +03:00
{ " PASSWORD " , net_rap_password } ,
2003-04-15 02:27:09 +04:00
{ " CHANGETRUSTPW " , net_changetrustpw } ,
2003-08-29 00:24:25 +04:00
{ " CHANGESECRETPW " , net_changesecretpw } ,
2001-12-11 08:21:50 +03:00
{ " TIME " , net_time } ,
2001-12-20 10:42:00 +03:00
{ " LOOKUP " , net_lookup } ,
2002-03-16 01:04:53 +03:00
{ " JOIN " , net_join } ,
2007-11-30 22:21:37 +03:00
{ " DOM " , net_dom } ,
2002-09-25 19:19:00 +04:00
{ " CACHE " , net_cache } ,
{ " GETLOCALSID " , net_getlocalsid } ,
{ " SETLOCALSID " , net_setlocalsid } ,
2006-05-16 21:40:28 +04:00
{ " SETDOMAINSID " , net_setdomainsid } ,
2002-09-25 19:19:00 +04:00
{ " GETDOMAINSID " , net_getdomainsid } ,
2003-01-15 20:22:48 +03:00
{ " MAXRID " , net_maxrid } ,
2007-06-08 14:15:19 +04:00
{ " IDMAP " , net_idmap } ,
2003-12-01 16:58:43 +03:00
{ " STATUS " , net_status } ,
2006-02-04 01:19:41 +03:00
{ " USERSHARE " , net_usershare } ,
2004-10-12 15:58:01 +04:00
{ " USERSIDLIST " , net_usersidlist } ,
2007-04-09 14:38:55 +04:00
{ " CONF " , net_conf } ,
2008-03-28 03:21:49 +03:00
{ " REGISTRY " , net_registry } ,
2003-09-07 20:36:13 +04:00
# ifdef WITH_FAKE_KASERVER
2005-02-10 20:38:49 +03:00
{ " AFS " , net_afs } ,
2003-09-07 20:36:13 +04:00
# endif
2001-12-11 01:25:21 +03:00
2001-11-24 16:26:01 +03:00
{ " HELP " , net_help } ,
{ NULL , NULL }
} ;
2001-11-13 01:50:48 +03:00
2001-10-19 20:51:26 +04:00
/****************************************************************************
main program
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-11-26 08:59:43 +03:00
int main ( int argc , const char * * argv )
2001-10-19 20:51:26 +04:00
{
2001-11-24 16:26:01 +03:00
int opt , i ;
char * p ;
int rc = 0 ;
int argc_new = 0 ;
const char * * argv_new ;
poptContext pc ;
2008-05-10 01:22:12 +04:00
TALLOC_CTX * frame = talloc_stackframe ( ) ;
struct net_context * c = talloc_zero ( frame , struct net_context ) ;
2001-11-24 16:26:01 +03:00
struct poptOption long_options [ ] = {
2002-03-15 23:03:07 +03:00
{ " help " , ' h ' , POPT_ARG_NONE , 0 , ' h ' } ,
2008-05-10 01:22:12 +04:00
{ " workgroup " , ' w ' , POPT_ARG_STRING , & c - > opt_target_workgroup } ,
{ " user " , ' U ' , POPT_ARG_STRING , & c - > opt_user_name , ' U ' } ,
2002-03-15 23:03:07 +03:00
{ " ipaddress " , ' I ' , POPT_ARG_STRING , 0 , ' I ' } ,
2008-05-10 01:22:12 +04:00
{ " port " , ' p ' , POPT_ARG_INT , & c - > opt_port } ,
{ " myname " , ' n ' , POPT_ARG_STRING , & c - > opt_requester_name } ,
{ " server " , ' S ' , POPT_ARG_STRING , & c - > opt_host } ,
2008-01-05 11:23:35 +03:00
{ " encrypt " , ' e ' , POPT_ARG_NONE , NULL , ' e ' , " Encrypt SMB transport (UNIX extended servers only) " } ,
2008-05-10 01:22:12 +04:00
{ " container " , ' c ' , POPT_ARG_STRING , & c - > opt_container } ,
{ " comment " , ' C ' , POPT_ARG_STRING , & c - > opt_comment } ,
{ " maxusers " , ' M ' , POPT_ARG_INT , & c - > opt_maxusers } ,
{ " flags " , ' F ' , POPT_ARG_INT , & c - > opt_flags } ,
{ " long " , ' l ' , POPT_ARG_NONE , & c - > opt_long_list_entries } ,
{ " reboot " , ' r ' , POPT_ARG_NONE , & c - > opt_reboot } ,
{ " force " , ' f ' , POPT_ARG_NONE , & c - > opt_force } ,
{ " stdin " , ' i ' , POPT_ARG_NONE , & c - > opt_stdin } ,
{ " timeout " , ' t ' , POPT_ARG_INT , & c - > opt_timeout } ,
{ " machine-pass " , ' P ' , POPT_ARG_NONE , & c - > opt_machine_pass } ,
{ " myworkgroup " , ' W ' , POPT_ARG_STRING , & c - > opt_workgroup } ,
{ " verbose " , ' v ' , POPT_ARG_NONE , & c - > opt_verbose } ,
{ " test " , ' T ' , POPT_ARG_NONE , & c - > opt_testmode } ,
2004-02-26 14:29:56 +03:00
/* Options for 'net groupmap set' */
2008-05-10 01:22:12 +04:00
{ " local " , ' L ' , POPT_ARG_NONE , & c - > opt_localgroup } ,
{ " domain " , ' D ' , POPT_ARG_NONE , & c - > opt_domaingroup } ,
{ " ntname " , ' N ' , POPT_ARG_STRING , & c - > opt_newntname } ,
{ " rid " , ' R ' , POPT_ARG_INT , & c - > opt_rid } ,
2004-08-10 18:27:17 +04:00
/* Options for 'net rpc share migrate' */
2008-05-10 01:22:12 +04:00
{ " acls " , 0 , POPT_ARG_NONE , & c - > opt_acls } ,
{ " attrs " , 0 , POPT_ARG_NONE , & c - > opt_attrs } ,
{ " timestamps " , 0 , POPT_ARG_NONE , & c - > opt_timestamps } ,
{ " exclude " , ' X ' , POPT_ARG_STRING , & c - > opt_exclude } ,
{ " destination " , 0 , POPT_ARG_STRING , & c - > opt_destination } ,
{ " tallocreport " , 0 , POPT_ARG_NONE , & c - > do_talloc_report } ,
2004-02-26 14:29:56 +03:00
2003-04-14 07:59:04 +04:00
POPT_COMMON_SAMBA
2001-11-24 16:26:01 +03:00
{ 0 , 0 , 0 , 0 }
} ;
2007-08-31 13:10:54 +04:00
2008-05-10 01:22:12 +04:00
zero_addr ( & c - > opt_dest_ip ) ;
2001-11-24 16:26:01 +03:00
2005-12-29 00:10:11 +03:00
load_case_tables ( ) ;
2003-07-19 04:23:08 +04:00
/* set default debug level to 0 regardless of what smb.conf sets */
DEBUGLEVEL_CLASS [ DBGC_ALL ] = 0 ;
2001-12-20 10:13:47 +03:00
dbf = x_stderr ;
2008-05-08 11:14:16 +04:00
pc = poptGetContext ( NULL , argc , ( const char * * ) argv , long_options ,
2001-11-24 16:26:01 +03:00
POPT_CONTEXT_KEEP_FIRST ) ;
2008-05-08 11:14:16 +04:00
2001-11-24 16:26:01 +03:00
while ( ( opt = poptGetNextOpt ( pc ) ) ! = - 1 ) {
switch ( opt ) {
case ' h ' :
2008-05-10 01:22:12 +04:00
net_help ( c , argc , argv ) ;
2001-11-24 16:26:01 +03:00
exit ( 0 ) ;
break ;
2008-01-05 11:23:35 +03:00
case ' e ' :
2008-05-10 01:22:12 +04:00
c - > smb_encrypt = true ;
2008-01-05 11:23:35 +03:00
break ;
2001-11-24 16:26:01 +03:00
case ' I ' :
2008-05-10 01:22:12 +04:00
if ( ! interpret_string_addr ( & c - > opt_dest_ip ,
2007-10-25 01:16:54 +04:00
poptGetOptArg ( pc ) , 0 ) ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " \n Invalid ip address specified \n " ) ;
2007-10-25 01:16:54 +04:00
} else {
2008-05-10 01:22:12 +04:00
c - > opt_have_ip = true ;
2007-10-25 01:16:54 +04:00
}
2001-11-24 16:26:01 +03:00
break ;
case ' U ' :
2008-05-10 01:22:12 +04:00
c - > opt_user_specified = True ;
c - > opt_user_name = SMB_STRDUP ( c - > opt_user_name ) ;
p = strchr ( c - > opt_user_name , ' % ' ) ;
2001-11-24 16:26:01 +03:00
if ( p ) {
* p = 0 ;
2008-05-10 01:22:12 +04:00
c - > opt_password = p + 1 ;
2001-11-24 16:26:01 +03:00
}
break ;
default :
2008-05-08 11:14:16 +04:00
d_fprintf ( stderr , " \n Invalid option %s: %s \n " ,
2003-01-21 17:08:33 +03:00
poptBadOption ( pc , 0 ) , poptStrerror ( opt ) ) ;
2008-05-10 01:22:12 +04:00
net_help ( c , argc , argv ) ;
2002-07-15 14:35:28 +04:00
exit ( 1 ) ;
2001-11-24 16:26:01 +03:00
}
2001-10-23 18:16:59 +04:00
}
2008-05-08 11:14:16 +04:00
2003-07-19 04:23:08 +04:00
/*
* Don ' t load debug level from smb . conf . It should be
* set by cmdline arg or remain default ( 0 )
*/
2008-05-10 01:22:12 +04:00
c - > AllowDebugChange = false ;
lp_load ( get_dyn_CONFIGFILE ( ) , true , false , false , true ) ;
2008-05-08 11:14:16 +04:00
2003-07-19 04:23:08 +04:00
argv_new = ( const char * * ) poptGetArgs ( pc ) ;
2001-10-19 20:51:26 +04:00
2001-11-24 16:26:01 +03:00
argc_new = argc ;
for ( i = 0 ; i < argc ; i + + ) {
if ( argv_new [ i ] = = NULL ) {
argc_new = i ;
break ;
}
}
2002-07-15 14:35:28 +04:00
2008-05-10 01:22:12 +04:00
if ( c - > do_talloc_report ) {
2006-07-11 22:01:26 +04:00
talloc_enable_leak_report ( ) ;
}
2008-05-10 01:22:12 +04:00
if ( c - > opt_requester_name ) {
set_global_myname ( c - > opt_requester_name ) ;
2001-11-24 16:26:01 +03:00
}
2008-05-10 01:22:12 +04:00
if ( ! c - > opt_user_name & & getenv ( " LOGNAME " ) ) {
c - > opt_user_name = getenv ( " LOGNAME " ) ;
2001-11-24 16:26:01 +03:00
}
2008-05-10 01:22:12 +04:00
if ( ! c - > opt_workgroup ) {
c - > opt_workgroup = smb_xstrdup ( lp_workgroup ( ) ) ;
2001-11-24 16:26:01 +03:00
}
2008-05-08 11:14:16 +04:00
2008-05-10 01:22:12 +04:00
if ( ! c - > opt_target_workgroup ) {
c - > opt_target_workgroup = smb_xstrdup ( lp_workgroup ( ) ) ;
2001-12-04 07:48:01 +03:00
}
2008-05-08 11:14:16 +04:00
2002-11-13 02:20:50 +03:00
if ( ! init_names ( ) )
exit ( 1 ) ;
2001-12-05 14:00:26 +03:00
2001-11-24 16:26:01 +03:00
load_interfaces ( ) ;
2008-05-08 11:14:16 +04:00
/* this makes sure that when we do things like call scripts,
2003-06-14 04:49:02 +04:00
that it won ' t assert becouse we are not root */
sec_init ( ) ;
2001-10-19 20:51:26 +04:00
2008-05-10 01:22:12 +04:00
if ( c - > opt_machine_pass ) {
2002-07-15 14:35:28 +04:00
/* it is very useful to be able to make ads queries as the
machine account for testing purposes and for domain leave */
2008-05-10 01:22:12 +04:00
net_use_krb_machine_account ( c ) ;
2002-07-15 14:35:28 +04:00
}
2003-04-14 07:59:04 +04:00
2008-05-10 01:22:12 +04:00
if ( ! c - > opt_password ) {
c - > opt_password = getenv ( " PASSWD " ) ;
2003-04-14 07:59:04 +04:00
}
2008-05-08 11:14:16 +04:00
2008-05-10 01:22:12 +04:00
rc = net_run_function ( c , argc_new - 1 , argv_new + 1 , net_func , net_help ) ;
2008-05-08 11:14:16 +04:00
2001-11-24 16:26:01 +03:00
DEBUG ( 2 , ( " return code = %d \n " , rc ) ) ;
2008-04-16 04:16:08 +04:00
2008-05-10 01:22:12 +04:00
libnetapi_free ( c - > netapi_ctx ) ;
2008-04-16 04:16:08 +04:00
2007-08-31 13:10:54 +04:00
TALLOC_FREE ( frame ) ;
2001-11-24 16:26:01 +03:00
return rc ;
}