]>
Commit | Line | Data |
---|---|---|
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 | ||
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", (hookfn) check_umode_change }, | |
40 | { "new_local_user", (hookfn) check_new_user }, | |
41 | { NULL, NULL } | |
42 | }; | |
43 | ||
44 | DECLARE_MODULE_AV1(ip_cloaking, _modinit, _moddeinit, NULL, NULL, | |
b076458c | 45 | ip_cloaking_hfnlist, "$Revision: 3526 $"); |
212380e3 AC |
46 | |
47 | static void | |
48 | distribute_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 | ||
69 | static void | |
762cc38c | 70 | do_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++) | |
5067fe0e | 95 | if (*tptr == ':') |
3213b626 | 96 | totalcount++; |
3213b626 JT |
97 | } |
98 | else if (!strchr(outbuf, '.')) | |
514235a7 AC |
99 | return; |
100 | ||
3213b626 | 101 | for (tptr = outbuf; *tptr != '\0'; tptr++) |
b42eac75 | 102 | { |
3213b626 JT |
103 | if (*tptr == ':' || *tptr == '.') |
104 | { | |
105 | sepcount++; | |
106 | continue; | |
107 | } | |
108 | ||
d9439a0e JT |
109 | if (ipv6 && sepcount < totalcount / 2) |
110 | continue; | |
3213b626 | 111 | |
d9439a0e JT |
112 | if (!ipv6 && sepcount < 2) |
113 | continue; | |
3213b626 | 114 | |
d9439a0e | 115 | *tptr = chartable[(*tptr + accum) % 20]; |
3213b626 | 116 | accum = (accum << 1) | (accum >> 31); |
b42eac75 | 117 | } |
762cc38c AC |
118 | } |
119 | ||
120 | static void | |
121 | do_host_cloak_host(const char *inbuf, char *outbuf) | |
122 | { | |
123 | char b26_alphabet[] = "abcdefghijklmnopqrstuvwxyz"; | |
124 | char *tptr; | |
3213b626 | 125 | uint32_t accum = fnv_hash((const unsigned char*) inbuf, 32); |
762cc38c | 126 | |
e1c1f08d | 127 | rb_strlcpy(outbuf, inbuf, HOSTLEN + 1); |
762cc38c AC |
128 | |
129 | /* pass 1: scramble first section of hostname using base26 | |
3213b626 | 130 | * alphabet toasted against the FNV hash of the string. |
762cc38c AC |
131 | * |
132 | * numbers are not changed at this time, only letters. | |
133 | */ | |
134 | for (tptr = outbuf; *tptr != '\0'; tptr++) | |
212380e3 | 135 | { |
762cc38c AC |
136 | if (*tptr == '.') |
137 | break; | |
138 | ||
139 | if (isdigit(*tptr) || *tptr == '-') | |
140 | continue; | |
141 | ||
3213b626 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 AC |
147 | |
148 | /* pass 2: scramble each number in the address */ | |
149 | for (tptr = outbuf; *tptr != '\0'; tptr++) | |
150 | { | |
151 | if (isdigit(*tptr)) | |
9d99a309 | 152 | *tptr = '0' + (*tptr + accum) % 10; |
3213b626 JT |
153 | |
154 | accum = (accum << 1) | (accum >> 31); | |
762cc38c | 155 | } |
212380e3 AC |
156 | } |
157 | ||
158 | static void | |
159 | check_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 | { | |
e1c1f08d | 180 | rb_strlcpy(source_p->host, source_p->localClient->mangledhost, HOSTLEN + 1); |
212380e3 AC |
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 | { | |
e1c1f08d | 192 | rb_strlcpy(source_p->host, source_p->orighost, HOSTLEN + 1); |
212380e3 AC |
193 | distribute_hostchange(source_p); |
194 | } | |
195 | } | |
196 | } | |
197 | ||
198 | static void | |
199 | check_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 | } | |
e1c1f08d | 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 AC |
213 | if (IsDynSpoof(source_p)) |
214 | source_p->umodes &= ~user_modes['h']; | |
215 | if (source_p->umodes & user_modes['h']) | |
216 | { | |
f427c8b0 | 217 | rb_strlcpy(source_p->host, source_p->localClient->mangledhost, sizeof(source_p->host)); |
212380e3 AC |
218 | if (irccmp(source_p->host, source_p->orighost)) |
219 | SetDynSpoof(source_p); | |
220 | } | |
221 | } |