]> jfr.im git - irc/rqf/shadowircd.git/blame - extensions/ip_cloaking.c
Let ops/voices bypass tgchange/floodcount if sending to users in their channel.
[irc/rqf/shadowircd.git] / extensions / ip_cloaking.c
CommitLineData
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
17static 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
27static 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
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
762cc38c 68#define Nval 0x8c3a48ac
69#define HOSTLEN 63
70#define INITDATA "98fwqefnoiqefv03f423t34gbv3vb89tg432t3b8" /* change this */
71
72static inline unsigned int
73get_string_entropy(const char *inbuf)
74{
75 unsigned int accum = 1;
76
77 while(*inbuf != '\0')
78 accum += *inbuf++;
79
80 return accum;
81}
82
83/* calls get_string_entropy() and toasts it against INITDATA */
84static inline unsigned int
85get_string_weighted_entropy(const char *inbuf)
86{
87 static int base_entropy = 0;
88 unsigned int accum = get_string_entropy(inbuf);
89
90 /* initialize the algorithm if it is not yet ready */
91 if (base_entropy == 0)
92 base_entropy = get_string_entropy(INITDATA);
93
94 return (Nval * accum) ^ base_entropy;
95}
96
212380e3 97static void
762cc38c 98do_host_cloak_ip(const char *inbuf, char *outbuf)
212380e3 99{
762cc38c 100 char *tptr;
101 unsigned int accum = get_string_weighted_entropy(inbuf);
102 char buf[HOSTLEN];
103
104 strncpy(buf, inbuf, HOSTLEN);
105 tptr = strrchr(buf, '.');
514235a7 106
107 if (tptr == NULL)
b076458c 108 {
109 strncpy(outbuf, inbuf, HOSTLEN);
514235a7 110 return;
b076458c 111 }
514235a7 112
762cc38c 113 *tptr++ = '\0';
212380e3 114
762cc38c 115 snprintf(outbuf, HOSTLEN, "%s.%x", buf, accum);
116}
117
118static void
119do_host_cloak_host(const char *inbuf, char *outbuf)
120{
121 char b26_alphabet[] = "abcdefghijklmnopqrstuvwxyz";
122 char *tptr;
123 unsigned int accum = get_string_weighted_entropy(inbuf);
124
125 strncpy(outbuf, inbuf, HOSTLEN);
126
127 /* pass 1: scramble first section of hostname using base26
128 * alphabet toasted against the weighted entropy of the string.
129 *
130 * numbers are not changed at this time, only letters.
131 */
132 for (tptr = outbuf; *tptr != '\0'; tptr++)
212380e3 133 {
762cc38c 134 if (*tptr == '.')
135 break;
136
137 if (isdigit(*tptr) || *tptr == '-')
138 continue;
139
140 *tptr = b26_alphabet[(*tptr * accum) % 26];
212380e3 141 }
762cc38c 142
143 /* pass 2: scramble each number in the address */
144 for (tptr = outbuf; *tptr != '\0'; tptr++)
145 {
146 if (isdigit(*tptr))
147 {
148 *tptr = 48 + ((*tptr * accum) % 10);
149 }
150 }
212380e3 151}
152
153static void
154check_umode_change(void *vdata)
155{
156 hook_data_umode_changed *data = (hook_data_umode_changed *)vdata;
157 struct Client *source_p = data->client;
158
159 if (!MyClient(source_p))
160 return;
161
162 /* didn't change +h umode, we don't need to do anything */
163 if (!((data->oldumodes ^ source_p->umodes) & user_modes['h']))
164 return;
165
166 if (source_p->umodes & user_modes['h'])
167 {
168 if (IsIPSpoof(source_p) || source_p->localClient->mangledhost == NULL || (IsDynSpoof(source_p) && strcmp(source_p->host, source_p->localClient->mangledhost)))
169 {
170 source_p->umodes &= ~user_modes['h'];
171 return;
172 }
173 if (strcmp(source_p->host, source_p->localClient->mangledhost))
174 {
907468c4 175 rb_strlcpy(source_p->host, source_p->localClient->mangledhost, HOSTLEN);
212380e3 176 distribute_hostchange(source_p);
177 }
178 else /* not really nice, but we need to send this numeric here */
179 sendto_one_numeric(source_p, RPL_HOSTHIDDEN, "%s :is now your hidden host",
180 source_p->host);
181 }
182 else if (!(source_p->umodes & user_modes['h']))
183 {
184 if (source_p->localClient->mangledhost != NULL &&
185 !strcmp(source_p->host, source_p->localClient->mangledhost))
186 {
907468c4 187 rb_strlcpy(source_p->host, source_p->orighost, HOSTLEN);
212380e3 188 distribute_hostchange(source_p);
189 }
190 }
191}
192
193static void
194check_new_user(void *vdata)
195{
196 struct Client *source_p = (void *)vdata;
197
198 if (IsIPSpoof(source_p))
199 {
200 source_p->umodes &= ~user_modes['h'];
201 return;
202 }
c51d32ba 203 source_p->localClient->mangledhost = rb_malloc(HOSTLEN);
212380e3 204 if (!irccmp(source_p->orighost, source_p->sockhost))
762cc38c 205 do_host_cloak_ip(source_p->orighost, source_p->localClient->mangledhost);
212380e3 206 else
762cc38c 207 do_host_cloak_host(source_p->orighost, source_p->localClient->mangledhost);
212380e3 208 if (IsDynSpoof(source_p))
209 source_p->umodes &= ~user_modes['h'];
210 if (source_p->umodes & user_modes['h'])
211 {
907468c4 212 rb_strlcpy(source_p->host, source_p->localClient->mangledhost, sizeof(source_p->host));
212380e3 213 if (irccmp(source_p->host, source_p->orighost))
214 SetDynSpoof(source_p);
215 }
216}