added 'component' login functionality to the lib code
authorerickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Fri, 18 Feb 2005 00:56:22 +0000 (00:56 +0000)
committererickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Fri, 18 Feb 2005 00:56:22 +0000 (00:56 +0000)
git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@85 9efc2488-bf62-4759-914b-345cdb29e865

include/opensrf/generic_utils.h
include/opensrf/transport_client.h
include/opensrf/transport_session.h
src/libtransport/Makefile
src/libtransport/basic_client.c
src/libtransport/transport_client.c
src/libtransport/transport_session.c

index 22632a7..83c52f3 100644 (file)
@@ -13,6 +13,7 @@
 #ifndef GENERIC_UTILS_H
 #define GENERIC_UTILS_H
 
+#define equals(a,b) !strcmp(a,b) 
 
 /** Malloc's, checks for NULL, clears all memory bits and 
   * returns the pointer
index 5f16b2c..64c678f 100644 (file)
@@ -36,7 +36,7 @@ typedef struct transport_client_struct transport_client;
 // The user must call client_free(client) when finished with the allocated
 // object.
 // ---------------------------------------------------------------------------
-transport_client* client_init( char* server, int port );
+transport_client* client_init( char* server, int port, int component );
 
 // ---------------------------------------------------------------------------
 // Connects to the Jabber server with the provided information. Returns 1 on
index 6107106..5c81e6e 100644 (file)
@@ -7,6 +7,8 @@
 #include "opensrf/transport_message.h"
 #include "opensrf/generic_utils.h"
 
+#include "sha.h"
+
 #include <string.h>
 #include <libxml/globals.h>
 #include <libxml/xmlerror.h>
@@ -140,6 +142,7 @@ struct transport_session_struct {
        growing_buffer* recipient_buffer;
        growing_buffer* status_buffer;
        growing_buffer* message_error_type;
+       growing_buffer* session_id;
        int message_error_code;
 
        /* for OILS extenstions */
