2 * Charybdis: an advanced ircd
3 * ip_cloaking.c: provide user hostname cloaking
5 * Written originally by nenolod, altered to use FNV by Elizabeth in 2008
23 /* add the usermode to the available slot */
24 user_modes
['h'] = find_umode_slot();
33 /* disable the umode and remove it from the available list */
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
},
46 DECLARE_MODULE_AV1(ip_cloaking
, _modinit
, _moddeinit
, NULL
, NULL
,
47 ip_cloaking_hfnlist
, "$Revision: 3526 $");
50 distribute_hostchange(struct Client
*client
)
52 if (irccmp(client
->host
, client
->orighost
))
53 sendto_one_numeric(client
, RPL_HOSTHIDDEN
, "%s :is now your hidden host",
56 sendto_one_numeric(client
, RPL_HOSTHIDDEN
, "%s :hostname reset",
59 sendto_server(NULL
, NULL
,
60 CAP_EUID
| CAP_TS6
, NOCAPS
, ":%s CHGHOST %s :%s",
61 use_id(&me
), use_id(client
), client
->host
);
62 sendto_server(NULL
, NULL
,
63 CAP_TS6
, CAP_EUID
, ":%s ENCAP * CHGHOST %s :%s",
64 use_id(&me
), use_id(client
), client
->host
);
65 if (irccmp(client
->host
, client
->orighost
))
68 ClearDynSpoof(client
);
72 do_host_cloak_ip(const char *inbuf
, char *outbuf
)
74 /* None of the characters in this table can be valid in an IP */
75 char chartable
[] = "ghijklmnopqrstuvwxyz";
77 uint32_t accum
= fnv_hash((const unsigned char*) inbuf
, 32);
82 rb_strlcpy(outbuf
, inbuf
, HOSTLEN
+ 1);
84 if (strchr(outbuf
, ':'))
89 * We count the number of colons so we can calculate how much
90 * of the host to cloak. This is because some hostmasks may not
91 * have as many octets as we'd like.
93 * We have to do this ahead of time because doing this during
94 * the actual cloaking would get ugly
96 for (tptr
= outbuf
; *tptr
!= '\0'; tptr
++)
100 else if (!strchr(outbuf
, '.'))
103 for (tptr
= outbuf
; *tptr
!= '\0'; tptr
++)
105 if (*tptr
== ':' || *tptr
== '.')
111 if (ipv6
&& sepcount
< totalcount
/ 2)
114 if (!ipv6
&& sepcount
< 2)
117 *tptr
= chartable
[(*tptr
+ accum
) % 20];
118 accum
= (accum
<< 1) | (accum
>> 31);
123 do_host_cloak_host(const char *inbuf
, char *outbuf
)
125 char b26_alphabet
[] = "abcdefghijklmnopqrstuvwxyz";
127 uint32_t accum
= fnv_hash((const unsigned char*) inbuf
, 32);
129 rb_strlcpy(outbuf
, inbuf
, HOSTLEN
+ 1);
131 /* pass 1: scramble first section of hostname using base26
132 * alphabet toasted against the FNV hash of the string.
134 * numbers are not changed at this time, only letters.
136 for (tptr
= outbuf
; *tptr
!= '\0'; tptr
++)
141 if (isdigit(*tptr
) || *tptr
== '-')
144 *tptr
= b26_alphabet
[(*tptr
+ accum
) % 26];
146 /* Rotate one bit to avoid all digits being turned odd or even */
147 accum
= (accum
<< 1) | (accum
>> 31);
150 /* pass 2: scramble each number in the address */
151 for (tptr
= outbuf
; *tptr
!= '\0'; tptr
++)
154 *tptr
= '0' + (*tptr
+ accum
) % 10;
156 accum
= (accum
<< 1) | (accum
>> 31);
161 check_umode_change(void *vdata
)
163 hook_data_umode_changed
*data
= (hook_data_umode_changed
*)vdata
;
164 struct Client
*source_p
= data
->client
;
166 if (!MyClient(source_p
))
169 /* didn't change +h umode, we don't need to do anything */
170 if (!((data
->oldumodes
^ source_p
->umodes
) & user_modes
['h']))
173 if (source_p
->umodes
& user_modes
['h'])
175 if (IsIPSpoof(source_p
) || source_p
->localClient
->mangledhost
== NULL
|| (IsDynSpoof(source_p
) && strcmp(source_p
->host
, source_p
->localClient
->mangledhost
)))
177 source_p
->umodes
&= ~user_modes
['h'];
180 if (strcmp(source_p
->host
, source_p
->localClient
->mangledhost
))
182 rb_strlcpy(source_p
->host
, source_p
->localClient
->mangledhost
, HOSTLEN
+ 1);
183 distribute_hostchange(source_p
);
185 else /* not really nice, but we need to send this numeric here */
186 sendto_one_numeric(source_p
, RPL_HOSTHIDDEN
, "%s :is now your hidden host",
189 else if (!(source_p
->umodes
& user_modes
['h']))
191 if (source_p
->localClient
->mangledhost
!= NULL
&&
192 !strcmp(source_p
->host
, source_p
->localClient
->mangledhost
))
194 rb_strlcpy(source_p
->host
, source_p
->orighost
, HOSTLEN
+ 1);
195 distribute_hostchange(source_p
);
201 check_new_user(void *vdata
)
203 struct Client
*source_p
= (void *)vdata
;
205 if (IsIPSpoof(source_p
))
207 source_p
->umodes
&= ~user_modes
['h'];
210 source_p
->localClient
->mangledhost
= rb_malloc(HOSTLEN
+ 1);
211 if (!irccmp(source_p
->orighost
, source_p
->sockhost
))
212 do_host_cloak_ip(source_p
->orighost
, source_p
->localClient
->mangledhost
);
214 do_host_cloak_host(source_p
->orighost
, source_p
->localClient
->mangledhost
);
215 if (IsDynSpoof(source_p
))
216 source_p
->umodes
&= ~user_modes
['h'];
217 if (source_p
->umodes
& user_modes
['h'])
219 rb_strlcpy(source_p
->host
, source_p
->localClient
->mangledhost
, sizeof(source_p
->host
));
220 if (irccmp(source_p
->host
, source_p
->orighost
))
221 SetDynSpoof(source_p
);