-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmpp_streams.c
1890 lines (1699 loc) · 58.3 KB
/
xmpp_streams.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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* mod_rayo for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2013-2015, Grasshopper
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mod_rayo for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is Grasshopper
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Chris Rienzo <[email protected]>
*
* xmpp_streams.c -- XMPP s2s and c2s streams
*
*/
#include <switch.h>
#include <iksemel.h>
#include <openssl/ssl.h>
#include "xmpp_streams.h"
#include "iks_helpers.h"
#include "sasl.h"
#define MAX_QUEUE_LEN 25000
/**
* Context for all streams
*/
struct xmpp_stream_context {
/** memory pool to use */
switch_memory_pool_t *pool;
/** domain for this context */
const char *domain;
/** synchronizes access to streams and routes hashes */
switch_mutex_t *streams_mutex;
/** map of stream JID to routable stream */
switch_hash_t *routes;
/** map of stream ID to stream */
switch_hash_t *streams;
/** map of user ID to password */
switch_hash_t *users;
/** shared secret for server dialback */
const char *dialback_secret;
/** callback when a new resource is bound */
xmpp_stream_bind_callback bind_callback;
/** callback when a new stream is ready */
xmpp_stream_ready_callback ready_callback;
/** callback when a stream is destroyed */
xmpp_stream_destroy_callback destroy_callback;
/** callback when a stanza is received */
xmpp_stream_recv_callback recv_callback;
/** context shutdown flag */
int shutdown;
/** prevents context shutdown until all threads are finished */
switch_thread_rwlock_t *shutdown_rwlock;
/** path to cert PEM file */
const char *cert_pem_file;
/** path to key PEM file */
const char *key_pem_file;
};
/**
* State of a stream
*/
enum xmpp_stream_state {
/** new connection */
XSS_CONNECT,
/** encrypted comms established */
XSS_SECURE,
/** remote party authenticated */
XSS_AUTHENTICATED,
/** client resource bound */
XSS_RESOURCE_BOUND,
/** ready to accept requests */
XSS_READY,
/** terminating stream */
XSS_SHUTDOWN,
/** unrecoverable error */
XSS_ERROR,
/** destroyed */
XSS_DESTROY
};
/**
* A client/server stream connection
*/
struct xmpp_stream {
/** stream state */
enum xmpp_stream_state state;
/** true if server-to-server connection */
int s2s;
/** true if incoming connection */
int incoming;
/** Jabber ID of remote party */
char *jid;
/** stream ID */
char *id;
/** stream pool */
switch_memory_pool_t *pool;
/** address of this stream */
const char *address;
/** port of this stream */
int port;
/** synchronizes access to this stream */
switch_mutex_t *mutex;
/** socket to remote party */
switch_socket_t *socket;
/** socket poll descriptor */
switch_pollfd_t *pollfd;
/** XML stream parser */
iksparser *parser;
/** outbound message queue */
switch_queue_t *msg_queue;
/** true if no activity last poll */
int idle;
/** context for this stream */
struct xmpp_stream_context *context;
/** user private data */
void *user_private;
};
/**
* A socket listening for new connections
*/
struct xmpp_listener {
/** listener pool */
switch_memory_pool_t *pool;
/** listen address */
char *addr;
/** listen port */
switch_port_t port;
/** access control list */
const char *acl;
/** listen socket */
switch_socket_t *socket;
/** pollset for listen socket */
switch_pollfd_t *read_pollfd;
/** true if server to server connections only */
int s2s;
/** context for new streams */
struct xmpp_stream_context *context;
};
static void xmpp_stream_new_id(struct xmpp_stream *stream);
static void xmpp_stream_set_id(struct xmpp_stream *stream, const char *id);
/**
* Convert xmpp stream state to string
* @param state the xmpp stream state
* @return the string value of state or "UNKNOWN"
*/
static const char *xmpp_stream_state_to_string(enum xmpp_stream_state state)
{
switch(state) {
case XSS_CONNECT: return "CONNECT";
case XSS_SECURE: return "SECURE";
case XSS_AUTHENTICATED: return "AUTHENTICATED";
case XSS_RESOURCE_BOUND: return "RESOURCE_BOUND";
case XSS_READY: return "READY";
case XSS_SHUTDOWN: return "SHUTDOWN";
case XSS_ERROR: return "ERROR";
case XSS_DESTROY: return "DESTROY";
}
return "UNKNOWN";
}
/**
* Handle XMPP stream logging callback
* @param user_data the xmpp stream
* @param data the log message
* @param size of the log message
* @param is_incoming true if this is a log for a received message
*/
static void on_stream_log(void *user_data, const char *data, size_t size, int is_incoming)
{
if (size > 0) {
struct xmpp_stream *stream = (struct xmpp_stream *)user_data;
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_DEBUG, "%s, %s:%i, %s_%s %s %s\n", stream->jid, stream->address, stream->port, stream->s2s ? "s2s" : "c2s",
stream->incoming ? "in" : "out", is_incoming ? "RECV" : "SEND", data);
}
}
/**
* Send stanza to stream.
*/
static void xmpp_stream_stanza_send(struct xmpp_stream *stream, iks *msg)
{
/* send directly if client or outbound s2s stream */
if (!stream->s2s || !stream->incoming) {
iks_send(stream->parser, msg);
iks_delete(msg);
} else {
/* route message to outbound server stream */
xmpp_stream_context_send(stream->context, stream->jid, msg);
iks_delete(msg);
}
}
/**
* Attach stream to connected socket
* @param stream the stream
* @param socket the connected socket
*/
static void xmpp_stream_set_socket(struct xmpp_stream *stream, switch_socket_t *socket)
{
stream->socket = socket;
switch_socket_create_pollset(&stream->pollfd, stream->socket, SWITCH_POLLIN | SWITCH_POLLERR, stream->pool);
/* connect XMPP stream parser to socket */
{
switch_os_socket_t os_socket;
switch_os_sock_get(&os_socket, stream->socket);
iks_connect_fd(stream->parser, os_socket);
/* TODO connect error checking */
}
}
/**
* Assign a new ID to the stream
* @param stream the stream
*/
static void xmpp_stream_new_id(struct xmpp_stream *stream)
{
char id[SWITCH_UUID_FORMATTED_LENGTH + 1] = { 0 };
switch_uuid_str(id, sizeof(id));
xmpp_stream_set_id(stream, id);
}
/**
* Send session reply to server <stream> after auth is done
* @param stream the xmpp stream
*/
static void xmpp_send_server_header_features(struct xmpp_stream *stream)
{
struct xmpp_stream_context *context = stream->context;
char *header = switch_mprintf(
"<stream:stream xmlns='"IKS_NS_SERVER"' xmlns:db='"IKS_NS_XMPP_DIALBACK"'"
" from='%s' id='%s' xml:lang='en' version='1.0'"
" xmlns:stream='"IKS_NS_XMPP_STREAMS"'><stream:features>"
"</stream:features>", context->domain, stream->id);
iks_send_raw(stream->parser, header);
free(header);
}
/**
* Send bind + session reply to client <stream>
* @param stream the xmpp stream
*/
static void xmpp_send_client_header_bind(struct xmpp_stream *stream)
{
struct xmpp_stream_context *context = stream->context;
char *header = switch_mprintf(
"<stream:stream xmlns='"IKS_NS_CLIENT"' xmlns:db='"IKS_NS_XMPP_DIALBACK"'"
" from='%s' id='%s' xml:lang='en' version='1.0'"
" xmlns:stream='"IKS_NS_XMPP_STREAMS"'><stream:features>"
"<bind xmlns='"IKS_NS_XMPP_BIND"'/>"
"<session xmlns='"IKS_NS_XMPP_SESSION"'/>"
"</stream:features>", context->domain, stream->id);
iks_send_raw(stream->parser, header);
free(header);
}
/**
* Handle <presence> message callback
* @param stream the stream
* @param node the presence message
*/
static void on_stream_presence(struct xmpp_stream *stream, iks *node)
{
struct xmpp_stream_context *context = stream->context;
const char *from = iks_find_attrib(node, "from");
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_DEBUG, "%s, %s:%i, presence, state = %s\n", stream->jid, stream->address, stream->port, xmpp_stream_state_to_string(stream->state));
if (!from) {
if (stream->s2s) {
/* from is required in s2s connections */
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_DEBUG, "%s, %s:%i, no presence from JID\n", stream->jid, stream->address, stream->port);
return;
}
/* use stream JID if a c2s connection */
from = stream->jid;
if (zstr(from)) {
/* error */
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_DEBUG, "%s, %s:%i, no presence from JID\n", stream->jid, stream->address, stream->port);
return;
}
iks_insert_attrib(node, "from", from);
}
if (context->recv_callback) {
context->recv_callback(stream, node);
}
}
/**
* Send <success> reply to xmpp stream <auth>
* @param stream the xmpp stream.
*/
static void xmpp_send_auth_success(struct xmpp_stream *stream)
{
iks_send_raw(stream->parser, "<success xmlns='"IKS_NS_XMPP_SASL"'/>");
}
/**
* Send <failure> reply to xmpp client <auth>
* @param stream the xmpp stream to use.
* @param reason the reason for failure
*/
static void xmpp_send_auth_failure(struct xmpp_stream *stream, const char *reason)
{
char *reply = switch_mprintf("<failure xmlns='"IKS_NS_XMPP_SASL"'>"
"<%s/></failure>", reason);
iks_send_raw(stream->parser, reply);
free(reply);
}
/**
* Validate username and password
* @param authzid authorization id
* @param authcid authentication id
* @param password
* @return 1 if authenticated
*/
static int verify_plain_auth(struct xmpp_stream_context *context, const char *authzid, const char *authcid, const char *password)
{
char *correct_password;
if (zstr(authzid) || zstr(authcid) || zstr(password)) {
return 0;
}
correct_password = switch_core_hash_find(context->users, authcid);
return !zstr(correct_password) && !strcmp(correct_password, password);
}
/**
* Send sasl reply to xmpp <stream>
* @param stream the xmpp stream
*/
static void xmpp_send_client_header_auth(struct xmpp_stream *stream)
{
struct xmpp_stream_context *context = stream->context;
char *header = switch_mprintf(
"<stream:stream xmlns='"IKS_NS_CLIENT"' xmlns:db='"IKS_NS_XMPP_DIALBACK"'"
" from='%s' id='%s' xml:lang='en' version='1.0'"
" xmlns:stream='"IKS_NS_XMPP_STREAMS"'><stream:features>"
"<mechanisms xmlns='"IKS_NS_XMPP_SASL"'>"
"<mechanism>PLAIN</mechanism>"
"</mechanisms></stream:features>", context->domain, stream->id);
iks_send_raw(stream->parser, header);
free(header);
}
/**
* Send sasl + starttls reply to xmpp <stream>
* @param stream the xmpp stream
*/
static void xmpp_send_client_header_tls(struct xmpp_stream *stream)
{
if (stream->context->key_pem_file && stream->context->cert_pem_file) {
struct xmpp_stream_context *context = stream->context;
char *header = switch_mprintf(
"<stream:stream xmlns='"IKS_NS_CLIENT"' xmlns:db='"IKS_NS_XMPP_DIALBACK"'"
" from='%s' id='%s' xml:lang='en' version='1.0'"
" xmlns:stream='"IKS_NS_XMPP_STREAMS"'><stream:features>"
"<starttls xmlns='"IKS_NS_XMPP_TLS"'><required/></starttls>"
"<mechanisms xmlns='"IKS_NS_XMPP_SASL"'>"
"<mechanism>PLAIN</mechanism>"
"</mechanisms></stream:features>", context->domain, stream->id);
iks_send_raw(stream->parser, header);
free(header);
} else {
/* not set up for TLS, skip it */
stream->state = XSS_SECURE;
xmpp_send_client_header_auth(stream);
}
}
/**
* Send sasl reply to xmpp <stream>
* @param stream the xmpp stream
*/
static void xmpp_send_server_header_auth(struct xmpp_stream *stream)
{
struct xmpp_stream_context *context = stream->context;
char *header = switch_mprintf(
"<stream:stream xmlns='"IKS_NS_SERVER"' xmlns:db='"IKS_NS_XMPP_DIALBACK"'"
" from='%s' id='%s' xml:lang='en' version='1.0'"
" xmlns:stream='"IKS_NS_XMPP_STREAMS"'>"
"<stream:features>"
"</stream:features>",
context->domain, stream->id);
iks_send_raw(stream->parser, header);
free(header);
}
/**
* Send dialback to receiving server
*/
static void xmpp_send_dialback_key(struct xmpp_stream *stream)
{
struct xmpp_stream_context *context = stream->context;
char *dialback_key = iks_server_dialback_key(context->dialback_secret, stream->jid, context->domain, stream->id);
if (dialback_key) {
char *dialback = switch_mprintf(
"<db:result from='%s' to='%s'>%s</db:result>",
context->domain, stream->jid,
dialback_key);
iks_send_raw(stream->parser, dialback);
free(dialback);
free(dialback_key);
} else {
/* TODO missing shared secret */
}
}
/**
* Send initial <stream> header to peer server
* @param stream the xmpp stream
*/
static void xmpp_send_outbound_server_header(struct xmpp_stream *stream)
{
struct xmpp_stream_context *context = stream->context;
char *header = switch_mprintf(
"<stream:stream xmlns='"IKS_NS_SERVER"' xmlns:db='"IKS_NS_XMPP_DIALBACK"'"
" from='%s' to='%s' xml:lang='en' version='1.0'"
" xmlns:stream='"IKS_NS_XMPP_STREAMS"'>", context->domain, stream->jid);
iks_send_raw(stream->parser, header);
free(header);
}
/**
* Handle <starttls> message.
* @param the xmpp stream
* @param node the <starttls> packet
*/
static void on_stream_starttls(struct xmpp_stream *stream, iks *node)
{
/* wait for handshake to start */
if (iks_proceed_tls(stream->parser, stream->context->cert_pem_file, stream->context->key_pem_file) == IKS_OK) {
stream->state = XSS_SECURE;
} else {
stream->state = XSS_ERROR;
}
}
/**
* Handle <auth> message. Only PLAIN supported.
* @param stream the xmpp stream
* @param node the <auth> packet
*/
static void on_stream_auth(struct xmpp_stream *stream, iks *node)
{
struct xmpp_stream_context *context = stream->context;
const char *xmlns, *mechanism;
iks *auth_body;
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_DEBUG, "%s, %s:%i, auth, state = %s\n", stream->jid, stream->address, stream->port, xmpp_stream_state_to_string(stream->state));
/* wrong state for authentication */
if (stream->state != XSS_SECURE) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_WARNING, "%s, %s:%i, auth UNEXPECTED, state = %s\n", stream->jid, stream->address, stream->port, xmpp_stream_state_to_string(stream->state));
/* on_auth unexpected error */
stream->state = XSS_ERROR;
return;
}
/* unsupported authentication type */
xmlns = iks_find_attrib_soft(node, "xmlns");
if (strcmp(IKS_NS_XMPP_SASL, xmlns)) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_WARNING, "%s, %s:%i, auth, state = %s, unsupported namespace: %s!\n", stream->jid, stream->address, stream->port, xmpp_stream_state_to_string(stream->state), xmlns);
/* on_auth namespace error */
stream->state = XSS_ERROR;
return;
}
/* unsupported SASL authentication mechanism */
mechanism = iks_find_attrib_soft(node, "mechanism");
if (strcmp("PLAIN", mechanism)) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_WARNING, "%s, %s:%i, auth, state = %s, unsupported SASL mechanism: %s!\n", stream->jid, stream->address, stream->port, xmpp_stream_state_to_string(stream->state), mechanism);
xmpp_send_auth_failure(stream, "invalid-mechanism");
stream->state = XSS_ERROR;
return;
}
if ((auth_body = iks_child(node)) && iks_type(auth_body) == IKS_CDATA) {
/* get user and password from auth */
char *message = iks_cdata(auth_body);
char *authzid = NULL, *authcid, *password;
/* TODO use library for SASL! */
parse_plain_auth_message(message, &authzid, &authcid, &password);
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_DEBUG, "%s, %s:%i, auth, state = %s, SASL/PLAIN decoded authzid = \"%s\" authcid = \"%s\"\n", stream->jid, stream->address, stream->port, xmpp_stream_state_to_string(stream->state), authzid, authcid);
if (verify_plain_auth(context, authzid, authcid, password)) {
stream->jid = switch_core_strdup(stream->pool, authzid);
if (!stream->s2s && !strchr(stream->jid, '@')) {
/* add missing domain on client stream */
stream->jid = switch_core_sprintf(stream->pool, "%s@%s", stream->jid, context->domain);
}
xmpp_send_auth_success(stream);
stream->state = XSS_AUTHENTICATED;
} else {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_WARNING, "%s, %s:%i, auth, state = %s, invalid user or password!\n", stream->jid, stream->address, stream->port, xmpp_stream_state_to_string(stream->state));
xmpp_send_auth_failure(stream, "not-authorized");
stream->state = XSS_ERROR;
}
switch_safe_free(authzid);
switch_safe_free(authcid);
switch_safe_free(password);
} else {
/* missing message */
stream->state = XSS_ERROR;
}
}
/**
* Handle <iq><session> request
* @param stream the xmpp stream
* @param node the <iq> node
* @return NULL
*/
static iks *on_iq_set_xmpp_session(struct xmpp_stream *stream, iks *node)
{
struct xmpp_stream_context *context = stream->context;
iks *reply;
switch(stream->state) {
case XSS_RESOURCE_BOUND: {
if (context->ready_callback && !context->ready_callback(stream)) {
reply = iks_new_error(node, STANZA_ERROR_INTERNAL_SERVER_ERROR);
stream->state = XSS_ERROR;
} else {
reply = iks_new_iq_result(node);
stream->state = XSS_READY;
/* add to available streams */
switch_mutex_lock(context->streams_mutex);
switch_core_hash_insert(context->routes, stream->jid, stream);
switch_mutex_unlock(context->streams_mutex);
}
break;
}
case XSS_AUTHENTICATED:
case XSS_READY:
default:
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_WARNING, "%s, %s:%i, iq UNEXPECTED <session>, state = %s\n", stream->jid, stream->address, stream->port, xmpp_stream_state_to_string(stream->state));
reply = iks_new_error(node, STANZA_ERROR_SERVICE_UNAVAILABLE);
break;
}
return reply;
}
/**
* Handle <iq><bind> request
* @param stream the xmpp stream
* @param node the <iq> node
*/
static iks *on_iq_set_xmpp_bind(struct xmpp_stream *stream, iks *node)
{
iks *reply = NULL;
switch(stream->state) {
case XSS_AUTHENTICATED: {
struct xmpp_stream_context *context = stream->context;
iks *bind = iks_find(node, "bind");
iks *x;
/* get optional client resource ID */
char *resource_id = iks_find_cdata(bind, "resource");
/* generate resource ID for client if not already set */
if (zstr(resource_id)) {
char resource_id_buf[SWITCH_UUID_FORMATTED_LENGTH + 1];
switch_uuid_str(resource_id_buf, sizeof(resource_id_buf));
resource_id = switch_core_strdup(stream->pool, resource_id_buf);
}
stream->jid = switch_core_sprintf(stream->pool, "%s/%s", stream->jid, resource_id);
if (context->bind_callback && !context->bind_callback(stream)) {
stream->jid = NULL;
reply = iks_new_error(node, STANZA_ERROR_CONFLICT);
} else {
stream->state = XSS_RESOURCE_BOUND;
reply = iks_new_iq_result(node);
x = iks_insert(reply, "bind");
iks_insert_attrib(x, "xmlns", IKS_NS_XMPP_BIND);
iks_insert_cdata(iks_insert(x, "jid"), stream->jid, strlen(stream->jid));
}
break;
}
default:
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_WARNING, "%s, %s:%i, iq UNEXPECTED <bind>\n", stream->jid, stream->address, stream->port);
reply = iks_new_error(node, STANZA_ERROR_NOT_ALLOWED);
break;
}
return reply;
}
/**
* Handle <iq> message callback
* @param stream the stream
* @param iq the packet
*/
static void on_stream_iq(struct xmpp_stream *stream, iks *iq)
{
struct xmpp_stream_context *context = stream->context;
switch(stream->state) {
case XSS_CONNECT:
case XSS_SECURE: {
iks *error = iks_new_error(iq, STANZA_ERROR_NOT_AUTHORIZED);
xmpp_stream_stanza_send(stream, error);
break;
}
case XSS_AUTHENTICATED: {
iks *cmd = iks_first_tag(iq);
if (cmd && !strcmp("bind", iks_name(cmd)) && !strcmp(IKS_NS_XMPP_BIND, iks_find_attrib_soft(cmd, "xmlns"))) {
iks *reply = on_iq_set_xmpp_bind(stream, iq);
xmpp_stream_stanza_send(stream, reply);
} else {
iks *error = iks_new_error(iq, STANZA_ERROR_SERVICE_UNAVAILABLE);
xmpp_stream_stanza_send(stream, error);
}
break;
}
case XSS_RESOURCE_BOUND: {
iks *cmd = iks_first_tag(iq);
if (cmd && !strcmp("session", iks_name(cmd)) && !strcmp(IKS_NS_XMPP_SESSION, iks_find_attrib_soft(cmd, "xmlns"))) {
iks *reply = on_iq_set_xmpp_session(stream, iq);
xmpp_stream_stanza_send(stream, reply);
} else {
iks *error = iks_new_error(iq, STANZA_ERROR_SERVICE_UNAVAILABLE);
xmpp_stream_stanza_send(stream, error);
}
break;
}
case XSS_READY: {
/* client requests */
if (context->recv_callback) {
context->recv_callback(stream, iq);
}
break;
}
case XSS_SHUTDOWN:
case XSS_DESTROY:
case XSS_ERROR: {
iks *error = iks_new_error(iq, STANZA_ERROR_UNEXPECTED_REQUEST);
xmpp_stream_stanza_send(stream, error);
break;
}
};
}
/**
* Handle </stream>
* @param stream the stream
*/
static void on_stream_stop(struct xmpp_stream *stream)
{
if (stream->state != XSS_SHUTDOWN) {
iks_send_raw(stream->parser, "</stream:stream>");
}
stream->state = XSS_DESTROY;
}
/**
* Handle <stream> from a client
* @param stream the stream
* @param node the stream message
*/
static void on_client_stream_start(struct xmpp_stream *stream, iks *node)
{
struct xmpp_stream_context *context = stream->context;
const char *to = iks_find_attrib_soft(node, "to");
const char *xmlns = iks_find_attrib_soft(node, "xmlns");
/* to is optional, must be server domain if set */
if (!zstr(to) && strcmp(context->domain, to)) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_INFO, "%s, %s:%i, wrong server domain!\n", stream->jid, stream->address, stream->port);
stream->state = XSS_ERROR;
return;
}
/* xmlns = client */
if (zstr(xmlns) || strcmp(xmlns, IKS_NS_CLIENT)) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_INFO, "%s, %s:%i, wrong stream namespace!\n", stream->jid, stream->address, stream->port);
stream->state = XSS_ERROR;
return;
}
switch (stream->state) {
case XSS_CONNECT:
xmpp_send_client_header_tls(stream);
break;
case XSS_SECURE:
xmpp_send_client_header_auth(stream);
break;
case XSS_AUTHENTICATED:
/* client bind required */
xmpp_stream_new_id(stream);
xmpp_send_client_header_bind(stream);
break;
case XSS_SHUTDOWN:
/* strange... I expect IKS_NODE_STOP, this is a workaround. */
stream->state = XSS_DESTROY;
break;
case XSS_RESOURCE_BOUND:
case XSS_READY:
case XSS_ERROR:
case XSS_DESTROY:
/* bad state */
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_INFO, "%s, %s:%i, bad state!\n", stream->jid, stream->address, stream->port);
stream->state = XSS_ERROR;
break;
}
}
/**
* Handle <db:result type='valid'>
*/
static void on_stream_dialback_result_valid(struct xmpp_stream *stream, iks *node)
{
struct xmpp_stream_context *context = stream->context;
/* TODO check domain pair and allow access if pending request exists */
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_DEBUG, "%s, %s:%i, valid dialback result\n", stream->jid, stream->address, stream->port);
if (context->ready_callback && !context->ready_callback(stream)) {
stream->state = XSS_ERROR;
} else {
/* this stream is routable */
stream->state = XSS_READY;
/* add to available streams */
switch_mutex_lock(context->streams_mutex);
switch_core_hash_insert(context->routes, stream->jid, stream);
switch_mutex_unlock(context->streams_mutex);
}
}
/**
* Handle <db:result type='valid'>
*/
static void on_stream_dialback_result_invalid(struct xmpp_stream *stream, iks *node)
{
/* close stream */
stream->state = XSS_ERROR;
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_INFO, "%s, %s:%i, invalid dialback result!\n", stream->jid, stream->address, stream->port);
}
/**
* Handle <db:result type='error'>
*/
static void on_stream_dialback_result_error(struct xmpp_stream *stream, iks *node)
{
/* close stream */
stream->state = XSS_ERROR;
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_INFO, "%s, %s:%i, error dialback result!\n", stream->jid, stream->address, stream->port);
}
/**
* Handle <db:result>
*/
static void on_stream_dialback_result_key(struct xmpp_stream *stream, iks *node)
{
struct xmpp_stream_context *context = stream->context;
const char *from = iks_find_attrib_soft(node, "from");
const char *to = iks_find_attrib_soft(node, "to");
iks *cdata = iks_child(node);
iks *reply;
const char *dialback_key = NULL;
if (cdata && iks_type(cdata) == IKS_CDATA) {
dialback_key = iks_cdata(cdata);
}
if (zstr(dialback_key)) {
iks *error = iks_new_error_detailed(node, STANZA_ERROR_BAD_REQUEST, "Missing dialback key");
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_INFO, "%s, %s:%i, dialback result missing key!\n", stream->jid, stream->address, stream->port);
iks_send(stream->parser, error);
iks_delete(error);
stream->state = XSS_ERROR;
return;
}
if (zstr(from)) {
iks *error = iks_new_error_detailed(node, STANZA_ERROR_BAD_REQUEST, "Missing from");
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_INFO, "%s, %s:%i, dialback result missing from!\n", stream->jid, stream->address, stream->port);
iks_send(stream->parser, error);
iks_delete(error);
stream->state = XSS_ERROR;
return;
}
if (zstr(to)) {
iks *error = iks_new_error_detailed(node, STANZA_ERROR_BAD_REQUEST, "Missing to");
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_INFO, "%s, %s:%i, dialback result missing to!\n", stream->jid, stream->address, stream->port);
iks_send(stream->parser, error);
iks_delete(error);
stream->state = XSS_ERROR;
return;
}
if (strcmp(context->domain, to)) {
iks *error = iks_new_error(node, STANZA_ERROR_ITEM_NOT_FOUND);
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_INFO, "%s, %s:%i, invalid domain!\n", stream->jid, stream->address, stream->port);
iks_send(stream->parser, error);
iks_delete(error);
stream->state = XSS_ERROR;
return;
}
/* this stream is not routable */
stream->state = XSS_READY;
stream->jid = switch_core_strdup(stream->pool, from);
if (context->ready_callback && !context->ready_callback(stream)) {
iks *error = iks_new_error(node, STANZA_ERROR_INTERNAL_SERVER_ERROR);
iks_send(stream->parser, error);
iks_delete(error);
stream->state = XSS_ERROR;
return;
}
/* TODO validate key */
reply = iks_new("db:result");
iks_insert_attrib(reply, "from", to);
iks_insert_attrib(reply, "to", from);
iks_insert_attrib(reply, "type", "valid");
iks_send(stream->parser, reply);
iks_delete(reply);
}
/**
* Handle <db:result>
*/
static void on_stream_dialback_result(struct xmpp_stream *stream, iks *node)
{
const char *type = iks_find_attrib_soft(node, "type");
if (stream->state == XSS_ERROR || stream->state == XSS_DESTROY) {
stream->state = XSS_ERROR;
return;
}
if (zstr(type)) {
on_stream_dialback_result_key(stream, node);
} else if (!strcmp("valid", type)) {
on_stream_dialback_result_valid(stream, node);
} else if (!strcmp("invalid", type)) {
on_stream_dialback_result_invalid(stream, node);
} else if (!strcmp("error", type)) {
on_stream_dialback_result_error(stream, node);
}
}
/**
* Handle <db:verify>
*/
static void on_stream_dialback_verify(struct xmpp_stream *stream, iks *node)
{
struct xmpp_stream_context *context = stream->context;
const char *from = iks_find_attrib_soft(node, "from");
const char *id = iks_find_attrib_soft(node, "id");
const char *to = iks_find_attrib_soft(node, "to");
iks *cdata = iks_child(node);
iks *reply;
const char *dialback_key = NULL;
char *expected_key = NULL;
int valid;
if (stream->state == XSS_ERROR || stream->state == XSS_DESTROY) {
stream->state = XSS_ERROR;
return;
}
if (cdata && iks_type(cdata) == IKS_CDATA) {
dialback_key = iks_cdata(cdata);
}
if (zstr(dialback_key)) {
iks *error = iks_new_error_detailed(node, STANZA_ERROR_BAD_REQUEST, "Missing dialback key");
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_INFO, "%s, %s:%i, dialback verify missing key!\n", stream->jid, stream->address, stream->port);
iks_send(stream->parser, error);
iks_delete(error);
return;
}
if (zstr(id)) {
iks *error = iks_new_error_detailed(node, STANZA_ERROR_BAD_REQUEST, "Missing id");
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_INFO, "%s, %s:%i, dialback verify missing stream ID!\n", stream->jid, stream->address, stream->port);
iks_send(stream->parser, error);
iks_delete(error);
return;
}
if (zstr(from)) {
iks *error = iks_new_error_detailed(node, STANZA_ERROR_BAD_REQUEST, "Missing from");
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_INFO, "%s, %s:%i, dialback verify missing from!\n", stream->jid, stream->address, stream->port);
iks_send(stream->parser, error);
iks_delete(error);
return;
}
if (zstr(to)) {
iks *error = iks_new_error_detailed(node, STANZA_ERROR_BAD_REQUEST, "Missing to");
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_INFO, "%s, %s:%i, dialback verify missing to!\n", stream->jid, stream->address, stream->port);
iks_send(stream->parser, error);
iks_delete(error);
return;
}
if (strcmp(context->domain, to)) {
iks *error = iks_new_error(node, STANZA_ERROR_ITEM_NOT_FOUND);
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_INFO, "%s, %s:%i, invalid domain!\n", stream->jid, stream->address, stream->port);
iks_send(stream->parser, error);
iks_delete(error);
return;
}
expected_key = iks_server_dialback_key(context->dialback_secret, from, to, id);
valid = expected_key && !strcmp(expected_key, dialback_key);
reply = iks_new("db:verify");
iks_insert_attrib(reply, "from", to);
iks_insert_attrib(reply, "to", from);
iks_insert_attrib(reply, "id", id);
iks_insert_attrib(reply, "type", valid ? "valid" : "invalid");
iks_send(stream->parser, reply);
iks_delete(reply);
free(expected_key);
if (!valid) {
/* close the stream */
stream->state = XSS_ERROR;
}
}
/**
* Handle <stream> from an outbound peer server
*/
static void on_outbound_server_stream_start(struct xmpp_stream *stream, iks *node)
{
const char *xmlns = iks_find_attrib_soft(node, "xmlns");
/* xmlns = server */
if (zstr(xmlns) || strcmp(xmlns, IKS_NS_SERVER)) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_INFO, "%s, %s:%i, wrong stream namespace!\n", stream->jid, stream->address, stream->port);
stream->state = XSS_ERROR;
return;
}
switch (stream->state) {
case XSS_CONNECT: {
/* get stream ID and send dialback */
const char *id = iks_find_attrib_soft(node, "id");
if (zstr(id)) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(stream->id), SWITCH_LOG_INFO, "%s, %s:%i, missing stream ID!\n", stream->jid, stream->address, stream->port);
stream->state = XSS_ERROR;
return;
}
xmpp_stream_set_id(stream, id);
/* send dialback */
xmpp_send_dialback_key(stream);
break;
}
case XSS_SHUTDOWN:
/* strange... I expect IKS_NODE_STOP, this is a workaround. */
stream->state = XSS_DESTROY;
break;
case XSS_SECURE:
case XSS_AUTHENTICATED:
case XSS_RESOURCE_BOUND:
case XSS_READY:
case XSS_ERROR:
case XSS_DESTROY:
/* bad state */
stream->state = XSS_ERROR;
break;
}
}
/**
* Handle <stream> from an inbound peer server