added DIGEST style authentication to the clients
authorerickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Fri, 18 Feb 2005 16:33:42 +0000 (16:33 +0000)
committererickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Fri, 18 Feb 2005 16:33:42 +0000 (16:33 +0000)
git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@87 9efc2488-bf62-4759-914b-345cdb29e865

include/opensrf/transport_client.h
include/opensrf/transport_session.h
src/libtransport/Makefile
src/libtransport/basic_client.c
src/libtransport/component.c
src/libtransport/transport_client.c
src/libtransport/transport_session.c
src/libtransport/transport_socket.c
src/router/Makefile
src/router/router.c
src/router/router.h

index 64c678f..695bbf0 100644 (file)
@@ -43,7 +43,8 @@ transport_client* client_init( char* server, int port, int component );
 // success, 0 otherwise.
 // ---------------------------------------------------------------------------
 int client_connect( transport_client* client, 
-               char* username, char* password, char* resource, int connect_timeout );
+               char* username, char* password, char* resource, 
+               int connect_timeout, enum TRANSPORT_AUTH_TYPE auth_type );
 
 int client_disconnect( transport_client* client );
 
index 5c81e6e..f044c01 100644 (file)
@@ -122,6 +122,9 @@ struct jabber_state_machine_struct {
 };
 typedef struct jabber_state_machine_struct jabber_machine;
 
+
+enum TRANSPORT_AUTH_TYPE { AUTH_PLAIN, AUTH_DIGEST };
+
 // ---------------------------------------------------------------------------------
 // Transport session.  This maintains all the various parts of a session
 // ---------------------------------------------------------------------------------
@@ -208,7 +211,9 @@ int session_free( transport_session* session );
 // seconds before failing
 // ------------------------------------------------------------------
 int session_connect( transport_session* session, 
-               const char* username, const char* password, const char* resource, int connect_timeout );
+               const char* username, const char* password, 
+               const char* resource, int connect_timeout, 
+               enum TRANSPORT_AUTH_TYPE auth_type );
 
 int session_disconnect( transport_session* session );
 
index 10490f1..585b336 100644 (file)
@@ -26,4 +26,4 @@ lib:
 
 
 clean:
