]> jfr.im git - solanum.git/blob - extensions/ip_cloaking_3.0.c
extensions/ip_cloaking*: Fix a bug accidentally introduced in 29d224a1 where the...
[solanum.git] / extensions / ip_cloaking_3.0.c
1 /* $Id: ip_cloaking.c 3526 2007-07-06 07:56:14Z nenolod $ */
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 "numeric.h"
13
14 /* if you're modifying this module, you'll probably to change this */
15 #define KEY 0x13748cfa
16
17 static int
18 _modinit(void)
19 {
20 /* add the usermode to the available slot */
21 user_modes['h'] = find_umode_slot();
22 construct_umodebuf();
23
24 return 0;
25 }
26
27 static void
28 _moddeinit(void)
29 {
30 /* disable the umode and remove it from the available list */
31 user_modes['h'] = 0;
32 construct_umodebuf();
33 }
34
35 static void check_umode_change(void *data);
36 static void check_new_user(void *data);
37 mapi_hfn_list_av1 ip_cloaking_hfnlist[] = {
38 { "umode_changed", (hookfn) check_umode_change },
39 { "new_local_user", (hookfn) check_new_user },
40 { NULL, NULL }
41 };
42
43 DECLARE_MODULE_AV1(ip_cloaking, _modinit, _moddeinit, NULL, NULL,
44 ip_cloaking_hfnlist, "$Revision: 3526 $");
45
46 static void
47 distribute_hostchange(struct Client *client_p, char *newhost)
48 {
49 if (newhost != client_p->orighost)
50 sendto_one_numeric(client_p, RPL_HOSTHIDDEN, "%s :is now your hidden host",
51 newhost);
52 else
53 sendto_one_numeric(client_p, RPL_HOSTHIDDEN, "%s :hostname reset",
54 newhost);
55
56 sendto_server(NULL, NULL,
57 CAP_EUID | CAP_TS6, NOCAPS, ":%s CHGHOST %s :%s",
58 use_id(&me), use_id(client_p), newhost);
59 sendto_server(NULL, NULL,
60 CAP_TS6, CAP_EUID, ":%s ENCAP * CHGHOST %s :%s",
61 use_id(&me), use_id(client_p), newhost);
62
63 change_nick_user_host(client_p, client_p->name, client_p->username, newhost, 0, "Changing host");
64
65 if (newhost != client_p->orighost)
66 SetDynSpoof(client_p);
67 else
68 ClearDynSpoof(client_p);
69 }
70
71 #define Nval 0x8c3a48ac
72 #define HOSTLEN 63
73 #define INITDATA "98fwqefnoiqefv03f423t34gbv3vb89tg432t3b8" /* change this */
74
75 static inline unsigned int
76 get_string_entropy(const char *inbuf)
77 {
78 unsigned int accum = 1;
79
80 while(*inbuf != '\0')
81 accum += *inbuf++;
82
83 return accum;
84 }
85
86 /* calls get_string_entropy() and toasts it against INITDATA */
87 static inline unsigned int
88 get_string_weighted_entropy(const char *inbuf)
89 {
90 static int base_entropy = 0;
91 unsigned int accum = get_string_entropy(inbuf);
92
93 /* initialize the algorithm if it is not yet ready */
94 if (base_entropy == 0)
95 base_entropy = get_string_entropy(INITDATA);
96
97 return (Nval * accum) ^ base_entropy;
98 }
99
100 static void
101 do_host_cloak_ip(const char *inbuf, char *outbuf)
102 {
103 char *tptr;
104 unsigned int accum = get_string_weighted_entropy(inbuf);
105 char buf[HOSTLEN];
106 int ipv6 = 0;
107
108 strncpy(buf, inbuf, HOSTLEN);
109 tptr = strrchr(buf, '.');
110
111 if (tptr == NULL)
112 {
113 tptr = strrchr(buf, ':');
114 ipv6 = 1;
115 }
116
117 if (tptr == NULL)
118 {
119 strncpy(outbuf, inbuf, HOSTLEN);
120 return;
121 }
122
123 *tptr++ = '\0';
124
125 if(ipv6)
126 {
127 rb_snprintf(outbuf, HOSTLEN, "%s:%x", buf, accum);
128 }
129 else
130 {
131 rb_snprintf(outbuf, HOSTLEN, "%s.%x", buf, accum);
132 }
133 }
134
135 static void
136 do_host_cloak_host(const char *inbuf, char *outbuf)
137 {
138 char b26_alphabet[] = "abcdefghijklmnopqrstuvwxyz";
139 char *tptr;
140 unsigned int accum = get_string_weighted_entropy(inbuf);
141
142 strncpy(outbuf, inbuf, HOSTLEN);
143
144 /* pass 1: scramble first section of hostname using base26
145 * alphabet toasted against the weighted entropy of the string.
146 *
147 * numbers are not changed at this time, only letters.
148 */
149 for (tptr = outbuf; *tptr != '\0'; tptr++)
150 {
151 if (*tptr == '.')
152 break;
153
154 if (isdigit(*tptr) || *tptr == '-')
155 continue;
156
157 *tptr = b26_alphabet[(*tptr * accum) % 26];
158 }
159
160 /* pass 2: scramble each number in the address */
161 for (tptr = outbuf; *tptr != '\0'; tptr++)
162 {
163 if (isdigit(*tptr))
164 {
165 *tptr = 48 + ((*tptr * accum) % 10);
166 }
167 }
168 }
169
170 static void
171 check_umode_change(void *vdata)
172 {
173 hook_data_umode_changed *data = (hook_data_umode_changed *)vdata;
174 struct Client *source_p = data->client;
175
176 if (!MyClient(source_p))
177 return;
178
179 /* didn't change +h umode, we don't need to do anything */
180 if (!((data->oldumodes ^ source_p->umodes) & user_modes['h']))
181 return;
182
183 if (source_p->umodes & user_modes['h'])
184 {
185 if (IsIPSpoof(source_p) || source_p->localClient->mangledhost == NULL || (IsDynSpoof(source_p) && strcmp(source_p->host, source_p->localClient->mangledhost)))
186 {
187 source_p->umodes &= ~user_modes['h'];
188 return;
189 }
190 if (strcmp(source_p->host, source_p->localClient->mangledhost))
191 {
192 distribute_hostchange(source_p, source_p->localClient->mangledhost);
193 }
194 else /* not really nice, but we need to send this numeric here */
195 sendto_one_numeric(source_p, RPL_HOSTHIDDEN, "%s :is now your hidden host",
196 source_p->host);
197 }
198 else if (!(source_p->umodes & user_modes['h']))
199 {
200 if (source_p->localClient->mangledhost != NULL &&
201 !strcmp(source_p->host, source_p->localClient->mangledhost))
202 {
203 distribute_hostchange(source_p, source_p->orighost);
204 }
205 }
206 }
207
208 static void
209 check_new_user(void *vdata)
210 {
211 struct Client *source_p = (void *)vdata;
212
213 if (IsIPSpoof(source_p))
214 {
215 source_p->umodes &= ~user_modes['h'];
216 return;
217 }
218 source_p->localClient->mangledhost = rb_malloc(HOSTLEN);
219 if (!irccmp(source_p->orighost, source_p->sockhost))
220 do_host_cloak_ip(source_p->orighost, source_p->localClient->mangledhost);
221 else
222 do_host_cloak_host(source_p->orighost, source_p->localClient->mangledhost);
223 if (IsDynSpoof(source_p))
224 source_p->umodes &= ~user_modes['h'];
225 if (source_p->umodes & user_modes['h'])
226 {
227 rb_strlcpy(source_p->host, source_p->localClient->mangledhost, sizeof(source_p->host));
228 if (irccmp(source_p->host, source_p->orighost))
229 SetDynSpoof(source_p);
230 }
231 }