1 /* $Id: ip_cloaking.c 3526 2007-07-06 07:56:14Z nenolod $ */
15 /* if you're modifying this module, you'll probably to change this */
16 #define KEY 0x13748cfa
21 /* add the usermode to the available slot */
22 user_modes
['h'] = find_umode_slot();
31 /* disable the umode and remove it from the available list */
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
},
44 DECLARE_MODULE_AV1(ip_cloaking
, _modinit
, _moddeinit
, NULL
, NULL
,
45 ip_cloaking_hfnlist
, "$Revision: 3526 $");
48 distribute_hostchange(struct Client
*client
)
50 if (irccmp(client
->host
, client
->orighost
))
51 sendto_one_numeric(client
, RPL_HOSTHIDDEN
, "%s :is now your hidden host",
54 sendto_one_numeric(client
, RPL_HOSTHIDDEN
, "%s :hostname reset",
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
);
63 if (irccmp(client
->host
, client
->orighost
))
66 ClearDynSpoof(client
);
70 do_host_cloak_ip(const char *inbuf
, char *outbuf
)
72 /* None of the characters in this table can be valid in an IP */
73 char chartable
[] = "ghijklmnopqrstuvwxyz";
75 uint32_t accum
= fnv_hash((const unsigned char*) inbuf
, 32);
80 rb_strlcpy(outbuf
, inbuf
, HOSTLEN
+ 1);
82 if (strchr(outbuf
, ':'))
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.
91 * We have to do this ahead of time because doing this during
92 * the actual cloaking would get ugly
94 for (tptr
= outbuf
; *tptr
!= '\0'; tptr
++)
98 else if (!strchr(outbuf
, '.'))
101 for (tptr
= outbuf
; *tptr
!= '\0'; tptr
++)
103 if (*tptr
== ':' || *tptr
== '.')
109 if (ipv6
&& sepcount
< totalcount
/ 2)
112 if (!ipv6
&& sepcount
< 2)
115 *tptr
= chartable
[(*tptr
+ accum
) % 20];
116 accum
= (accum
<< 1) | (accum
>> 31);
121 do_host_cloak_host(const char *inbuf
, char *outbuf
)
123 char b26_alphabet
[] = "abcdefghijklmnopqrstuvwxyz";
125 uint32_t accum
= fnv_hash((const unsigned char*) inbuf
, 32);
127 rb_strlcpy(outbuf
, inbuf
, HOSTLEN
+ 1);
129 /* pass 1: scramble first section of hostname using base26
130 * alphabet toasted against the FNV hash of the string.
132 * numbers are not changed at this time, only letters.
134 for (tptr
= outbuf
; *tptr
!= '\0'; tptr
++)
139 if (isdigit(*tptr
) || *tptr
== '-')
142 *tptr
= b26_alphabet
[(*tptr
+ accum
) % 26];
144 /* Rotate one bit to avoid all digits being turned odd or even */
145 accum
= (accum
<< 1) | (accum
>> 31);
148 /* pass 2: scramble each number in the address */
149 for (tptr
= outbuf
; *tptr
!= '\0'; tptr
++)
152 *tptr
= '0' + (*tptr
+ accum
) % 10;
154 accum
= (accum
<< 1) | (accum
>> 31);
159 check_umode_change(void *vdata
)
161 hook_data_umode_changed
*data
= (hook_data_umode_changed
*)vdata
;
162 struct Client
*source_p
= data
->client
;
164 if (!MyClient(source_p
))
167 /* didn't change +h umode, we don't need to do anything */
168 if (!((data
->oldumodes
^ source_p
->umodes
) & user_modes
['h']))
171 if (source_p
->umodes
& user_modes
['h'])
173 if (IsIPSpoof(source_p
) || source_p
->localClient
->mangledhost
== NULL
|| (IsDynSpoof(source_p
) && strcmp(source_p
->host
, source_p
->localClient
->mangledhost
)))
175 source_p
->umodes
&= ~user_modes
['h'];
178 if (strcmp(source_p
->host
, source_p
->localClient
->mangledhost
))
180 rb_strlcpy(source_p
->host
, source_p
->localClient
->mangledhost
, HOSTLEN
+ 1);
181 distribute_hostchange(source_p
);
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",
187 else if (!(source_p
->umodes
& user_modes
['h']))
189 if (source_p
->localClient
->mangledhost
!= NULL
&&
190 !strcmp(source_p
->host
, source_p
->localClient
->mangledhost
))
192 rb_strlcpy(source_p
->host
, source_p
->orighost
, HOSTLEN
+ 1);
193 distribute_hostchange(source_p
);
199 check_new_user(void *vdata
)
201 struct Client
*source_p
= (void *)vdata
;
203 if (IsIPSpoof(source_p
))
205 source_p
->umodes
&= ~user_modes
['h'];
208 source_p
->localClient
->mangledhost
= rb_malloc(HOSTLEN
+ 1);
209 if (!irccmp(source_p
->orighost
, source_p
->sockhost
))
210 do_host_cloak_ip(source_p
->orighost
, source_p
->localClient
->mangledhost
);
212 do_host_cloak_host(source_p
->orighost
, source_p
->localClient
->mangledhost
);
213 if (IsDynSpoof(source_p
))
214 source_p
->umodes
&= ~user_modes
['h'];
215 if (source_p
->umodes
& user_modes
['h'])
217 rb_strlcpy(source_p
->host
, source_p
->localClient
->mangledhost
, sizeof(source_p
->host
));
218 if (irccmp(source_p
->host
, source_p
->orighost
))
219 SetDynSpoof(source_p
);