]> jfr.im git - irc/rqf/shadowircd.git/blob - extensions/ip_cloaking.c
Add description for LOCOPS message.
[irc/rqf/shadowircd.git] / extensions / ip_cloaking.c
1 /* $Id: ip_cloaking.c 3526 2007-07-06 07:56:14Z nenolod $ */
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 "hash.h"
10 #include "s_conf.h"
11 #include "s_user.h"
12 #include "s_serv.h"
13 #include "numeric.h"
14
15 /* if you're modifying this module, you'll probably to change this */
16 #define KEY 0x13748cfa
17
18 static int
19 _modinit(void)
20 {
21 /* add the usermode to the available slot */
22 user_modes['h'] = find_umode_slot();
23 construct_umodebuf();
24
25 return 0;
26 }
27
28 static void
29 _moddeinit(void)
30 {
31 /* disable the umode and remove it from the available list */
32 user_modes['h'] = 0;
33 construct_umodebuf();
34 }
35
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 },
41 { NULL, NULL }
42 };
43
44 DECLARE_MODULE_AV1(ip_cloaking, _modinit, _moddeinit, NULL, NULL,
45 ip_cloaking_hfnlist, "$Revision: 3526 $");
46
47 static void
48 distribute_hostchange(struct Client *client)
49 {
50 if (irccmp(client->host, client->orighost))
51 sendto_one_numeric(client, RPL_HOSTHIDDEN, "%s :is now your hidden host",
52 client->host);
53 else
54 sendto_one_numeric(client, RPL_HOSTHIDDEN, "%s :hostname reset",
55 client->host);
56
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))
64 SetDynSpoof(client);
65 else
66 ClearDynSpoof(client);
67 }
68
69 static void
70 do_host_cloak_ip(const char *inbuf, char *outbuf)
71 {
72 /* None of the characters in this table can be valid in an IP */
73 char chartable[] = "ghijklmnopqrstuvwxyz";
74 char *tptr;
75 uint32_t accum = fnv_hash((const unsigned char*) inbuf, 32);
76 int sepcount = 0;
77 int totalcount = 0;
78 int ipv6 = 0;
79
80 rb_strlcpy(outbuf, inbuf, HOSTLEN + 1);
81
82 if (strchr(outbuf, ':'))
83 {
84 ipv6 = 1;
85
86 /* Damn you IPv6...
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.
90 *
91 * We have to do this ahead of time because doing this during
92 * the actual cloaking would get ugly
93 */
94 for (tptr = outbuf; *tptr != '\0'; tptr++)
95 if (*tptr == ':')
96 totalcount++;
97 }
98 else if (!strchr(outbuf, '.'))
99 return;
100
101 for (tptr = outbuf; *tptr != '\0'; tptr++)
102 {
103 if (*tptr == ':' || *tptr == '.')
104 {
105 sepcount++;
106 continue;
107 }
108
109 if (ipv6 && sepcount < totalcount / 2)
110 continue;
111
112 if (!ipv6 && sepcount < 2)
113 continue;
114
115 *tptr = chartable[(*tptr + accum) % 20];
116 accum = (accum << 1) | (accum >> 31);
117 }
118 }
119
120 static void
121 do_host_cloak_host(const char *inbuf, char *outbuf)
122 {
123 char b26_alphabet[] = "abcdefghijklmnopqrstuvwxyz";
124 char *tptr;
125 uint32_t accum = fnv_hash((const unsigned char*) inbuf, 32);
126
127 rb_strlcpy(outbuf, inbuf, HOSTLEN + 1);
128
129 /* pass 1: scramble first section of hostname using base26
130 * alphabet toasted against the FNV hash of the string.
131 *
132 * numbers are not changed at this time, only letters.
133 */
134 for (tptr = outbuf; *tptr != '\0'; tptr++)
135 {
136 if (*tptr == '.')
137 break;
138
139 if (isdigit(*tptr) || *tptr == '-')
140 continue;
141
142 *tptr = b26_alphabet[(*tptr + accum) % 26];
143
144 /* Rotate one bit to avoid all digits being turned odd or even */
145 accum = (accum << 1) | (accum >> 31);
146 }
147
148 /* pass 2: scramble each number in the address */
149 for (tptr = outbuf; *tptr != '\0'; tptr++)
150 {
151 if (isdigit(*tptr))
152 *tptr = '0' + (*tptr + accum) % 10;
153
154 accum = (accum << 1) | (accum >> 31);
155 }
156 }
157
158 static void
159 check_umode_change(void *vdata)
160 {
161 hook_data_umode_changed *data = (hook_data_umode_changed *)vdata;
162 struct Client *source_p = data->client;
163
164 if (!MyClient(source_p))
165 return;
166
167 /* didn't change +h umode, we don't need to do anything */
168 if (!((data->oldumodes ^ source_p->umodes) & user_modes['h']))
169 return;
170
171 if (source_p->umodes & user_modes['h'])
172 {
173 if (IsIPSpoof(source_p) || source_p->localClient->mangledhost == NULL || (IsDynSpoof(source_p) && strcmp(source_p->host, source_p->localClient->mangledhost)))
174 {
175 source_p->umodes &= ~user_modes['h'];
176 return;
177 }
178 if (strcmp(source_p->host, source_p->localClient->mangledhost))
179 {
180 rb_strlcpy(source_p->host, source_p->localClient->mangledhost, HOSTLEN + 1);
181 distribute_hostchange(source_p);
182 }
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",
185 source_p->host);
186 }
187 else if (!(source_p->umodes & user_modes['h']))
188 {
189 if (source_p->localClient->mangledhost != NULL &&
190 !strcmp(source_p->host, source_p->localClient->mangledhost))
191 {
192 rb_strlcpy(source_p->host, source_p->orighost, HOSTLEN + 1);
193 distribute_hostchange(source_p);
194 }
195 }
196 }
197
198 static void
199 check_new_user(void *vdata)
200 {
201 struct Client *source_p = (void *)vdata;
202
203 if (IsIPSpoof(source_p))
204 {
205 source_p->umodes &= ~user_modes['h'];
206 return;
207 }
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);
211 else
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'])
216 {
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);
220 }
221 }