]> jfr.im git - irc/rqf/shadowircd.git/blame - extensions/ip_cloaking.c
[svn] - forgot to do something
[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"
9#include "s_conf.h"
10#include "s_user.h"
11#include "s_serv.h"
12#include "tools.h"
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);
63 sendto_server(NULL, NULL,
64 NOCAPS, CAP_TS6, ":%s ENCAP * CHGHOST %s :%s",
65 me.name, client->name, client->host);
66 if (irccmp(client->host, client->orighost))
67 SetDynSpoof(client);
68 else
69 ClearDynSpoof(client);
70}
71
762cc38c 72#define Nval 0x8c3a48ac
73#define HOSTLEN 63
74#define INITDATA "98fwqefnoiqefv03f423t34gbv3vb89tg432t3b8" /* change this */
75
76static inline unsigned int
77get_string_entropy(const char *inbuf)
78{
79 unsigned int accum = 1;
80
81 while(*inbuf != '\0')
82 accum += *inbuf++;
83
84 return accum;
85}
86
87/* calls get_string_entropy() and toasts it against INITDATA */
88static inline unsigned int
89get_string_weighted_entropy(const char *inbuf)
90{
91 static int base_entropy = 0;
92 unsigned int accum = get_string_entropy(inbuf);
93
94 /* initialize the algorithm if it is not yet ready */
95 if (base_entropy == 0)
96 base_entropy = get_string_entropy(INITDATA);
97
98 return (Nval * accum) ^ base_entropy;
99}
100
212380e3 101static void
762cc38c 102do_host_cloak_ip(const char *inbuf, char *outbuf)
212380e3 103{
762cc38c 104 char *tptr;
105 unsigned int accum = get_string_weighted_entropy(inbuf);
106 char buf[HOSTLEN];
107
108 strncpy(buf, inbuf, HOSTLEN);
109 tptr = strrchr(buf, '.');
514235a7 110
111 if (tptr == NULL)
b076458c 112 {
113 strncpy(outbuf, inbuf, HOSTLEN);
514235a7 114 return;
b076458c 115 }
514235a7 116
762cc38c 117 *tptr++ = '\0';
212380e3 118
762cc38c 119 snprintf(outbuf, HOSTLEN, "%s.%x", buf, accum);
120}
121
122static void
123do_host_cloak_host(const char *inbuf, char *outbuf)
124{
125 char b26_alphabet[] = "abcdefghijklmnopqrstuvwxyz";
126 char *tptr;
127 unsigned int accum = get_string_weighted_entropy(inbuf);
128
129 strncpy(outbuf, inbuf, HOSTLEN);
130
131 /* pass 1: scramble first section of hostname using base26
132 * alphabet toasted against the weighted entropy of the string.
133 *
134 * numbers are not changed at this time, only letters.
135 */
136 for (tptr = outbuf; *tptr != '\0'; tptr++)
212380e3 137 {
762cc38c 138 if (*tptr == '.')
139 break;
140
141 if (isdigit(*tptr) || *tptr == '-')
142 continue;
143
144 *tptr = b26_alphabet[(*tptr * accum) % 26];
212380e3 145 }
762cc38c 146
147 /* pass 2: scramble each number in the address */
148 for (tptr = outbuf; *tptr != '\0'; tptr++)
149 {
150 if (isdigit(*tptr))
151 {
152 *tptr = 48 + ((*tptr * accum) % 10);
153 }
154 }
212380e3 155}
156
157static void
158check_umode_change(void *vdata)
159{
160 hook_data_umode_changed *data = (hook_data_umode_changed *)vdata;
161 struct Client *source_p = data->client;
162
163 if (!MyClient(source_p))
164 return;
165
166 /* didn't change +h umode, we don't need to do anything */
167 if (!((data->oldumodes ^ source_p->umodes) & user_modes['h']))
168 return;
169
170 if (source_p->umodes & user_modes['h'])
171 {
172 if (IsIPSpoof(source_p) || source_p->localClient->mangledhost == NULL || (IsDynSpoof(source_p) && strcmp(source_p->host, source_p->localClient->mangledhost)))
173 {
174 source_p->umodes &= ~user_modes['h'];
175 return;
176 }
177 if (strcmp(source_p->host, source_p->localClient->mangledhost))
178 {
179 strlcpy(source_p->host, source_p->localClient->mangledhost, HOSTLEN);
180 distribute_hostchange(source_p);
181 }
182 else /* not really nice, but we need to send this numeric here */
183 sendto_one_numeric(source_p, RPL_HOSTHIDDEN, "%s :is now your hidden host",
184 source_p->host);
185 }
186 else if (!(source_p->umodes & user_modes['h']))
187 {
188 if (source_p->localClient->mangledhost != NULL &&
189 !strcmp(source_p->host, source_p->localClient->mangledhost))
190 {
191 strlcpy(source_p->host, source_p->orighost, HOSTLEN);
192 distribute_hostchange(source_p);
193 }
194 }
195}
196
197static void
198check_new_user(void *vdata)
199{
200 struct Client *source_p = (void *)vdata;
201
202 if (IsIPSpoof(source_p))
203 {
204 source_p->umodes &= ~user_modes['h'];
205 return;
206 }
207 source_p->localClient->mangledhost = MyMalloc(HOSTLEN);
208 if (!irccmp(source_p->orighost, source_p->sockhost))
762cc38c 209 do_host_cloak_ip(source_p->orighost, source_p->localClient->mangledhost);
212380e3 210 else
762cc38c 211 do_host_cloak_host(source_p->orighost, source_p->localClient->mangledhost);
212380e3 212 if (IsDynSpoof(source_p))
213 source_p->umodes &= ~user_modes['h'];
214 if (source_p->umodes & user_modes['h'])
215 {
216 strlcpy(source_p->host, source_p->localClient->mangledhost, sizeof(source_p->host));
217 if (irccmp(source_p->host, source_p->orighost))
218 SetDynSpoof(source_p);
219 }
220}