1999-04-23 15:01:35 +00:00
/*
Unix SMB / Netbios implementation .
Version 2.1 .
MSRPC client : scheduler service
Copyright ( C ) Matthew Chapman 1999
Copyright ( C ) Luke Kenneth Casson Leighton 1996 - 1999
Copyright ( C ) Andrew Tridgell 1994 - 1999
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 .
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 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# ifdef SYSLOG
# undef SYSLOG
# endif
# include "includes.h"
# include "nterr.h"
extern int DEBUGLEVEL ;
# define DEBUG_TESTING
extern struct cli_state * smb_cli ;
extern FILE * out_hnd ;
/****************************************************************************
checks for a / OPTION : param style option
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static BOOL checkopt ( char * input , char * optname , char * * params )
{
char * inend ;
if ( * input + + ! = ' / ' )
return False ;
for ( inend = input ; * inend ! = 0 & & * inend ! = ' : ' ; inend + + ) ;
if ( params ! = NULL )
{
* inend = 0 ;
* params = inend ;
}
while ( input < inend )
{
if ( toupper ( * input + + ) ! = * optname + + )
return False ;
}
return True ;
}
1999-05-07 18:20:59 +00:00
extern char * daynames_short [ ] ;
extern char * daynames [ ] ;
1999-04-23 15:01:35 +00:00
/****************************************************************************
parses a list of days of the week and month
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static BOOL at_parse_days ( char * str , uint32 * monthdays , uint8 * weekdays )
{
char * tok ;
char * nexttok = str ;
int day ;
do {
tok = nexttok ;
if ( ( nexttok = strchr ( tok , ' , ' ) ) ! = NULL )
{
* nexttok + + = 0 ;
}
1999-05-01 05:56:55 +00:00
if ( isdigit ( ( int ) * tok ) )
1999-04-23 15:01:35 +00:00
{
day = strtol ( tok , NULL , 10 ) ;
if ( day = = 0 | | day > 31 )
{
printf ( " \t Invalid day of month. \n \n " ) ;
return False ;
}
* monthdays | = ( 1 < < ( day - 1 ) ) ;
}
else
{
if ( strlen ( tok ) < 3 )
{
for ( day = 0 ; day < 7 ; day + + )
{
if ( ! strcasecmp ( tok , daynames_short [ day ] ) )
break ;
}
}
else
{
for ( day = 0 ; day < 7 ; day + + )
{
if ( ! strncasecmp ( tok , daynames [ day ] , 3 ) )
break ;
}
}
if ( day < 7 )
{
* weekdays | = ( 1 < < day ) ;
}
else
{
printf ( " \t Invalid day of week \n \n " ) ;
return False ;
}
}
} while ( nexttok ! = NULL ) ;
return True ;
}
1999-05-01 05:56:55 +00:00
# define SOON_OFFSET 2 /* seconds */
/****************************************************************************
schedule the job ' soon '
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static BOOL at_soon ( char * dest_srv , uint32 * hours , uint32 * minutes , uint32 * seconds )
{
uint16 nt_pipe_fnum ;
TIME_OF_DAY_INFO tod ;
BOOL res = True ;
/* open srvsvc session. */
res = res ? cli_nt_session_open ( smb_cli , PIPE_SRVSVC , & nt_pipe_fnum ) : False ;
/* enumerate files on server */
res = res ? do_srv_net_remote_tod ( smb_cli , nt_pipe_fnum ,
dest_srv , & tod ) : False ;
/* Close the session */
cli_nt_session_close ( smb_cli , nt_pipe_fnum ) ;
if ( res )
{
* hours = ( tod . hours - ( ( int ) tod . zone / 60 ) ) % 24 ;
* minutes = tod . mins ;
* seconds = ( tod . secs + SOON_OFFSET ) % 60 ;
return True ;
}
return False ;
}
1999-04-23 15:01:35 +00:00
/****************************************************************************
scheduler add job
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1999-11-22 19:02:39 +00:00
void cmd_at ( struct client_info * info , int argc , char * argv [ ] )
1999-04-23 15:01:35 +00:00
{
uint16 nt_pipe_fnum ;
fstring dest_wks ;
BOOL add = False ;
BOOL del = False ;
char * p ;
uint32 jobid = - 1 ;
unsigned int hours , minutes , seconds = 0 ;
uint32 monthdays = 0 ;
uint8 weekdays = 0 ;
uint8 flags = JOB_NONINTERACTIVE ;
1999-07-21 00:30:18 +00:00
pstring command ;
1999-04-23 15:01:35 +00:00
1999-11-22 19:46:26 +00:00
while ( argc > 1 )
1999-04-23 15:01:35 +00:00
{
1999-11-22 19:46:26 +00:00
argc - - ;
argv + + ;
if ( checkopt ( argv [ 0 ] , " DELETE " , NULL ) )
1999-04-23 15:01:35 +00:00
{
del = True ;
continue ;
}
1999-11-22 19:46:26 +00:00
else if ( checkopt ( argv [ 0 ] , " YES " , NULL ) )
1999-04-23 15:01:35 +00:00
{
/* Compatibility */
continue ;
}
1999-11-22 19:46:26 +00:00
jobid = strtol ( argv [ 0 ] , & p , 10 ) ;
1999-04-23 15:01:35 +00:00
if ( * p = = 0 ) /* Entirely numeric field */
continue ;
1999-11-22 19:46:26 +00:00
if ( ! strcasecmp ( argv [ 0 ] , " NOW " ) )
1999-04-23 15:01:35 +00:00
{
1999-05-01 05:56:55 +00:00
if ( ! at_soon ( dest_wks , & hours , & minutes , & seconds ) )
{
return ;
}
1999-04-23 15:01:35 +00:00
}
1999-11-22 19:46:26 +00:00
else if ( sscanf ( argv [ 0 ] , " %d:%d:%d " , & hours , & minutes , & seconds ) > = 2 )
1999-04-23 15:01:35 +00:00
{
1999-11-22 19:46:26 +00:00
p = strchr ( argv [ 0 ] , 0 ) ;
1999-04-23 15:01:35 +00:00
1999-05-01 05:56:55 +00:00
if ( ! strcasecmp ( p - 2 , " AM " ) )
{
hours = ( hours = = 12 ) ? 0 : hours ;
}
1999-04-23 15:01:35 +00:00
1999-05-01 05:56:55 +00:00
if ( ! strcasecmp ( p - 2 , " PM " ) )
{
hours = ( hours = = 12 ) ? 12 : hours + 12 ;
}
if ( hours > 23 | | minutes > 59 | | seconds > 59 )
{
printf ( " \t Invalid time. \n \n " ) ;
return ;
}
}
else
1999-04-23 15:01:35 +00:00
{
1999-05-01 05:56:55 +00:00
printf ( " at { {time | NOW} [/INTERACTIVE] [{/EVERY|/NEXT}:5,Sun,...] command \n \t | [/DEL] [jobid] } \n \n " ) ;
1999-04-23 15:01:35 +00:00
return ;
}
add = True ;
command [ 0 ] = 0 ;
p = NULL ;
1999-11-22 19:46:26 +00:00
if ( argc < = 1 ) break ;
argc - - ;
argv + + ;
1999-04-23 15:01:35 +00:00
1999-11-22 19:46:26 +00:00
if ( checkopt ( argv [ 0 ] , " INTERACTIVE " , NULL ) )
1999-04-23 15:01:35 +00:00
{
flags & = ~ JOB_NONINTERACTIVE ;
1999-11-22 19:46:26 +00:00
if ( argc < = 1 ) break ;
argc - - ;
argv + + ;
1999-04-23 15:01:35 +00:00
}
1999-11-22 19:46:26 +00:00
if ( checkopt ( argv [ 0 ] , " EVERY " , & p ) )
1999-04-23 15:01:35 +00:00
{
flags | = JOB_PERIODIC ;
}
else
{
1999-11-22 19:46:26 +00:00
checkopt ( argv [ 0 ] , " NEXT " , & p ) ;
1999-04-23 15:01:35 +00:00
}
if ( p ! = NULL )
{
if ( * p = = ' : ' )
{
if ( ! at_parse_days ( p , & monthdays , & weekdays ) )
return ;
}
else
{
weekdays = 0x7F ;
}
1999-11-22 19:46:26 +00:00
if ( argc < = 1 ) break ;
argc - - ;
argv + + ;
1999-04-23 15:01:35 +00:00
}
while ( True )
{
1999-11-22 19:46:26 +00:00
safe_strcat ( command , argv [ 0 ] , sizeof ( command ) ) ;
1999-04-23 15:01:35 +00:00
1999-11-22 19:46:26 +00:00
if ( argc < = 1 ) break ;
argc - - ;
argv + + ;
1999-04-23 15:01:35 +00:00
1999-07-21 00:30:18 +00:00
safe_strcat ( command , " " , sizeof ( command ) ) ;
1999-04-23 15:01:35 +00:00
}
break ;
}
if ( add & & ! command [ 0 ] )
{
printf ( " \t No command specified. \n \n " ) ;
return ;
}
1999-07-21 00:30:18 +00:00
safe_strcpy ( dest_wks , " \\ \\ " , sizeof ( dest_wks ) ) ;
safe_strcat ( dest_wks , info - > dest_host , sizeof ( dest_wks ) ) ;
1999-04-23 15:01:35 +00:00
strupper ( dest_wks ) ;
/* open scheduler session. */
if ( ! cli_nt_session_open ( smb_cli , PIPE_ATSVC , & nt_pipe_fnum ) )
return ;
if ( add ) /* add job */
{
AT_JOB_INFO job ;
job . time = ( ( ( ( hours * 60 ) + minutes ) * 60 ) + seconds ) * 1000 ;
job . monthdays = monthdays ;
job . weekdays = weekdays ;
job . flags = flags ;
job . ptr_command = 1 ;
display_at_job_info ( out_hnd , ACTION_HEADER , & job , command ) ;
display_at_job_info ( out_hnd , ACTION_ENUMERATE , & job , command ) ;
display_at_job_info ( out_hnd , ACTION_FOOTER , & job , command ) ;
if ( at_add_job ( smb_cli , nt_pipe_fnum , dest_wks , & job ,
command , & jobid ) )
{
fprintf ( out_hnd , " \t Job ID: %d \n \n " , jobid ) ;
}
}
else if ( del ) /* delete */
{
if ( jobid = = - 1 )
{
fprintf ( out_hnd , " \t Deleting all jobs. \n \n " ) ;
at_del_job ( smb_cli , nt_pipe_fnum , dest_wks ,
0 , 0xffffffff ) ;
}
else
{
fprintf ( out_hnd , " \t Deleting job %d. \n \n " , jobid ) ;
at_del_job ( smb_cli , nt_pipe_fnum , dest_wks ,
jobid , jobid ) ;
}
}
else if ( jobid = = - 1 ) /* enumerate */
{
AT_ENUM_INFO jobs [ AT_MAX_JOBS ] ;
fstring commands [ AT_MAX_JOBS ] ;
uint32 num_jobs ;
if ( at_enum_jobs ( smb_cli , nt_pipe_fnum , dest_wks , & num_jobs ,
jobs , commands ) )
{
display_at_enum_info ( out_hnd , ACTION_HEADER , num_jobs , jobs , commands ) ;
display_at_enum_info ( out_hnd , ACTION_ENUMERATE , num_jobs , jobs , commands ) ;
display_at_enum_info ( out_hnd , ACTION_FOOTER , num_jobs , jobs , commands ) ;
}
}
else /* job info */
{
AT_JOB_INFO job ;
if ( at_query_job ( smb_cli , nt_pipe_fnum , dest_wks , jobid , & job , command ) )
{
display_at_job_info ( out_hnd , ACTION_HEADER , & job , command ) ;
display_at_job_info ( out_hnd , ACTION_ENUMERATE , & job , command ) ;
display_at_job_info ( out_hnd , ACTION_FOOTER , & job , command ) ;
}
}
/* close the session */
cli_nt_session_close ( smb_cli , nt_pipe_fnum ) ;
}