Ticket #1: jabber.patch
| File jabber.patch, 19.8 KB (added by Simon, 8 months ago) |
|---|
-
jabberd-2.1.
diff -r -U4 jabberd-2.1.23/c2s/c2s.h jabberd-2.1.23+sa/c2s/c2s.h
old new 111 111 112 112 /** starttls pemfile */ 113 113 char *host_pemfile; 114 114 115 /** certificate chain */ 116 char *host_cachain; 117 115 118 /** verify-mode */ 116 119 int host_verify_mode; 117 120 118 121 /** require starttls */ -
jabberd-2.1.
diff -r -U4 jabberd-2.1.23/c2s/main.c jabberd-2.1.23+sa/c2s/main.c
old new 240 240 host->realm = (realm != NULL) ? realm : pstrdup(xhash_pool(c2s->hosts), id); 241 241 242 242 host->host_pemfile = j_attr((const char **) elem->attrs[i], "pemfile"); 243 243 244 host->host_cachain = j_attr((const char **) elem->attrs[i], "cachain"); 245 244 246 host->host_verify_mode = j_atoi(j_attr((const char **) elem->attrs[i], "verify-mode"), 0); 245 247 246 248 #ifdef HAVE_SSL 247 if(c2s->sx_ssl == NULL && host->host_pemfile != NULL) { 248 c2s->sx_ssl = sx_env_plugin(c2s->sx_env, sx_ssl_init, host->host_pemfile, NULL, host->host_verify_mode); 249 if(host->host_pemfile != NULL) { 249 250 if(c2s->sx_ssl == NULL) { 250 log_write(c2s->log, LOG_ERR, "failed to load %s SSL pemfile", host->realm); 251 host->host_pemfile = NULL; 251 c2s->sx_ssl = sx_env_plugin(c2s->sx_env, sx_ssl_init, host->realm, host->host_pemfile, host->host_cachain, host->host_verify_mode); 252 if(c2s->sx_ssl == NULL) { 253 log_write(c2s->log, LOG_ERR, "failed to load %s SSL pemfile", host->realm); 254 host->host_pemfile = NULL; 255 } 256 } else { 257 if(sx_ssl_server_addcert(c2s->sx_ssl, host->realm, host->host_pemfile, host->host_cachain, host->host_verify_mode) != 0) { 258 log_write(c2s->log, LOG_ERR, "failed to load %s SSL pemfile", host->realm); 259 host->host_pemfile = NULL; 260 } 252 261 } 253 262 } 254 263 #endif 255 264 … … 636 645 637 646 #ifdef HAVE_SSL 638 647 /* get the ssl context up and running */ 639 648 if(c2s->local_pemfile != NULL) { 640 c2s->sx_ssl = sx_env_plugin(c2s->sx_env, sx_ssl_init, c2s->local_pemfile, NULL, c2s->local_verify_mode);649 c2s->sx_ssl = sx_env_plugin(c2s->sx_env, sx_ssl_init, NULL, c2s->local_pemfile, NULL, c2s->local_verify_mode); 641 650 if(c2s->sx_ssl == NULL) { 642 651 log_write(c2s->log, LOG_ERR, "failed to load local SSL pemfile, SSL will not be available to clients"); 643 652 c2s->local_pemfile = NULL; 644 653 } 645 654 } 646 655 647 656 /* try and get something online, so at least we can encrypt to the router */ 648 657 if(c2s->sx_ssl == NULL && c2s->router_pemfile != NULL) { 649 c2s->sx_ssl = sx_env_plugin(c2s->sx_env, sx_ssl_init, c2s->router_pemfile, NULL, NULL);658 c2s->sx_ssl = sx_env_plugin(c2s->sx_env, sx_ssl_init, NULL, c2s->router_pemfile, NULL, NULL); 650 659 if(c2s->sx_ssl == NULL) { 651 660 log_write(c2s->log, LOG_ERR, "failed to load router SSL pemfile, channel to router will not be SSL encrypted"); 652 661 c2s->router_pemfile = NULL; 653 662 } -
resolver/resolver.c
diff -r -U4 jabberd-2.1.23/resolver/resolver.c jabberd-2.1.23+sa/resolver/resolver.c
old new 597 597 r->sx_env = sx_env_new(); 598 598 599 599 #ifdef HAVE_SSL 600 600 if(r->router_pemfile != NULL) { 601 r->sx_ssl = sx_env_plugin(r->sx_env, sx_ssl_init, r->router_pemfile, NULL, NULL);601 r->sx_ssl = sx_env_plugin(r->sx_env, sx_ssl_init, NULL, r->router_pemfile, NULL, NULL); 602 602 if(r->sx_ssl == NULL) { 603 603 log_write(r->log, LOG_ERR, "failed to load SSL pemfile, SSL disabled"); 604 604 r->router_pemfile = NULL; 605 605 } -
router/main.c
diff -r -U4 jabberd-2.1.23/router/main.c jabberd-2.1.23+sa/router/main.c
old new 393 393 r->sx_env = sx_env_new(); 394 394 395 395 #ifdef HAVE_SSL 396 396 if(r->local_pemfile != NULL) { 397 r->sx_ssl = sx_env_plugin(r->sx_env, sx_ssl_init, r->local_pemfile, NULL, NULL);397 r->sx_ssl = sx_env_plugin(r->sx_env, sx_ssl_init, NULL, r->local_pemfile, NULL, NULL); 398 398 if(r->sx_ssl == NULL) 399 399 log_write(r->log, LOG_ERR, "failed to load SSL pemfile, SSL disabled"); 400 400 } 401 401 #endif -
jabberd-2.1.
diff -r -U4 jabberd-2.1.23/s2s/main.c jabberd-2.1.23+sa/s2s/main.c
old new 19 19 */ 20 20 21 21 #include "s2s.h" 22 22 23 #include <stringprep.h> 24 23 25 static sig_atomic_t s2s_shutdown = 0; 24 26 sig_atomic_t s2s_lost_router = 0; 25 27 static sig_atomic_t s2s_logrotate = 0; 26 28 … … 137 139 if(s2s->local_secret == NULL) 138 140 s2s->local_secret = "secret"; 139 141 140 142 s2s->local_pemfile = config_get_one(s2s->config, "local.pemfile", 0); 141 if (s2s->local_pemfile != NULL)142 log_debug(ZONE,"loaded local pemfile for peer s2s connections");143 s2s->local_cachain = config_get_one(s2s->config, "local.cachain", 0); 144 s2s->local_verify_mode = j_atoi(config_get_one(s2s->config, "local.verify-mode", 0), 0); 143 145 144 146 s2s->io_max_fds = j_atoi(config_get_one(s2s->config, "io.max_fds", 0), 1024); 145 147 146 148 s2s->stanza_size_limit = j_atoi(config_get_one(s2s->config, "io.limits.stanzasize", 0), 0); … … 151 153 s2s->check_idle = j_atoi(config_get_one(s2s->config, "check.idle", 0), 86400); 152 154 153 155 } 154 156 157 static void _s2s_hosts_expand(s2s_t s2s) 158 { 159 char *realm; 160 config_elem_t elem; 161 char id[1024]; 162 int i; 163 164 elem = config_get(s2s->config, "local.id"); 165 for(i = 0; i < elem->nvalues; i++) { 166 host_t host = (host_t) pmalloco(xhash_pool(s2s->hosts), sizeof(struct host_st)); 167 if(!host) { 168 log_write(s2s->log, LOG_ERR, "cannot allocate memory for new host, aborting"); 169 exit(1); 170 } 171 172 realm = j_attr((const char **) elem->attrs[i], "realm"); 173 174 /* stringprep ids (domain names) so that they are in canonical form */ 175 strncpy(id, elem->values[i], 1024); 176 id[1023] = '\0'; 177 if (stringprep_nameprep(id, 1024) != 0) { 178 log_write(s2s->log, LOG_ERR, "cannot stringprep id %s, aborting", id); 179 exit(1); 180 } 181 182 host->realm = (realm != NULL) ? realm : pstrdup(xhash_pool(s2s->hosts), id); 183 184 host->host_pemfile = j_attr((const char **) elem->attrs[i], "pemfile"); 185 186 host->host_cachain = j_attr((const char **) elem->attrs[i], "cachain"); 187 188 host->host_verify_mode = j_atoi(j_attr((const char **) elem->attrs[i], "verify-mode"), 0); 189 190 #ifdef HAVE_SSL 191 if(host->host_pemfile != NULL) { 192 if(s2s->sx_ssl == NULL) { 193 s2s->sx_ssl = sx_env_plugin(s2s->sx_env, sx_ssl_init, host->realm, host->host_pemfile, host->host_cachain, host->host_verify_mode); 194 if(s2s->sx_ssl == NULL) { 195 log_write(s2s->log, LOG_ERR, "failed to load %s SSL pemfile", host->realm); 196 host->host_pemfile = NULL; 197 } 198 } else { 199 if(sx_ssl_server_addcert(s2s->sx_ssl, host->realm, host->host_pemfile, host->host_cachain, host->host_verify_mode) != 0) { 200 log_write(s2s->log, LOG_ERR, "failed to load %s SSL pemfile", host->realm); 201 host->host_pemfile = NULL; 202 } 203 } 204 } 205 #endif 206 207 /* insert into vHosts xhash */ 208 xhash_put(s2s->hosts, pstrdup(xhash_pool(s2s->hosts), id), host); 209 210 log_write(s2s->log, LOG_NOTICE, "[%s] configured; realm=%s", id, host->realm); 211 } 212 } 213 155 214 static int _s2s_router_connect(s2s_t s2s) { 156 215 log_write(s2s->log, LOG_NOTICE, "attempting connection to router at %s, port=%d", s2s->router_ip, s2s->router_port); 157 216 158 217 s2s->fd = mio_connect(s2s->mio, s2s->router_port, s2s->router_ip, s2s_router_mio_callback, (void *) s2s); … … 486 545 487 546 #ifdef HAVE_SSL 488 547 /* get the ssl context up and running */ 489 548 if(s2s->local_pemfile != NULL) { 490 s2s->sx_ssl = sx_env_plugin(s2s->sx_env, sx_ssl_init, s2s->local_pemfile, s2s->local_cachain, s2s->local_verify_mode);549 s2s->sx_ssl = sx_env_plugin(s2s->sx_env, sx_ssl_init, NULL, s2s->local_pemfile, s2s->local_cachain, s2s->local_verify_mode); 491 550 492 551 if(s2s->sx_ssl == NULL) { 493 552 log_write(s2s->log, LOG_ERR, "failed to load local SSL pemfile, SSL will not be available to peers"); 494 553 s2s->local_pemfile = NULL; … … 497 556 } 498 557 499 558 /* try and get something online, so at least we can encrypt to the router */ 500 559 if(s2s->sx_ssl == NULL && s2s->router_pemfile != NULL) { 501 s2s->sx_ssl = sx_env_plugin(s2s->sx_env, sx_ssl_init, s2s->router_pemfile, NULL, NULL);560 s2s->sx_ssl = sx_env_plugin(s2s->sx_env, sx_ssl_init, NULL, s2s->router_pemfile, NULL, NULL); 502 561 if(s2s->sx_ssl == NULL) { 503 562 log_write(s2s->log, LOG_ERR, "failed to load router SSL pemfile, channel to router will not be SSL encrypted"); 504 563 s2s->router_pemfile = NULL; 505 564 } … … 511 570 if(s2s->sx_sasl == NULL) { 512 571 log_write(s2s->log, LOG_ERR, "failed to initialise SASL context, aborting"); 513 572 exit(1); 514 573 } 515 574 575 /* hosts mapping */ 576 s2s->hosts = xhash_new(1021); 577 _s2s_hosts_expand(s2s); 578 516 579 s2s->sx_db = sx_env_plugin(s2s->sx_env, s2s_db_init); 517 580 518 581 s2s->mio = mio_new(s2s->io_max_fds); 519 582 … … 664 727 xhash_free(s2s->out); 665 728 xhash_free(s2s->in); 666 729 xhash_free(s2s->in_accept); 667 730 xhash_free(s2s->dnscache); 731 xhash_free(s2s->hosts); 668 732 669 733 prep_cache_free(s2s->pc); 670 734 671 735 jqueue_free(s2s->dead); -
jabberd-2.1.
diff -r -U4 jabberd-2.1.23/s2s/out.c jabberd-2.1.23+sa/s2s/out.c
old new 283 283 284 284 #ifdef HAVE_SSL 285 285 /* Send a stream version of 1.0 if we can do STARTTLS */ 286 286 if(out->s2s->sx_ssl != NULL && out->s2s->local_pemfile != NULL) { 287 sx_client_init(out->s, S2S_DB_HEADER, uri_SERVER, pkt->to->domain, NULL, "1.0");287 sx_client_init(out->s, S2S_DB_HEADER, uri_SERVER, pkt->to->domain, pkt->from->domain, "1.0"); 288 288 } else { 289 289 sx_client_init(out->s, S2S_DB_HEADER, uri_SERVER, NULL, NULL, NULL); 290 290 } 291 291 #else -
jabberd-2.1.
diff -r -U4 jabberd-2.1.23/s2s/s2s.h jabberd-2.1.23+sa/s2s/s2s.h
old new 32 32 # include <sys/stat.h> 33 33 #endif 34 34 35 35 /* forward decl */ 36 typedef struct host_st *host_t; 36 37 typedef struct s2s_st *s2s_t; 37 38 typedef struct pkt_st *pkt_t; 38 39 typedef struct conn_st *conn_t; 39 40 typedef struct dnscache_st *dnscache_t; 40 41 42 struct host_st { 43 /** our realm */ 44 char *realm; 45 46 /** starttls pemfile */ 47 char *host_pemfile; 48 49 /** certificate chain */ 50 char *host_cachain; 51 52 /** verify-mode */ 53 int host_verify_mode; 54 }; 55 41 56 struct s2s_st { 42 57 /** our id (hostname) with the router */ 43 58 char *id; 44 59 … … 105 120 106 121 /** verify-mode */ 107 122 int local_verify_mode; 108 123 124 /** hosts mapping */ 125 xht hosts; 126 109 127 /** max file descriptors */ 110 128 int io_max_fds; 111 129 112 130 /** maximum stanza size */ -
jabberd-2.1.
diff -r -U4 jabberd-2.1.23/sm/main.c jabberd-2.1.23+sa/sm/main.c
old new 309 309 sm->sx_env = sx_env_new(); 310 310 311 311 #ifdef HAVE_SSL 312 312 if(sm->router_pemfile != NULL) { 313 sm->sx_ssl = sx_env_plugin(sm->sx_env, sx_ssl_init, sm->router_pemfile, NULL, NULL);313 sm->sx_ssl = sx_env_plugin(sm->sx_env, sx_ssl_init, NULL, sm->router_pemfile, NULL, NULL); 314 314 if(sm->sx_ssl == NULL) { 315 315 log_write(sm->log, LOG_ERR, "failed to load SSL pemfile, SSL disabled"); 316 316 sm->router_pemfile = NULL; 317 317 } -
sx/plugins.h
diff -r -U4 jabberd-2.1.23/sx/plugins.h jabberd-2.1.23+sa/sx/plugins.h
old new 59 59 60 60 /** init function */ 61 61 JABBERD2_API int sx_ssl_init(sx_env_t env, sx_plugin_t p, va_list args); 62 62 63 /** add cert function */ 64 JABBERD2_API int sx_ssl_server_addcert(sx_plugin_t p, char *name, char *pemfile, char *cachain, int mode); 65 63 66 /** trigger for client starttls */ 64 67 JABBERD2_API int sx_ssl_client_starttls(sx_plugin_t p, sx_t s, char *pemfile); 65 68 66 69 /* previous states */ -
jabberd-2.1.
diff -r -U4 jabberd-2.1.23/sx/ssl.c jabberd-2.1.23+sa/sx/ssl.c
old new 66 66 return preverify_ok; 67 67 } 68 68 69 69 static void _sx_ssl_starttls_notify_proceed(sx_t s, void *arg) { 70 char *to = NULL; 70 71 _sx_debug(ZONE, "preparing for starttls"); 71 72 73 /* store the destination so we can select an ssl context */ 74 if(s->req_to != NULL) to = strdup(s->req_to); 75 72 76 _sx_reset(s); 73 77 78 /* restore destination */ 79 if(s->req_to == NULL) 80 s->req_to = to; 81 else /* ? */ 82 free(to); 83 74 84 /* start listening */ 75 85 sx_server_init(s, s->flags | SX_SSL_WRAPPER); 76 86 } 77 87 … … 482 492 } 483 493 484 494 static void _sx_ssl_client(sx_t s, sx_plugin_t p) { 485 495 _sx_ssl_conn_t sc; 496 SSL_CTX *ctx; 486 497 char *pemfile = NULL; 487 498 int ret; 488 499 489 500 /* only bothering if they asked for wrappermode */ 490 501 if(!(s->flags & SX_SSL_WRAPPER) || s->ssf > 0) 491 502 return; 492 503 493 _sx_debug(ZONE, "preparing for ssl connect for %d", s->tag); 504 _sx_debug(ZONE, "preparing for ssl connect for %d from %s", s->tag, s->req_from); 505 506 /* find the ssl context for this source */ 507 ctx = xhash_get((xht) p->private, s->req_from); 508 if(ctx == NULL) { 509 _sx_debug(ZONE, "using default ssl context for %d", s->tag); 510 ctx = xhash_get((xht) p->private, "*"); 511 } else { 512 _sx_debug(ZONE, "using configured ssl context for %d", s->tag); 513 } 514 assert((int) (ctx != NULL)); 494 515 495 516 sc = (_sx_ssl_conn_t) calloc(1, sizeof(struct _sx_ssl_conn_st)); 496 517 497 518 /* create the buffers */ 498 519 sc->rbio = BIO_new(BIO_s_mem()); 499 520 sc->wbio = BIO_new(BIO_s_mem()); 500 521 501 522 /* new ssl conn */ 502 sc->ssl = SSL_new( (SSL_CTX *) p->private);523 sc->ssl = SSL_new(ctx); 503 524 SSL_set_bio(sc->ssl, sc->rbio, sc->wbio); 504 525 SSL_set_connect_state(sc->ssl); 505 526 SSL_set_ssl_method(sc->ssl, TLSv1_client_method()); 506 527 … … 563 584 } 564 585 565 586 static void _sx_ssl_server(sx_t s, sx_plugin_t p) { 566 587 _sx_ssl_conn_t sc; 588 SSL_CTX *ctx; 567 589 568 590 /* only bothering if they asked for wrappermode */ 569 591 if(!(s->flags & SX_SSL_WRAPPER) || s->ssf > 0) 570 592 return; 571 593 572 _sx_debug(ZONE, "preparing for ssl accept for %d", s->tag); 594 _sx_debug(ZONE, "preparing for ssl accept for %d to %s", s->tag, s->req_to); 595 596 /* find the ssl context for this destination */ 597 ctx = xhash_get((xht) p->private, s->req_to); 598 if(ctx == NULL) { 599 _sx_debug(ZONE, "using default ssl context for %d", s->tag); 600 ctx = xhash_get((xht) p->private, "*"); 601 } else { 602 _sx_debug(ZONE, "using configured ssl context for %d", s->tag); 603 } 604 assert((int) (ctx != NULL)); 573 605 574 606 sc = (_sx_ssl_conn_t) calloc(1, sizeof(struct _sx_ssl_conn_st)); 575 607 576 608 /* create the buffers */ 577 609 sc->rbio = BIO_new(BIO_s_mem()); 578 610 sc->wbio = BIO_new(BIO_s_mem()); 579 611 580 612 /* new ssl conn */ 581 sc->ssl = SSL_new( (SSL_CTX *) p->private);613 sc->ssl = SSL_new(ctx); 582 614 SSL_set_bio(sc->ssl, sc->rbio, sc->wbio); 583 615 SSL_set_accept_state(sc->ssl); 584 616 585 617 /* empty external_id */ … … 626 658 s->plugin_data[p->index] = NULL; 627 659 } 628 660 629 661 static void _sx_ssl_unload(sx_plugin_t p) { 630 SSL_CTX_free((SSL_CTX *) p->private); 662 xht contexts = (xht) p->private; 663 void *ctx; 664 665 if(xhash_iter_first(contexts)) 666 do { 667 xhash_iter_get(contexts, NULL, &ctx); 668 SSL_CTX_free((SSL_CTX *) ctx); 669 } while(xhash_iter_next(contexts)); 670 671 xhash_free(contexts); 631 672 } 632 673 633 674 int sx_openssl_initialized = 0; 634 675 635 /** args: pemfile, cachain, mode */676 /** args: name, pemfile, cachain, mode */ 636 677 int sx_ssl_init(sx_env_t env, sx_plugin_t p, va_list args) { 637 char *pemfile, *cachain; 638 SSL_CTX *ctx; 678 char *name, *pemfile, *cachain; 639 679 int ret; 640 680 int mode; 641 681 642 682 _sx_debug(ZONE, "initialising ssl plugin"); 643 683 684 name = va_arg(args, char *); 644 685 pemfile = va_arg(args, char *); 645 686 if(pemfile == NULL) 646 687 return 1; 647 688 … … 653 694 654 695 /* !!! output openssl error messages to the debug log */ 655 696 656 697 /* openssl startup */ 657 SSL_library_init(); 658 SSL_load_error_strings(); 659 698 if(!sx_openssl_initialized) { 699 SSL_library_init(); 700 SSL_load_error_strings(); 701 } 660 702 sx_openssl_initialized = 1; 661 703 704 ret = sx_ssl_server_addcert(p, name, pemfile, cachain, mode); 705 if(ret) 706 return 1; 707 708 p->magic = SX_SSL_MAGIC; 709 710 p->unload = _sx_ssl_unload; 711 712 p->client = _sx_ssl_client; 713 p->server = _sx_ssl_server; 714 p->rio = _sx_ssl_rio; 715 p->wio = _sx_ssl_wio; 716 p->features = _sx_ssl_features; 717 p->process = _sx_ssl_process; 718 p->free = _sx_ssl_free; 719 720 return 0; 721 } 722 723 /** args: name, pemfile, cachain, mode */ 724 int sx_ssl_server_addcert(sx_plugin_t p, char *name, char *pemfile, char *cachain, int mode) { 725 xht contexts = (xht) p->private; 726 SSL_CTX *ctx; 727 SSL_CTX *tmp; 728 int ret; 729 730 if(!sx_openssl_initialized) { 731 _sx_debug(ZONE, "ssl plugin not initialised"); 732 return 1; 733 } 734 735 if(name == NULL) 736 name = "*"; 737 738 if(pemfile == NULL) 739 return 1; 740 741 /* !!! output openssl error messages to the debug log */ 742 662 743 /* create the context */ 663 744 ctx = SSL_CTX_new(SSLv23_method()); 664 745 if(ctx == NULL) { 665 746 _sx_debug(ZONE, "ssl context creation failed"); … … 697 778 SSL_CTX_free(ctx); 698 779 return 1; 699 780 } 700 781 701 _sx_debug(ZONE, " Setting verify mode to %02x", mode);782 _sx_debug(ZONE, "setting ssl context '%s' verify mode to %02x", name, mode); 702 783 SSL_CTX_set_verify(ctx, mode, _sx_ssl_verify_callback); 703 784 704 /* its good */ 705 _sx_debug(ZONE, "ssl context initialised; certificate and key loaded from %s", pemfile); 706 707 p->magic = SX_SSL_MAGIC; 785 /* create hash and create default context */ 786 if(contexts == NULL) { 787 contexts = xhash_new(1021); 788 p->private = (void *) contexts; 789 790 /* this is the first context, if it's not the default then make a copy of it as the default */ 791 if(!(name[0] == '*' && name[1] == 0)) { 792 int ret = sx_ssl_server_addcert(p, "*", pemfile, cachain, mode); 793 794 if(ret) { 795 /* uh-oh */ 796 xhash_free(contexts); 797 p->private = NULL; 798 return 1; 799 } 800 } 801 } 708 802 709 p->private = (void *) ctx;803 _sx_debug(ZONE, "ssl context '%s' initialised; certificate and key loaded from %s", name, pemfile); 710 804 711 p->unload = _sx_ssl_unload; 805 /* remove an existing context with the same name before replacing it */ 806 tmp = xhash_get(contexts, name); 807 if(tmp != NULL) 808 SSL_CTX_free((SSL_CTX *) tmp); 712 809 713 p->client = _sx_ssl_client; 714 p->server = _sx_ssl_server; 715 p->rio = _sx_ssl_rio; 716 p->wio = _sx_ssl_wio; 717 p->features = _sx_ssl_features; 718 p->process = _sx_ssl_process; 719 p->free = _sx_ssl_free; 810 xhash_put(contexts, name, ctx); 720 811 721 812 return 0; 722 813 } 723 814
