@ -83,7 +83,7 @@ static struct IP4ACCESS *ipaccess = NULL;
# define SOCK_REC_BUFSZ 1024
# define SOCK_REC_BUFSZ 1024
# define QUEUE 10
# define QUEUE 10
//#define ALLIP4 "0.0.0.0"
# define ALLIP4 "0.0.0.0"
static const char * localaddr = " 127.0.0.1 " ;
static const char * localaddr = " 127.0.0.1 " ;
static const char * UNAVAILABLE = " - API will not be available " ;
static const char * UNAVAILABLE = " - API will not be available " ;
static const char * MUNAVAILABLE = " - API multicast listener will not be available " ;
static const char * MUNAVAILABLE = " - API multicast listener will not be available " ;
@ -503,8 +503,10 @@ static char *gethelp(char *params)
{
{
* buffer = ' \0 ' ;
* buffer = ' \0 ' ;
char * p = buffer ;
char * p = buffer ;
for ( int i = 0 ; i < CMDMAX - 1 ; i + + )
for ( int i = 0 ; i < CMDMAX - 1 ; i + + ) {
p + = sprintf ( p , " %s \n " , cmds [ i ] . name ) ;
bool displayed = ! cmds [ i ] . iswritemode | | opt_api_allow ;
if ( displayed ) p + = sprintf ( p , " %s \n " , cmds [ i ] . name ) ;
}
sprintf ( p , " | " ) ;
sprintf ( p , " | " ) ;
return buffer ;
return buffer ;
}
}
@ -673,15 +675,14 @@ static int websocket_handshake(SOCKETTYPE c, char *result, char *clientkey)
static void setup_groups ( )
static void setup_groups ( )
{
{
const char * api_groups = opt_api_groups ? opt_api_groups : " " ;
const char * api_groups = opt_api_groups ? opt_api_groups : " " ;
char * buf , * ptr , * next , * colon ;
char * buf , * cmd , * ptr , * next , * colon ;
char commands [ 512 ] = { 0 } ;
char cmdbuf [ 128 ] = { 0 } ;
char group ;
char group ;
char commands [ 512 ] ;
bool addstar ;
char cmdbuf [ 100 ] ;
char * cmd ;
bool addstar , did ;
int i ;
int i ;
buf = ( char * ) malloc ( strlen ( api_groups ) + 1 ) ;
buf = ( char * ) calloc ( 1 , strlen ( api_groups ) + 1 ) ;
if ( unlikely ( ! buf ) )
if ( unlikely ( ! buf ) )
proper_exit ( 1 ) ; //, "Failed to malloc ipgroups buf");
proper_exit ( 1 ) ; //, "Failed to malloc ipgroups buf");
@ -731,8 +732,8 @@ static void setup_groups()
if ( strcmp ( ptr , " * " ) = = 0 )
if ( strcmp ( ptr , " * " ) = = 0 )
addstar = true ;
addstar = true ;
else {
else {
did = false ;
bool did = false ;
for ( i = 0 ; cmds [ i ] . name ! = NULL ; i + + ) {
for ( i = 0 ; i < CMDMAX - 1 ; i + + ) {
if ( strcasecmp ( ptr , cmds [ i ] . name ) = = 0 ) {
if ( strcasecmp ( ptr , cmds [ i ] . name ) = = 0 ) {
did = true ;
did = true ;
break ;
break ;
@ -757,7 +758,7 @@ static void setup_groups()
// * = allow all non-iswritemode commands
// * = allow all non-iswritemode commands
if ( addstar ) {
if ( addstar ) {
for ( i = 0 ; cmds [ i ] . name ! = NULL ; i + + ) {
for ( i = 0 ; i < CMDMAX - 1 ; i + + ) {
if ( cmds [ i ] . iswritemode = = false ) {
if ( cmds [ i ] . iswritemode = = false ) {
// skip duplicates
// skip duplicates
sprintf ( cmdbuf , " |%s| " , cmds [ i ] . name ) ;
sprintf ( cmdbuf , " |%s| " , cmds [ i ] . name ) ;
@ -771,7 +772,7 @@ static void setup_groups()
}
}
}
}
ptr = apigroups [ GROUPOFFSET ( group ) ] . commands = ( char * ) malloc ( strlen ( commands ) + 1 ) ;
ptr = apigroups [ GROUPOFFSET ( group ) ] . commands = ( char * ) calloc ( 1 , strlen ( commands ) + 1 ) ;
if ( unlikely ( ! ptr ) )
if ( unlikely ( ! ptr ) )
proper_exit ( 1 ) ; //, "Failed to malloc group commands buf");
proper_exit ( 1 ) ; //, "Failed to malloc group commands buf");
@ -782,7 +783,7 @@ static void setup_groups()
cmd = & ( commands [ 0 ] ) ;
cmd = & ( commands [ 0 ] ) ;
* ( cmd + + ) = ' | ' ;
* ( cmd + + ) = ' | ' ;
* cmd = ' \0 ' ;
* cmd = ' \0 ' ;
for ( i = 0 ; cmds [ i ] . name ! = NULL ; i + + ) {
for ( i = 0 ; i < CMDMAX - 1 ; i + + ) {
if ( cmds [ i ] . iswritemode = = false ) {
if ( cmds [ i ] . iswritemode = = false ) {
strcpy ( cmd , cmds [ i ] . name ) ;
strcpy ( cmd , cmds [ i ] . name ) ;
cmd + = strlen ( cmds [ i ] . name ) ;
cmd + = strlen ( cmds [ i ] . name ) ;
@ -791,7 +792,7 @@ static void setup_groups()
}
}
}
}
ptr = apigroups [ GROUPOFFSET ( NOPRIVGROUP ) ] . commands = ( char * ) malloc ( strlen ( commands ) + 1 ) ;
ptr = apigroups [ GROUPOFFSET ( NOPRIVGROUP ) ] . commands = ( char * ) calloc ( 1 , strlen ( commands ) + 1 ) ;
if ( unlikely ( ! ptr ) )
if ( unlikely ( ! ptr ) )
proper_exit ( 1 ) ; //, "Failed to malloc noprivgroup commands buf");
proper_exit ( 1 ) ; //, "Failed to malloc noprivgroup commands buf");
@ -857,7 +858,7 @@ static void setup_ipaccess()
ipaccess [ ips ] . group = group ;
ipaccess [ ips ] . group = group ;
if ( strcmp ( ptr , ALLIPS ) = = 0 )
if ( strcmp ( ptr , ALLIPS ) = = 0 | | strcmp ( ptr , ALLIP4 ) = = 0 )
ipaccess [ ips ] . ip = ipaccess [ ips ] . mask = 0 ;
ipaccess [ ips ] . ip = ipaccess [ ips ] . mask = 0 ;
else
else
{
{
@ -920,6 +921,8 @@ static bool check_connect(struct sockaddr_in *cli, char **connectaddr, char *gro
}
}
}
}
}
}
else if ( strcmp ( opt_api_bind , ALLIP4 ) = = 0 )
addrok = true ;
else
else
addrok = ( strcmp ( * connectaddr , localaddr ) = = 0 ) ;
addrok = ( strcmp ( * connectaddr , localaddr ) = = 0 ) ;
@ -995,13 +998,13 @@ static void mcast()
}
}
expect_code_len = sizeof ( expect ) + strlen ( opt_api_mcast_code ) ;
expect_code_len = sizeof ( expect ) + strlen ( opt_api_mcast_code ) ;
expect_code = ( char * ) malloc ( expect_code_len + 1 ) ;
expect_code = ( char * ) calloc ( 1 , expect_code_len + 1 ) ;
if ( ! expect_code )
if ( ! expect_code )
proper_exit ( 1 ) ; //, "Failed to malloc mcast expect_code");
proper_exit ( 1 ) ; //, "Failed to malloc mcast expect_code");
snprintf ( expect_code , expect_code_len + 1 , " %s%s- " , expect , opt_api_mcast_code ) ;
snprintf ( expect_code , expect_code_len + 1 , " %s%s- " , expect , opt_api_mcast_code ) ;
count = 0 ;
count = 0 ;
while ( 80085 ) {
while ( 42 ) {
sleep ( 1 ) ;
sleep ( 1 ) ;
count + + ;
count + + ;
@ -1016,8 +1019,9 @@ static void mcast()
addrok = check_connect ( & came_from , & connectaddr , & group ) ;
addrok = check_connect ( & came_from , & connectaddr , & group ) ;
applog ( LOG_DEBUG , " API mcast from %s - %s " ,
applog ( LOG_DEBUG , " API mcast from %s - %s " ,
connectaddr , addrok ? " Accepted " : " Ignored " ) ;
connectaddr , addrok ? " Accepted " : " Ignored " ) ;
if ( ! addrok )
if ( ! addrok ) {
continue ;
continue ;
}
buf [ rep ] = ' \0 ' ;
buf [ rep ] = ' \0 ' ;
if ( rep > 0 & & buf [ rep - 1 ] = = ' \n ' )
if ( rep > 0 & & buf [ rep - 1 ] = = ' \n ' )
@ -1044,14 +1048,12 @@ static void mcast()
snprintf ( replybuf , sizeof ( replybuf ) ,
snprintf ( replybuf , sizeof ( replybuf ) ,
" ccm-%s-%d-%s " , opt_api_mcast_code , opt_api_port , opt_api_mcast_des ) ;
" ccm-%s-%d-%s " , opt_api_mcast_code , opt_api_port , opt_api_mcast_des ) ;
rep = sendto ( reply_sock , replybuf , strlen ( replybuf ) + 1 ,
rep = sendto ( reply_sock , replybuf , ( int ) strlen ( replybuf ) + 1 ,
0 , ( struct sockaddr * ) ( & came_from ) ,
0 , ( struct sockaddr * ) ( & came_from ) , ( int ) sizeof ( came_from ) ) ;
sizeof ( came_from ) ) ;
if ( SOCKETFAIL ( rep ) ) {
if ( SOCKETFAIL ( rep ) ) {
applog ( LOG_DEBUG , " API mcast send reply failed (%s) (%d) " ,
applog ( LOG_DEBUG , " API mcast send reply failed (%s) (%d) " ,
strerror ( errno ) , ( int ) reply_sock ) ;
strerror ( errno ) , ( int ) reply_sock ) ;
}
} else {
else {
applog ( LOG_DEBUG , " API mcast send reply (%s) succeeded (%d) (%d) " ,
applog ( LOG_DEBUG , " API mcast send reply (%s) succeeded (%d) (%d) " ,
replybuf , ( int ) rep , ( int ) reply_sock ) ;
replybuf , ( int ) rep , ( int ) reply_sock ) ;
}
}
@ -1064,7 +1066,6 @@ static void mcast()
}
}
die :
die :
CLOSESOCKET ( mcast_sock ) ;
CLOSESOCKET ( mcast_sock ) ;
}
}
@ -1211,14 +1212,12 @@ static void api()
return ;
return ;
}
}
if ( opt_api_allow )
if ( opt_api_allow & & strcmp ( opt_api_bind , " 127.0.0.1 " ) = = 0 )
applog ( LOG_WARNING , " API running in IP access mode on port %d (%d) " , port , ( int ) * apisock ) ;
applog ( LOG_WARNING , " API open locally in full access mode on port %d " , opt_api_port ) ;
else {
else if ( opt_api_allow )
if ( strcmp ( opt_api_bind , " 127.0.0.1 " ) )
applog ( LOG_WARNING , " API open in full access mode to %s on port %d " , opt_api_allow , opt_api_port ) ;
applog ( LOG_WARNING , " API running in UNRESTRICTED read access mode on port %d (%d) " , port , ( int ) * apisock ) ;
else if ( strcmp ( opt_api_bind , " 127.0.0.1 " ) ! = 0 )
else
applog ( LOG_INFO , " API open to the network in read-only mode on port %d " , opt_api_port ) ;
applog ( LOG_WARNING , " API running in local read access mode on port %d (%d) " , port , ( int ) * apisock ) ;
}
if ( opt_api_mcast )
if ( opt_api_mcast )
mcast_init ( ) ;
mcast_init ( ) ;
@ -1309,8 +1308,8 @@ static void api()
break ;
break ;
}
}
}
}
CLOSESOCKET ( c ) ;
}
}
CLOSESOCKET ( c ) ;
}
}
}
}