2003-08-13 01:53:07 +00:00
/*
Samba Unix / Linux SMB client library
Distributed SMB / CIFS Server Management Utility
Copyright ( C ) 2001 Steve French ( sfrench @ us . ibm . com )
Copyright ( C ) 2001 Jim McDonough ( jmcd @ us . ibm . com )
Copyright ( C ) 2001 Andrew Tridgell ( tridge @ samba . org )
Copyright ( C ) 2001 Andrew Bartlett ( abartlet @ samba . org )
2004-08-18 09:33:54 +00:00
Copyright ( C ) 2004 Stefan Metzmacher ( metze @ samba . org )
Largely rewritten by metze in August 2004
2003-08-13 01:53:07 +00:00
Originally written by Steve and Jim . Largely rewritten by tridge in
November 2001.
Reworked again by abartlet in December 2001
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-10 02:07:03 +00:00
the Free Software Foundation ; either version 3 of the License , or
2003-08-13 01:53:07 +00:00
( at your option ) any later version .
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
2007-07-10 02:07:03 +00:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2004-08-18 09:33:54 +00:00
*/
2003-08-13 01:53:07 +00: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). */
/* */
/*****************************************************/
# include "includes.h"
2004-11-02 11:42:35 +00:00
# include "utils/net/net.h"
2004-11-02 02:57:18 +00:00
# include "lib/cmdline/popt_common.h"
2006-03-06 23:34:57 +00:00
# include "lib/ldb/include/ldb.h"
2006-03-18 15:42:57 +00:00
# include "librpc/rpc/dcerpc.h"
2007-12-04 19:33:00 +01:00
# include "param/param.h"
2008-04-14 12:43:37 -04:00
# include "lib/events/events.h"
# include "auth/credentials/credentials.h"
2003-08-13 01:53:07 +00:00
/*
run a function from a function table . If not found then
call the specified usage function
*/
2004-08-18 09:33:54 +00:00
int net_run_function ( struct net_context * ctx ,
int argc , const char * * argv ,
const struct net_functable * functable ,
2004-08-18 12:47:08 +00:00
int ( * usage_fn ) ( struct net_context * ctx , int argc , const char * * argv ) )
2003-08-13 01:53:07 +00:00
{
int i ;
2004-08-18 12:47:08 +00:00
2005-02-16 21:51:37 +00:00
if ( argc = = 0 ) {
2004-08-18 12:47:08 +00:00
return usage_fn ( ctx , argc , argv ) ;
2005-02-16 21:51:37 +00:00
} else if ( argc = = 1 & & strequal ( argv [ 0 ] , " help " ) ) {
return net_help ( ctx , functable ) ;
2003-08-13 01:53:07 +00:00
}
2004-08-18 12:47:08 +00:00
2004-08-18 09:33:54 +00:00
for ( i = 0 ; functable [ i ] . name ; i + + ) {
2005-08-30 11:55:05 +00:00
if ( strcasecmp_m ( argv [ 0 ] , functable [ i ] . name ) = = 0 )
2004-08-18 09:33:54 +00:00
return functable [ i ] . fn ( ctx , argc - 1 , argv + 1 ) ;
2003-08-13 01:53:07 +00:00
}
2004-08-18 12:47:08 +00:00
2003-08-13 01:53:07 +00:00
d_printf ( " No command: %s \n " , argv [ 0 ] ) ;
2004-08-18 12:47:08 +00:00
return usage_fn ( ctx , argc , argv ) ;
2003-08-13 01:53:07 +00:00
}
/*
2004-08-18 12:47:08 +00:00
run a usage function from a function table . If not found then fail
*/
int net_run_usage ( struct net_context * ctx ,
int argc , const char * * argv ,
const struct net_functable * functable )
{
int i ;
2006-05-07 13:40:56 +00:00
2004-08-18 12:47:08 +00:00
for ( i = 0 ; functable [ i ] . name ; i + + ) {
2005-08-30 11:55:05 +00:00
if ( strcasecmp_m ( argv [ 0 ] , functable [ i ] . name ) = = 0 )
2004-08-18 12:47:08 +00:00
if ( functable [ i ] . usage ) {
return functable [ i ] . usage ( ctx , argc - 1 , argv + 1 ) ;
}
}
2005-02-16 21:51:37 +00:00
d_printf ( " No usage information for command: %s \n " , argv [ 0 ] ) ;
2004-08-18 12:47:08 +00:00
return 1 ;
}
2005-02-16 21:51:37 +00:00
/* main function table */
static const struct net_functable net_functable [ ] = {
{ " password " , " change password \n " , net_password , net_password_usage } ,
{ " time " , " get remote server's time \n " , net_time , net_time_usage } ,
{ " join " , " join a domain \n " , net_join , net_join_usage } ,
2005-05-02 14:17:19 +00:00
{ " samdump " , " dump the sam of a domain \n " , net_samdump , net_samdump_usage } ,
2008-04-09 14:59:32 +10:00
{ " vampire " , " join and syncronise an AD domain onto the local server \n " , net_vampire , net_vampire_usage } ,
{ " samsync " , " synchronise into the local ldb the sam of an NT4 domain \n " , net_samsync_ldb , net_samsync_ldb_usage } ,
2005-02-16 21:51:37 +00:00
{ " user " , " manage user accounts \n " , net_user , net_user_usage } ,
{ NULL , NULL , NULL , NULL }
} ;
2004-08-18 12:47:08 +00:00
2005-02-16 21:51:37 +00:00
int net_help ( struct net_context * ctx , const struct net_functable * ftable )
{
int i = 0 ;
const char * name = ftable [ i ] . name ;
const char * desc = ftable [ i ] . desc ;
d_printf ( " Available commands: \n " ) ;
while ( name & & desc ) {
d_printf ( " \t %s \t \t %s " , name , desc ) ;
name = ftable [ + + i ] . name ;
desc = ftable [ i ] . desc ;
2003-08-13 01:53:07 +00:00
}
2004-08-18 12:47:08 +00:00
return 0 ;
2003-08-13 01:53:07 +00:00
}
2004-08-18 12:47:08 +00:00
static int net_usage ( struct net_context * ctx , int argc , const char * * argv )
2004-08-18 09:33:54 +00:00
{
2005-02-16 21:51:37 +00:00
d_printf ( " Usage: \n " ) ;
d_printf ( " net <command> [options] \n " ) ;
return 0 ;
2004-08-18 09:33:54 +00:00
}
2003-08-13 01:53:07 +00:00
/****************************************************************************
main program
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-08-18 09:33:54 +00:00
static int binary_net ( int argc , const char * * argv )
2003-08-13 01:53:07 +00:00
{
int opt , i ;
2004-08-18 09:33:54 +00:00
int rc ;
int argc_new ;
const char * * argv_new ;
2008-04-14 12:43:37 -04:00
struct event_context * ev ;
2004-12-02 04:51:56 +00:00
struct net_context * ctx = NULL ;
2003-08-13 01:53:07 +00:00
poptContext pc ;
2004-08-18 13:00:28 +00:00
struct poptOption long_options [ ] = {
2004-08-19 12:24:58 +00:00
POPT_AUTOHELP
POPT_COMMON_SAMBA
POPT_COMMON_CONNECTION
POPT_COMMON_CREDENTIALS
POPT_COMMON_VERSION
2006-09-06 12:28:01 +00:00
{ NULL }
2004-08-18 13:00:28 +00:00
} ;
2004-08-18 09:33:54 +00:00
2006-09-10 14:19:38 +00:00
setlinebuf ( stdout ) ;
2004-08-18 09:33:54 +00:00
2004-08-19 12:24:58 +00:00
pc = poptGetContext ( " net " , argc , ( const char * * ) argv , long_options ,
2005-06-13 08:12:39 +00:00
POPT_CONTEXT_KEEP_FIRST ) ;
2004-08-18 09:33:54 +00:00
2003-08-13 01:53:07 +00:00
while ( ( opt = poptGetNextOpt ( pc ) ) ! = - 1 ) {
switch ( opt ) {
default :
2004-08-18 09:33:54 +00:00
d_printf ( " Invalid option %s: %s \n " ,
2003-08-13 01:53:07 +00:00
poptBadOption ( pc , 0 ) , poptStrerror ( opt ) ) ;
2005-02-16 21:51:37 +00:00
net_usage ( ctx , argc , argv ) ;
2003-08-13 01:53:07 +00:00
exit ( 1 ) ;
}
}
argv_new = ( const char * * ) poptGetArgs ( pc ) ;
argc_new = argc ;
for ( i = 0 ; i < argc ; i + + ) {
if ( argv_new [ i ] = = NULL ) {
argc_new = i ;
break ;
}
}
2004-08-18 09:33:54 +00:00
if ( argc_new < 2 ) {
2005-02-16 21:51:37 +00:00
return net_usage ( ctx , argc , argv ) ;
2003-08-13 01:53:07 +00:00
}
2005-12-30 22:46:16 +00:00
dcerpc_init ( ) ;
2004-11-09 09:26:47 +00:00
2008-06-14 13:00:53 -04:00
ev = s4_event_context_init ( NULL ) ;
2008-04-14 12:43:37 -04:00
if ( ! ev ) {
d_printf ( " Failed to create an event context \n " ) ;
exit ( 1 ) ;
}
ctx = talloc ( ev , struct net_context ) ;
2004-08-19 12:36:05 +00:00
if ( ! ctx ) {
2008-04-14 12:43:37 -04:00
d_printf ( " Failed to talloc a net_context \n " ) ;
2004-08-19 12:36:05 +00:00
exit ( 1 ) ;
}
ZERO_STRUCTP ( ctx ) ;
2007-12-10 04:33:16 +01:00
ctx - > lp_ctx = cmdline_lp_ctx ;
2005-03-21 02:08:38 +00:00
ctx - > credentials = cmdline_credentials ;
2008-04-16 22:30:15 +02:00
ctx - > event_ctx = ev ;
2004-08-19 12:36:05 +00:00
2004-08-18 12:47:08 +00:00
rc = net_run_function ( ctx , argc_new - 1 , argv_new + 1 , net_functable , net_usage ) ;
2003-08-13 01:53:07 +00:00
2004-08-18 09:33:54 +00:00
if ( rc ! = 0 ) {
DEBUG ( 0 , ( " return code = %d \n " , rc ) ) ;
2003-08-13 01:53:07 +00:00
}
2004-08-18 12:47:08 +00:00
2008-04-14 12:43:37 -04:00
talloc_free ( ev ) ;
2003-08-13 01:53:07 +00:00
return rc ;
}
2004-08-18 09:33:54 +00:00
int main ( int argc , const char * * argv )
{
return binary_net ( argc , argv ) ;
}