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