@@ -156,6 +159,8 @@ struct transport_session_struct {
         */
        void* user_data;
 
+       int component; /* true if we're a component */
+
        /* the Jabber message callback */
        void (*message_callback) ( void* user_data, transport_message* msg );
        //void (iq_callback) ( void* user_data, transport_iq_message* iq );
@@ -167,7 +172,7 @@ typedef struct transport_session_struct transport_session;
 // Allocates and initializes the necessary transport session
 // data structures.
 // ------------------------------------------------------------------
-transport_session* init_transport(  char* server, int port, void* user_data );
+transport_session* init_transport( char* server, int port, void* user_data, int component );
 
 // ------------------------------------------------------------------
 // Returns the value of the given XML attribute
index 2ffd1b0..10490f1 100644 (file)
@@ -5,11 +5,14 @@ CC = gcc
 LIB_DIR=../../lib
 CC_OPTS = -Wall -O2 -I /usr/include/libxml2 -I /usr/include/libxml2/libxml -I ../../include #-I /home/erickson/cc/libxml2-2.6.16
 EXE_LD_OPTS = -L $(LIB_DIR) -lxml2 -lopensrf_transport 
-LIB_SOURCES = generic_utils.c transport_socket.c transport_session.c transport_message.c transport_client.c
+LIB_SOURCES = generic_utils.c transport_socket.c transport_session.c transport_message.c transport_client.c sha.c
 
-TARGETS=generic_utils.o transport_socket.o transport_message.o transport_session.o transport_client.o 
+TARGETS=generic_utils.o transport_socket.o transport_message.o transport_session.o transport_client.o  sha.o
 
-all: basic_client
+all: basic_client component
+
+component: lib
+       $(CC) $(CC_OPTS) $(EXE_LD_OPTS) component.c -o $@
 
 basic_client: lib
        $(CC) $(CC_OPTS) $(EXE_LD_OPTS) basic_client.c -o $@
index bf5ac0a..b57aa31 100644 (file)
@@ -16,10 +16,10 @@ int main( int argc, char** argv ) {
        }
 
        transport_message* send;
-       transport_client* client = client_init( argv[2], 5222 );
+       transport_client* client = client_init( argv[2], 5222, 0 );
 
        // try to connect, allow 15 second connect timeout 
-       if( client_connect( client, argv[1], "asdfjkjk", argv[3], 15 ) ) 
+       if( client_connect( client, argv[1], "jkjkasdf", argv[3], 15 ) ) 
                info_handler("Connected...\n");
         else  
                fatal_handler( "NOT Connected...\n" ); 
index 53e2825..d486d30 100644 (file)
@@ -45,7 +45,7 @@ int main( int argc, char** argv ) {
 */
 
 
-transport_client* client_init( char* server, int port ) {
+transport_client* client_init( char* server, int port, int component ) {
 
        if(server == NULL) return NULL;
 
@@ -58,7 +58,7 @@ transport_client* client_init( char* server, int port ) {
        client->m_list = (transport_message_list*) safe_malloc( l_size );
 
        client->m_list->type = MESSAGE_LIST_HEAD;
-       client->session = init_transport( server, port, client );
+       client->session = init_transport( server, port, client, component );
 
 
        if(client->session == NULL) {
index 9acd507..f817972 100644 (file)
@@ -6,7 +6,7 @@
 // returns a built and allocated transport_session object.
 // This codes does no network activity, only memory initilization
 // ---------------------------------------------------------------------------------
-transport_session* init_transport(  char* server, int port, void* user_data ) {
+transport_session* init_transport(  char* server, int port, void* user_data, int component ) {
 
        /* create the session struct */
        transport_session* session = 
@@ -14,6 +14,8 @@ transport_session* init_transport(  char* server, int port, void* user_data ) {
 
        session->user_data = user_data;
 
+       session->component = component;
+
        /* initialize the data buffers */
        session->body_buffer                    = buffer_init( JABBER_BODY_BUFSIZE );
        session->subject_buffer         = buffer_init( JABBER_SUBJECT_BUFSIZE );
@@ -22,8 +24,9 @@ transport_session* init_transport(  char* server, int port, void* user_data ) {
        session->status_buffer          = buffer_init( JABBER_STATUS_BUFSIZE );
        session->recipient_buffer       = buffer_init( JABBER_JID_BUFSIZE );
        session->message_error_type = buffer_init( JABBER_JID_BUFSIZE );
+       session->session_id                     = buffer_init( 64 ); 
 
-       /* for OILS extensions */
+       /* for OpenSRF extensions */
        session->router_to_buffer               = buffer_init( JABBER_JID_BUFSIZE );
        session->router_from_buffer     = buffer_init( JABBER_JID_BUFSIZE );
        session->router_class_buffer    = buffer_init( JABBER_JID_BUFSIZE );
@@ -35,7 +38,8 @@ transport_session* init_transport(  char* server, int port, void* user_data ) {
                        session->thread_buffer  == NULL || session->from_buffer          == NULL        ||
                        session->status_buffer  == NULL || session->recipient_buffer == NULL ||
                        session->router_to_buffer       == NULL || session->router_from_buffer   == NULL ||
-                       session->router_class_buffer == NULL || session->router_command_buffer == NULL ) { 
+                       session->router_class_buffer == NULL || session->router_command_buffer == NULL ||
+                       session->session_id == NULL ) { 
 
                fatal_handler( "init_transport(): buffer_init returned NULL" );
                return 0;
@@ -88,6 +92,7 @@ int session_free( transport_session* session ) {
        buffer_free(session->router_from_buffer);
        buffer_free(session->router_class_buffer);
        buffer_free(session->router_command_buffer);
+       buffer_free(session->session_id);
 
 
        free( session );
@@ -158,42 +163,88 @@ int session_connect( transport_session* session,
                        return 0;
        }
 
-       /* the first Jabber connect stanza */
-       size1 = 100 + strlen( server );
-       char stanza1[ size1 ]; 
-       memset( stanza1, 0, size1 );
-       sprintf( stanza1, 
-                       "<stream:stream to='%s' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>",
+       if( session->component ) {
+
+               /* the first Jabber connect stanza */
+               char* our_hostname = getenv("HOSTNAME");
+               size1 = 150 + strlen( server );
+               char stanza1[ size1 ]; 
+               memset( stanza1, 0, size1 );
+               sprintf( stanza1, 
+                               "<stream:stream version='1.0' xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:component:accept' to='%s' from='%s' xml:lang='en'>",
+                               username, our_hostname );
+
+               /* send the first stanze */
+               session->state_machine->connecting = CONNECTING_1;
+               if( ! tcp_send( session->sock_obj, stanza1 ) ) {
+                       warning_handler("error sending");
+                       return 0;
+               }
+       
+               /* wait for reply */
+               tcp_wait( session->sock_obj, connect_timeout ); /* make the timeout smarter XXX */
+       
+               /* server acknowledges our existence, now see if we can login */
+               if( session->state_machine->connecting == CONNECTING_2 ) {
+       
+                       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 );
+                       size2 = 100 + strlen( hash );
+                       char stanza2[ size2 ];
+                       memset( stanza2, 0, size2 );
+                       sprintf( stanza2, "<handshake>%s</handshake>", hash );
+       
+                       if( ! tcp_send( session->sock_obj, stanza2 )  ) {
+                               warning_handler("error sending");
+                               return 0;
+                       }
+               }
+
+       } else { /* we're not a component */
+
+               /* the first Jabber connect stanza */
+               size1 = 100 + strlen( server );
+               char stanza1[ size1 ]; 
+               memset( stanza1, 0, size1 );
+               sprintf( stanza1, 
+                               "<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;
-       if( ! tcp_send( session->sock_obj, stanza1 ) ) {
-               warning_handler("error sending");
-               return 0;
-       }
+               /* 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 );
 
-       /* wait for reply */
-       tcp_wait( session->sock_obj, connect_timeout ); /* make the timeout smarter XXX */
 
-       /* server acknowledges our existence, now see if we can login */
-       if( session->state_machine->connecting == CONNECTING_2 ) {
-               if( ! tcp_send( session->sock_obj, stanza2 )  ) {
+               /* send the first stanze */
+               session->state_machine->connecting = CONNECTING_1;
+               if( ! tcp_send( session->sock_obj, stanza1 ) ) {
                        warning_handler("error sending");
                        return 0;
                }
-       }
+       
+               /* wait for reply */
+               tcp_wait( session->sock_obj, connect_timeout ); /* make the timeout smarter XXX */
+       
+               /* 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 );
 
@@ -281,9 +332,17 @@ void startElementHandler(
        if( strcmp( name, "stream:stream" ) == 0 ) {
                if( ses->state_machine->connecting == CONNECTING_1 ) {
                        ses->state_machine->connecting = CONNECTING_2;
+                       buffer_add( ses->session_id, get_xml_attr(atts, "id") );
                }
        }
 
+       if( strcmp( name, "handshake" ) == 0 ) {
+               ses->state_machine->connected = 1;
+               ses->state_machine->connecting = 0;
+               return;
+       }
+
+
        if( strcmp( name, "error" ) == 0 ) {
                ses->state_machine->in_message_error = 1;
                buffer_add( ses->message_error_type, get_xml_attr( atts, "type" ) );
@@ -429,6 +488,7 @@ int reset_session_buffers( transport_session* ses ) {
        buffer_reset( ses->router_class_buffer );
        buffer_reset( ses->router_command_buffer );
        buffer_reset( ses->message_error_type );
+       buffer_reset( ses->session_id );
 
        return 1;
 }