]> jfr.im git - irc/rqf/shadowircd.git/blob - extensions/ip_cloaking.c
Removal of ancient SVN ID's part one
[irc/rqf/shadowircd.git] / extensions / ip_cloaking.c
1
2 #include "stdinc.h"
3 #include "modules.h"
4 #include "hook.h"
5 #include "client.h"
6 #include "ircd.h"
7 #include "send.h"
8 #include "hash.h"
9 #include "s_conf.h"
10 #include "s_user.h"
11 #include "s_serv.h"
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['x'] = 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['x'] = 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,
44 ip_cloaking_hfnlist, "$Revision: 3526 $");
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);
62 if (irccmp(client->host, client->orighost))
63 SetDynSpoof(client);
64 else
65 ClearDynSpoof(client);
66 }
67
68 static void
69 do_host_cloak_ip(const char *inbuf, char *outbuf)
70 {
71 /* None of the characters in this table can be valid in an IP */
72 char chartable[] = "ghijklmnopqrstuvwxyz";
73 char *tptr;
74 uint32_t accum = fnv_hash((const unsigned char*) inbuf, 32);
75 int sepcount = 0;
76 int totalcount = 0;
77 int ipv6 = 0;
78
79 rb_strlcpy(outbuf, inbuf, HOSTLEN + 1);
80
81 if (strchr(outbuf, ':'))
82 {
83 ipv6 = 1;
84
85 /* Damn you IPv6...
86 * We count the number of colons so we can calculate how much
87 * of the host to cloak. This is because some hostmasks may not
88 * have as many octets as we'd like.
89 *
90 * We have to do this ahead of time because doing this during
91 * the actual cloaking would get ugly
92 */
93 for (tptr = outbuf; *tptr != '\0'; tptr++)
94 if (*tptr == ':')
95 totalcount++;
96 }
97 else if (!strchr(outbuf, '.'))
98 return;
99
100 for (tptr = outbuf; *tptr != '\0'; tptr++)
101 {
102 if (*tptr == ':' || *tptr == '.')
103 {
104 sepcount++;
105 continue;
106 }
107
108 if (ipv6 && sepcount < totalcount / 2)
109 continue;
110
111 if (!ipv6 && sepcount < 2)
112 continue;
113
114 *tptr = chartable[(*tptr + accum) % 20];
115 accum = (accum << 1) | (accum >> 31);
116 }
117 }
118
119 static void
120 do_host_cloak_host(const char *inbuf, char *outbuf)
121 {
122 char b26_alphabet[] = "abcdefghijklmnopqrstuvwxyz";
123 char *tptr;
124 uint32_t accum = fnv_hash((const unsigned char*) inbuf, 32);
125
126 rb_strlcpy(outbuf, inbuf, HOSTLEN + 1);
127
128 /* pass 1: scramble first section of hostname using base26
129 * alphabet toasted against the FNV hash of the string.
130 *
131 * numbers are not changed at this time, only letters.
132 */
133 for (tptr = outbuf; *tptr != '\0'; tptr++)
134 {
135 if (*tptr == '.')
136 break;
137
138 if (isdigit(*tptr) || *tptr == '-')
139 continue;
140
141 *tptr = b26_alphabet[(*tptr + accum) % 26];
142
143 /* Rotate one bit to avoid all digits being turned odd or even */
144 accum = (accum << 1) | (accum >> 31);
145 }
146
147 /* pass 2: scramble each number in the address */
148 for (tptr = outbuf; *tptr != '\0'; tptr++)
149 {
150 if (isdigit(*tptr))
151 *tptr = '0' + (*tptr + accum) % 10;
152
153 accum = (accum << 1) | (accum >> 31);
154 }
155 }
156
157 static void
158 check_umode_change(void *vdata)
159 {
160 hook_data_umode_changed *data = (hook_data_umode_changed *)vdata;
161 struct Client *source_p = data->client;
162
163 if (!MyClient(source_p))
164 return;
165
166 /* didn't change +h umode, we don't need to do anything */
167 if (!((data->oldumodes ^ source_p->umodes) & user_modes['x']))
168 return;
169
170 if (source_p->umodes & user_modes['x'])
171 {
172 if (IsIPSpoof(source_p) || source_p->localClient->mangledhost == NULL || (IsDynSpoof(source_p) && strcmp(source_p->host, source_p->localClient->mangledhost)))
173 {
174 source_p->umodes &= ~user_modes['x'];
175 return;
176 }
177 if (strcmp(source_p->host, source_p->localClient->mangledhost))
178 {
179 rb_strlcpy(source_p->host, source_p->localClient->mangledhost, HOSTLEN + 1);
180 distribute_hostchange(source_p);
181 }
182 else /* not really nice, but we need to send this numeric here */
183 sendto_one_numeric(source_p, RPL_HOSTHIDDEN, "%s :is now your hidden host",
184 source_p->host);
185 }
186 else if (!(source_p->umodes & user_modes['x']))
187 {
188 if (source_p->localClient->mangledhost != NULL &&
189 !strcmp(source_p->host, source_p->localClient->mangledhost))
190 {
191 rb_strlcpy(source_p->host, source_p->orighost, HOSTLEN + 1);
192 distribute_hostchange(source_p);
193 }
194 }
195 }
196
197 static void
198 check_new_user(void *vdata)
199 {
200 struct Client *source_p = (void *)vdata;
201
202 if (IsIPSpoof(source_p))
203 {
204 source_p->umodes &= ~user_modes['x'];
205 return;
206 }
207 source_p->localClient->mangledhost = rb_malloc(HOSTLEN + 1);
208 if (!irccmp(source_p->orighost, source_p->sockhost))
209 do_host_cloak_ip(source_p->orighost, source_p->localClient->mangledhost);
210 else
211 do_host_cloak_host(source_p->orighost, source_p->localClient->mangledhost);
212 if (IsDynSpoof(source_p))
213 source_p->umodes &= ~user_modes['x'];
214 if (source_p->umodes & user_modes['x'])
215 {
216 rb_strlcpy(source_p->host, source_p->localClient->mangledhost, sizeof(source_p->host));
217 if (irccmp(source_p->host, source_p->orighost))
218 SetDynSpoof(source_p);
219 }
220 }