- 1/*
- 2 * IRC - Internet Relay Chat, ircd/ircd_cloaking.c
- 3 * Copyright (C) 1999 Thomas Helvey
- 4 *
- 5 * This program is free software; you can redistribute it and/or modify
- 6 * it under the terms of the GNU General Public License as published by
- 7 * the Free Software Foundation; either version 1, or (at your option)
- 8 * any later version.
- 9 *
- 10 * This program is distributed in the hope that it will be useful,
- 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
- 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- 13 * GNU General Public License for more details.
- 14 *
- 15 * You should have received a copy of the GNU General Public License
- 16 * along with this program; if not, write to the Free Software
- 17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- 18 */
- 19/** @file
- 20 * @brief Implementation of IP and host cloaking functions..
- 21 * @version $Id$
- 22 */
- 23#include "config.h"
- 24
- 25#include "ircd_chattr.h"
- 26#include "ircd_cloaking.h"
- 27#include "ircd_defs.h"
- 28#include "ircd_features.h"
- 29#include "ircd_md5.h"
- 30#include "ircd_snprintf.h"
- 31#include "res.h"
- 32
- 33#include <netinet/in.h>
- 34#include <string.h>
- 35
- 36#undef KEY1
- 37#undef KEY2
- 38#undef KEY3
- 39#undef PREFIX
- 40#define KEY1 feature_str(FEAT_HOST_HIDING_KEY1)
- 41#define KEY2 feature_str(FEAT_HOST_HIDING_KEY2)
- 42#define KEY3 feature_str(FEAT_HOST_HIDING_KEY3)
- 43#define PREFIX feature_str(FEAT_HOST_HIDING_PREFIX)
- 44
- 45/** Downsamples a 128bit result to 32bits (md5 -> unsigned int) */
- 46static inline unsigned int downsample(unsigned char *i)
- 47{
- 48unsigned char r[4];
- 49
- 50 r[0] = i[0] ^ i[1] ^ i[2] ^ i[3];
- 51 r[1] = i[4] ^ i[5] ^ i[6] ^ i[7];
- 52 r[2] = i[8] ^ i[9] ^ i[10] ^ i[11];
- 53 r[3] = i[12] ^ i[13] ^ i[14] ^ i[15];
- 54
- 55 return ( ((unsigned int)r[0] << 24) +
- 56 ((unsigned int)r[1] << 16) +
- 57 ((unsigned int)r[2] << 8) +
- 58 (unsigned int)r[3]);
- 59}
- 60
- 61/** Downsamples a 128bit result to 24bits (md5 > unsigned int) */
- 62static inline unsigned int downsample24(unsigned char *i)
- 63{
- 64unsigned char r[4];
- 65
- 66 r[0] = i[0] ^ i[1] ^ i[2] ^ i[3] ^ i[4];
- 67 r[1] = i[5] ^ i[6] ^ i[7] ^ i[8] ^ i[9] ^ i[10];
- 68 r[2] = i[11] ^ i[12] ^ i[13] ^ i[14] ^ i[15];
- 69
- 70 return ( ((unsigned int)r[0] << 16) +
- 71 ((unsigned int)r[1] << 8) +
- 72 (unsigned int)r[2]);
- 73}
- 74
- 75
- 76char *hidehost_ipv4(struct irc_in_addr *ip)
- 77{
- 78unsigned int a, b, c, d;
- 79static char buf[512], res[512], res2[512], result[128];
- 80unsigned long n;
- 81unsigned int alpha, beta, gamma, delta;
- 82unsigned char *pch;
- 83
- 84 /*
- 85 * Output: ALPHA.BETA.GAMMA.DELTA.IP
- 86 * ALPHA is unique for a.b.c.d
- 87 * BETA is unique for a.b.c.*
- 88 * GAMMA is unique for a.b.*
- 89 * We cloak like this:
- 90 * ALPHA = downsample24(md5(md5("KEY2:A.B.C.D:KEY3")+"KEY1"));
- 91 * BETA = downsample24(md5(md5("KEY3:A.B.C:KEY1")+"KEY2"));
- 92 * GAMMA = downsample24(md5(md5("KEY1:A.B:KEY2")+"KEY3"));
- 93 * DELTA = downsample24(md5(md5("KEY2:A:KEY1:KEY3")+"KEY1"));
- 94 */
- 95 if (!irc_in_addr_is_ipv4(ip))
- 96 return hidehost_ipv6(ip);
- 97
- 98 pch = (unsigned char*)&ip->in6_16[6];
- 99 a = *pch++;
- 100 b = *pch;
- 101 pch = (unsigned char*)&ip->in6_16[7];
- 102 c = *pch++;
- 103 d = *pch;
- 104
- 105 /* ALPHA... */
- 106 ircd_snprintf(0, buf, 512, "%s:%03d.%03d.%03d.%03d:%s", KEY2, a, b, c, d, KEY3);
- 107 DoMD5((unsigned char *)&res, (unsigned char *)&buf, strlen(buf));
- 108 strcpy(res+16, KEY1); /* first 16 bytes are filled, append our key.. */
- 109 n = strlen(res+16) + 16;
- 110 DoMD5((unsigned char *)&res2, (unsigned char *)&res, n);
- 111 alpha = downsample24((unsigned char *)&res2);
- 112
- 113 /* BETA... */
- 114 ircd_snprintf(0, buf, 512, "%s:%03d.%03d.%03d:%s", KEY3, a, b, c, KEY1);
- 115 DoMD5((unsigned char *)&res, (unsigned char *)&buf, strlen(buf));
- 116 strcpy(res+16, KEY2); /* first 16 bytes are filled, append our key.. */
- 117 n = strlen(res+16) + 16;
- 118 DoMD5((unsigned char *)&res2, (unsigned char *)&res, n);
- 119 beta = downsample24((unsigned char *)&res2);
- 120
- 121 /* GAMMA... */
- 122 ircd_snprintf(0, buf, 512, "%s:%03d.%03d:%s", KEY1, a, b, KEY2);
- 123 DoMD5((unsigned char *)&res, (unsigned char *)&buf, strlen(buf));
- 124 strcpy(res+16, KEY3); /* first 16 bytes are filled, append our key.. */
- 125 n = strlen(res+16) + 16;
- 126 DoMD5((unsigned char *)&res2, (unsigned char *)&res, n);
- 127 gamma = downsample24((unsigned char *)&res2);
- 128
- 129 /* DELTA... */
- 130 ircd_snprintf(0, buf, 512, "%s:%03d:%s:%s", KEY2, a, KEY1, KEY3);
- 131 DoMD5((unsigned char *)&res, (unsigned char *)&buf, strlen(buf));
- 132 strcpy(res+16, KEY1); /* first 16 bytes are filled, append our key.. */
- 133 n = strlen(res+16) + 16;
- 134 DoMD5((unsigned char *)&res2, (unsigned char *)&res, n);
- 135 delta = downsample24((unsigned char *)&res2);
- 136
- 137 ircd_snprintf(0, result, HOSTLEN, "%X.%X.%X.%X.IP", alpha, beta, gamma, delta);
- 138 return result;
- 139}
- 140
- 141char *hidehost_ipv6(struct irc_in_addr *ip)
- 142{
- 143unsigned int a, b, c, d, e, f, g, h;
- 144static char buf[512], res[512], res2[512], result[128];
- 145unsigned long n;
- 146unsigned int alpha, beta, gamma, delta;
- 147
- 148 /*
- 149 * Output: ALPHA:BETA:GAMMA:IP
- 150 * ALPHA is unique for a:b:c:d:e:f:g:h
- 151 * BETA is unique for a:b:c:d:e:f:g
- 152 * GAMMA is unique for a:b:c:d
- 153 * We cloak like this:
- 154 * ALPHA = downsample24(md5(md5("KEY2:a:b:c:d:e:f:g:h:KEY3")+"KEY1"));
- 155 * BETA = downsample24(md5(md5("KEY3:a:b:c:d:e:f:g:KEY1")+"KEY2"));
- 156 * GAMMA = downsample24(md5(md5("KEY1:a:b:c:d:KEY2")+"KEY3"));
- 157 * DELTA = downsample24(md5(md5("KEY2:a:b:KEY1:KEY3")+"KEY1"));
- 158 */
- 159
- 160 if (irc_in_addr_is_ipv4(ip))
- 161 return hidehost_ipv4(ip);
- 162
- 163 a = ntohs(ip->in6_16[0]);
- 164 b = ntohs(ip->in6_16[1]);
- 165 c = ntohs(ip->in6_16[2]);
- 166 d = ntohs(ip->in6_16[3]);
- 167 e = ntohs(ip->in6_16[4]);
- 168 f = ntohs(ip->in6_16[5]);
- 169 g = ntohs(ip->in6_16[6]);
- 170 h = ntohs(ip->in6_16[7]);
- 171
- 172 /* ALPHA... */
- 173 ircd_snprintf(0, buf, 512, "%s:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%s", KEY2, a, b, c, d, e, f, g, h, KEY3);
- 174 DoMD5((unsigned char *)&res, (unsigned char *)&buf, strlen(buf));
- 175 strcpy(res+16, KEY1); /* first 16 bytes are filled, append our key */
- 176 n = strlen(res+16) + 16;
- 177 DoMD5((unsigned char *)&res2, (unsigned char *)&res, n);
- 178 alpha = downsample24((unsigned char *)&res2);
- 179
- 180 /* BETA... */
- 181 ircd_snprintf(0, buf, 512, "%s:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%s", KEY3, a, b, c, d, e, f, g, KEY1);
- 182 DoMD5((unsigned char *)&res, (unsigned char *)&buf, strlen(buf));
- 183 strcpy(res+16, KEY2); /* first 16 bytes are filled, append our key.. */
- 184 n = strlen(res+16) + 16;
- 185 DoMD5((unsigned char *)&res2, (unsigned char *)&res, n);
- 186 beta = downsample24((unsigned char *)&res2);
- 187
- 188 /* GAMMA... */
- 189 ircd_snprintf(0, buf, 512, "%s:%04x:%04x:%04x:%04x:%s", KEY1, a, b, c, d, KEY2);
- 190 DoMD5((unsigned char *)&res, (unsigned char *)&buf, strlen(buf));
- 191 strcpy(res+16, KEY3); /* first 16 bytes are filled, append our key.. */
- 192 n = strlen(res+16) + 16;
- 193 DoMD5((unsigned char *)&res2, (unsigned char *)&res, n);
- 194 gamma = downsample24((unsigned char *)&res2);
- 195
- 196 /* DELTA... */
- 197 ircd_snprintf(0, buf, 512, "%s:%04x:%04x:%s:%s", KEY2, a, b, KEY1, KEY3);
- 198 DoMD5((unsigned char *)&res, (unsigned char *)&buf, strlen(buf));
- 199 strcpy(res+16, KEY1); /* first 16 bytes are filled, append our key.. */
- 200 n = strlen(res+16) + 16;
- 201 DoMD5((unsigned char *)&res2, (unsigned char *)&res, n);
- 202 delta = downsample24((unsigned char *)&res2);
- 203
- 204 ircd_snprintf(0, result, HOSTLEN, "%X:%X:%X:%X:IP", alpha, beta, gamma, delta);
- 205 return result;
- 206}
- 207
- 208char *hidehost_normalhost(char *host, int components)
- 209{
- 210char *p, *c;
- 211static char buf[512], res[512], res2[512], result[HOSTLEN+1];
- 212unsigned int alpha, n;
- 213int comps = 0;
- 214
- 215 ircd_snprintf(0, buf, 512, "%s:%s:%s", KEY1, host, KEY2);
- 216 DoMD5((unsigned char *)&res, (unsigned char *)&buf, strlen(buf));
- 217 strcpy(res+16, KEY3); /* first 16 bytes are filled, append our key */
- 218 n = strlen(res+16) + 16;
- 219 DoMD5((unsigned char *)&res2, (unsigned char *)&res, n);
- 220 alpha = downsample((unsigned char *)&res2);
- 221
- 222 for (p = host; *p; p++) {
- 223 if (*p == '.') {
- 224 comps++;
- 225 if ((comps >= components) && IsHostChar(*(p + 1)))
- 226 break;
- 227 }
- 228 }
- 229
- 230 if (*p)
- 231 {
- 232 unsigned int len;
- 233 p++;
- 234
- 235 ircd_snprintf(0, result, HOSTLEN, "%s-%X.", PREFIX, alpha);
- 236 len = strlen(result) + strlen(p);
- 237 if (len <= HOSTLEN)
- 238 strcat(result, p);
- 239 else
- 240 {
- 241 c = p + (len - HOSTLEN);
- 242 if ((*c == '.') && *(c+1))
- 243 c++;
- 244 strcat(result, c);
- 245 }
- 246 } else
- 247 ircd_snprintf(0, result, HOSTLEN, "%s-%X", PREFIX, alpha);
- 248
- 249 return result;
- 250}
Raw Paste