1 /* $Id: ip_cloaking.c 3526 2007-07-06 07:56:14Z nenolod $ */
14 /* if you're modifying this module, you'll probably to change this */
15 #define KEY 0x13748cfa
20 /* add the usermode to the available slot */
21 user_modes
['h'] = find_umode_slot();
30 /* disable the umode and remove it from the available list */
35 static void check_umode_change(void *data
);
36 static void check_new_user(void *data
);
37 mapi_hfn_list_av1 ip_cloaking_hfnlist
[] = {
38 { "umode_changed", (hookfn
) check_umode_change
},
39 { "new_local_user", (hookfn
) check_new_user
},
43 DECLARE_MODULE_AV1(ip_cloaking
, _modinit
, _moddeinit
, NULL
, NULL
,
44 ip_cloaking_hfnlist
, "$Revision: 3526 $");
47 distribute_hostchange(struct Client
*client
)
49 if (irccmp(client
->host
, client
->orighost
))
50 sendto_one_numeric(client
, RPL_HOSTHIDDEN
, "%s :is now your hidden host",
53 sendto_one_numeric(client
, RPL_HOSTHIDDEN
, "%s :hostname reset",
56 sendto_server(NULL
, NULL
,
57 CAP_EUID
| CAP_TS6
, NOCAPS
, ":%s CHGHOST %s :%s",
58 use_id(&me
), use_id(client
), client
->host
);
59 sendto_server(NULL
, NULL
,
60 CAP_TS6
, CAP_EUID
, ":%s ENCAP * CHGHOST %s :%s",
61 use_id(&me
), use_id(client
), client
->host
);
62 sendto_server(NULL
, NULL
,
63 NOCAPS
, CAP_TS6
, ":%s ENCAP * CHGHOST %s :%s",
64 me
.name
, client
->name
, client
->host
);
65 if (irccmp(client
->host
, client
->orighost
))
68 ClearDynSpoof(client
);
71 #define Nval 0x8c3a48ac
73 #define INITDATA "98fwqefnoiqefv03f423t34gbv3vb89tg432t3b8" /* change this */
75 static inline unsigned int
76 get_string_entropy(const char *inbuf
)
78 unsigned int accum
= 1;
86 /* calls get_string_entropy() and toasts it against INITDATA */
87 static inline unsigned int
88 get_string_weighted_entropy(const char *inbuf
)
90 static int base_entropy
= 0;
91 unsigned int accum
= get_string_entropy(inbuf
);
93 /* initialize the algorithm if it is not yet ready */
94 if (base_entropy
== 0)
95 base_entropy
= get_string_entropy(INITDATA
);
97 return (Nval
* accum
) ^ base_entropy
;
101 do_host_cloak_ip(const char *inbuf
, char *outbuf
)
104 unsigned int accum
= get_string_weighted_entropy(inbuf
);
107 strncpy(buf
, inbuf
, HOSTLEN
);
108 tptr
= strrchr(buf
, '.');
112 strncpy(outbuf
, inbuf
, HOSTLEN
);
118 snprintf(outbuf
, HOSTLEN
, "%s.%x", buf
, accum
);
122 do_host_cloak_host(const char *inbuf
, char *outbuf
)
124 char b26_alphabet
[] = "abcdefghijklmnopqrstuvwxyz";
126 unsigned int accum
= get_string_weighted_entropy(inbuf
);
128 strncpy(outbuf
, inbuf
, HOSTLEN
);
130 /* pass 1: scramble first section of hostname using base26
131 * alphabet toasted against the weighted entropy of the string.
133 * numbers are not changed at this time, only letters.
135 for (tptr
= outbuf
; *tptr
!= '\0'; tptr
++)
140 if (isdigit(*tptr
) || *tptr
== '-')
143 *tptr
= b26_alphabet
[(*tptr
* accum
) % 26];
146 /* pass 2: scramble each number in the address */
147 for (tptr
= outbuf
; *tptr
!= '\0'; tptr
++)
151 *tptr
= 48 + ((*tptr
* accum
) % 10);
157 check_umode_change(void *vdata
)
159 hook_data_umode_changed
*data
= (hook_data_umode_changed
*)vdata
;
160 struct Client
*source_p
= data
->client
;
162 if (!MyClient(source_p
))
165 /* didn't change +h umode, we don't need to do anything */
166 if (!((data
->oldumodes
^ source_p
->umodes
) & user_modes
['h']))
169 if (source_p
->umodes
& user_modes
['h'])
171 if (IsIPSpoof(source_p
) || source_p
->localClient
->mangledhost
== NULL
|| (IsDynSpoof(source_p
) && strcmp(source_p
->host
, source_p
->localClient
->mangledhost
)))
173 source_p
->umodes
&= ~user_modes
['h'];
176 if (strcmp(source_p
->host
, source_p
->localClient
->mangledhost
))
178 rb_strlcpy(source_p
->host
, source_p
->localClient
->mangledhost
, HOSTLEN
);
179 distribute_hostchange(source_p
);
181 else /* not really nice, but we need to send this numeric here */
182 sendto_one_numeric(source_p
, RPL_HOSTHIDDEN
, "%s :is now your hidden host",
185 else if (!(source_p
->umodes
& user_modes
['h']))
187 if (source_p
->localClient
->mangledhost
!= NULL
&&
188 !strcmp(source_p
->host
, source_p
->localClient
->mangledhost
))
190 rb_strlcpy(source_p
->host
, source_p
->orighost
, HOSTLEN
);
191 distribute_hostchange(source_p
);
197 check_new_user(void *vdata
)
199 struct Client
*source_p
= (void *)vdata
;
201 if (IsIPSpoof(source_p
))
203 source_p
->umodes
&= ~user_modes
['h'];
206 source_p
->localClient
->mangledhost
= rb_malloc(HOSTLEN
);
207 if (!irccmp(source_p
->orighost
, source_p
->sockhost
))
208 do_host_cloak_ip(source_p
->orighost
, source_p
->localClient
->mangledhost
);
210 do_host_cloak_host(source_p
->orighost
, source_p
->localClient
->mangledhost
);
211 if (IsDynSpoof(source_p
))
212 source_p
->umodes
&= ~user_modes
['h'];
213 if (source_p
->umodes
& user_modes
['h'])
215 rb_strlcpy(source_p
->host
, source_p
->localClient
->mangledhost
, sizeof(source_p
->host
));
216 if (irccmp(source_p
->host
, source_p
->orighost
))
217 SetDynSpoof(source_p
);