]> jfr.im git - irc/rqf/shadowircd.git/blame - extensions/ip_cloaking.c
libratbox: Clean up uses of strcpy().
[irc/rqf/shadowircd.git] / extensions / ip_cloaking.c
CommitLineData
212380e3 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"
799483e7 8#include "hash.h"
212380e3 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
17static int
18_modinit(void)
19{
20 /* add the usermode to the available slot */
78d9febe 21 user_modes['x'] = find_umode_slot();
212380e3 22 construct_umodebuf();
23
24 return 0;
25}
26
27static void
28_moddeinit(void)
29{
30 /* disable the umode and remove it from the available list */
78d9febe 31 user_modes['x'] = 0;
212380e3 32 construct_umodebuf();
33}
34
35static void check_umode_change(void *data);
36static void check_new_user(void *data);
37mapi_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
43DECLARE_MODULE_AV1(ip_cloaking, _modinit, _moddeinit, NULL, NULL,
b076458c 44 ip_cloaking_hfnlist, "$Revision: 3526 $");
212380e3 45
46static void
47distribute_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
68static void
762cc38c 69do_host_cloak_ip(const char *inbuf, char *outbuf)
212380e3 70{
799483e7
JT
71 /* None of the characters in this table can be valid in an IP */
72 char chartable[] = "ghijklmnopqrstuvwxyz";
762cc38c 73 char *tptr;
799483e7
JT
74 uint32_t accum = fnv_hash((const unsigned char*) inbuf, 32);
75 int sepcount = 0;
76 int totalcount = 0;
3f46cd00 77 int ipv6 = 0;
762cc38c 78
8109e8e3 79 rb_strlcpy(outbuf, inbuf, HOSTLEN + 1);
514235a7 80
799483e7 81 if (strchr(outbuf, ':'))
3f46cd00 82 {
3f46cd00 83 ipv6 = 1;
3f46cd00 84
799483e7
JT
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++)
00b7ddcc 94 if (*tptr == ':')
799483e7 95 totalcount++;
799483e7
JT
96 }
97 else if (!strchr(outbuf, '.'))
514235a7 98 return;
99
799483e7 100 for (tptr = outbuf; *tptr != '\0'; tptr++)
3f46cd00 101 {
799483e7
JT
102 if (*tptr == ':' || *tptr == '.')
103 {
104 sepcount++;
105 continue;
106 }
107
8e28bf29
JT
108 if (ipv6 && sepcount < totalcount / 2)
109 continue;
799483e7 110
8e28bf29
JT
111 if (!ipv6 && sepcount < 2)
112 continue;
799483e7 113
8e28bf29 114 *tptr = chartable[(*tptr + accum) % 20];
799483e7 115 accum = (accum << 1) | (accum >> 31);
3f46cd00 116 }
762cc38c 117}
118
119static void
120do_host_cloak_host(const char *inbuf, char *outbuf)
121{
122 char b26_alphabet[] = "abcdefghijklmnopqrstuvwxyz";
123 char *tptr;
799483e7 124 uint32_t accum = fnv_hash((const unsigned char*) inbuf, 32);
762cc38c 125
8109e8e3 126 rb_strlcpy(outbuf, inbuf, HOSTLEN + 1);
762cc38c 127
128 /* pass 1: scramble first section of hostname using base26
799483e7 129 * alphabet toasted against the FNV hash of the string.
762cc38c 130 *
131 * numbers are not changed at this time, only letters.
132 */
133 for (tptr = outbuf; *tptr != '\0'; tptr++)
212380e3 134 {
762cc38c 135 if (*tptr == '.')
136 break;
137
138 if (isdigit(*tptr) || *tptr == '-')
139 continue;
140
799483e7
JT
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);
212380e3 145 }
762cc38c 146
147 /* pass 2: scramble each number in the address */
148 for (tptr = outbuf; *tptr != '\0'; tptr++)
149 {
150 if (isdigit(*tptr))
9b9c8484 151 *tptr = '0' + (*tptr + accum) % 10;
799483e7
JT
152
153 accum = (accum << 1) | (accum >> 31);
762cc38c 154 }
212380e3 155}
156
157static void
158check_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 */
78d9febe 167 if (!((data->oldumodes ^ source_p->umodes) & user_modes['x']))
212380e3 168 return;
169
78d9febe 170 if (source_p->umodes & user_modes['x'])
212380e3 171 {
172 if (IsIPSpoof(source_p) || source_p->localClient->mangledhost == NULL || (IsDynSpoof(source_p) && strcmp(source_p->host, source_p->localClient->mangledhost)))
173 {
78d9febe 174 source_p->umodes &= ~user_modes['x'];
212380e3 175 return;
176 }
177 if (strcmp(source_p->host, source_p->localClient->mangledhost))
178 {
8109e8e3 179 rb_strlcpy(source_p->host, source_p->localClient->mangledhost, HOSTLEN + 1);
212380e3 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 }
78d9febe 186 else if (!(source_p->umodes & user_modes['x']))
212380e3 187 {
188 if (source_p->localClient->mangledhost != NULL &&
189 !strcmp(source_p->host, source_p->localClient->mangledhost))
190 {
8109e8e3 191 rb_strlcpy(source_p->host, source_p->orighost, HOSTLEN + 1);
212380e3 192 distribute_hostchange(source_p);
193 }
194 }
195}
196
197static void
198check_new_user(void *vdata)
199{
200 struct Client *source_p = (void *)vdata;
201
202 if (IsIPSpoof(source_p))
203 {
78d9febe 204 source_p->umodes &= ~user_modes['x'];
212380e3 205 return;
206 }
8109e8e3 207 source_p->localClient->mangledhost = rb_malloc(HOSTLEN + 1);
212380e3 208 if (!irccmp(source_p->orighost, source_p->sockhost))
762cc38c 209 do_host_cloak_ip(source_p->orighost, source_p->localClient->mangledhost);
212380e3 210 else
762cc38c 211 do_host_cloak_host(source_p->orighost, source_p->localClient->mangledhost);
212380e3 212 if (IsDynSpoof(source_p))
78d9febe
JH
213 source_p->umodes &= ~user_modes['x'];
214 if (source_p->umodes & user_modes['x'])
212380e3 215 {
907468c4 216 rb_strlcpy(source_p->host, source_p->localClient->mangledhost, sizeof(source_p->host));
212380e3 217 if (irccmp(source_p->host, source_p->orighost))
218 SetDynSpoof(source_p);
219 }
220}