added a debug_handler for verbose output
authorerickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Mon, 21 Feb 2005 17:49:06 +0000 (17:49 +0000)
committererickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Mon, 21 Feb 2005 17:49:06 +0000 (17:49 +0000)
added a utility method for extracting the domain of a jid
add trusted server domains to the router.  now any router level commands will only be
fulfilled if the sender is in a trusted server domain.  more testing to follow

git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@92 9efc2488-bf62-4759-914b-345cdb29e865

include/opensrf/generic_utils.h
include/opensrf/transport_message.h
src/libtransport/generic_utils.c
src/libtransport/transport_message.c
src/router/router.c
src/router/router.h

index 83c52f3..f6e39ba 100644 (file)
@@ -55,6 +55,7 @@ void get_timestamp( char buf_25chars[]);
 void fatal_handler( char* message, ...);
 void warning_handler( char* message, ... );
 void info_handler( char* message, ... );
+void debug_handler( char* message, ... );
 
 // ---------------------------------------------------------------------------------
 // Config file module
index ab20f5d..a1f7836 100644 (file)
@@ -85,6 +85,10 @@ void jid_get_username( const char* jid, char buf[] );
 // ---------------------------------------------------------------------------------
 void jid_get_resource( const char* jid, char buf[] );
 
+/** Puts the domain portion of the given jid into the pre-allocated buffer */
+void jid_get_domain( const char* jid, char buf[] );
+
 void set_msg_error( transport_message*, char* error_type, int error_code);
 
+
 #endif
index e6c9a13..6fb1722 100644 (file)
@@ -9,6 +9,7 @@ int balance = 0;
 #define LOG_ERROR 1
 #define LOG_WARNING 2
 #define LOG_INFO 3
