]>
Commit | Line | Data |
---|---|---|
b076458c | 1 | /* $Id: ip_cloaking.c 3526 2007-07-06 07:56:14Z nenolod $ */ |
212380e3 | 2 | |
3 | #include "stdinc.h" | |
4 | #include "modules.h" | |
5 | #include "hook.h" | |
6 | #include "client.h" | |
7 | #include "ircd.h" | |
8 | #include "send.h" | |
9 | #include "s_conf.h" | |
10 | #include "s_user.h" | |
11 | #include "s_serv.h" | |
212380e3 | 12 | #include "numeric.h" |
13 | ||
14 | /* if you're modifying this module, you'll probably to change this */ | |
15 | #define KEY 0x13748cfa | |
16 | ||
17 | static int | |
18 | _modinit(void) | |
19 | { | |
20 | /* add the usermode to the available slot */ | |
21 | user_modes['h'] = find_umode_slot(); | |
22 | construct_umodebuf(); | |
23 | ||
24 | return 0; | |
25 | } | |
26 | ||
27 | static void | |
28 | _moddeinit(void) | |
29 | { | |
30 | /* disable the umode and remove it from the available list */ | |
31 | user_modes['h'] = 0; | |
32 | construct_umodebuf(); | |
33 | } | |
34 | ||
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 }, | |
40 | { NULL, NULL } | |
41 | }; | |
42 | ||
43 | DECLARE_MODULE_AV1(ip_cloaking, _modinit, _moddeinit, NULL, NULL, | |
b076458c | 44 | ip_cloaking_hfnlist, "$Revision: 3526 $"); |
212380e3 | 45 | |
46 | static void | |
47 | distribute_hostchange(struct Client *client) | |
48 | { | |
49 | if (irccmp(client->host, client->orighost)) | |
50 | sendto_one_numeric(client, RPL_HOSTHIDDEN, "%s :is now your hidden host", | |
51 | client->host); | |
52 | else | |
53 | sendto_one_numeric(client, RPL_HOSTHIDDEN, "%s :hostname reset", | |
54 | client->host); | |
55 | ||
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); | |
212380e3 | 62 | if (irccmp(client->host, client->orighost)) |
63 | SetDynSpoof(client); | |
64 | else | |
65 | ClearDynSpoof(client); | |
66 | } | |
67 | ||
762cc38c | 68 | #define HOSTLEN 63 |
762cc38c | 69 | |
212380e3 | 70 | static void |
762cc38c | 71 | do_host_cloak_ip(const char *inbuf, char *outbuf) |
212380e3 | 72 | { |
762cc38c | 73 | char *tptr; |
18395f4f | 74 | unsigned int accum = fnv_hash(inbuf, 32); |
762cc38c | 75 | char buf[HOSTLEN]; |
3f46cd00 | 76 | int ipv6 = 0; |
762cc38c | 77 | |
78 | strncpy(buf, inbuf, HOSTLEN); | |
79 | tptr = strrchr(buf, '.'); | |
514235a7 | 80 | |
3f46cd00 VY |
81 | if (tptr == NULL) |
82 | { | |
83 | tptr = strrchr(buf, ':'); | |
84 | ipv6 = 1; | |
85 | } | |
86 | ||
514235a7 | 87 | if (tptr == NULL) |
b076458c | 88 | { |
89 | strncpy(outbuf, inbuf, HOSTLEN); | |
514235a7 | 90 | return; |
b076458c | 91 | } |
514235a7 | 92 | |
762cc38c | 93 | *tptr++ = '\0'; |
212380e3 | 94 | |
3f46cd00 VY |
95 | if(ipv6) |
96 | { | |
5b0a5279 | 97 | rb_snprintf(outbuf, HOSTLEN, "%s:%x", buf, accum); |
3f46cd00 VY |
98 | } |
99 | else | |
100 | { | |
5b0a5279 | 101 | rb_snprintf(outbuf, HOSTLEN, "%s.%x", buf, accum); |
3f46cd00 | 102 | } |
762cc38c | 103 | } |
104 | ||
105 | static void | |
106 | do_host_cloak_host(const char *inbuf, char *outbuf) | |
107 | { | |
108 | char b26_alphabet[] = "abcdefghijklmnopqrstuvwxyz"; | |
109 | char *tptr; | |
18395f4f | 110 | unsigned int accum = fnv_hash(inbuf, 32); |
762cc38c | 111 | |
112 | strncpy(outbuf, inbuf, HOSTLEN); | |
113 | ||
114 | /* pass 1: scramble first section of hostname using base26 | |
115 | * alphabet toasted against the weighted entropy of the string. | |
116 | * | |
117 | * numbers are not changed at this time, only letters. | |
118 | */ | |
119 | for (tptr = outbuf; *tptr != '\0'; tptr++) | |
212380e3 | 120 | { |
762cc38c | 121 | if (*tptr == '.') |
122 | break; | |
123 | ||
124 | if (isdigit(*tptr) || *tptr == '-') | |
125 | continue; | |
126 | ||
127 | *tptr = b26_alphabet[(*tptr * accum) % 26]; | |
212380e3 | 128 | } |
762cc38c | 129 | |
130 | /* pass 2: scramble each number in the address */ | |
131 | for (tptr = outbuf; *tptr != '\0'; tptr++) | |
132 | { | |
133 | if (isdigit(*tptr)) | |
134 | { | |
135 | *tptr = 48 + ((*tptr * accum) % 10); | |
136 | } | |
137 | } | |
212380e3 | 138 | } |
139 | ||
140 | static void | |
141 | check_umode_change(void *vdata) | |
142 | { | |
143 | hook_data_umode_changed *data = (hook_data_umode_changed *)vdata; | |
144 | struct Client *source_p = data->client; | |
145 | ||
146 | if (!MyClient(source_p)) | |
147 | return; | |
148 | ||
149 | /* didn't change +h umode, we don't need to do anything */ | |
150 | if (!((data->oldumodes ^ source_p->umodes) & user_modes['h'])) | |
151 | return; | |
152 | ||
153 | if (source_p->umodes & user_modes['h']) | |
154 | { | |
155 | if (IsIPSpoof(source_p) || source_p->localClient->mangledhost == NULL || (IsDynSpoof(source_p) && strcmp(source_p->host, source_p->localClient->mangledhost))) | |
156 | { | |
157 | source_p->umodes &= ~user_modes['h']; | |
158 | return; | |
159 | } | |
160 | if (strcmp(source_p->host, source_p->localClient->mangledhost)) | |
161 | { | |
907468c4 | 162 | rb_strlcpy(source_p->host, source_p->localClient->mangledhost, HOSTLEN); |
212380e3 | 163 | distribute_hostchange(source_p); |
164 | } | |
165 | else /* not really nice, but we need to send this numeric here */ | |
166 | sendto_one_numeric(source_p, RPL_HOSTHIDDEN, "%s :is now your hidden host", | |
167 | source_p->host); | |
168 | } | |
169 | else if (!(source_p->umodes & user_modes['h'])) | |
170 | { | |
171 | if (source_p->localClient->mangledhost != NULL && | |
172 | !strcmp(source_p->host, source_p->localClient->mangledhost)) | |
173 | { | |
907468c4 | 174 | rb_strlcpy(source_p->host, source_p->orighost, HOSTLEN); |
212380e3 | 175 | distribute_hostchange(source_p); |
176 | } | |
177 | } | |
178 | } | |
179 | ||
180 | static void | |
181 | check_new_user(void *vdata) | |
182 | { | |
183 | struct Client *source_p = (void *)vdata; | |
184 | ||
185 | if (IsIPSpoof(source_p)) | |
186 | { | |
187 | source_p->umodes &= ~user_modes['h']; | |
188 | return; | |
189 | } | |
c51d32ba | 190 | source_p->localClient->mangledhost = rb_malloc(HOSTLEN); |
212380e3 | 191 | if (!irccmp(source_p->orighost, source_p->sockhost)) |
762cc38c | 192 | do_host_cloak_ip(source_p->orighost, source_p->localClient->mangledhost); |
212380e3 | 193 | else |
762cc38c | 194 | do_host_cloak_host(source_p->orighost, source_p->localClient->mangledhost); |
212380e3 | 195 | if (IsDynSpoof(source_p)) |
196 | source_p->umodes &= ~user_modes['h']; | |
197 | if (source_p->umodes & user_modes['h']) | |
198 | { | |
907468c4 | 199 | rb_strlcpy(source_p->host, source_p->localClient->mangledhost, sizeof(source_p->host)); |
212380e3 | 200 | if (irccmp(source_p->host, source_p->orighost)) |
201 | SetDynSpoof(source_p); | |
202 | } | |
203 | } |