-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiGtalk.c
executable file
·738 lines (529 loc) · 15.2 KB
/
iGtalk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/*
** client.c -- a socket client demo
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/time.h>
#include <sys/select.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <termios.h>
#include <recv_xml.h>
#include <gsasl.h>
//#include <gsasl-compat.h>
//#include <gsasl-mech.h>
//#include <gsasl-compat.h>
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
//#include <gnutls/gnutlsxx.h>
#include <stdint.h>
#include <arpa/inet.h>
#define MAXDATASIZE 2048// max number of bytes we can get at once
#define CAFILE "/etc/ssl/certs/ca-certificates.crt"
struct roster_entry{
struct roster_entry * next;
struct roster_entry * prev;
char status[MAXDATASIZE];
char jid[MAXDATASIZE];
char show[MAXDATASIZE];
};
// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa){
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
void sendstr(int sockfd,char* buf){
int sent = 0;
int bufsize = strlen(buf);
while( sent < bufsize){
sent += send(sockfd, &buf[sent], bufsize-sent, 0);
}
}
void print_roster(struct roster_entry *roster){
struct roster_entry * curr = roster;
while(curr != NULL){
char username[MAXDATASIZE];
int u=0;
while(curr->jid[u] != '/'){
username[u] = curr->jid[u];
u++;
}
username[u] = '\0';
printf("%s\t%s\n\t%s\n", curr->show, username,curr->status);
curr = curr->next;
}
}
void print_message(Xml_Stanza* stanza){
char username[MAXDATASIZE];
int u=0;
while(stanza->data.message_data.from[u] != '/'){
username[u] = stanza->data.message_data.from[u];
u++;
}
username[u] = '\0';
printf("%s: %s\n", username, stanza->data.message_data.message);
}
void sendstr_tls(gnutls_session_t session,char* buf){
int sent = 0;
int bufsize = strlen(buf);
while( sent < bufsize){
sent += gnutls_record_send(session, &buf[sent], bufsize-sent);
}
}
void sendchat(gnutls_session_t ses,char * tojid, char * fromjid, char * message){
char chatxml[MAXDATASIZE];
sprintf(chatxml, "<message to='%s' from='%s' type='chat' xml:lang='en'><body>%s</body></message>",tojid, fromjid, message);
sendstr_tls(ses, chatxml);
}
Xml_Stanza recv_and_print(sockfd){
Xml_Stanza *tempStanza;
tempStanza=NULL;
while(tempStanza == NULL){
tempStanza = recv_xml_stream(sockfd);
}
// printf("client: received '%s'\n",tempStanza->stream);
return *tempStanza;
}
gnutls_session_t tlsInit(int sd)
{
int ret;//,sd, ii;
gnutls_session_t session;
// char buffer[MAXDATASIZE];
const char *err;
gnutls_certificate_credentials_t xcred;
gnutls_global_init ();
/* X509 stuff */
gnutls_certificate_allocate_credentials (&xcred);
/* sets the trusted cas file
*/
gnutls_certificate_set_x509_trust_file (xcred, CAFILE, GNUTLS_X509_FMT_PEM);
// gnutls_certificate_set_verify_function (xcred, _verify_certificate_callback);
/* If client holds a certificate it can be set using the following:
*
gnutls_certificate_set_x509_key_file (xcred,
"cert.pem", "key.pem",
GNUTLS_X509_FMT_PEM);
*/
/* Initialize TLS session
*/
gnutls_init (&session, GNUTLS_CLIENT);
gnutls_session_set_ptr (session, (void *) "my_host_name");
gnutls_server_name_set (session, GNUTLS_NAME_DNS, "my_host_name",
strlen("my_host_name"));
/* Use default priorities */
ret = gnutls_priority_set_direct (session, "NORMAL", &err);
if (ret < 0)
{
if (ret == GNUTLS_E_INVALID_REQUEST)
{
fprintf (stderr, "Syntax error at: %s\n", err);
}
exit (1);
}
/* put the x509 credentials to the current session
*/
gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, xcred);
/* connect to the peer
*/
//sd = tcp_connect ();
// int32_t sd32 = (int32_t)(sd & 0xFFFFFFFF);
long unsigned int sdlong = (long unsigned int) sd;
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sdlong);
// gnutls_handshake_set_timeout (session, GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
/* Perform the TLS handshake
*/
do
{
ret = gnutls_handshake (session);
}
while (ret < 0 && gnutls_error_is_fatal (ret) == 0);
if (ret < 0)
{
fprintf (stderr, "*** Handshake failed\n");
gnutls_perror (ret);
goto end;
}
/* else
{
printf ("- Handshake was completed\n");
}
// gnutls_record_send (session, MSG, strlen (MSG));
ret = gnutls_record_recv (session, buffer, MAXDATASIZE);
if (ret == 0)
{
printf ("- Peer has closed the TLS connection\n");
goto end;
}
else if (ret < 0)
{
fprintf (stderr, "*** Error: %s\n", gnutls_strerror (ret));
goto end;
}
printf ("- Received %d bytes: ", ret);
for (ii = 0; ii < ret; ii++)
{
fputc (buffer[ii], stdout);
}
fputs ("\n", stdout);
*/
// gnutls_bye (session, GNUTLS_SHUT_RDWR);
end:
// tcp_close (sd);
// gnutls_deinit (session);
gnutls_certificate_free_credentials (xcred);
// gnutls_global_deinit ();
return session;
}
static char*
client_authenticate (Gsasl_session * session)
{
char buf[BUFSIZ] = "";
char *p;
int rc;
/* This loop mimics a protocol where the client send data first. */
do
{
/* Generate client output. */
rc = gsasl_step64 (session, buf, &p);
if (rc == GSASL_NEEDS_MORE || rc == GSASL_OK)
{
/* If sucessful, print it. */
// printf ("Output:\n%s\n", p);
// gsasl_free (p);
}
if (rc == GSASL_NEEDS_MORE)
{
/* If the client need more data from server, get it here. */
printf ("Input base64 encoded data from server:\n");
p = fgets (buf, sizeof (buf) - 1, stdin);
if (p == NULL)
{
perror ("fgets");
return NULL;
}
if (buf[strlen (buf) - 1] == '\n')
buf[strlen (buf) - 1] = '\0';
}
}
while (rc == GSASL_NEEDS_MORE);
printf ("\n");
if (rc != GSASL_OK)
{
printf ("Authentication error (%d): %s\n", rc, gsasl_strerror (rc));
return NULL;
}
return p;
}
static char*
client (Gsasl * ctx)
{
Gsasl_session *session;
const char *mech = "PLAIN";
int rc;
struct termios oflags, nflags;
/* disabling echo */
tcgetattr(fileno(stdin), &oflags);
nflags = oflags;
nflags.c_lflag &= ~ECHO;
nflags.c_lflag |= ECHONL;
//prompt for username/password
char usrname[MAXDATASIZE];
char passwd[MAXDATASIZE];
printf("Username: ");
scanf("%s", usrname);
if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) {
perror("tcsetattr");
return "EXIT_FAILURE";
}
printf("Password: ");
scanf("%s", passwd);
/* restore terminal */
if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) {
perror("tcsetattr");
return "EXIT_FAILURE";
}
/* Create new authentication session. */
if ((rc = gsasl_client_start (ctx, mech, &session)) != GSASL_OK)
{
printf ("Cannot initialize client (%d): %s\n", rc, gsasl_strerror (rc));
return NULL;
}
/* Set username and password in session handle. This info will be
lost when this session is deallocated below. */
gsasl_property_set (session, GSASL_AUTHID, usrname);
gsasl_property_set (session, GSASL_PASSWORD, passwd);
// gsasl_property_set (session, GSASL_AUTHID, "parsoj");
// gsasl_property_set (session, GSASL_PASSWORD, "jeff88831");
/* Do it. */
char * p = client_authenticate (session);
/* Cleanup. */
gsasl_finish (session);
return p;
}
void update_roster(struct roster_entry ** roster, Xml_Stanza *stanza){
struct roster_entry * curr = *roster;
int found = 0;
struct roster_entry * prev = NULL;
if(stanza->type == ST_AVAILABLE){
while( curr != NULL){
if(stanza->data.available_data.jid == curr->jid){
// strcpy(curr->rdata.jid, stanza->data.available_data.jid);
strcpy(curr->show, stanza->data.available_data.show);
strcpy(curr->status, stanza->data.available_data.status);
found = 1;
}
prev = curr;
curr = curr->next;
}
if(found == 0){
curr = (struct roster_entry*)malloc(sizeof(struct roster_entry));
if(*roster == NULL) *roster = curr;
if(prev != NULL) prev->next = curr;
strcpy(curr->jid, stanza->data.available_data.jid);
strcpy(curr->show, stanza->data.available_data.show);
strcpy(curr->status, stanza->data.available_data.status);
curr->next = NULL;
}
}
else{
while(curr != NULL){
if(stanza->data.available_data.jid == curr->jid){
prev->next = curr->next;
free(curr);
}
prev = curr;
curr = curr->next;
}
}
return;
}
int main(int argc, char *argv[])
{
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
char s[INET6_ADDRSTRLEN];
if (argc != 3) {
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
//strcpy(buf,"<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' to='gmail.com' version='1.0'>");
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((rv = getaddrinfo(argv[1], argv[2], &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("client: socket");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("client: connect");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "client: failed to connect\n");
return 2;
}
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),
s, sizeof s);
printf("client: connecting to %s:port %s\n", s,argv[2]);
freeaddrinfo(servinfo); // all done with this structure
sendstr(sockfd, "<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' to='gmail.com' version='1.0'>");
// recv_and_print(sockfd);
// recv_and_print(sockfd);
Xml_Stanza *tempStanza;
tempStanza=NULL;
while(tempStanza == NULL){
tempStanza = recv_xml_stream(sockfd);
}
// printf("client: received '%s'\n",tempStanza->stream);
tempStanza=NULL;
while(tempStanza == NULL){
tempStanza = recv_xml_stream(sockfd);
}
// printf("client: received '%s'\n",tempStanza->stream);
//p4--PART4
sendstr(sockfd,"<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
tempStanza=NULL;
while(tempStanza == NULL){
tempStanza = recv_xml_stream(sockfd);
}
//printf("client: received '%s'\n",tempStanza->stream);
gnutls_session_t tlsSession = tlsInit(sockfd);
//p5--PART5
Gsasl *ctx = NULL;
int rc;
//init GSASL
if ((rc = gsasl_init (&ctx)) != GSASL_OK)
{
printf ("Cannot initialize libgsasl (%d): %s",
rc, gsasl_strerror (rc));
return 1;
}
char * step64out = client(ctx);
sendstr_tls(tlsSession, "<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' to='gmail.com' version='1.0'>");
tempStanza = NULL;
while(tempStanza == NULL){
tempStanza = recv_tls_xml_stream(tlsSession);
}
// printf("client: received '%s'\n",tempStanza->stream);
tempStanza = NULL;
while(tempStanza == NULL){
tempStanza = recv_tls_xml_stream(tlsSession);
}
// printf("client: received '%s'\n",tempStanza->stream);
char saslAuthXml[MAXDATASIZE];
sprintf(saslAuthXml,"<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN' xmlns:ga='http://www.google.com/talk/protocol/auth' ga:client-uses-full-bind-result='true'>%s</auth>",step64out );
// printf("%s\n",saslAuthXml);
gsasl_free(step64out);
sendstr_tls(tlsSession, saslAuthXml);
tempStanza = NULL;
while(tempStanza == NULL){
tempStanza = recv_tls_xml_stream(tlsSession);
}
// printf("client: received '%s'\n",tempStanza->stream);
//p6--PART6
sendstr_tls(tlsSession, "<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' to='gmail.com' version='1.0'>");
tempStanza = NULL;
while(tempStanza == NULL){
tempStanza = recv_tls_xml_stream(tlsSession);
}
// printf("client: received '%s'\n",tempStanza->stream);
tempStanza = NULL;
while(tempStanza == NULL){
tempStanza = recv_tls_xml_stream(tlsSession);
}
// printf("client: received '%s'\n",tempStanza->stream);
sendstr_tls(tlsSession, "<iq type='set' id='bind 1'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource> gmail.</resource></bind></iq>");
//get JID here
tempStanza = NULL;
while(tempStanza == NULL){
tempStanza = recv_tls_xml_stream(tlsSession);
}
//printf("client: received '%s'\n",tempStanza->stream);
char jid[MAXDATASIZE];
strcpy(jid,tempStanza->data.bind_data.jid);
// printf("JID is: %s\n",jid);
sendstr_tls(tlsSession, "<iq to='gmail.com' type='set' id='sess 1'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>");
tempStanza = NULL;
while(tempStanza == NULL){
tempStanza = recv_tls_xml_stream(tlsSession);
}
// printf("DEBUG:client: received '%s'\n",tempStanza->stream);
sendstr_tls(tlsSession, "<presence/>");
tempStanza = NULL;
while(tempStanza == NULL){
tempStanza = recv_tls_xml_stream(tlsSession);
}
// printf("DEBUG: client: received '%s'\n",tempStanza->stream);
//p7--PART7
fd_set rd, wr, ex;
char buf[MAXDATASIZE];
struct roster_entry * roster = NULL;
char toUsrname[MAXDATASIZE];
toUsrname[0] = '\0';
for(;;){
FD_ZERO(&rd);
FD_ZERO(&wr);
FD_ZERO(&ex);
FD_SET(sockfd, &rd);
FD_SET(fileno(stdin), &rd);
FD_SET(sockfd,&wr);
if (select(sockfd+1,&rd,&wr,&ex,NULL) == -1){
perror("select:");
exit(1);
}
//if there are any data ready to read from the socket
if (FD_ISSET(sockfd, &rd)){
//printf it
tempStanza = NULL;
while(tempStanza == NULL){
tempStanza = recv_tls_xml_stream(tlsSession);
}
if(tempStanza->type == ST_AVAILABLE){
update_roster(&roster, tempStanza);
}
if(tempStanza->type == ST_UNAVAILABLE){
update_roster(&roster, tempStanza);
}
if(tempStanza->type == ST_MESSAGE){
print_message(tempStanza);
}
// printf("client: received '%s'\n",tempStanza->stream);
}
// if there is something in stdin
if (FD_ISSET(fileno(stdin), &rd)){
// if(FD_ISSET(sockfd, &wr)){
int numbytes;
//read from keyboard
numbytes = read(fileno(stdin), buf, MAXDATASIZE);
buf[numbytes] = '\0';
//printf("DEBUG: %d BYTES READ\n contents--%s\n",numbytes,buf);
//check for :
if(buf[0] ==':'){
//check for :q
if((numbytes == 3) && (buf[1] == 'q')){
sendstr_tls(tlsSession,"<presence from='%s' type='unavailable'/>");
sendstr_tls(tlsSession,"</stream:stream>");
break;
}
//check for :roster
else if((numbytes == 8) && (strncmp(buf, ":roster", 7) == 0)){
if(roster == NULL) printf("ROSTER EMPTY\n");
print_roster(roster);
}
//check for :to
else if((numbytes > 5) && (buf[1] == 't') && (buf[2] == 'o')){
strcpy(toUsrname, &buf[4]);
// printf("Messages will be directed towards %s.\n",toUsrname);
}
}
else{
struct roster_entry* curr = roster;
int found = 0;
while(curr != NULL){
if( 0 == strncmp(toUsrname,curr->jid, strlen(toUsrname)-1)){
sendchat(tlsSession, curr->jid,jid, buf);
found = 1;
break;
}
curr = curr->next;
}
if(found == 0){
// printf("targeted Gtalk User is not available\n");
}
}
// }
}
}
//printf("Goodbye!\n");
struct roster_entry * coll = roster;
while(coll != NULL){
struct roster_entry * tail = coll;
coll = coll->next;
free(tail);
}
//gsasl_free(tlsSession);
gnutls_deinit (tlsSession);
// gnutls_certificate_free_credentials (xcred);
gnutls_global_deinit ();
gsasl_done(ctx);
close(sockfd);
return 0;
}