+#define LOG_DEBUG 4
 
 void get_timestamp( char buf_25chars[]) {
        time_t epoch = time(NULL);      
@@ -145,6 +146,43 @@ void info_handler( char* msg, ... ) {
 }
 
 
+void debug_handler( char* msg, ... ) {
+
+       char buf[25];
+       memset( buf, 0, 25 );
+       get_timestamp( buf );
+       pid_t  pid = getpid();
+       va_list args;
+       
+       if( _init_log() ) {
+
+               if( log_level < LOG_DEBUG )
+                       return;
+
+               pthread_mutex_lock( &(mutex) );
+               fprintf( log_file, "[%s %d] [%s] ", buf, pid, "DEBG" );
+       
+               va_start(args, msg);
+               vfprintf(log_file, msg, args);
+               va_end(args);
+       
+               fprintf(log_file, "\n");
+               fflush( log_file );
+               pthread_mutex_unlock( &(mutex) );
+
+       } else {
+
+               fprintf( stderr, "[%s %d] [%s] ", buf, pid, "DEBG" );
+               va_start(args, msg);
+               vfprintf(stderr, msg, args);
+               va_end(args);
+               fprintf( stderr, "\n" );
+       }
+
+}
+
+
+
 int _init_log() {
 
        if( log_level != -1 )
@@ -297,7 +335,7 @@ void config_reader_init( char* config_file ) {
 char* config_value( const char* xp_query, ... ) {
 
        if( conf_reader == NULL || xp_query == NULL ) {
-               fatal_handler( "config_value(): NULL param(s)" );
+               fatal_handler( "config_value(): NULL conf_reader or NULL param(s)" );
                return NULL;
        }
 
@@ -374,7 +412,7 @@ char* config_value( const char* xp_query, ... ) {
        }
 
        char* value = strdup(val);
-       if( value == NULL ) { fatal_handler( "config_value(): Out of Memory!" ); }
+       if( value == NULL ) { warning_handler( "config_value(): Empty config value or Out of Memory!" ); }
 
        // Free XPATH structures
        if( xpathObj != NULL ) xmlXPathFreeObject( xpathObj );
index c78ec62..f98747b 100644 (file)
@@ -363,6 +363,22 @@ void jid_get_resource( const char* jid, char buf[])  {
        }
 }
 
+void jid_get_domain( const char* jid, char buf[] ) {
+       if(jid == NULL) return;
+       int len = strlen(jid);
+       int i;
+       int index1 = 0; 
+       int index2 = 0;
+       for( i = 0; i!= len; i++ ) {
+               if(jid[i] == 64) /* ascii @ */
+                       index1 = i + 1;
+               else if(jid[i] == 47) /* ascii / */
+                       index2 = i;
+       }
+       if( index1 > 0 && index2 > 0 && index2 > index1 )
+               memcpy( buf, jid + index1, index2 - index1 );
+}
+
 void set_msg_error( transport_message* msg, char* type, int err_code ) {
 
        if( type != NULL && strlen( type ) > 0 ) {
index 4775472..cfb0dfa 100644 (file)
@@ -6,6 +6,8 @@
 char* router_resource;
 transport_router_registrar* routt;
 
+void _build_trusted_sites( transport_router_registrar* router );
+
 void sig_hup_handler( int a ) { 
        router_registrar_free( routt ); 
        config_reader_free();   
@@ -22,6 +24,10 @@ int main( int argc, char* argv[] ) {
                exit(0);
        }
 
+       char b[256];
+       memset(b,0,256);
+       jid_get_domain( "client@elroy/test", b );
+       fprintf(stderr, "Domain %s\n", b );
 
        config_reader_init( argv[1] );  
        if( conf_reader == NULL ) fatal_handler( "main(): Config is NULL" ); 
@@ -46,7 +52,6 @@ int main( int argc, char* argv[] ) {
        if(component) 
                icomponent = atoi(component);
 
-
        if( iport < 1 ) { 
                fatal_handler( "Port is negative or 0" );
                return 99;
@@ -107,10 +112,45 @@ transport_router_registrar* router_registrar_init( char* server,
 
        router_registrar->client_timeout        = client_timeout;
        router_registrar->jabber = jabber_connect_init( server, port, username, password, resource, con_timeout, component );
+       _build_trusted_sites( router_registrar );
+       info_handler( "Trusted stuff %s, %s, %s", router_registrar->trusted_servers[0], 
+               router_registrar->trusted_clients[0], router_registrar->trusted_clients[1] );
        return router_registrar;
 
 }
 
+void _build_trusted_sites( transport_router_registrar* router ) {
+
+       router->trusted_servers = (char**) safe_malloc(sizeof(char**));
+       router->trusted_clients = (char**) safe_malloc(sizeof(char**));
+
+       *(router->trusted_servers) = (char*) safe_malloc(ROUTER_MAX_TRUSTED);
+       *(router->trusted_clients) = (char*) safe_malloc(ROUTER_MAX_TRUSTED);
+
+       int i = 0;
+       while( ++i ) {
+               char* server = config_value("//router/trusted_domains/server%d", i );
+               if(server == NULL)
+                       break;
+               
+               router->trusted_servers[i-1] = server;
+       }
+
+       i = 0;
+       while( ++i ) {
+               char* client = config_value( "//router/trusted_domains/client%d", i );
+               if(client == NULL)
+                       break;
+               router->trusted_clients[i-1] = client;
+       }
+
+       if( router->trusted_servers[0] == NULL ||
+                       router->trusted_clients[0] == NULL )
+
+               fatal_handler( "You must specify at least one trusted server and client in the config file");
+}
+
+
 jabber_connect* jabber_connect_init( char* server, 
                int port, char* username, char* password, char* resource, int connect_timeout, int component ) {
 
@@ -208,7 +248,33 @@ void listen_loop( transport_router_registrar* router ) {
                        
                        if( FD_ISSET( router_fd, &listen_set ) ) {
                                cur_msg = client_recv( router->jabber->t_client, 1 );
-                               router_registrar_handle_msg( router, cur_msg );
+
+                               /* We only process a message if we have some trusted servers and the current
+                                       message came from one of those servers */
+                               if(router->trusted_servers && router->trusted_servers[0]) {
+                                       int i = 0;
+                                       int found = 0;
+
+                                       char server_buf[256];
+                                       memset(server_buf,0,256);
+                                       jid_get_domain( cur_msg->sender, server_buf );
+                                       info_handler("Received top level message from %s", server_buf );
+
+                                       while(1) {
+                                               if(router->trusted_servers[i] == NULL)
+                                                       break;
+                                               if(!strcmp(router->trusted_servers[i], server_buf)) {
+                                                       found = 1;
+                                                       break;
+                                               }
+                                               i++;
+                                       }
+                                       if(found)
+                                               router_registrar_handle_msg( router, cur_msg );
+                                       else
+                                               warning_handler( "Received top level message from unpriveleged sender %s", cur_msg->sender );
+                               }
+
                                message_free( cur_msg );
                                if( ++num_handled == select_ret ) 
                                        continue;
@@ -777,6 +843,23 @@ int router_registrar_free( transport_router_registrar* router_registrar ) {
                remove_server_class(router_registrar, router_registrar->server_class_list);
        }
 
+       transport_router_registrar* router = router_registrar;
+
+       int i = 0;
+       
+       while(1) {
+
+               if( router->trusted_servers[i] == NULL &&
+                               router->trusted_clients[i] == NULL )
+                       break;
+
+               if(router->trusted_servers[i] != NULL)
+                       free(router->trusted_servers[i]);
+               if(router->trusted_clients[i] != NULL)
+                       free(router->trusted_clients[i]);
+               i++;
+       }
+
        free( router_registrar );
 
 
index cda1e8b..2ab1aa7 100644 (file)
@@ -6,6 +6,8 @@
 #ifndef ROUTER_H
 #define ROUTER_H
 
+#define ROUTER_MAX_TRUSTED 256
+
 // ----------------------------------------------------------------------
 // Jabber router_registrar/load balancer.  There is a top level linked list of 
 // server_class_nodes.  A server class represents the a cluster of Jabber
@@ -111,6 +113,9 @@ struct transport_router_registrar_struct {
        /* true if we connect to jabber as a jabber component */
        int component;
 
+       char** trusted_servers;
+       char** trusted_clients;
+
 
 };
 typedef struct transport_router_registrar_struct transport_router_registrar;