]> jfr.im git - solanum.git/blame - extensions/ip_cloaking.c
Change default --with-nicklen to 31, like atheme-services.
[solanum.git] / extensions / ip_cloaking.c
CommitLineData
89bfeb8f
EM
1/*
2 * Charybdis: an advanced ircd
3 * ip_cloaking.c: provide user hostname cloaking
4 *
5 * Written originally by nenolod, altered to use FNV by Elizabeth in 2008
6 */
212380e3
AC
7
8#include "stdinc.h"
9#include "modules.h"
10#include "hook.h"
11#include "client.h"
12#include "ircd.h"
13#include "send.h"
3213b626 14#include "hash.h"
212380e3
AC
15#include "s_conf.h"
16#include "s_user.h"
17#include "s_serv.h"
212380e3
AC
18#include "numeric.h"
19
212380e3
AC
20static int
21_modinit(void)
22{
23 /* add the usermode to the available slot */
24 user_modes['h'] = find_umode_slot();
25 construct_umodebuf();
26
27 return 0;
28}
29
30static void
31_moddeinit(void)
32{
33 /* disable the umode and remove it from the available list */
34 user_modes['h'] = 0;
35 construct_umodebuf();
36}
37
38static void check_umode_change(void *data);
39static void check_new_user(void *data);
40mapi_hfn_list_av1 ip_cloaking_hfnlist[] = {
41 { "umode_changed", (hookfn) check_umode_change },
42 { "new_local_user", (hookfn) check_new_user },
43 { NULL, NULL }
44};
45
46DECLARE_MODULE_AV1(ip_cloaking, _modinit, _moddeinit, NULL, NULL,
b076458c 47 ip_cloaking_hfnlist, "$Revision: 3526 $");
212380e3
AC
48
49static void
50distribute_hostchange(struct Client *client)
51{
52 if (irccmp(client->host, client->orighost))
53 sendto_one_numeric(client, RPL_HOSTHIDDEN, "%s :is now your hidden host",
54 client->host);
55 else
56 sendto_one_numeric(client, RPL_HOSTHIDDEN, "%s :hostname reset",
57 client->host);
58
59 sendto_server(NULL, NULL,
60 CAP_EUID | CAP_TS6, NOCAPS, ":%s CHGHOST %s :%s",
61 use_id(&me), use_id(client), client->host);
62 sendto_server(NULL, NULL,
63 CAP_TS6, CAP_EUID, ":%s ENCAP * CHGHOST %s :%s",
64 use_id(&me), use_id(client), client->host);
212380e3
AC
65 if (irccmp(client->host, client->orighost))
66 SetDynSpoof(client);
67 else
68 ClearDynSpoof(client);
69}
70
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
e1c1f08d 82 rb_strlcpy(outbuf, inbuf, HOSTLEN + 1);
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++)
5067fe0e 97 if (*tptr == ':')
3213b626 98 totalcount++;
3213b626
JT
99 }
100 else if (!strchr(outbuf, '.'))
514235a7
AC
101 return;
102
3213b626 103 for (tptr = outbuf; *tptr != '\0'; tptr++)
b42eac75 104 {
3213b626
JT
105 if (*tptr == ':' || *tptr == '.')
106 {
107 sepcount++;
108 continue;
109 }
110
d9439a0e
JT
111 if (ipv6 && sepcount < totalcount / 2)
112 continue;
3213b626 113
d9439a0e
JT
114 if (!ipv6 && sepcount < 2)
115 continue;
3213b626 116
d9439a0e 117 *tptr = chartable[(*tptr + accum) % 20];
3213b626 118 accum = (accum << 1) | (accum >> 31);
b42eac75 119 }
762cc38c
AC
120}
121
122static void
123do_host_cloak_host(const char *inbuf, char *outbuf)
124{
125 char b26_alphabet[] = "abcdefghijklmnopqrstuvwxyz";
126 char *tptr;
3213b626 127 uint32_t accum = fnv_hash((const unsigned char*) inbuf, 32);
762cc38c 128
e1c1f08d 129 rb_strlcpy(outbuf, inbuf, HOSTLEN + 1);
762cc38c
AC
130
131 /* pass 1: scramble first section of hostname using base26
3213b626 132 * alphabet toasted against the FNV hash of the string.
762cc38c
AC
133 *
134 * numbers are not changed at this time, only letters.
135 */
136 for (tptr = outbuf; *tptr != '\0'; tptr++)
212380e3 137 {
762cc38c
AC
138 if (*tptr == '.')
139 break;
140
141 if (isdigit(*tptr) || *tptr == '-')
142 continue;
143
3213b626
JT
144 *tptr = b26_alphabet[(*tptr + accum) % 26];
145
146 /* Rotate one bit to avoid all digits being turned odd or even */
147 accum = (accum << 1) | (accum >> 31);
212380e3 148 }
762cc38c
AC
149
150 /* pass 2: scramble each number in the address */
151 for (tptr = outbuf; *tptr != '\0'; tptr++)
152 {
153 if (isdigit(*tptr))
9d99a309 154 *tptr = '0' + (*tptr + accum) % 10;
3213b626
JT
155
156 accum = (accum << 1) | (accum >> 31);
762cc38c 157 }
212380e3
AC
158}
159
160static void
161check_umode_change(void *vdata)
162{
163 hook_data_umode_changed *data = (hook_data_umode_changed *)vdata;
164 struct Client *source_p = data->client;
165
166 if (!MyClient(source_p))
167 return;
168
169 /* didn't change +h umode, we don't need to do anything */
170 if (!((data->oldumodes ^ source_p->umodes) & user_modes['h']))
171 return;
172
173 if (source_p->umodes & user_modes['h'])
174 {
175 if (IsIPSpoof(source_p) || source_p->localClient->mangledhost == NULL || (IsDynSpoof(source_p) && strcmp(source_p->host, source_p->localClient->mangledhost)))
176 {
177 source_p->umodes &= ~user_modes['h'];
178 return;
179 }
180 if (strcmp(source_p->host, source_p->localClient->mangledhost))
181 {
e1c1f08d 182 rb_strlcpy(source_p->host, source_p->localClient->mangledhost, HOSTLEN + 1);
212380e3
AC
183 distribute_hostchange(source_p);
184 }
185 else /* not really nice, but we need to send this numeric here */
186 sendto_one_numeric(source_p, RPL_HOSTHIDDEN, "%s :is now your hidden host",
187 source_p->host);
188 }
189 else if (!(source_p->umodes & user_modes['h']))
190 {
191 if (source_p->localClient->mangledhost != NULL &&
192 !strcmp(source_p->host, source_p->localClient->mangledhost))
193 {
e1c1f08d 194 rb_strlcpy(source_p->host, source_p->orighost, HOSTLEN + 1);
212380e3
AC
195 distribute_hostchange(source_p);
196 }
197 }
198}
199
200static void
201check_new_user(void *vdata)
202{
203 struct Client *source_p = (void *)vdata;
204
205 if (IsIPSpoof(source_p))
206 {
207 source_p->umodes &= ~user_modes['h'];
208 return;
209 }
e1c1f08d 210 source_p->localClient->mangledhost = rb_malloc(HOSTLEN + 1);
212380e3 211 if (!irccmp(source_p->orighost, source_p->sockhost))
762cc38c 212 do_host_cloak_ip(source_p->orighost, source_p->localClient->mangledhost);
212380e3 213 else
762cc38c 214 do_host_cloak_host(source_p->orighost, source_p->localClient->mangledhost);
212380e3
AC
215 if (IsDynSpoof(source_p))
216 source_p->umodes &= ~user_modes['h'];
217 if (source_p->umodes & user_modes['h'])
218 {
f427c8b0 219 rb_strlcpy(source_p->host, source_p->localClient->mangledhost, sizeof(source_p->host));
212380e3
AC
220 if (irccmp(source_p->host, source_p->orighost))
221 SetDynSpoof(source_p);
222 }
223}