-       /bin/rm -f *.o $(LIB_DIR)/libopensrf_transport.so basic_client
+       /bin/rm -f *.o $(LIB_DIR)/libopensrf_transport.so basic_client component
index b57aa31..d2413bf 100644 (file)
@@ -19,7 +19,7 @@ int main( int argc, char** argv ) {
        transport_client* client = client_init( argv[2], 5222, 0 );
 
        // try to connect, allow 15 second connect timeout 
-       if( client_connect( client, argv[1], "jkjkasdf", argv[3], 15 ) ) 
+       if( client_connect( client, argv[1], "jkjkasdf", argv[3], 15, AUTH_DIGEST ) ) 
                info_handler("Connected...\n");
         else  
                fatal_handler( "NOT Connected...\n" ); 
index 0dcb70c..952b6b4 100644 (file)
@@ -13,7 +13,7 @@ int main( int argc, char** argv ) {
        transport_client* client = client_init( argv[1], port, 1 );
 
        // try to connect, allow 15 second connect timeout 
-       if( client_connect( client, argv[3], argv[4], "", 15 ) ) 
+       if( client_connect( client, argv[3], argv[4], "", 15, 1 ) ) 
                info_handler("Connected...\n");
         else  
                fatal_handler( "NOT Connected...\n" ); 
index d486d30..e09a51e 100644 (file)
@@ -71,9 +71,11 @@ transport_client* client_init( char* server, int port, int component ) {
 }
 
 int client_connect( transport_client* client, 
-               char* username, char* password, char* resource, int connect_timeout ) {
+               char* username, char* password, char* resource, 
+               int connect_timeout, enum TRANSPORT_AUTH_TYPE  auth_type ) {
        if(client == NULL) return 0; 
-       return session_connect( client->session, username, password, resource, connect_timeout );
+       return session_connect( client->session, username, 
+                       password, resource, connect_timeout, auth_type );
 }
 
 int client_disconnect( transport_client* client ) {
index f817972..607821b 100644 (file)
@@ -131,7 +131,8 @@ int session_send_msg(
 
 /* connects to server and connects to jabber */
 int session_connect( transport_session* session, 
-               const char* username, const char* password, const char* resource, int connect_timeout ) {
+               const char* username, const char* password, 
+               const char* resource, int connect_timeout, enum TRANSPORT_AUTH_TYPE auth_type ) {
 
        int size1 = 0;
        int size2 = 0;
@@ -214,16 +215,6 @@ int session_connect( transport_session* session,
                                "<stream:stream to='%s' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>",
                        server );
        
-               /* the second jabber connect stanza including login info*/
-               /* currently, we only support plain text login */
-               size2 = 150 + strlen( username ) + strlen(password) + strlen(resource);
-               char stanza2[ size2 ];
-               memset( stanza2, 0, size2 );
-       
-               sprintf( stanza2, 
-                               "<iq id='123456789' type='set'><query xmlns='jabber:iq:auth'><username>%s</username><password>%s</password><resource>%s</resource></query></iq>",
-                               username, password, resource );
-
 
                /* send the first stanze */
                session->state_machine->connecting = CONNECTING_1;
@@ -231,24 +222,88 @@ int session_connect( transport_session* session,
                        warning_handler("error sending");
                        return 0;
                }
-       
+
+
                /* wait for reply */
                tcp_wait( session->sock_obj, connect_timeout ); /* make the timeout smarter XXX */
+
+               if( auth_type == AUTH_PLAIN ) {
+
+                       /* the second jabber connect stanza including login info*/
+                       size2 = 150 + strlen( username ) + strlen(password) + strlen(resource);
+                       char stanza2[ size2 ];
+                       memset( stanza2, 0, size2 );
+               
+                       sprintf( stanza2, 
+                                       "<iq id='123456789' type='set'><query xmlns='jabber:iq:auth'><username>%s</username><password>%s</password><resource>%s</resource></query></iq>",
+                                       username, password, resource );
        
-               /* server acknowledges our existence, now see if we can login */
-               if( session->state_machine->connecting == CONNECTING_2 ) {
-                       if( ! tcp_send( session->sock_obj, stanza2 )  ) {
-                               warning_handler("error sending");
-                               return 0;
+
+               /*
+               <iq type='set' id='auth2'>
+                       <query xmlns='jabber:iq:auth'>
+                               <username>bill</username>
+                          <digest>48fc78be9ec8f86d8ce1c39c320c97c21d62334d</digest>
+                               <resource>globe</resource>
+                       </query>
+               </iq>
+               */
+       
+       
+                       /* server acknowledges our existence, now see if we can login */
+                       if( session->state_machine->connecting == CONNECTING_2 ) {
+                               if( ! tcp_send( session->sock_obj, stanza2 )  ) {
+                                       warning_handler("error sending");
+                                       return 0;
+                               }
+                       }
+
+               } else if( auth_type == AUTH_DIGEST ) {
+
+                       int ss = session->session_id->n_used + strlen(password) + 5;
+                       char hashstuff[ss];
+                       memset(hashstuff,0,ss);
+                       sprintf( hashstuff, "%s%s", session->session_id->buf, password );
+
+                       char* hash = shahash( hashstuff );
+
+                       /* the second jabber connect stanza including login info*/
+                       size2 = 150 + strlen( hash ) + strlen(password) + strlen(resource);
+                       char stanza2[ size2 ];
+                       memset( stanza2, 0, size2 );
+               
+                       sprintf( stanza2, 
+                                       "<iq id='123456789' type='set'><query xmlns='jabber:iq:auth'><username>%s</username><digest>%s</digest><resource>%s</resource></query></iq>",
+                                       username, hash, resource );
+       
+
+               /*
+               <iq type='set' id='auth2'>
+                       <query xmlns='jabber:iq:auth'>
+                               <username>bill</username>
+                          <digest>48fc78be9ec8f86d8ce1c39c320c97c21d62334d</digest>
+                               <resource>globe</resource>
+                       </query>
+               </iq>
+               */
+       
+       
+                       /* server acknowledges our existence, now see if we can login */
+                       if( session->state_machine->connecting == CONNECTING_2 ) {
+                               if( ! tcp_send( session->sock_obj, stanza2 )  ) {
+                                       warning_handler("error sending");
+                                       return 0;
+                               }
                        }
+
                }
 
        } // not component
-       
+
+
        /* wait for reply */
        tcp_wait( session->sock_obj, connect_timeout );
 
-
        if( session->state_machine->connected ) {
                /* yar! */
                return 1;
index 49864eb..012bd96 100644 (file)
@@ -116,7 +116,7 @@ int tcp_send( transport_socket* sock_obj, const char* data ){
                return 0;
        }
 
-       //fprintf( stderr, "TCP Sending: \n%s\n", data );
+       fprintf( stderr, "TCP Sending: \n%s\n", data );
 
        // ------------------------------------------------------------------
        // Send the data down the TCP pipe
@@ -210,7 +210,7 @@ int tcp_wait( transport_socket* sock_obj, int timeout ){
 #else // read everything we can
 
        while( (n = recv(sock_fd, buf, BUFSIZE-1, 0) ) > 0 ) {
-               //printf("\nReceived:  %s\n", buf);
+               printf("\nReceived:  %s\n", buf);
                sock_obj->data_received_callback( sock_obj->user_data, buf );
                memset( &buf, 0, BUFSIZE );
        }
index 0317075..09aa6e5 100644 (file)
@@ -5,7 +5,7 @@ CC = gcc
 CC_OPTS = -Wall -O2 -I /usr/include/libxml2 -I /usr/include/libxml2/libxml -I ../../include
 LD_OPTS = -lxml2
 LP=../libtransport
-LIB_SOURCES = $(LP)/generic_utils.c $(LP)/transport_socket.c $(LP)/transport_session.c $(LP)/transport_message.c $(LP)/transport_client.c
+LIB_SOURCES = $(LP)/generic_utils.c $(LP)/transport_socket.c $(LP)/transport_session.c $(LP)/transport_message.c $(LP)/transport_client.c $(LP)/sha.c
 
 all: router router_query
 
index 843735d..4775472 100644 (file)
@@ -34,6 +34,7 @@ int main( int argc, char* argv[] ) {
        router_resource         = config_value("//router/transport/resource");
        char* con_timeout               = config_value("//router/transport/connect_timeout" );
        char* max_retries               = config_value("//router/transport/max_reconnect_attempts" );
+       char* component         = config_value("//router/component" );
 
        fprintf(stderr, "Router connecting as \nserver: %s \nport: %s \nuser:%s \nresource:%s\n", 
                        server, port, username, router_resource );
@@ -41,6 +42,10 @@ int main( int argc, char* argv[] ) {
        int iport                       = atoi( port );
        int con_itimeout        = atoi( con_timeout );
        int max_retries_        = atoi(max_retries);
+       int icomponent = 0;
+       if(component) 
+               icomponent = atoi(component);
+
 
        if( iport < 1 ) { 
                fatal_handler( "Port is negative or 0" );
@@ -50,7 +55,7 @@ int main( int argc, char* argv[] ) {
 
        /* build the router_registrar */
        transport_router_registrar* router_registrar = 
-               router_registrar_init( server, iport, username, password, router_resource, 0, con_itimeout ); 
+               router_registrar_init( server, iport, username, password, router_resource, 0, con_itimeout, icomponent ); 
 
        routt = router_registrar;
 
@@ -92,7 +97,7 @@ int main( int argc, char* argv[] ) {
 
 transport_router_registrar* router_registrar_init( char* server, 
                int port, char* username, char* password, 
-               char* resource, int client_timeout, int con_timeout ) {
+               char* resource, int client_timeout, int con_timeout, int component ) {
 
        if( server == NULL ) { return NULL; }
        
@@ -101,13 +106,13 @@ transport_router_registrar* router_registrar_init( char* server,
        transport_router_registrar* router_registrar = (transport_router_registrar*) safe_malloc( size );
 
        router_registrar->client_timeout        = client_timeout;
-       router_registrar->jabber = jabber_connect_init( server, port, username, password, resource, con_timeout );
+       router_registrar->jabber = jabber_connect_init( server, port, username, password, resource, con_timeout, component );
        return router_registrar;
 
 }
 
 jabber_connect* jabber_connect_init( char* server, 
-               int port, char* username, char* password, char* resource, int connect_timeout ) {
+               int port, char* username, char* password, char* resource, int connect_timeout, int component ) {
 
        size_t len = sizeof(jabber_connect);
        jabber_connect* jabber = (jabber_connect*) safe_malloc( len );
@@ -127,7 +132,7 @@ jabber_connect* jabber_connect_init( char* server,
        }
 
        /* build the transport client */
-       jabber->t_client = client_init( jabber->server, jabber->port );
+       jabber->t_client = client_init( jabber->server, jabber->port, component );
 
        return jabber;
 }
@@ -141,7 +146,8 @@ int router_registrar_connect( transport_router_registrar* router ) {
 int j_connect( jabber_connect* jabber ) {
        if( jabber == NULL ) { return 0; }
        return client_connect( jabber->t_client, 
-                       jabber->username, jabber->password, jabber->resource, jabber->connect_timeout );
+                       jabber->username, jabber->password, jabber->resource, 
+                       jabber->connect_timeout, AUTH_DIGEST );
 }
 
 int fill_fd_set( transport_router_registrar* router, fd_set* set ) {
@@ -523,7 +529,7 @@ server_class_node* init_server_class(
 
        node->jabber = jabber_connect_init( router->jabber->server,
                        router->jabber->port, router->jabber->username, 
-                       router->jabber->password, server_class, router->jabber->connect_timeout );
+                       router->jabber->password, server_class, router->jabber->connect_timeout, router->component );
 
 
 
@@ -707,20 +713,19 @@ int router_return_server_info(
 
                do {
 
-                       char buf[36];
-                       memset(buf,0, 36);
-
-                       char* localtime = strdup( ctime( &(cur_node->la_time) ) );
-                       strcpy( buf, localtime );
-                       buf[ strlen(localtime)-1] = '\0'; // remove newline
-                       free(localtime);
+                       char tbuf[124];
+                       memset(tbuf,0,124);
+                       sprintf( tbuf, "%d", (int)cur_node->reg_time ); 
+                       buffer_add( buffer, tbuf );
+                       buffer_add( buffer, " | ");
 
-                       buffer_add( buffer, buf );
+                       memset(tbuf,0,124);
+                       sprintf( tbuf, "%d", (int)cur_node->upd_time ); 
+                       buffer_add( buffer, tbuf );
                        buffer_add( buffer, " | ");
 
-                       char tbuf[124];
                        memset(tbuf,0,124);
-                       sprintf( tbuf, "%d", cur_node->la_time );       
+                       sprintf( tbuf, "%d", (int)cur_node->la_time );  
                        buffer_add( buffer, tbuf );
                        buffer_add( buffer, " | ");
 
@@ -729,7 +734,7 @@ int router_return_server_info(
                        memset(sbuf,0,64);
                        sprintf(sbuf,"%d",cur_node->serve_count);
 
-                       buffer_add( buffer, "serve_count: " ); 
+                       buffer_add( buffer, "#" ); 
                        buffer_add( buffer, sbuf ); 
                        buffer_add( buffer, " | ");
 
index 0162172..cda1e8b 100644 (file)
@@ -108,6 +108,9 @@ struct transport_router_registrar_struct {
        /* our top level connection to the jabber server */
        jabber_connect* jabber; 
 
+       /* true if we connect to jabber as a jabber component */
+       int component;
+
 
 };
 typedef struct transport_router_registrar_struct transport_router_registrar;
@@ -124,7 +127,7 @@ typedef struct transport_router_registrar_struct transport_router_registrar;
 // ----------------------------------------------------------------------
 transport_router_registrar* router_registrar_init( char* server, 
                int port, char* username, char* password, char* resource, 
-               int client_timeout, int connect_timeout );
+               int client_timeout, int connect_timeout, int component );
 
 // ----------------------------------------------------------------------
 // Connects the top level router_registrar object to the Jabber server.
@@ -143,7 +146,7 @@ int j_connect( jabber_connect* jabber );
 // ----------------------------------------------------------------------
 jabber_connect* jabber_connect_init( char* server, 
                int port, char* username, char* password, 
-               char* resource, int connect_timeout );
+               char* resource, int connect_timeout, int component );
 
 // ----------------------------------------------------------------------
 // Allocates and initializes a server class instance.  This will be