]> jfr.im git - irc/rqf/shadowircd.git/blame - extensions/ip_cloaking.c
Log to serverlog when throwing out a server because of a bad nickname.
[irc/rqf/shadowircd.git] / extensions / ip_cloaking.c
CommitLineData
b076458c 1/* $Id: ip_cloaking.c 3526 2007-07-06 07:56:14Z nenolod $ */
212380e3 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"
799483e7 9#include "hash.h"
212380e3 10#include "s_conf.h"
11#include "s_user.h"
12#include "s_serv.h"
212380e3 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 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 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{
799483e7
JT
72 /* None of the characters in this table can be valid in an IP */
73 char chartable[] = "ghijklmnopqrstuvwxyz";
762cc38c 74 char *tptr;
799483e7
JT
75 uint32_t accum = fnv_hash((const unsigned char*) inbuf, 32);
76 int sepcount = 0;
77 int totalcount = 0;
3f46cd00 78 int ipv6 = 0;
762cc38c 79
8109e8e3 80 rb_strlcpy(outbuf, inbuf, HOSTLEN + 1);
514235a7 81
799483e7 82 if (strchr(outbuf, ':'))
3f46cd00 83 {
3f46cd00 84 ipv6 = 1;
3f46cd00 85
799483e7
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++)
00b7ddcc 95 if (*tptr == ':')
799483e7 96 totalcount++;
799483e7
JT
97 }
98 else if (!strchr(outbuf, '.'))
514235a7 99 return;
100
799483e7 101 for (tptr = outbuf; *tptr != '\0'; tptr++)
3f46cd00 102 {
799483e7
JT
103 if (*tptr == ':' || *tptr == '.')
104 {
105 sepcount++;
106 continue;
107 }
108
8e28bf29
JT
109 if (ipv6 && sepcount < totalcount / 2)
110 continue;
799483e7 111
8e28bf29
JT
112 if (!ipv6 && sepcount < 2)
113 continue;
799483e7 114
8e28bf29 115 *tptr = chartable[(*tptr + accum) % 20];
799483e7 116 accum = (accum << 1) | (accum >> 31);
3f46cd00 117 }
762cc38c 118}
119
120static void
121do_host_cloak_host(const char *inbuf, char *outbuf)
122{
123 char b26_alphabet[] = "abcdefghijklmnopqrstuvwxyz";
124 char *tptr;
799483e7 125 uint32_t accum = fnv_hash((const unsigned char*) inbuf, 32);
762cc38c 126
8109e8e3 127 rb_strlcpy(outbuf, inbuf, HOSTLEN + 1);
762cc38c 128
129 /* pass 1: scramble first section of hostname using base26
799483e7 130 * alphabet toasted against the FNV hash of the string.
762cc38c 131 *
132 * numbers are not changed at this time, only letters.
133 */
134 for (tptr = outbuf; *tptr != '\0'; tptr++)
212380e3 135 {
762cc38c 136 if (*tptr == '.')
137 break;
138
139 if (isdigit(*tptr) || *tptr == '-')
140 continue;
141
799483e7
JT
142 *tptr = b26_alphabet[(*tptr + accum) % 26];
143
144 /* Rotate one bit to avoid all digits being turned odd or even */
145 accum = (accum << 1) | (accum >> 31);
212380e3 146 }
762cc38c 147
148 /* pass 2: scramble each number in the address */
149 for (tptr = outbuf; *tptr != '\0'; tptr++)
150 {
151 if (isdigit(*tptr))
9b9c8484 152 *tptr = '0' + (*tptr + accum) % 10;
799483e7
JT
153
154 accum = (accum << 1) | (accum >> 31);
762cc38c 155 }
212380e3 156}
157
158static void
159check_umode_change(void *vdata)
160{
161 hook_data_umode_changed *data = (hook_data_umode_changed *)vdata;
162 struct Client *source_p = data->client;
163
164 if (!MyClient(source_p))
165 return;
166
167 /* didn't change +h umode, we don't need to do anything */
168 if (!((data->oldumodes ^ source_p->umodes) & user_modes['h']))
169 return;
170
171 if (source_p->umodes & user_modes['h'])
172 {
173 if (IsIPSpoof(source_p) || source_p->localClient->mangledhost == NULL || (IsDynSpoof(source_p) && strcmp(source_p->host, source_p->localClient->mangledhost)))
174 {
175 source_p->umodes &= ~user_modes['h'];
176 return;
177 }
178 if (strcmp(source_p->host, source_p->localClient->mangledhost))
179 {
8109e8e3 180 rb_strlcpy(source_p->host, source_p->localClient->mangledhost, HOSTLEN + 1);
212380e3 181 distribute_hostchange(source_p);
182 }
183 else /* not really nice, but we need to send this numeric here */
184 sendto_one_numeric(source_p, RPL_HOSTHIDDEN, "%s :is now your hidden host",
185 source_p->host);
186 }
187 else if (!(source_p->umodes & user_modes['h']))
188 {
189 if (source_p->localClient->mangledhost != NULL &&
190 !strcmp(source_p->host, source_p->localClient->mangledhost))
191 {
8109e8e3 192 rb_strlcpy(source_p->host, source_p->orighost, HOSTLEN + 1);
212380e3 193 distribute_hostchange(source_p);
194 }
195 }
196}
197
198static void
199check_new_user(void *vdata)
200{
201 struct Client *source_p = (void *)vdata;
202
203 if (IsIPSpoof(source_p))
204 {
205 source_p->umodes &= ~user_modes['h'];
206 return;
207 }
8109e8e3 208 source_p->localClient->mangledhost = rb_malloc(HOSTLEN + 1);
212380e3 209 if (!irccmp(source_p->orighost, source_p->sockhost))
762cc38c 210 do_host_cloak_ip(source_p->orighost, source_p->localClient->mangledhost);
212380e3 211 else
762cc38c 212 do_host_cloak_host(source_p->orighost, source_p->localClient->mangledhost);
212380e3 213 if (IsDynSpoof(source_p))
214 source_p->umodes &= ~user_modes['h'];
215 if (source_p->umodes & user_modes['h'])
216 {
907468c4 217 rb_strlcpy(source_p->host, source_p->localClient->mangledhost, sizeof(source_p->host));
212380e3 218 if (irccmp(source_p->host, source_p->orighost))
219 SetDynSpoof(source_p);
220 }
221}