]>
Commit | Line | Data |
---|---|---|
55abcbb2 | 1 | /* |
89bfeb8f EM |
2 | * Charybdis: an advanced ircd |
3 | * ip_cloaking.c: provide user hostname cloaking | |
4 | * | |
5 | * Written originally by nenolod, altered to use FNV by Elizabeth in 2008 | |
6 | */ | |
212380e3 AC |
7 | |
8 | #include "stdinc.h" | |
9 | #include "modules.h" | |
10 | #include "hook.h" | |
11 | #include "client.h" | |
12 | #include "ircd.h" | |
13 | #include "send.h" | |
3213b626 | 14 | #include "hash.h" |
212380e3 AC |
15 | #include "s_conf.h" |
16 | #include "s_user.h" | |
17 | #include "s_serv.h" | |
212380e3 AC |
18 | #include "numeric.h" |
19 | ||
212380e3 AC |
20 | static int |
21 | _modinit(void) | |
22 | { | |
23 | /* add the usermode to the available slot */ | |
24 | user_modes['h'] = find_umode_slot(); | |
25 | construct_umodebuf(); | |
26 | ||
27 | return 0; | |
28 | } | |
29 | ||
30 | static void | |
31 | _moddeinit(void) | |
32 | { | |
33 | /* disable the umode and remove it from the available list */ | |
34 | user_modes['h'] = 0; | |
35 | construct_umodebuf(); | |
36 | } | |
37 | ||
38 | static void check_umode_change(void *data); | |
39 | static void check_new_user(void *data); | |
40 | mapi_hfn_list_av1 ip_cloaking_hfnlist[] = { | |
41 | { "umode_changed", (hookfn) check_umode_change }, | |
42 | { "new_local_user", (hookfn) check_new_user }, | |
43 | { NULL, NULL } | |
44 | }; | |
45 | ||
46 | DECLARE_MODULE_AV1(ip_cloaking, _modinit, _moddeinit, NULL, NULL, | |
b076458c | 47 | ip_cloaking_hfnlist, "$Revision: 3526 $"); |
212380e3 AC |
48 | |
49 | static void | |
29d224a1 | 50 | distribute_hostchange(struct Client *client_p, char *newhost) |
212380e3 | 51 | { |
29d224a1 KB |
52 | if (newhost != client_p->orighost) |
53 | sendto_one_numeric(client_p, RPL_HOSTHIDDEN, "%s :is now your hidden host", | |
9f409b63 | 54 | newhost); |
212380e3 | 55 | else |
29d224a1 | 56 | sendto_one_numeric(client_p, RPL_HOSTHIDDEN, "%s :hostname reset", |
9f409b63 | 57 | newhost); |
212380e3 AC |
58 | |
59 | sendto_server(NULL, NULL, | |
60 | CAP_EUID | CAP_TS6, NOCAPS, ":%s CHGHOST %s :%s", | |
9f409b63 | 61 | use_id(&me), use_id(client_p), newhost); |
212380e3 AC |
62 | sendto_server(NULL, NULL, |
63 | CAP_TS6, CAP_EUID, ":%s ENCAP * CHGHOST %s :%s", | |
9f409b63 | 64 | use_id(&me), use_id(client_p), newhost); |
29d224a1 KB |
65 | |
66 | change_nick_user_host(client_p, client_p->name, client_p->username, newhost, 0, "Changing host"); | |
67 | ||
68 | if (newhost != client_p->orighost) | |
69 | SetDynSpoof(client_p); | |
212380e3 | 70 | else |
29d224a1 | 71 | ClearDynSpoof(client_p); |
212380e3 AC |
72 | } |
73 | ||
74 | static void | |
762cc38c | 75 | do_host_cloak_ip(const char *inbuf, char *outbuf) |
212380e3 | 76 | { |
3213b626 JT |
77 | /* None of the characters in this table can be valid in an IP */ |
78 | char chartable[] = "ghijklmnopqrstuvwxyz"; | |
762cc38c | 79 | char *tptr; |
3213b626 JT |
80 | uint32_t accum = fnv_hash((const unsigned char*) inbuf, 32); |
81 | int sepcount = 0; | |
82 | int totalcount = 0; | |
b42eac75 | 83 | int ipv6 = 0; |
762cc38c | 84 | |
e1c1f08d | 85 | rb_strlcpy(outbuf, inbuf, HOSTLEN + 1); |
514235a7 | 86 | |
3213b626 | 87 | if (strchr(outbuf, ':')) |
b42eac75 | 88 | { |
b42eac75 | 89 | ipv6 = 1; |
b42eac75 | 90 | |
55abcbb2 | 91 | /* Damn you IPv6... |
3213b626 JT |
92 | * We count the number of colons so we can calculate how much |
93 | * of the host to cloak. This is because some hostmasks may not | |
94 | * have as many octets as we'd like. | |
95 | * | |
96 | * We have to do this ahead of time because doing this during | |
97 | * the actual cloaking would get ugly | |
98 | */ | |
99 | for (tptr = outbuf; *tptr != '\0'; tptr++) | |
5067fe0e | 100 | if (*tptr == ':') |
3213b626 | 101 | totalcount++; |
3213b626 JT |
102 | } |
103 | else if (!strchr(outbuf, '.')) | |
514235a7 AC |
104 | return; |
105 | ||
55abcbb2 | 106 | for (tptr = outbuf; *tptr != '\0'; tptr++) |
b42eac75 | 107 | { |
3213b626 JT |
108 | if (*tptr == ':' || *tptr == '.') |
109 | { | |
110 | sepcount++; | |
111 | continue; | |
112 | } | |
113 | ||
d9439a0e JT |
114 | if (ipv6 && sepcount < totalcount / 2) |
115 | continue; | |
3213b626 | 116 | |
d9439a0e JT |
117 | if (!ipv6 && sepcount < 2) |
118 | continue; | |
3213b626 | 119 | |
d9439a0e | 120 | *tptr = chartable[(*tptr + accum) % 20]; |
3213b626 | 121 | accum = (accum << 1) | (accum >> 31); |
b42eac75 | 122 | } |
762cc38c AC |
123 | } |
124 | ||
125 | static void | |
126 | do_host_cloak_host(const char *inbuf, char *outbuf) | |
127 | { | |
128 | char b26_alphabet[] = "abcdefghijklmnopqrstuvwxyz"; | |
129 | char *tptr; | |
3213b626 | 130 | uint32_t accum = fnv_hash((const unsigned char*) inbuf, 32); |
762cc38c | 131 | |
e1c1f08d | 132 | rb_strlcpy(outbuf, inbuf, HOSTLEN + 1); |
762cc38c | 133 | |
55abcbb2 | 134 | /* pass 1: scramble first section of hostname using base26 |
3213b626 | 135 | * alphabet toasted against the FNV hash of the string. |
762cc38c AC |
136 | * |
137 | * numbers are not changed at this time, only letters. | |
138 | */ | |
139 | for (tptr = outbuf; *tptr != '\0'; tptr++) | |
212380e3 | 140 | { |
762cc38c AC |
141 | if (*tptr == '.') |
142 | break; | |
143 | ||
29c92cf9 | 144 | if (isdigit((unsigned char)*tptr) || *tptr == '-') |
762cc38c AC |
145 | continue; |
146 | ||
3213b626 JT |
147 | *tptr = b26_alphabet[(*tptr + accum) % 26]; |
148 | ||
149 | /* Rotate one bit to avoid all digits being turned odd or even */ | |
150 | accum = (accum << 1) | (accum >> 31); | |
212380e3 | 151 | } |
762cc38c AC |
152 | |
153 | /* pass 2: scramble each number in the address */ | |
154 | for (tptr = outbuf; *tptr != '\0'; tptr++) | |
155 | { | |
29c92cf9 | 156 | if (isdigit((unsigned char)*tptr)) |
9d99a309 | 157 | *tptr = '0' + (*tptr + accum) % 10; |
3213b626 JT |
158 | |
159 | accum = (accum << 1) | (accum >> 31); | |
55abcbb2 | 160 | } |
212380e3 AC |
161 | } |
162 | ||
163 | static void | |
164 | check_umode_change(void *vdata) | |
165 | { | |
166 | hook_data_umode_changed *data = (hook_data_umode_changed *)vdata; | |
167 | struct Client *source_p = data->client; | |
168 | ||
169 | if (!MyClient(source_p)) | |
170 | return; | |
171 | ||
172 | /* didn't change +h umode, we don't need to do anything */ | |
173 | if (!((data->oldumodes ^ source_p->umodes) & user_modes['h'])) | |
174 | return; | |
175 | ||
176 | if (source_p->umodes & user_modes['h']) | |
177 | { | |
178 | if (IsIPSpoof(source_p) || source_p->localClient->mangledhost == NULL || (IsDynSpoof(source_p) && strcmp(source_p->host, source_p->localClient->mangledhost))) | |
179 | { | |
180 | source_p->umodes &= ~user_modes['h']; | |
181 | return; | |
182 | } | |
183 | if (strcmp(source_p->host, source_p->localClient->mangledhost)) | |
184 | { | |
29d224a1 | 185 | distribute_hostchange(source_p, source_p->localClient->mangledhost); |
212380e3 AC |
186 | } |
187 | else /* not really nice, but we need to send this numeric here */ | |
188 | sendto_one_numeric(source_p, RPL_HOSTHIDDEN, "%s :is now your hidden host", | |
189 | source_p->host); | |
190 | } | |
191 | else if (!(source_p->umodes & user_modes['h'])) | |
192 | { | |
193 | if (source_p->localClient->mangledhost != NULL && | |
194 | !strcmp(source_p->host, source_p->localClient->mangledhost)) | |
195 | { | |
29d224a1 | 196 | distribute_hostchange(source_p, source_p->orighost); |
212380e3 AC |
197 | } |
198 | } | |
199 | } | |
200 | ||
201 | static void | |
202 | check_new_user(void *vdata) | |
203 | { | |
204 | struct Client *source_p = (void *)vdata; | |
205 | ||
206 | if (IsIPSpoof(source_p)) | |
207 | { | |
208 | source_p->umodes &= ~user_modes['h']; | |
209 | return; | |
210 | } | |
e1c1f08d | 211 | source_p->localClient->mangledhost = rb_malloc(HOSTLEN + 1); |
212380e3 | 212 | if (!irccmp(source_p->orighost, source_p->sockhost)) |
762cc38c | 213 | do_host_cloak_ip(source_p->orighost, source_p->localClient->mangledhost); |
212380e3 | 214 | else |
762cc38c | 215 | do_host_cloak_host(source_p->orighost, source_p->localClient->mangledhost); |
212380e3 AC |
216 | if (IsDynSpoof(source_p)) |
217 | source_p->umodes &= ~user_modes['h']; | |
218 | if (source_p->umodes & user_modes['h']) | |
219 | { | |
f427c8b0 | 220 | rb_strlcpy(source_p->host, source_p->localClient->mangledhost, sizeof(source_p->host)); |
212380e3 AC |
221 | if (irccmp(source_p->host, source_p->orighost)) |
222 | SetDynSpoof(source_p); | |
223 | } | |
224 | } |