]> jfr.im git - solanum.git/blame - extensions/ip_cloaking.c
ip_cloaking: fix digits in hostnames
[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
69static void
762cc38c 70do_host_cloak_ip(const char *inbuf, char *outbuf)
212380e3 71{
3213b626
JT
72 /* None of the characters in this table can be valid in an IP */
73 char chartable[] = "ghijklmnopqrstuvwxyz";
762cc38c 74 char *tptr;
3213b626
JT
75 uint32_t accum = fnv_hash((const unsigned char*) inbuf, 32);
76 int sepcount = 0;
77 int totalcount = 0;
b42eac75 78 int ipv6 = 0;
762cc38c 79
e1c1f08d 80 rb_strlcpy(outbuf, inbuf, HOSTLEN + 1);
514235a7 81
3213b626 82 if (strchr(outbuf, ':'))
b42eac75 83 {
b42eac75 84 ipv6 = 1;
b42eac75 85
3213b626
JT
86 /* Damn you IPv6...
87 * We count the number of colons so we can calculate how much
88 * of the host to cloak. This is because some hostmasks may not
89 * have as many octets as we'd like.
90 *
91 * We have to do this ahead of time because doing this during
92 * the actual cloaking would get ugly
93 */
94 for (tptr = outbuf; *tptr != '\0'; tptr++)
95 {
96 if (*tptr == ':') {
97 totalcount++;
98 }
99 }
100 }
101 else if (!strchr(outbuf, '.'))
b076458c 102 {
514235a7 103 return;
b076458c 104 }
514235a7 105
3213b626 106 for (tptr = outbuf; *tptr != '\0'; tptr++)
b42eac75 107 {
3213b626
JT
108 if (*tptr == ':' || *tptr == '.')
109 {
110 sepcount++;
111 continue;
112 }
113
114 switch (ipv6)
115 {
116 case 1:
117 if (sepcount < totalcount / 2)
118 break;
119 case 0:
120 if (sepcount < 2)
121 break;
122 default:
123 *tptr = chartable[(*tptr + accum) % 20];
124
125 }
126
127 accum = (accum << 1) | (accum >> 31);
b42eac75 128 }
762cc38c
AC
129}
130
131static void
132do_host_cloak_host(const char *inbuf, char *outbuf)
133{
134 char b26_alphabet[] = "abcdefghijklmnopqrstuvwxyz";
135 char *tptr;
3213b626 136 uint32_t accum = fnv_hash((const unsigned char*) inbuf, 32);
762cc38c 137
e1c1f08d 138 rb_strlcpy(outbuf, inbuf, HOSTLEN + 1);
762cc38c
AC
139
140 /* pass 1: scramble first section of hostname using base26
3213b626 141 * alphabet toasted against the FNV hash of the string.
762cc38c
AC
142 *
143 * numbers are not changed at this time, only letters.
144 */
145 for (tptr = outbuf; *tptr != '\0'; tptr++)
212380e3 146 {
762cc38c
AC
147 if (*tptr == '.')
148 break;
149
150 if (isdigit(*tptr) || *tptr == '-')
151 continue;
152
3213b626
JT
153 *tptr = b26_alphabet[(*tptr + accum) % 26];
154
155 /* Rotate one bit to avoid all digits being turned odd or even */
156 accum = (accum << 1) | (accum >> 31);
212380e3 157 }
762cc38c
AC
158
159 /* pass 2: scramble each number in the address */
160 for (tptr = outbuf; *tptr != '\0'; tptr++)
161 {
162 if (isdigit(*tptr))
163 {
9d99a309 164 *tptr = '0' + (*tptr + accum) % 10;
762cc38c 165 }
3213b626
JT
166
167 accum = (accum << 1) | (accum >> 31);
762cc38c 168 }
212380e3
AC
169}
170
171static void
172check_umode_change(void *vdata)
173{
174 hook_data_umode_changed *data = (hook_data_umode_changed *)vdata;
175 struct Client *source_p = data->client;
176
177 if (!MyClient(source_p))
178 return;
179
180 /* didn't change +h umode, we don't need to do anything */
181 if (!((data->oldumodes ^ source_p->umodes) & user_modes['h']))
182 return;
183
184 if (source_p->umodes & user_modes['h'])
185 {
186 if (IsIPSpoof(source_p) || source_p->localClient->mangledhost == NULL || (IsDynSpoof(source_p) && strcmp(source_p->host, source_p->localClient->mangledhost)))
187 {
188 source_p->umodes &= ~user_modes['h'];
189 return;
190 }
191 if (strcmp(source_p->host, source_p->localClient->mangledhost))
192 {
e1c1f08d 193 rb_strlcpy(source_p->host, source_p->localClient->mangledhost, HOSTLEN + 1);
212380e3
AC
194 distribute_hostchange(source_p);
195 }
196 else /* not really nice, but we need to send this numeric here */
197 sendto_one_numeric(source_p, RPL_HOSTHIDDEN, "%s :is now your hidden host",
198 source_p->host);
199 }
200 else if (!(source_p->umodes & user_modes['h']))
201 {
202 if (source_p->localClient->mangledhost != NULL &&
203 !strcmp(source_p->host, source_p->localClient->mangledhost))
204 {
e1c1f08d 205 rb_strlcpy(source_p->host, source_p->orighost, HOSTLEN + 1);
212380e3
AC
206 distribute_hostchange(source_p);
207 }
208 }
209}
210
211static void
212check_new_user(void *vdata)
213{
214 struct Client *source_p = (void *)vdata;
215
216 if (IsIPSpoof(source_p))
217 {
218 source_p->umodes &= ~user_modes['h'];
219 return;
220 }
e1c1f08d 221 source_p->localClient->mangledhost = rb_malloc(HOSTLEN + 1);
212380e3 222 if (!irccmp(source_p->orighost, source_p->sockhost))
762cc38c 223 do_host_cloak_ip(source_p->orighost, source_p->localClient->mangledhost);
212380e3 224 else
762cc38c 225 do_host_cloak_host(source_p->orighost, source_p->localClient->mangledhost);
212380e3
AC
226 if (IsDynSpoof(source_p))
227 source_p->umodes &= ~user_modes['h'];
228 if (source_p->umodes & user_modes['h'])
229 {
f427c8b0 230 rb_strlcpy(source_p->host, source_p->localClient->mangledhost, sizeof(source_p->host));
212380e3
AC
231 if (irccmp(source_p->host, source_p->orighost))
232 SetDynSpoof(source_p);
233 }
234}