23 #include <stringprep.h>
60 if((f = fopen(pidfile,
"w+")) == NULL) {
61 log_write(s2s->
log, LOG_ERR,
"couldn't open %s for writing: %s", pidfile, strerror(errno));
65 if(fprintf(f,
"%d", pid) < 0) {
66 log_write(s2s->
log, LOG_ERR,
"couldn't write to %s: %s", pidfile, strerror(errno));
73 log_write(s2s->
log, LOG_INFO,
"process id is %d, written to %s", pid, pidfile);
78 char *str, secret[41];
113 if(strcmp(str,
"file") == 0)
115 else if(strcmp(str,
"syslog") == 0)
154 for(i = 0; i < 40; i++) {
155 r = (int) (36.0 * rand() / RAND_MAX);
156 secret[i] = (r >= 0 && r <= 9) ? (r + 48) : (r + 87);
213 if (elem)
for(i = 0; i < elem->
nvalues; i++) {
216 log_write(s2s->
log, LOG_ERR,
"cannot allocate memory for new host, aborting");
220 realm =
j_attr((
const char **) elem->
attrs[i],
"realm");
223 strncpy(
id, elem->
values[i], 1024);
225 if (stringprep_nameprep(
id, 1024) != 0) {
226 log_write(s2s->
log, LOG_ERR,
"cannot stringprep id %s, aborting",
id);
266 if(s2s->
fd == NULL) {
267 if(errno == ECONNREFUSED)
284 time_t now, dialback_time;
299 log_write(s2s->
log, LOG_NOTICE,
"[%d] [%s, port=%d] dialback for %s route '%.*s' timed out", conn->
fd->
fd, conn->
ip, conn->
port, direction, rkeylen, rkey);
340 log_debug(
ZONE,
"running time checks for %.*s", keylen, rkey);
341 c = memchr(rkey,
'/', keylen);
343 c_len = keylen - (c - rkey);
347 if(dns != NULL && dns->pending) {
350 log_write(s2s->
log, LOG_NOTICE,
"dns lookup for %.*s timed out", c_len, c);
358 if (dns->query != NULL) {
359 if (dns->query->query != NULL)
360 dns_cancel(NULL, dns->query->query);
363 free(dns->query->name);
391 log_write(s2s->
log, LOG_NOTICE,
"[%d] [%s, port=%d] connection to %s timed out", conn->
fd->
fd, conn->
ip, conn->
port, c);
423 log_debug(
ZONE,
"checking dialback state for outgoing conn %.*s", keylen, key);
425 log_debug(
ZONE,
"checking pending verify requests for outgoing conn %.*s", keylen, key);
427 log_write(s2s->
log, LOG_NOTICE,
"[%d] [%s, port=%d] dialback verify request timed out", conn->
fd->
fd, conn->
ip, conn->
port);
440 log_debug(
ZONE,
"checking pending verify requests for outgoing conn %s (%s)", conn->
dkey, conn->
key);
442 log_write(s2s->
log, LOG_NOTICE,
"[%d] [%s, port=%d] dialback verify request timed out", conn->
fd->
fd, conn->
ip, conn->
port);
456 log_debug(
ZONE,
"checking dialback state for incoming conn %.*s", keylen, key);
460 log_write(s2s->
log, LOG_NOTICE,
"[%d] [%s, port=%d] no dialback started", conn->
fd->
fd, conn->
ip, conn->
port);
472 log_debug(
ZONE,
"checking stream connection state for incoming conn %i", conn->
fd->
fd);
474 log_write(s2s->
log, LOG_NOTICE,
"[%d] [%s, port=%d] stream initiation timed out", conn->
fd->
fd, conn->
ip, conn->
port);
517 log_debug(
ZONE,
"checking idle state for %.*s", keylen, key);
541 log_debug(
ZONE,
"checking idle state for %.*s", keylen, key);
571 if (dns->
query != NULL) {
581 else if (dns == NULL) {
590 if (res && now > res->
expiry) {
597 else if (res == NULL) {
609 dns_ioevent(0, time(NULL));
624 for (i = 0, j = 0; i < nvalues; i++) {
625 elem_len = strlen(values[i]);
627 log_debug(
ZONE,
"whitelist domain element is too large, skipping");
631 log_debug(
ZONE,
"whitelist domain element is blank, skipping");
654 char **segments = NULL;
656 char *seg_tmp = NULL;
665 char *domain_ptr = &domain[0];
668 strncpy(domain, in_domain,
sizeof(domain));
669 domain[
sizeof(domain)-1] =
'\0';
670 domain_len = strlen((
const char *)&domain);
672 if (domain_len <= 0) {
673 log_write(s2s->
log, LOG_NOTICE,
"s2s_domain_in_whitelist: in_domain is empty");
688 if (!strncmp((
const char *)&domain, s2s->
whitelist_domains[wl_index], (domain_len > wl_len) ? domain_len : wl_len)) {
689 log_debug(
ZONE,
"domain \"%s\" matches whitelist entry", &domain);
698 for (dotcount = 0, x = 0; domain[x] !=
'\0'; x++) {
699 if (domain[x] ==
'.')
703 segments = (
char **)malloc(
sizeof(
char*) * (dotcount + 1));
704 if (segments == NULL) {
705 log_write(s2s->
log, LOG_ERR,
"s2s_domain_in_whitelist: malloc() error");
708 memset((
char **)segments, 0, (
sizeof(
char*) * (dotcount + 1)));
711 if (segcount > (dotcount+1)) {
712 log_write(s2s->
log, LOG_ERR,
"s2s_domain_in_whitelist: did not malloc enough room for domain segments; should never get here");
713 if (seg_tmp != NULL) {
717 for (x = 0; x < segcount; x++) {
725 seg_tmp = strsep(&domain_ptr,
".");
726 if (seg_tmp == NULL) {
730 seg_tmp_len = strlen(seg_tmp);
733 if (seg_tmp != NULL) {
737 for (x = 0; x < segcount; x++) {
745 dst = &segments[segcount];
746 *dst = (
char *)malloc(seg_tmp_len + 1);
748 strncpy(*dst, seg_tmp, seg_tmp_len + 1);
749 (*dst)[seg_tmp_len] =
'\0';
751 if (seg_tmp != NULL) {
755 for (x = 0; x < segcount; x++) {
761 log_write(s2s->
log, LOG_ERR,
"s2s_domain_in_whitelist: malloc() error");
765 }
while (seg_tmp != NULL);
768 for (domain_index = segcount-2; domain_index > 0; domain_index--) {
770 for (i = domain_index; i < segcount; i++) {
771 if (i > domain_index) {
772 strncat((
char *)&matchstr,
".",
sizeof(matchstr));
773 matchstr[
sizeof(matchstr)-1] =
'\0';
775 strncat((
char *)&matchstr, (
char *)segments[i],
sizeof(matchstr));
776 matchstr[
sizeof(matchstr)-1] =
'\0';
780 matchstr_len = strlen((
const char *)&matchstr);
781 if (!strncmp((
const char *)&matchstr, s2s->
whitelist_domains[wl_index], (wl_len > matchstr_len ? wl_len : matchstr_len))) {
782 log_debug(
ZONE,
"matchstr \"%s\" matches whitelist entry", &matchstr);
783 for (x = 0; x < segcount; x++) {
797 for (x = 0; x < segcount; x++) {
807 JABBER_MAIN(
"jabberd2s2s",
"Jabber 2 S2S",
"Jabber Open Source Server: Server to Server",
"jabberd2router\0")
817 time_t check_time = 0, now = 0;
818 const char *cli_id = 0;
821 umask((mode_t) 0027);
826 #ifdef HAVE_WINSOCK2_H
829 WORD wVersionRequested;
833 wVersionRequested = MAKEWORD( 2, 2 );
835 err = WSAStartup( wVersionRequested, &wsaData );
860 config_file = CONFIG_DIR
"/s2s.xml";
863 while((optchar = getopt(argc, argv,
"Dc:hi:?")) >= 0)
868 config_file = optarg;
874 printf(
"WARN: Debugging not enabled. Ignoring -D.\n");
880 case 'h':
case '?':
default:
882 "s2s - jabberd server-to-server connector (" VERSION
")\n"
883 "Usage: s2s <options>\n"
885 " -c <config> config file to use [default: " CONFIG_DIR
"/s2s.xml]\n"
886 " -i id Override <id> config element\n"
888 " -D Show debug output\n"
899 fputs(
"s2s: couldn't load config, aborting\n", stderr);
931 log_write(s2s->
log, LOG_ERR,
"failed to load local SSL pemfile, SSL will not be available to peers");
934 log_debug(
ZONE,
"loaded pemfile for SSL connections to peers");
941 log_write(s2s->
log, LOG_ERR,
"failed to load router SSL pemfile, channel to router will not be SSL encrypted");
956 log_write(s2s->
log, LOG_ERR,
"failed to initialise SASL context, aborting");
968 if((s2s->
udns_fd = dns_init(NULL, 1)) < 0) {
969 log_write(s2s->
log, LOG_ERR,
"unable to initialize dns library, aborting");
978 mio_run(s2s->
mio, dns_timeouts(0, 5, time(NULL)));
995 log_write(s2s->
log, LOG_NOTICE,
"attempting reconnect");
1055 if(now > check_time + 60) {
1060 int fd = open(s2s->
packet_stats, O_TRUNC | O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP);
1063 int len = snprintf(buf, 100,
"%lld\n", s2s->
packet_count);
1064 write(fd, buf, len);
1133 if(conn->
key != NULL) free(conn->
key);
1134 if(conn->
dkey != NULL) free(conn->
dkey);
1155 if (dns->
query != NULL) {
1174 if (dns_active(NULL) > 0)
1175 log_debug(
ZONE,
"there are still active dns queries (%d)", dns_active(NULL));