From db4ddf9f6aeec91d43907e914c23c28e36bbac19 Mon Sep 17 00:00:00 2001 From: erickson Date: Fri, 18 Feb 2005 16:33:42 +0000 Subject: [PATCH] added DIGEST style authentication to the clients git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@87 9efc2488-bf62-4759-914b-345cdb29e865 --- include/opensrf/transport_client.h | 3 +- include/opensrf/transport_session.h | 7 ++- src/libtransport/Makefile | 2 +- src/libtransport/basic_client.c | 2 +- src/libtransport/component.c | 2 +- src/libtransport/transport_client.c | 6 ++- src/libtransport/transport_session.c | 93 ++++++++++++++++++++++++++++-------- src/libtransport/transport_socket.c | 4 +- src/router/Makefile | 2 +- src/router/router.c | 41 +++++++++------- src/router/router.h | 7 ++- 11 files changed, 120 insertions(+), 49 deletions(-) diff --git a/include/opensrf/transport_client.h b/include/opensrf/transport_client.h index 64c678f..695bbf0 100644 --- a/include/opensrf/transport_client.h +++ b/include/opensrf/transport_client.h @@ -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 ); diff --git a/include/opensrf/transport_session.h b/include/opensrf/transport_session.h index 5c81e6e..f044c01 100644 --- a/include/opensrf/transport_session.h +++ b/include/opensrf/transport_session.h @@ -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 ); diff --git a/src/libtransport/Makefile b/src/libtransport/Makefile index 10490f1..585b336 100644 --- a/src/libtransport/Makefile +++ b/src/libtransport/Makefile @@ -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 diff --git a/src/libtransport/basic_client.c b/src/libtransport/basic_client.c index b57aa31..d2413bf 100644 --- a/src/libtransport/basic_client.c +++ b/src/libtransport/basic_client.c @@ -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" ); diff --git a/src/libtransport/component.c b/src/libtransport/component.c index 0dcb70c..952b6b4 100644 --- a/src/libtransport/component.c +++ b/src/libtransport/component.c @@ -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" ); diff --git a/src/libtransport/transport_client.c b/src/libtransport/transport_client.c index d486d30..e09a51e 100644 --- a/src/libtransport/transport_client.c +++ b/src/libtransport/transport_client.c @@ -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 ) { diff --git a/src/libtransport/transport_session.c b/src/libtransport/transport_session.c index f817972..607821b 100644 --- a/src/libtransport/transport_session.c +++ b/src/libtransport/transport_session.c @@ -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, "", 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, - "%s%s%s", - 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, + "%s%s%s", + 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; + + /* + + + bill + 48fc78be9ec8f86d8ce1c39c320c97c21d62334d + globe + + + */ + + + /* 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, + "%s%s%s", + username, hash, resource ); + + + /* + + + bill + 48fc78be9ec8f86d8ce1c39c320c97c21d62334d + globe + + + */ + + + /* 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; diff --git a/src/libtransport/transport_socket.c b/src/libtransport/transport_socket.c index 49864eb..012bd96 100644 --- a/src/libtransport/transport_socket.c +++ b/src/libtransport/transport_socket.c @@ -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 ); } diff --git a/src/router/Makefile b/src/router/Makefile index 0317075..09aa6e5 100644 --- a/src/router/Makefile +++ b/src/router/Makefile @@ -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 diff --git a/src/router/router.c b/src/router/router.c index 843735d..4775472 100644 --- a/src/router/router.c +++ b/src/router/router.c @@ -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, " | "); diff --git a/src/router/router.h b/src/router/router.h index 0162172..cda1e8b 100644 --- a/src/router/router.h +++ b/src/router/router.h @@ -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 -- 2.11.0