]> jfr.im git - solanum.git/blame - extensions/ip_cloaking.c
More ip_cloaking improvements from Spaz.
[solanum.git] / extensions / ip_cloaking.c
CommitLineData
b076458c 1/* $Id: ip_cloaking.c 3526 2007-07-06 07:56:14Z nenolod $ */
212380e3
AC
2
3#include "stdinc.h"
4#include "modules.h"
5#include "hook.h"
6#include "client.h"
7#include "ircd.h"
8#include "send.h"
3213b626 9#include "hash.h"
212380e3
AC
10#include "s_conf.h"
11#include "s_user.h"
12#include "s_serv.h"
212380e3
AC
13#include "numeric.h"
14
15/* if you're modifying this module, you'll probably to change this */
16#define KEY 0x13748cfa
17
18static int
19_modinit(void)
20{
21 /* add the usermode to the available slot */
22 user_modes['h'] = find_umode_slot();
23 construct_umodebuf();
24
25 return 0;
26}
27
28static void
29_moddeinit(void)
30{
31 /* disable the umode and remove it from the available list */
32 user_modes['h'] = 0;
33 construct_umodebuf();
34}
35
36static void check_umode_change(void *data);
37static void check_new_user(void *data);
38mapi_hfn_list_av1 ip_cloaking_hfnlist[] = {
39 { "umode_changed", (hookfn) check_umode_change },
40 { "new_local_user", (hookfn) check_new_user },
41 { NULL, NULL }
42};
43
44DECLARE_MODULE_AV1(ip_cloaking, _modinit, _moddeinit, NULL, NULL,
b076458c 45 ip_cloaking_hfnlist, "$Revision: 3526 $");
212380e3
AC
46
47static void
48distribute_hostchange(struct Client *client)
49{
50 if (irccmp(client->host, client->orighost))
51 sendto_one_numeric(client, RPL_HOSTHIDDEN, "%s :is now your hidden host",
52 client->host);
53 else
54 sendto_one_numeric(client, RPL_HOSTHIDDEN, "%s :hostname reset",
55 client->host);
56
57 sendto_server(NULL, NULL,
58 CAP_EUID | CAP_TS6, NOCAPS, ":%s CHGHOST %s :%s",
59 use_id(&me), use_id(client), client->host);
60 sendto_server(NULL, NULL,
61 CAP_TS6, CAP_EUID, ":%s ENCAP * CHGHOST %s :%s",
62 use_id(&me), use_id(client), client->host);
212380e3
AC
63 if (irccmp(client->host, client->orighost))
64 SetDynSpoof(client);
65 else
66 ClearDynSpoof(client);
67}
68
762cc38c 69#define HOSTLEN 63
762cc38c 70
212380e3 71static void
762cc38c 72do_host_cloak_ip(const char *inbuf, char *outbuf)
212380e3 73{
3213b626
JT
74 /* None of the characters in this table can be valid in an IP */
75 char chartable[] = "ghijklmnopqrstuvwxyz";
762cc38c 76 char *tptr;
3213b626
JT
77 uint32_t accum = fnv_hash((const unsigned char*) inbuf, 32);
78 int sepcount = 0;
79 int totalcount = 0;
b42eac75 80 int ipv6 = 0;
762cc38c 81
3213b626 82 strncpy(outbuf, inbuf, HOSTLEN);
514235a7 83
3213b626 84 if (strchr(outbuf, ':'))
b42eac75 85 {
b42eac75 86 ipv6 = 1;
b42eac75 87
3213b626
JT
88 /* Damn you IPv6...
89 * We count the number of colons so we can calculate how much
90 * of the host to cloak. This is because some hostmasks may not
91 * have as many octets as we'd like.
92 *
93 * We have to do this ahead of time because doing this during
94 * the actual cloaking would get ugly
95 */
96 for (tptr = outbuf; *tptr != '\0'; tptr++)
97 {
98 if (*tptr == ':') {
99 totalcount++;
100 }
101 }
102 }
103 else if (!strchr(outbuf, '.'))
b076458c 104 {
514235a7 105 return;
b076458c 106 }
514235a7 107
3213b626 108 for (tptr = outbuf; *tptr != '\0'; tptr++)
b42eac75 109 {
3213b626
JT
110 if (*tptr == ':' || *tptr == '.')
111 {
112 sepcount++;
113 continue;
114 }
115
116 switch (ipv6)
117 {
118 case 1:
119 if (sepcount < totalcount / 2)
120 break;
121 case 0:
122 if (sepcount < 2)
123 break;
124 default:
125 *tptr = chartable[(*tptr + accum) % 20];
126
127 }
128
129 accum = (accum << 1) | (accum >> 31);
b42eac75 130 }
762cc38c
AC
131}
132
133static void
134do_host_cloak_host(const char *inbuf, char *outbuf)
135{
136 char b26_alphabet[] = "abcdefghijklmnopqrstuvwxyz";
137 char *tptr;
3213b626 138 uint32_t accum = fnv_hash((const unsigned char*) inbuf, 32);
762cc38c
AC
139
140 strncpy(outbuf, inbuf, HOSTLEN);
141
142 /* pass 1: scramble first section of hostname using base26
3213b626 143 * alphabet toasted against the FNV hash of the string.
762cc38c
AC
144 *
145 * numbers are not changed at this time, only letters.
146 */
147 for (tptr = outbuf; *tptr != '\0'; tptr++)
212380e3 148 {
762cc38c
AC
149 if (*tptr == '.')
150 break;
151
152 if (isdigit(*tptr) || *tptr == '-')
153 continue;
154
3213b626
JT
155 *tptr = b26_alphabet[(*tptr + accum) % 26];
156
157 /* Rotate one bit to avoid all digits being turned odd or even */
158 accum = (accum << 1) | (accum >> 31);
212380e3 159 }
762cc38c
AC
160
161 /* pass 2: scramble each number in the address */
162 for (tptr = outbuf; *tptr != '\0'; tptr++)
163 {
164 if (isdigit(*tptr))
165 {
3213b626 166 *tptr = (*tptr + accum) % 10;
762cc38c 167 }
3213b626
JT
168
169 accum = (accum << 1) | (accum >> 31);
762cc38c 170 }
212380e3
AC
171}
172
173static void
174check_umode_change(void *vdata)
175{
176 hook_data_umode_changed *data = (hook_data_umode_changed *)vdata;
177 struct Client *source_p = data->client;
178
179 if (!MyClient(source_p))
180 return;
181
182 /* didn't change +h umode, we don't need to do anything */
183 if (!((data->oldumodes ^ source_p->umodes) & user_modes['h']))
184 return;
185
186 if (source_p->umodes & user_modes['h'])
187 {
188 if (IsIPSpoof(source_p) || source_p->localClient->mangledhost == NULL || (IsDynSpoof(source_p) && strcmp(source_p->host, source_p->localClient->mangledhost)))
189 {
190 source_p->umodes &= ~user_modes['h'];
191 return;
192 }
193 if (strcmp(source_p->host, source_p->localClient->mangledhost))
194 {
f427c8b0 195 rb_strlcpy(source_p->host, source_p->localClient->mangledhost, HOSTLEN);
212380e3
AC
196 distribute_hostchange(source_p);
197 }
198 else /* not really nice, but we need to send this numeric here */
199 sendto_one_numeric(source_p, RPL_HOSTHIDDEN, "%s :is now your hidden host",
200 source_p->host);
201 }
202 else if (!(source_p->umodes & user_modes['h']))
203 {
204 if (source_p->localClient->mangledhost != NULL &&
205 !strcmp(source_p->host, source_p->localClient->mangledhost))
206 {
f427c8b0 207 rb_strlcpy(source_p->host, source_p->orighost, HOSTLEN);
212380e3
AC
208 distribute_hostchange(source_p);
209 }
210 }
211}
212
213static void
214check_new_user(void *vdata)
215{
216 struct Client *source_p = (void *)vdata;
217
218 if (IsIPSpoof(source_p))
219 {
220 source_p->umodes &= ~user_modes['h'];
221 return;
222 }
954012d3 223 source_p->localClient->mangledhost = rb_malloc(HOSTLEN);
212380e3 224 if (!irccmp(source_p->orighost, source_p->sockhost))
762cc38c 225 do_host_cloak_ip(source_p->orighost, source_p->localClient->mangledhost);
212380e3 226 else
762cc38c 227 do_host_cloak_host(source_p->orighost, source_p->localClient->mangledhost);
212380e3
AC
228 if (IsDynSpoof(source_p))
229 source_p->umodes &= ~user_modes['h'];
230 if (source_p->umodes & user_modes['h'])
231 {
f427c8b0 232 rb_strlcpy(source_p->host, source_p->localClient->mangledhost, sizeof(source_p->host));
212380e3
AC
233 if (irccmp(source_p->host, source_p->orighost))
234 SetDynSpoof(source_p);
235 }
236}