jabberd2  2.2.16
s2s/util.c
Go to the documentation of this file.
00001 /*
00002  * jabberd - Jabber Open Source Server
00003  * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
00004  *                    Ryan Eatmon, Robert Norris
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
00019  */
00020 
00021 #define _GNU_SOURCE
00022 #include <string.h>
00023 
00024 #include "s2s.h"
00025 
00027 char *s2s_route_key(pool_t p, char *local, char *remote) {
00028     char *key;
00029 
00030     if(local == NULL) local = "";
00031     if(remote == NULL) remote = "";
00032 
00033     if(p == NULL)
00034         key = (char *) malloc(strlen(local) + strlen(remote) + 2);
00035     else
00036         key = (char *) pmalloc(p, strlen(local) + strlen(remote) + 2);
00037 
00038     sprintf(key, "%s/%s", local, remote);
00039 
00040     return key;
00041 }
00042 
00044 int s2s_route_key_match(char *local, char *remote, char *rkey, int rkeylen) {
00045     char *klocal, *kremote;
00046     int ret;
00047 
00048     klocal = strndup(rkey, rkeylen);
00049     kremote = strchr(klocal, '/');
00050     if(kremote != NULL) *kremote++ = '\0';
00051 
00052     ret  = (local == NULL || (klocal != NULL && !strcmp(local, klocal)))
00053         && (remote == NULL || (kremote != NULL && !strcmp(remote, kremote)));
00054 
00055     free(klocal);
00056 
00057     return ret;
00058 }
00059 
00061 char *s2s_db_key(pool_t p, char *secret, char *remote, char *id) {
00062     char hash[41], buf[1024];
00063 
00064     _sx_debug(ZONE, "generating dialback key, secret %s, remote %s, id %s", secret, remote, id);
00065 
00066     shahash_r(secret, hash);
00067 
00068     snprintf(buf, 1024, "%s%s", hash, remote);
00069     shahash_r(buf, hash);
00070 
00071     snprintf(buf, 1024, "%s%s", hash, id);
00072     shahash_r(buf, hash);
00073 
00074     _sx_debug(ZONE, "dialback key generated: %s", hash);
00075 
00076     if(p == NULL)
00077         return strdup(hash);
00078     else
00079         return pstrdup(p, hash);
00080 }