]> jfr.im git - solanum.git/blob - extensions/ip_cloaking_3.0.c
chm_regmsg: don't duplicate nick in 415
[solanum.git] / extensions / ip_cloaking_3.0.c
1
2 #include "stdinc.h"
3 #include "modules.h"
4 #include "hook.h"
5 #include "client.h"
6 #include "ircd.h"
7 #include "send.h"
8 #include "s_conf.h"
9 #include "s_user.h"
10 #include "s_serv.h"
11 #include "numeric.h"
12
13 static const char ip_cloaking_desc[] = "The old IP cloaking mechanism version 3.0";
14
15 /* if you're modifying this module, you'll probably want to change this */
16 #define KEY 0x13748cfa
17
18 static 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
28 static 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
36 static void check_umode_change(void *data);
37 static void check_new_user(void *data);
38 mapi_hfn_list_av1 ip_cloaking_hfnlist[] = {
39 { "umode_changed", check_umode_change },
40 { "new_local_user", check_new_user },
41 { NULL, NULL }
42 };
43
44 DECLARE_MODULE_AV2(ip_cloaking, _modinit, _moddeinit, NULL, NULL,
45 ip_cloaking_hfnlist, NULL, NULL, ip_cloaking_desc);
46
47 static void
48 distribute_hostchange(struct Client *client_p, char *newhost)
49 {
50 if (newhost != client_p->orighost)
51 sendto_one_numeric(client_p, RPL_HOSTHIDDEN, "%s :is now your hidden host",
52 newhost);
53 else
54 sendto_one_numeric(client_p, RPL_HOSTHIDDEN, "%s :hostname reset",
55 newhost);
56
57 sendto_server(NULL, NULL,
58 CAP_EUID | CAP_TS6, NOCAPS, ":%s CHGHOST %s :%s",
59 use_id(&me), use_id(client_p), newhost);
60 sendto_server(NULL, NULL,
61 CAP_TS6, CAP_EUID, ":%s ENCAP * CHGHOST %s :%s",
62 use_id(&me), use_id(client_p), newhost);
63
64 change_nick_user_host(client_p, client_p->name, client_p->username, newhost, 0, "Changing host");
65
66 if (newhost != client_p->orighost)
67 SetDynSpoof(client_p);
68 else
69 ClearDynSpoof(client_p);
70 }
71
72 #define Nval 0x8c3a48ac
73 #define HOSTLEN 63
74 #define INITDATA "98fwqefnoiqefv03f423t34gbv3vb89tg432t3b8" /* change this */
75
76 static inline unsigned int
77 get_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 */
88 static inline unsigned int
89 get_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
101 static void
102 do_host_cloak_ip(const char *inbuf, char *outbuf)
103 {
104 char *tptr;
105 unsigned int accum = get_string_weighted_entropy(inbuf);
106 char buf[HOSTLEN + 1] = { 0 };
107 int ipv6 = 0;
108
109 strncpy(buf, inbuf, HOSTLEN);
110 tptr = strrchr(buf, '.');
111
112 if (tptr == NULL)
113 {
114 tptr = strrchr(buf, ':');
115 ipv6 = 1;
116 }
117
118 if (tptr == NULL)
119 {
120 strncpy(outbuf, inbuf, HOSTLEN);
121 return;
122 }
123
124 *tptr++ = '\0';
125
126 if(ipv6)
127 {
128 snprintf(outbuf, HOSTLEN, "%.60s:%x", buf, accum);
129 }
130 else
131 {
132 snprintf(outbuf, HOSTLEN, "%.60s.%x", buf, accum);
133 }
134 }
135
136 static void
137 do_host_cloak_host(const char *inbuf, char *outbuf)
138 {
139 char b26_alphabet[] = "abcdefghijklmnopqrstuvwxyz";
140 char *tptr;
141 unsigned int accum = get_string_weighted_entropy(inbuf);
142
143 strncpy(outbuf, inbuf, HOSTLEN);
144
145 /* pass 1: scramble first section of hostname using base26
146 * alphabet toasted against the weighted entropy of the string.
147 *
148 * numbers are not changed at this time, only letters.
149 */
150 for (tptr = outbuf; *tptr != '\0'; tptr++)
151 {
152 if (*tptr == '.')
153 break;
154
155 if (isdigit((unsigned char)*tptr) || *tptr == '-')
156 continue;
157
158 *tptr = b26_alphabet[(*tptr * accum) % 26];
159 }
160
161 /* pass 2: scramble each number in the address */
162 for (tptr = outbuf; *tptr != '\0'; tptr++)
163 {
164 if (isdigit((unsigned char)*tptr))
165 {
166 *tptr = 48 + ((*tptr * accum) % 10);
167 }
168 }
169 }
170
171 static void
172 check_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 {
193 distribute_hostchange(source_p, source_p->localClient->mangledhost);
194 }
195 else /* not really nice, but we need to send this numeric here */
196 sendto_one_numeric(source_p, RPL_HOSTHIDDEN, "%s :is now your hidden host",
197 source_p->host);
198 }
199 else if (!(source_p->umodes & user_modes['h']))
200 {
201 if (source_p->localClient->mangledhost != NULL &&
202 !strcmp(source_p->host, source_p->localClient->mangledhost))
203 {
204 distribute_hostchange(source_p, source_p->orighost);
205 }
206 }
207 }
208
209 static void
210 check_new_user(void *vdata)
211 {
212 struct Client *source_p = (void *)vdata;
213
214 if (IsIPSpoof(source_p))
215 {
216 source_p->umodes &= ~user_modes['h'];
217 return;
218 }
219 source_p->localClient->mangledhost = rb_malloc(HOSTLEN + 1);
220 if (!irccmp(source_p->orighost, source_p->sockhost))
221 do_host_cloak_ip(source_p->orighost, source_p->localClient->mangledhost);
222 else
223 do_host_cloak_host(source_p->orighost, source_p->localClient->mangledhost);
224 if (IsDynSpoof(source_p))
225 source_p->umodes &= ~user_modes['h'];
226 if (source_p->umodes & user_modes['h'])
227 {
228 rb_strlcpy(source_p->host, source_p->localClient->mangledhost, sizeof(source_p->host));
229 if (irccmp(source_p->host, source_p->orighost))
230 SetDynSpoof(source_p);
231 }
232 }