]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libtransport/transport_session.c
added DIGEST style authentication to the clients
[OpenSRF.git] / src / libtransport / transport_session.c
1 #include "opensrf/transport_session.h"
2
3
4
5 // ---------------------------------------------------------------------------------
6 // returns a built and allocated transport_session object.
7 // This codes does no network activity, only memory initilization
8 // ---------------------------------------------------------------------------------
9 transport_session* init_transport(  char* server, int port, void* user_data, int component ) {
10
11         /* create the session struct */
12         transport_session* session = 
13                 (transport_session*) safe_malloc( sizeof(transport_session) );
14
15         session->user_data = user_data;
16
17         session->component = component;
18
19         /* initialize the data buffers */
20         session->body_buffer                    = buffer_init( JABBER_BODY_BUFSIZE );
21         session->subject_buffer         = buffer_init( JABBER_SUBJECT_BUFSIZE );
22         session->thread_buffer          = buffer_init( JABBER_THREAD_BUFSIZE );
23         session->from_buffer                    = buffer_init( JABBER_JID_BUFSIZE );
24         session->status_buffer          = buffer_init( JABBER_STATUS_BUFSIZE );
25         session->recipient_buffer       = buffer_init( JABBER_JID_BUFSIZE );
26         session->message_error_type = buffer_init( JABBER_JID_BUFSIZE );
27         session->session_id                     = buffer_init( 64 ); 
28
29         /* for OpenSRF extensions */
30         session->router_to_buffer               = buffer_init( JABBER_JID_BUFSIZE );
31         session->router_from_buffer     = buffer_init( JABBER_JID_BUFSIZE );
32         session->router_class_buffer    = buffer_init( JABBER_JID_BUFSIZE );
33         session->router_command_buffer  = buffer_init( JABBER_JID_BUFSIZE );
34
35
36
37         if(     session->body_buffer            == NULL || session->subject_buffer       == NULL        ||
38                         session->thread_buffer  == NULL || session->from_buffer          == NULL        ||
39                         session->status_buffer  == NULL || session->recipient_buffer == NULL ||
40                         session->router_to_buffer       == NULL || session->router_from_buffer   == NULL ||
41                         session->router_class_buffer == NULL || session->router_command_buffer == NULL ||
42                         session->session_id == NULL ) { 
43
44                 fatal_handler( "init_transport(): buffer_init returned NULL" );
45                 return 0;
46         }
47
48
49         /* initialize the jabber state machine */
50         session->state_machine = (jabber_machine*) safe_malloc( sizeof(jabber_machine) );
51
52         /* initialize the sax push parser */
53         session->parser_ctxt = xmlCreatePushParserCtxt(SAXHandler, session, "", 0, NULL);
54
55         /* initialize the transport_socket structure */
56         session->sock_obj = (transport_socket*) safe_malloc( sizeof(transport_socket) );
57
58         //int serv_size = strlen( server );
59         session->sock_obj->server = server;
60         session->sock_obj->port = port;
61         session->sock_obj->data_received_callback = &grab_incoming;
62
63         /* this will be handed back to us in callbacks */
64         session->sock_obj->user_data = session; 
65
66         return session;
67 }
68
69 /* XXX FREE THE BUFFERS */
70 int session_free( transport_session* session ) {
71         if( ! session ) { return 0; }
72
73         if( session->sock_obj ) 
74                 free( session->sock_obj );
75
76         if( session->state_machine ) free( session->state_machine );
77         if( session->parser_ctxt) {
78                 xmlCleanupCharEncodingHandlers();
79                 xmlFreeDoc( session->parser_ctxt->myDoc );
80                 xmlFreeParserCtxt(session->parser_ctxt);
81         }
82         xmlCleanupParser();
83
84         buffer_free(session->body_buffer);
85         buffer_free(session->subject_buffer);
86         buffer_free(session->thread_buffer);
87         buffer_free(session->from_buffer);
88         buffer_free(session->recipient_buffer);
89         buffer_free(session->status_buffer);
90         buffer_free(session->message_error_type);
91         buffer_free(session->router_to_buffer);
92         buffer_free(session->router_from_buffer);
93         buffer_free(session->router_class_buffer);
94         buffer_free(session->router_command_buffer);
95         buffer_free(session->session_id);
96
97
98         free( session );
99         return 1;
100 }
101
102
103 int session_wait( transport_session* session, int timeout ) {
104         if( ! session || ! session->sock_obj ) {
105                 return 0;
106         }
107         int ret =  tcp_wait( session->sock_obj, timeout );
108         if( ! ret ) {
109                 session->state_machine->connected = 0;
110         }
111         return ret;
112 }
113
114 int session_send_msg( 
115                 transport_session* session, transport_message* msg ) {
116
117         if( ! session ) { return 0; }
118
119         if( ! session->state_machine->connected ) {
120                 warning_handler("State machine is not connected in send_msg()");
121                 return 0;
122         }
123
124         message_prepare_xml( msg );
125         tcp_send( session->sock_obj, msg->msg_xml );
126
127         return 1;
128
129 }
130
131
132 /* connects to server and connects to jabber */
133 int session_connect( transport_session* session, 
134                 const char* username, const char* password, 
135                 const char* resource, int connect_timeout, enum TRANSPORT_AUTH_TYPE auth_type ) {
136
137         int size1 = 0;
138         int size2 = 0;
139
140         if( ! session ) { 
141                 warning_handler( "session is null in connect" );
142                 return 0; 
143         }
144
145
146         /*
147         session->state_machine->connected = 
148                 tcp_connected( session->sock_obj );
149
150         if( session->state_machine->connected ) {
151                 return 1;
152         }
153         */
154
155
156         char* server = session->sock_obj->server;
157
158         if( ! session->sock_obj ) {
159                 return 0;
160         }
161
162         if( ! session->sock_obj->connected ) {
163                 if( ! tcp_connect( session->sock_obj ))
164                         return 0;
165         }
166
167         if( session->component ) {
168
169                 /* the first Jabber connect stanza */
170                 char* our_hostname = getenv("HOSTNAME");
171                 size1 = 150 + strlen( server );
172                 char stanza1[ size1 ]; 
173                 memset( stanza1, 0, size1 );
174                 sprintf( stanza1, 
175                                 "<stream:stream version='1.0' xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:component:accept' to='%s' from='%s' xml:lang='en'>",
176                                 username, our_hostname );
177
178                 /* send the first stanze */
179                 session->state_machine->connecting = CONNECTING_1;
180                 if( ! tcp_send( session->sock_obj, stanza1 ) ) {
181                         warning_handler("error sending");
182                         return 0;
183                 }
184         
185                 /* wait for reply */
186                 tcp_wait( session->sock_obj, connect_timeout ); /* make the timeout smarter XXX */
187         
188                 /* server acknowledges our existence, now see if we can login */
189                 if( session->state_machine->connecting == CONNECTING_2 ) {
190         
191                         int ss = session->session_id->n_used + strlen(password) + 5;
192                         char hashstuff[ss];
193                         memset(hashstuff,0,ss);
194                         sprintf( hashstuff, "%s%s", session->session_id->buf, password );
195
196                         char* hash = shahash( hashstuff );
197                         size2 = 100 + strlen( hash );
198                         char stanza2[ size2 ];
199                         memset( stanza2, 0, size2 );
200                         sprintf( stanza2, "<handshake>%s</handshake>", hash );
201         
202                         if( ! tcp_send( session->sock_obj, stanza2 )  ) {
203                                 warning_handler("error sending");
204                                 return 0;
205                         }
206                 }
207
208         } else { /* we're not a component */
209
210                 /* the first Jabber connect stanza */
211                 size1 = 100 + strlen( server );
212                 char stanza1[ size1 ]; 
213                 memset( stanza1, 0, size1 );
214                 sprintf( stanza1, 
215                                 "<stream:stream to='%s' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>",
216                         server );
217         
218
219                 /* send the first stanze */
220                 session->state_machine->connecting = CONNECTING_1;
221                 if( ! tcp_send( session->sock_obj, stanza1 ) ) {
222                         warning_handler("error sending");
223                         return 0;
224                 }
225
226
227                 /* wait for reply */
228                 tcp_wait( session->sock_obj, connect_timeout ); /* make the timeout smarter XXX */
229
230                 if( auth_type == AUTH_PLAIN ) {
231
232                         /* the second jabber connect stanza including login info*/
233                         size2 = 150 + strlen( username ) + strlen(password) + strlen(resource);
234                         char stanza2[ size2 ];
235                         memset( stanza2, 0, size2 );
236                 
237                         sprintf( stanza2, 
238                                         "<iq id='123456789' type='set'><query xmlns='jabber:iq:auth'><username>%s</username><password>%s</password><resource>%s</resource></query></iq>",
239                                         username, password, resource );
240         
241
242                 /*
243                 <iq type='set' id='auth2'>
244                         <query xmlns='jabber:iq:auth'>
245                                 <username>bill</username>
246                            <digest>48fc78be9ec8f86d8ce1c39c320c97c21d62334d</digest>
247                                 <resource>globe</resource>
248                         </query>
249                 </iq>
250                 */
251         
252         
253                         /* server acknowledges our existence, now see if we can login */
254                         if( session->state_machine->connecting == CONNECTING_2 ) {
255                                 if( ! tcp_send( session->sock_obj, stanza2 )  ) {
256                                         warning_handler("error sending");
257                                         return 0;
258                                 }
259                         }
260
261                 } else if( auth_type == AUTH_DIGEST ) {
262
263                         int ss = session->session_id->n_used + strlen(password) + 5;
264                         char hashstuff[ss];
265                         memset(hashstuff,0,ss);
266                         sprintf( hashstuff, "%s%s", session->session_id->buf, password );
267
268                         char* hash = shahash( hashstuff );
269
270                         /* the second jabber connect stanza including login info*/
271                         size2 = 150 + strlen( hash ) + strlen(password) + strlen(resource);
272                         char stanza2[ size2 ];
273                         memset( stanza2, 0, size2 );
274                 
275                         sprintf( stanza2, 
276                                         "<iq id='123456789' type='set'><query xmlns='jabber:iq:auth'><username>%s</username><digest>%s</digest><resource>%s</resource></query></iq>",
277                                         username, hash, resource );
278         
279
280                 /*
281                 <iq type='set' id='auth2'>
282                         <query xmlns='jabber:iq:auth'>
283                                 <username>bill</username>
284                            <digest>48fc78be9ec8f86d8ce1c39c320c97c21d62334d</digest>
285                                 <resource>globe</resource>
286                         </query>
287                 </iq>
288                 */
289         
290         
291                         /* server acknowledges our existence, now see if we can login */
292                         if( session->state_machine->connecting == CONNECTING_2 ) {
293                                 if( ! tcp_send( session->sock_obj, stanza2 )  ) {
294                                         warning_handler("error sending");
295                                         return 0;
296                                 }
297                         }
298
299                 }
300
301         } // not component
302
303
304         /* wait for reply */
305         tcp_wait( session->sock_obj, connect_timeout );
306
307         if( session->state_machine->connected ) {
308                 /* yar! */
309                 return 1;
310         }
311
312         return 0;
313 }
314
315 // ---------------------------------------------------------------------------------
316 // TCP data callback. Shove the data into the push parser.
317 // ---------------------------------------------------------------------------------
318 void grab_incoming( void * session, char* data ) {
319         transport_session* ses = (transport_session*) session;
320         if( ! ses ) { return; }
321         xmlParseChunk(ses->parser_ctxt, data, strlen(data), 0);
322 }
323
324
325 void startElementHandler(
326         void *session, const xmlChar *name, const xmlChar **atts) {
327
328         transport_session* ses = (transport_session*) session;
329         if( ! ses ) { return; }
330
331         
332         if( strcmp( name, "message" ) == 0 ) {
333                 ses->state_machine->in_message = 1;
334                 buffer_add( ses->from_buffer, get_xml_attr( atts, "from" ) );
335                 buffer_add( ses->recipient_buffer, get_xml_attr( atts, "to" ) );
336                 buffer_add( ses->router_from_buffer, get_xml_attr( atts, "router_from" ) );
337                 buffer_add( ses->router_to_buffer, get_xml_attr( atts, "router_to" ) );
338                 buffer_add( ses->router_class_buffer, get_xml_attr( atts, "router_class" ) );
339                 buffer_add( ses->router_command_buffer, get_xml_attr( atts, "router_command" ) );
340                 char* broadcast = get_xml_attr( atts, "broadcast" );
341                 if( broadcast )
342                         ses->router_broadcast = atoi( broadcast );
343
344                 return;
345         }
346
347         if( ses->state_machine->in_message ) {
348
349                 if( strcmp( name, "body" ) == 0 ) {
350                         ses->state_machine->in_message_body = 1;
351                         return;
352                 }
353         
354                 if( strcmp( name, "subject" ) == 0 ) {
355                         ses->state_machine->in_subject = 1;
356                         return;
357                 }
358         
359                 if( strcmp( name, "thread" ) == 0 ) {
360                         ses->state_machine->in_thread = 1;
361                         return;
362                 }
363
364         }
365
366         if( strcmp( name, "presence" ) == 0 ) {
367                 ses->state_machine->in_presence = 1;
368                 buffer_add( ses->from_buffer, get_xml_attr( atts, "from" ) );
369                 buffer_add( ses->recipient_buffer, get_xml_attr( atts, "to" ) );
370                 return;
371         }
372
373         if( strcmp( name, "status" ) == 0 ) {
374                 ses->state_machine->in_status = 1;
375                 return;
376         }
377
378
379         if( strcmp( name, "stream:error" ) == 0 ) {
380                 ses->state_machine->in_error = 1;
381                 warning_handler( "Received <stream:error> message from Jabber server" );
382                 return;
383         }
384
385
386         /* first server response from a connect attempt */
387         if( strcmp( name, "stream:stream" ) == 0 ) {
388                 if( ses->state_machine->connecting == CONNECTING_1 ) {
389                         ses->state_machine->connecting = CONNECTING_2;
390                         buffer_add( ses->session_id, get_xml_attr(atts, "id") );
391                 }
392         }
393
394         if( strcmp( name, "handshake" ) == 0 ) {
395                 ses->state_machine->connected = 1;
396                 ses->state_machine->connecting = 0;
397                 return;
398         }
399
400
401         if( strcmp( name, "error" ) == 0 ) {
402                 ses->state_machine->in_message_error = 1;
403                 buffer_add( ses->message_error_type, get_xml_attr( atts, "type" ) );
404                 ses->message_error_code = atoi( get_xml_attr( atts, "code" ) );
405                 warning_handler( "Received <error> message" );
406                 return;
407         }
408
409         if( strcmp( name, "iq" ) == 0 ) {
410                 ses->state_machine->in_iq = 1;
411
412                 if( strcmp( get_xml_attr(atts, "type"), "result") == 0 
413                                 && ses->state_machine->connecting == CONNECTING_2 ) {
414                         ses->state_machine->connected = 1;
415                         ses->state_machine->connecting = 0;
416                         return;
417                 }
418
419                 if( strcmp( get_xml_attr(atts, "type"), "error") == 0 ) {
420                         warning_handler( "Error connecting to jabber" );
421                         return;
422                 }
423         }
424 }
425
426 char* get_xml_attr( const xmlChar** atts, char* attr_name ) {
427         int i;
428         if (atts != NULL) {
429                 for(i = 0;(atts[i] != NULL);i++) {
430                         if( strcmp( atts[i++], attr_name ) == 0 ) {
431                                 if( atts[i] != NULL ) {
432                                         return (char*) atts[i];
433                                 }
434                         }
435                 }
436         }
437         return NULL;
438 }
439
440
441 // ------------------------------------------------------------------
442 // See which tags are ending
443 // ------------------------------------------------------------------
444 void endElementHandler( void *session, const xmlChar *name) {
445         transport_session* ses = (transport_session*) session;
446         if( ! ses ) { return; }
447
448         if( strcmp( name, "message" ) == 0 ) {
449
450                 /* pass off the message info the callback */
451                 if( ses->message_callback ) {
452
453                         /* here it's ok to pass in the raw buffers because
454                                 message_init allocates new space for the chars 
455                                 passed in */
456                         transport_message* msg =  message_init( 
457                                 ses->body_buffer->buf, 
458                                 ses->subject_buffer->buf,
459                                 ses->thread_buffer->buf, 
460                                 ses->recipient_buffer->buf, 
461                                 ses->from_buffer->buf );
462
463                         message_set_router_info( msg, 
464                                 ses->router_from_buffer->buf, 
465                                 ses->router_to_buffer->buf, 
466                                 ses->router_class_buffer->buf,
467                                 ses->router_command_buffer->buf,
468                                 ses->router_broadcast );
469
470                         if( ses->message_error_type->n_used > 0 ) {
471                                 set_msg_error( msg, ses->message_error_type->buf, ses->message_error_code );
472                         }
473
474                         if( msg == NULL ) { return; }
475                         ses->message_callback( ses->user_data, msg );
476                 }
477
478                 ses->state_machine->in_message = 0;
479                 reset_session_buffers( session );
480                 return;
481         }
482         
483         if( strcmp( name, "body" ) == 0 ) {
484                 ses->state_machine->in_message_body = 0;
485                 return;
486         }
487
488         if( strcmp( name, "subject" ) == 0 ) {
489                 ses->state_machine->in_subject = 0;
490                 return;
491         }
492
493         if( strcmp( name, "thread" ) == 0 ) {
494                 ses->state_machine->in_thread = 0;
495                 return;
496         }
497         
498         if( strcmp( name, "iq" ) == 0 ) {
499                 ses->state_machine->in_iq = 0;
500                 if( ses->message_error_code > 0 ) {
501                         warning_handler( "Error in IQ packet: code %d",  ses->message_error_code );
502                         warning_handler( "Error 401 means not authorized" );
503                 }
504                 reset_session_buffers( session );
505                 return;
506         }
507
508         if( strcmp( name, "presence" ) == 0 ) {
509                 ses->state_machine->in_presence = 0;
510                 /*
511                 if( ses->presence_callback ) {
512                         // call the callback with the status, etc.
513                 }
514                 */
515                 reset_session_buffers( session );
516                 return;
517         }
518
519         if( strcmp( name, "status" ) == 0 ) {
520                 ses->state_machine->in_status = 0;
521                 return;
522         }
523
524         if( strcmp( name, "error" ) == 0 ) {
525                 ses->state_machine->in_message_error = 0;
526                 return;
527         }
528
529         if( strcmp( name, "error:error" ) == 0 ) {
530                 ses->state_machine->in_error = 0;
531                 return;
532         }
533 }
534
535 int reset_session_buffers( transport_session* ses ) {
536         buffer_reset( ses->body_buffer );
537         buffer_reset( ses->subject_buffer );
538         buffer_reset( ses->thread_buffer );
539         buffer_reset( ses->from_buffer );
540         buffer_reset( ses->recipient_buffer );
541         buffer_reset( ses->router_from_buffer );
542         buffer_reset( ses->router_to_buffer );
543         buffer_reset( ses->router_class_buffer );
544         buffer_reset( ses->router_command_buffer );
545         buffer_reset( ses->message_error_type );
546         buffer_reset( ses->session_id );
547
548         return 1;
549 }
550
551 // ------------------------------------------------------------------
552 // takes data out of the body of the message and pushes it into
553 // the appropriate buffer
554 // ------------------------------------------------------------------
555 void characterHandler(
556                 void *session, const xmlChar *ch, int len) {
557
558         char data[len+1];
559         memset( data, 0, len+1 );
560         strncpy( data, (char*) ch, len );
561         data[len] = 0;
562
563         //printf( "Handling characters: %s\n", data );
564         transport_session* ses = (transport_session*) session;
565         if( ! ses ) { return; }
566
567         /* set the various message parts */
568         if( ses->state_machine->in_message ) {
569
570                 if( ses->state_machine->in_message_body ) {
571                         buffer_add( ses->body_buffer, data );
572                 }
573
574                 if( ses->state_machine->in_subject ) {
575                         buffer_add( ses->subject_buffer, data );
576                 }
577
578                 if( ses->state_machine->in_thread ) {
579                         buffer_add( ses->thread_buffer, data );
580                 }
581         }
582
583         /* set the presence status */
584         if( ses->state_machine->in_presence && ses->state_machine->in_status ) {
585                 buffer_add( ses->status_buffer, data );
586         }
587
588         if( ses->state_machine->in_error ) {
589                 /* for now... */
590                 warning_handler( "ERROR Xml fragment: %s\n", ch );
591         }
592
593 }
594
595 /* XXX change to warning handlers */
596 void  parseWarningHandler( void *session, const char* msg, ... ) {
597
598         va_list args;
599         va_start(args, msg);
600         fprintf(stdout, "WARNING");
601         vfprintf(stdout, msg, args);
602         va_end(args);
603         fprintf(stderr, "XML WARNING: %s\n", msg ); 
604 }
605
606 void  parseErrorHandler( void *session, const char* msg, ... ){
607
608         va_list args;
609         va_start(args, msg);
610         fprintf(stdout, "ERROR");
611         vfprintf(stdout, msg, args);
612         va_end(args);
613         fprintf(stderr, "XML ERROR: %s\n", msg ); 
614
615 }
616
617 int session_disconnect( transport_session* session ) {
618         if( session == NULL ) { return 0; }
619         tcp_send( session->sock_obj, "</stream:stream>");
620         return tcp_disconnect( session->sock_obj );
621 }
622