forked from jabberd2/jabberd2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc2s.c
1257 lines (961 loc) · 46.5 KB
/
c2s.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
/*
* jabberd - Jabber Open Source Server
* Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
* Ryan Eatmon, Robert Norris
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
*/
#include "c2s.h"
#include <stringprep.h>
static int _c2s_client_sx_callback(sx_t s, sx_event_t e, void *data, void *arg) {
sess_t sess = (sess_t) arg;
sx_buf_t buf = (sx_buf_t) data;
int rlen, len, ns, elem, attr;
sx_error_t *sxe;
nad_t nad;
char root[9];
bres_t bres, ires;
switch(e) {
case event_WANT_READ:
log_debug(ZONE, "want read");
mio_read(sess->c2s->mio, sess->fd);
break;
case event_WANT_WRITE:
log_debug(ZONE, "want write");
mio_write(sess->c2s->mio, sess->fd);
break;
case event_READ:
log_debug(ZONE, "reading from %d", sess->fd->fd);
/* check rate limits */
if(sess->rate != NULL) {
if(rate_check(sess->rate) == 0) {
/* inform the app if we haven't already */
if(!sess->rate_log) {
if(s->state >= state_STREAM && sess->resources != NULL)
log_write(sess->c2s->log, LOG_NOTICE, "[%d] [%s] is being byte rate limited", sess->fd->fd, jid_user(sess->resources->jid));
else
log_write(sess->c2s->log, LOG_NOTICE, "[%d] [%s, port=%d] is being byte rate limited", sess->fd->fd, sess->ip, sess->port);
sess->rate_log = 1;
}
log_debug(ZONE, "%d is throttled, delaying read", sess->fd->fd);
buf->len = 0;
return 0;
}
/* find out how much we can have */
rlen = rate_left(sess->rate);
if(rlen > buf->len)
rlen = buf->len;
}
/* do the read */
len = recv(sess->fd->fd, buf->data, buf->len, 0);
if(len < 0) {
if(MIO_WOULDBLOCK) {
buf->len = 0;
return 0;
}
if(s->state >= state_STREAM && sess->resources != NULL)
log_write(sess->c2s->log, LOG_NOTICE, "[%d] [%s] read error: %s (%d)", sess->fd->fd, jid_user(sess->resources->jid), MIO_STRERROR(MIO_ERROR), MIO_ERROR);
else
log_write(sess->c2s->log, LOG_NOTICE, "[%d] [%s, port=%d] read error: %s (%d)", sess->fd->fd, sess->ip, sess->port, MIO_STRERROR(MIO_ERROR), MIO_ERROR);
sx_kill(s);
return -1;
}
else if(len == 0) {
/* they went away */
sx_kill(s);
return -1;
}
log_debug(ZONE, "read %d bytes", len);
/* If the first chars are "GET " then it's for HTTP (GET ....)
and if we configured http client forwarding to a real http server */
if (sess->c2s->http_forward && !sess->active && !sess->sasl_authd
&& sess->result == NULL && len >= 4 && strncmp("GET ", buf->data, 4) == 0) {
char* http =
"HTTP/1.0 301 Found\r\n"
"Location: %s\r\n"
"Server: " PACKAGE_STRING "\r\n"
"Expires: Fri, 10 Oct 1997 10:10:10 GMT\r\n"
"Pragma: no-cache\r\n"
"Cache-control: private\r\n"
"Connection: close\r\n\r\n";
char *answer;
len = strlen(sess->c2s->http_forward) + strlen(http);
answer = malloc(len * sizeof(char));
sprintf (answer, http, sess->c2s->http_forward);
log_write(sess->c2s->log, LOG_NOTICE, "[%d] bouncing HTTP request to %s", sess->fd->fd, sess->c2s->http_forward);
/* send HTTP answer */
len = send(sess->fd->fd, answer, len-1, 0);
free(answer);
/* close connection */
sx_kill(s);
return -1;
}
buf->len = len;
return len;
case event_WRITE:
log_debug(ZONE, "writing to %d", sess->fd->fd);
len = send(sess->fd->fd, buf->data, buf->len, 0);
if(len >= 0) {
log_debug(ZONE, "%d bytes written", len);
return len;
}
if(MIO_WOULDBLOCK)
return 0;
if(s->state >= state_OPEN && sess->resources != NULL)
log_write(sess->c2s->log, LOG_NOTICE, "[%d] [%s] write error: %s (%d)", sess->fd->fd, jid_user(sess->resources->jid), MIO_STRERROR(MIO_ERROR), MIO_ERROR);
else
log_write(sess->c2s->log, LOG_NOTICE, "[%d] [%s. port=%d] write error: %s (%d)", sess->fd->fd, sess->ip, sess->port, MIO_STRERROR(MIO_ERROR), MIO_ERROR);
sx_kill(s);
return -1;
case event_ERROR:
sxe = (sx_error_t *) data;
if(sess->resources != NULL)
log_write(sess->c2s->log, LOG_NOTICE, "[%d] [%s] error: %s (%s)", sess->fd->fd, jid_user(sess->resources->jid), sxe->generic, sxe->specific);
else
log_write(sess->c2s->log, LOG_NOTICE, "[%d] [%s, port=%d] error: %s (%s)", sess->fd->fd, sess->ip, sess->port, sxe->generic, sxe->specific);
break;
case event_STREAM:
if(s->req_to == NULL) {
log_debug(ZONE, "no stream to provided, closing");
sx_error(s, stream_err_HOST_UNKNOWN, "no 'to' attribute on stream header");
sx_close(s);
return 0;
}
/* setup the host */
sess->host = xhash_get(sess->c2s->hosts, s->req_to);
if(sess->host == NULL && sess->c2s->vhost == NULL) {
log_debug(ZONE, "no host available for requested domain '%s'", s->req_to);
sx_error(s, stream_err_HOST_UNKNOWN, "service requested for unknown domain");
sx_close(s);
return 0;
}
if(xhash_get(sess->c2s->sm_avail, s->req_to) == NULL) {
log_debug(ZONE, "sm for domain '%s' is not online", s->req_to);
sx_error(s, stream_err_HOST_GONE, "session manager for requested domain is not available");
sx_close(s);
return 0;
}
if(sess->host == NULL) {
/* create host on-fly */
sess->host = (host_t) pmalloc(xhash_pool(sess->c2s->hosts), sizeof(struct host_st));
memcpy(sess->host, sess->c2s->vhost, sizeof(struct host_st));
sess->host->realm = pstrdup(xhash_pool(sess->c2s->hosts), s->req_to);
xhash_put(sess->c2s->hosts, pstrdup(xhash_pool(sess->c2s->hosts), s->req_to), sess->host);
}
#ifdef HAVE_SSL
if(sess->host->host_pemfile != NULL)
sess->s->flags |= SX_SSL_STARTTLS_OFFER;
if(sess->host->host_require_starttls)
sess->s->flags |= SX_SSL_STARTTLS_REQUIRE;
#endif
break;
case event_PACKET:
/* we're counting packets */
sess->packet_count++;
sess->c2s->packet_count++;
nad = (nad_t) data;
/* we only want (message|presence|iq) in jabber:client, everything else gets dropped */
snprintf(root, 9, "%.*s", NAD_ENAME_L(nad, 0), NAD_ENAME(nad, 0));
if(NAD_ENS(nad, 0) != nad_find_namespace(nad, 0, uri_CLIENT, NULL) ||
(strcmp(root, "message") != 0 && strcmp(root, "presence") != 0 && strcmp(root, "iq") != 0)) {
nad_free(nad);
return 0;
}
/* resource bind */
if((ns = nad_find_scoped_namespace(nad, uri_BIND, NULL)) >= 0 && (elem = nad_find_elem(nad, 0, ns, "bind", 1)) >= 0 && nad_find_attr(nad, 0, -1, "type", "set") >= 0) {
bres_t bres;
jid_t jid = jid_new(sess->c2s->pc, sess->s->auth_id, -1);
/* get the resource */
elem = nad_find_elem(nad, elem, ns, "resource", 1);
/* user-specified resource */
if(elem >= 0) {
char resource_buf[1024];
if(NAD_CDATA_L(nad, elem) == 0) {
log_debug(ZONE, "empty resource specified on bind");
sx_nad_write(sess->s, stanza_error(nad, 0, stanza_err_BAD_REQUEST));
return 0;
}
snprintf(resource_buf, 1024, "%.*s", NAD_CDATA_L(nad, elem), NAD_CDATA(nad, elem));
/* Put resource into JID */
if (jid == NULL || jid_reset_components(jid, jid->node, jid->domain, resource_buf) == NULL) {
log_debug(ZONE, "invalid jid data");
sx_nad_write(sess->s, stanza_error(nad, 0, stanza_err_BAD_REQUEST));
return 0;
}
/* check if resource already bound */
for(bres = sess->resources; bres != NULL; bres = bres->next)
if(strcmp(bres->jid->resource, jid->resource) == 0){
log_debug(ZONE, "resource /%s already bound - generating", jid->resource);
jid_random_part(jid, jid_RESOURCE);
}
}
else {
/* generate random resource */
log_debug(ZONE, "no resource given - generating");
jid_random_part(jid, jid_RESOURCE);
}
/* attach new bound jid holder */
bres = (bres_t) calloc(1, sizeof(struct bres_st));
bres->jid = jid;
if(sess->resources != NULL) {
for(ires = sess->resources; ires->next != NULL; ires = ires->next);
ires->next = bres;
} else
sess->resources = bres;
sess->bound += 1;
log_write(sess->c2s->log, LOG_NOTICE, "[%d] bound: jid=%s", sess->s->tag, jid_full(bres->jid));
/* build a result packet, we'll send this back to the client after we have a session for them */
sess->result = nad_new(sess->s->nad_cache);
ns = nad_add_namespace(sess->result, uri_CLIENT, NULL);
nad_append_elem(sess->result, ns, "iq", 0);
nad_set_attr(sess->result, 0, -1, "type", "result", 6);
attr = nad_find_attr(nad, 0, -1, "id", NULL);
if(attr >= 0)
nad_set_attr(sess->result, 0, -1, "id", NAD_AVAL(nad, attr), NAD_AVAL_L(nad, attr));
ns = nad_add_namespace(sess->result, uri_BIND, NULL);
nad_append_elem(sess->result, ns, "bind", 1);
nad_append_elem(sess->result, ns, "jid", 2);
nad_append_cdata(sess->result, jid_full(bres->jid), strlen(jid_full(bres->jid)), 3);
/* our local id */
sprintf(bres->c2s_id, "%d", sess->s->tag);
/* start a session with the sm */
sm_start(sess, bres);
/* finished with the nad */
nad_free(nad);
/* handled */
return 0;
}
/* resource unbind */
if((ns = nad_find_scoped_namespace(nad, uri_BIND, NULL)) >= 0 && (elem = nad_find_elem(nad, 0, ns, "unbind", 1)) >= 0 && nad_find_attr(nad, 0, -1, "type", "set") >= 0) {
char resource_buf[1024];
bres_t bres;
/* get the resource */
elem = nad_find_elem(nad, elem, ns, "resource", 1);
if(elem < 0 || NAD_CDATA_L(nad, elem) == 0) {
log_debug(ZONE, "no/empty resource given to unbind");
sx_nad_write(sess->s, stanza_error(nad, 0, stanza_err_BAD_REQUEST));
return 0;
}
snprintf(resource_buf, 1024, "%.*s", NAD_CDATA_L(nad, elem), NAD_CDATA(nad, elem));
if(stringprep_xmpp_resourceprep(resource_buf, 1024) != 0) {
log_debug(ZONE, "cannot resourceprep");
sx_nad_write(sess->s, stanza_error(nad, 0, stanza_err_BAD_REQUEST));
return 0;
}
/* check if resource bound */
for(bres = sess->resources; bres != NULL; bres = bres->next)
if(strcmp(bres->jid->resource, resource_buf) == 0)
break;
if(bres == NULL) {
log_debug(ZONE, "resource /%s not bound", resource_buf);
sx_nad_write(sess->s, stanza_error(nad, 0, stanza_err_ITEM_NOT_FOUND));
return 0;
}
/* build a result packet, we'll send this back to the client after we close a session for them */
sess->result = nad_new(sess->s->nad_cache);
ns = nad_add_namespace(sess->result, uri_CLIENT, NULL);
nad_append_elem(sess->result, ns, "iq", 0);
nad_set_attr(sess->result, 0, -1, "type", "result", 6);
attr = nad_find_attr(nad, 0, -1, "id", NULL);
if(attr >= 0)
nad_set_attr(sess->result, 0, -1, "id", NAD_AVAL(nad, attr), NAD_AVAL_L(nad, attr));
/* end a session with the sm */
sm_end(sess, bres);
/* finished with the nad */
nad_free(nad);
/* handled */
return 0;
}
/* pre-session requests */
if(!sess->active && sess->sasl_authd && sess->result == NULL && strcmp(root, "iq") == 0 && nad_find_attr(nad, 0, -1, "type", "set") >= 0) {
log_debug(ZONE, "unrecognised pre-session packet, bye");
log_write(sess->c2s->log, LOG_NOTICE, "[%d] unrecognized pre-session packet, closing stream", sess->s->tag);
sx_error(s, stream_err_NOT_AUTHORIZED, "unrecognized pre-session stanza");
sx_close(s);
nad_free(nad);
return 0;
}
#ifdef HAVE_SSL
/* drop packets if they have to starttls and they haven't */
if((sess->s->flags & SX_SSL_STARTTLS_REQUIRE) && sess->s->ssf == 0) {
log_debug(ZONE, "pre STARTTLS packet, dropping");
log_write(sess->c2s->log, LOG_NOTICE, "[%d] got pre STARTTLS packet, dropping", sess->s->tag);
sx_error(s, stream_err_NOT_AUTHORIZED, "stanza sent before starttls");
nad_free(nad);
return 0;
}
#endif
/* handle iq:auth packets */
if(authreg_process(sess->c2s, sess, nad) == 0)
return 0;
/* drop it if no session */
if(!sess->active) {
log_debug(ZONE, "pre-session packet, bye");
log_write(sess->c2s->log, LOG_NOTICE, "[%d] packet sent before session start, closing stream", sess->s->tag);
sx_error(s, stream_err_NOT_AUTHORIZED, "stanza sent before session start");
sx_close(s);
nad_free(nad);
return 0;
}
/* validate 'from' */
assert(sess->resources != NULL);
if(sess->bound > 1) {
bres = NULL;
if((attr = nad_find_attr(nad, 0, -1, "from", NULL)) >= 0)
for(bres = sess->resources; bres != NULL; bres = bres->next)
if(strncmp(jid_full(bres->jid), NAD_AVAL(nad, attr), NAD_AVAL_L(nad, attr)) == 0)
break;
if(bres == NULL) {
if(attr >= 0) {
log_debug(ZONE, "packet from: %.*s that has not bound the resource", NAD_AVAL_L(nad, attr), NAD_AVAL(nad, attr));
} else {
log_debug(ZONE, "packet without 'from' on multiple resource stream");
}
sx_nad_write(sess->s, stanza_error(nad, 0, stanza_err_UNKNOWN_SENDER));
return 0;
}
} else
bres = sess->resources;
/* pass it on to the session manager */
sm_packet(sess, bres, nad);
break;
case event_OPEN:
/* only send a result and bring us online if this wasn't a sasl auth */
if(strlen(s->auth_method) < 4 || strncmp("SASL", s->auth_method, 4) != 0) {
/* return the auth result to the client */
sx_nad_write(s, sess->result);
sess->result = NULL;
/* we're good to go */
sess->active = 1;
}
/* they sasl auth'd, so we only want the new-style session start */
else {
log_write(sess->c2s->log, LOG_NOTICE, "[%d] SASL authentication succeeded: mechanism=%s; authzid=%s%s", sess->s->tag, &sess->s->auth_method[5], sess->s->auth_id, sess->s->ssf ? ", TLS negotiated" : "");
sess->sasl_authd = 1;
}
break;
case event_CLOSED:
mio_close(sess->c2s->mio, sess->fd);
return -1;
}
return 0;
}
static int _c2s_client_accept_check(c2s_t c2s, mio_fd_t fd, char *ip) {
rate_t rt;
if(access_check(c2s->access, ip) == 0) {
log_write(c2s->log, LOG_NOTICE, "[%d] [%s] access denied by configuration", fd->fd, ip);
return 1;
}
if(c2s->conn_rate_total != 0) {
rt = (rate_t) xhash_get(c2s->conn_rates, ip);
if(rt == NULL) {
rt = rate_new(c2s->conn_rate_total, c2s->conn_rate_seconds, c2s->conn_rate_wait);
xhash_put(c2s->conn_rates, pstrdup(xhash_pool(c2s->conn_rates), ip), (void *) rt);
pool_cleanup(xhash_pool(c2s->conn_rates), (void (*)(void *)) rate_free, rt);
}
if(rate_check(rt) == 0) {
log_write(c2s->log, LOG_NOTICE, "[%d] [%s] is being rate limited", fd->fd, ip);
return 1;
}
rate_add(rt, 1);
}
return 0;
}
static int _c2s_client_mio_callback(mio_t m, mio_action_t a, mio_fd_t fd, void *data, void *arg) {
sess_t sess = (sess_t) arg;
c2s_t c2s = (c2s_t) arg;
bres_t bres;
struct sockaddr_storage sa;
int namelen = sizeof(sa), port, nbytes, flags = 0;
switch(a) {
case action_READ:
log_debug(ZONE, "read action on fd %d", fd->fd);
/* they did something */
sess->last_activity = time(NULL);
ioctl(fd->fd, FIONREAD, &nbytes);
if(nbytes == 0) {
sx_kill(sess->s);
return 0;
}
return sx_can_read(sess->s);
case action_WRITE:
log_debug(ZONE, "write action on fd %d", fd->fd);
return sx_can_write(sess->s);
case action_CLOSE:
log_debug(ZONE, "close action on fd %d", fd->fd);
log_write(sess->c2s->log, LOG_NOTICE, "[%d] [%s, port=%d] disconnect jid=%s, packets: %i", sess->fd->fd, sess->ip, sess->port, ((sess->resources)?((char*) jid_full(sess->resources->jid)):"unbound"), sess->packet_count);
/* tell the sm to close their session */
if(sess->active)
for(bres = sess->resources; bres != NULL; bres = bres->next)
sm_end(sess, bres);
jqueue_push(sess->c2s->dead, (void *) sess->s, 0);
xhash_zap(sess->c2s->sessions, sess->skey);
jqueue_push(sess->c2s->dead_sess, (void *) sess, 0);
break;
case action_ACCEPT:
log_debug(ZONE, "accept action on fd %d", fd->fd);
getpeername(fd->fd, (struct sockaddr *) &sa, &namelen);
port = j_inet_getport(&sa);
log_write(c2s->log, LOG_NOTICE, "[%d] [%s, port=%d] connect", fd->fd, (char *) data, port);
if(_c2s_client_accept_check(c2s, fd, (char *) data) != 0)
return 1;
sess = (sess_t) calloc(1, sizeof(struct sess_st));
sess->c2s = c2s;
sess->fd = fd;
sess->ip = strdup((char *) data);
sess->port = port;
/* they did something */
sess->last_activity = time(NULL);
sess->s = sx_new(c2s->sx_env, fd->fd, _c2s_client_sx_callback, (void *) sess);
mio_app(m, fd, _c2s_client_mio_callback, (void *) sess);
if(c2s->stanza_size_limit != 0)
sess->s->rbytesmax = c2s->stanza_size_limit;
if(c2s->byte_rate_total != 0)
sess->rate = rate_new(c2s->byte_rate_total, c2s->byte_rate_seconds, c2s->byte_rate_wait);
/* find out which port this is */
getsockname(fd->fd, (struct sockaddr *) &sa, &namelen);
port = j_inet_getport(&sa);
/* remember it */
sprintf(sess->skey, "%d", fd->fd);
xhash_put(c2s->sessions, sess->skey, (void *) sess);
flags = SX_SASL_OFFER;
#ifdef HAVE_SSL
/* go ssl wrappermode if they're on the ssl port */
if(port == c2s->local_ssl_port)
flags |= SX_SSL_WRAPPER;
#endif
#ifdef HAVE_LIBZ
if(c2s->compression)
flags |= SX_COMPRESS_OFFER;
#endif
sx_server_init(sess->s, flags);
break;
}
return 0;
}
static void _c2s_component_presence(c2s_t c2s, nad_t nad) {
int attr;
char from[1024];
sess_t sess;
union xhashv xhv;
if((attr = nad_find_attr(nad, 0, -1, "from", NULL)) < 0) {
nad_free(nad);
return;
}
strncpy(from, NAD_AVAL(nad, attr), NAD_AVAL_L(nad, attr));
from[NAD_AVAL_L(nad, attr)] = '\0';
if(nad_find_attr(nad, 0, -1, "type", NULL) < 0) {
log_debug(ZONE, "component available from '%s'", from);
log_debug(ZONE, "sm for serviced domain '%s' online", from);
xhash_put(c2s->sm_avail, pstrdup(xhash_pool(c2s->sm_avail), from), (void *) 1);
nad_free(nad);
return;
}
if(nad_find_attr(nad, 0, -1, "type", "unavailable") < 0) {
nad_free(nad);
return;
}
log_debug(ZONE, "component unavailable from '%s'", from);
if(xhash_get(c2s->sm_avail, from) != NULL) {
log_debug(ZONE, "sm for serviced domain '%s' offline", from);
if(xhash_iter_first(c2s->sessions))
do {
xhv.sess_val = &sess;
xhash_iter_get(c2s->sessions, NULL, xhv.val);
if(sess->resources != NULL && strcmp(sess->resources->jid->domain, from) == 0) {
log_debug(ZONE, "killing session %s", jid_user(sess->resources->jid));
sess->active = 0;
sx_close(sess->s);
}
} while(xhash_iter_next(c2s->sessions));
xhash_zap(c2s->sm_avail, from);
}
}
int c2s_router_sx_callback(sx_t s, sx_event_t e, void *data, void *arg) {
c2s_t c2s = (c2s_t) arg;
sx_buf_t buf = (sx_buf_t) data;
sx_error_t *sxe;
nad_t nad;
int len, elem, from, c2sid, smid, action, id, ns, attr, scan, replaced;
char skey[10];
sess_t sess;
bres_t bres, ires;
switch(e) {
case event_WANT_READ:
log_debug(ZONE, "want read");
mio_read(c2s->mio, c2s->fd);
break;
case event_WANT_WRITE:
log_debug(ZONE, "want write");
mio_write(c2s->mio, c2s->fd);
break;
case event_READ:
log_debug(ZONE, "reading from %d", c2s->fd->fd);
/* do the read */
len = recv(c2s->fd->fd, buf->data, buf->len, 0);
if(len < 0) {
if(MIO_WOULDBLOCK) {
buf->len = 0;
return 0;
}
log_write(c2s->log, LOG_NOTICE, "[%d] [router] read error: %s (%d)", c2s->fd->fd, MIO_STRERROR(MIO_ERROR), MIO_ERROR);
sx_kill(s);
return -1;
}
else if(len == 0) {
/* they went away */
sx_kill(s);
return -1;
}
log_debug(ZONE, "read %d bytes", len);
buf->len = len;
return len;
case event_WRITE:
log_debug(ZONE, "writing to %d", c2s->fd->fd);
len = send(c2s->fd->fd, buf->data, buf->len, 0);
if(len >= 0) {
log_debug(ZONE, "%d bytes written", len);
return len;
}
if(MIO_WOULDBLOCK)
return 0;
log_write(c2s->log, LOG_NOTICE, "[%d] [router] write error: %s (%d)", c2s->fd->fd, MIO_STRERROR(MIO_ERROR), MIO_ERROR);
sx_kill(s);
return -1;
case event_ERROR:
sxe = (sx_error_t *) data;
log_write(c2s->log, LOG_NOTICE, "error from router: %s (%s)", sxe->generic, sxe->specific);
if(sxe->code == SX_ERR_AUTH)
sx_close(s);
break;
case event_STREAM:
break;
case event_OPEN:
log_write(c2s->log, LOG_NOTICE, "connection to router established");
/* reset connection attempts counter */
c2s->retry_left = c2s->retry_init;
nad = nad_new(c2s->router->nad_cache);
ns = nad_add_namespace(nad, uri_COMPONENT, NULL);
nad_append_elem(nad, ns, "bind", 0);
nad_append_attr(nad, -1, "name", c2s->id);
log_debug(ZONE, "requesting component bind for '%s'", c2s->id);
sx_nad_write(c2s->router, nad);
return 0;
case event_PACKET:
nad = (nad_t) data;
/* drop unqualified packets */
if(NAD_ENS(nad, 0) < 0) {
nad_free(nad);
return 0;
}
/* watch for the features packet */
if(s->state == state_STREAM) {
if(NAD_NURI_L(nad, NAD_ENS(nad, 0)) != strlen(uri_STREAMS) || strncmp(uri_STREAMS, NAD_NURI(nad, NAD_ENS(nad, 0)), strlen(uri_STREAMS)) != 0 || NAD_ENAME_L(nad, 0) != 8 || strncmp("features", NAD_ENAME(nad, 0), 8) != 0) {
log_debug(ZONE, "got a non-features packet on an unauth'd stream, dropping");
nad_free(nad);
return 0;
}
#ifdef HAVE_SSL
/* starttls if we can */
if(c2s->sx_ssl != NULL && c2s->router_pemfile != NULL && s->ssf == 0) {
ns = nad_find_scoped_namespace(nad, uri_TLS, NULL);
if(ns >= 0) {
elem = nad_find_elem(nad, 0, ns, "starttls", 1);
if(elem >= 0) {
if(sx_ssl_client_starttls(c2s->sx_ssl, s, c2s->router_pemfile) == 0) {
nad_free(nad);
return 0;
}
log_write(c2s->log, LOG_ERR, "unable to establish encrypted session with router");
}
}
}
#endif
/* !!! pull the list of mechanisms, and choose the best one.
* if there isn't an appropriate one, error and bail */
/* authenticate */
sx_sasl_auth(c2s->sx_sasl, s, "jabberd-router", "DIGEST-MD5", c2s->router_user, c2s->router_pass);
nad_free(nad);
return 0;
}
/* watch for the bind response */
if(s->state == state_OPEN && !c2s->online) {
if(NAD_NURI_L(nad, NAD_ENS(nad, 0)) != strlen(uri_COMPONENT) || strncmp(uri_COMPONENT, NAD_NURI(nad, NAD_ENS(nad, 0)), strlen(uri_COMPONENT)) != 0 || NAD_ENAME_L(nad, 0) != 4 || strncmp("bind", NAD_ENAME(nad, 0), 4) != 0) {
log_debug(ZONE, "got a packet from router, but we're not online, dropping");
nad_free(nad);
return 0;
}
/* catch errors */
attr = nad_find_attr(nad, 0, -1, "error", NULL);
if(attr >= 0) {
log_write(c2s->log, LOG_ERR, "router refused bind request (%.*s)", NAD_AVAL_L(nad, attr), NAD_AVAL(nad, attr));
exit(1);
}
log_debug(ZONE, "coming online");
/* if we're coming online for the first time, setup listening sockets */
#ifdef HAVE_SSL
if(c2s->server_fd == 0 && c2s->server_ssl_fd == 0) {
#else
if(c2s->server_fd == 0) {
#endif
if(c2s->local_port != 0) {
c2s->server_fd = mio_listen(c2s->mio, c2s->local_port, c2s->local_ip, _c2s_client_mio_callback, (void *) c2s);
if(c2s->server_fd == NULL)
log_write(c2s->log, LOG_ERR, "[%s, port=%d] failed to listen", c2s->local_ip, c2s->local_port);
else
log_write(c2s->log, LOG_NOTICE, "[%s, port=%d] listening for connections", c2s->local_ip, c2s->local_port);
} else
c2s->server_fd = NULL;
#ifdef HAVE_SSL
if(c2s->local_ssl_port != 0 && c2s->local_pemfile != NULL) {
c2s->server_ssl_fd = mio_listen(c2s->mio, c2s->local_ssl_port, c2s->local_ip, _c2s_client_mio_callback, (void *) c2s);
if(c2s->server_ssl_fd == NULL)
log_write(c2s->log, LOG_ERR, "[%s, port=%d] failed to listen", c2s->local_ip, c2s->local_ssl_port);
else
log_write(c2s->log, LOG_NOTICE, "[%s, port=%d] listening for SSL connections", c2s->local_ip, c2s->local_ssl_port);
} else
c2s->server_ssl_fd = NULL;
#endif
}
#ifdef HAVE_SSL
if(c2s->server_fd == NULL && c2s->server_ssl_fd == NULL) {
log_write(c2s->log, LOG_ERR, "both normal and SSL ports are disabled, nothing to do!");
#else
if(c2s->server_fd == NULL) {
log_write(c2s->log, LOG_ERR, "server port is disabled, nothing to do!");
#endif
exit(1);
}
/* we're online */
c2s->online = c2s->started = 1;
log_write(c2s->log, LOG_NOTICE, "ready for connections", c2s->id);
nad_free(nad);
return 0;
}
/* need component packets */
if(NAD_NURI_L(nad, NAD_ENS(nad, 0)) != strlen(uri_COMPONENT) || strncmp(uri_COMPONENT, NAD_NURI(nad, NAD_ENS(nad, 0)), strlen(uri_COMPONENT)) != 0) {
log_debug(ZONE, "wanted component packet, dropping");
nad_free(nad);
return 0;
}
/* component presence */
if(NAD_ENAME_L(nad, 0) == 8 && strncmp("presence", NAD_ENAME(nad, 0), 8) == 0) {
_c2s_component_presence(c2s, nad);
return 0;
}
/* we want route */
if(NAD_ENAME_L(nad, 0) != 5 || strncmp("route", NAD_ENAME(nad, 0), 5) != 0) {
log_debug(ZONE, "wanted {component}route, dropping");
nad_free(nad);
return 0;
}
/* only handle unicasts */
if(nad_find_attr(nad, 0, -1, "type", NULL) >= 0) {
log_debug(ZONE, "non-unicast packet, dropping");
nad_free(nad);
return 0;
}
/* need some payload */
if(nad->ecur == 1) {
log_debug(ZONE, "no route payload, dropping");
nad_free(nad);
return 0;
}
ns = nad_find_namespace(nad, 1, uri_SESSION, NULL);
if(ns < 0) {
log_debug(ZONE, "not a c2s packet, dropping");
nad_free(nad);
return 0;
}
/* figure out the session */
c2sid = nad_find_attr(nad, 1, ns, "c2s", NULL);
if(c2sid < 0) {
log_debug(ZONE, "no c2s id on payload, dropping");
nad_free(nad);
return 0;
}
snprintf(skey, 10, "%.*s", NAD_AVAL_L(nad, c2sid), NAD_AVAL(nad, c2sid));
/* find the session, quietly drop if we don't have it */
sess = xhash_get(c2s->sessions, skey);
if(sess == NULL) {
/* !!! might want to send a stop to the sm; maybe it thinks we're still here? */
log_debug(ZONE, "no session for %s", skey);
nad_free(nad);
return 0;
}
/* if they're pre-stream, then this is leftovers from a previous session */
if(sess->s->state < state_STREAM) {
log_debug(ZONE, "session %s is pre-stream", skey);
nad_free(nad);
return 0;
}
/* check the sm session id if they gave us one */
smid = nad_find_attr(nad, 1, ns, "sm", NULL);
/* get the action attribute */
action = nad_find_attr(nad, 1, -1, "action", NULL);
/* first user created packets - these are out of session */
if(action >= 0 && NAD_AVAL_L(nad, action) == 7 && strncmp("created", NAD_AVAL(nad, action), 7) == 0) {
nad_free(nad);
/* return the result to the client */
sx_nad_write(sess->s, sess->result);
sess->result = NULL;
return 0;
}
/* route errors */
if(nad_find_attr(nad, 0, -1, "error", NULL) >= 0) {
log_debug(ZONE, "routing error");
sx_error(sess->s, stream_err_INTERNAL_SERVER_ERROR, "internal server error");
sx_close(sess->s);
nad_free(nad);
return 0;
}
/* all other packets need to contain an sm ID */
if (smid < 0) {
log_debug(ZONE, "received packet from sm without an sm ID, dropping");
nad_free(nad);
return 0;
}
/* find resource that we got packet for */
bres = NULL;
if(smid >= 0)
for(bres = sess->resources; bres != NULL; bres = bres->next){
if(bres->sm_id[0] == '\0' || (strlen(bres->sm_id) == NAD_AVAL_L(nad, smid) && strncmp(bres->sm_id, NAD_AVAL(nad, smid), NAD_AVAL_L(nad, smid)) == 0))
break;
}
if(bres == NULL) {
jid_t jid = NULL;
bres_t tres = NULL;
/* if it's a failure, just drop it */
if(nad_find_attr(nad, 1, ns, "failed", NULL) >= 0) {
nad_free(nad);
return 0;
}
/* build temporary resource to close session for */
jid = jid_new(sess->c2s->pc, sess->s->auth_id, -1);
tres = (bres_t) calloc(1, sizeof(struct bres_st));
tres->jid = jid;
sprintf(tres->c2s_id, "%d", sess->s->tag);
snprintf(tres->sm_id, 41, "%.*s", NAD_AVAL_L(nad, smid), NAD_AVAL(nad, smid));
if(sess->resources) {
log_debug(ZONE, "expected packet from sm session %s, but got one from %.*s, ending sm session", sess->resources->sm_id, NAD_AVAL_L(nad, smid), NAD_AVAL(nad, smid));
} else {
log_debug(ZONE, "no resource bound yet, but got packet from sm session %.*s, ending sm session", NAD_AVAL_L(nad, smid), NAD_AVAL(nad, smid));
}
/* end a session with the sm */
sm_end(sess, tres);
/* finished with the nad */
nad_free(nad);
/* free temp objects */
jid_free(jid);
free(tres);
return 0;
}
/* it has to have come from the session manager */