]> jfr.im git - solanum.git/blame - extensions/ip_cloaking_3.0.c
modules: Add AV2 descriptions to all m_u* modules
[solanum.git] / extensions / ip_cloaking_3.0.c
CommitLineData
0469849f
AC
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 "s_conf.h"
9#include "s_user.h"
10#include "s_serv.h"
11#include "numeric.h"
12
13/* if you're modifying this module, you'll probably to change this */
14#define KEY 0x13748cfa
15
16static int
17_modinit(void)
18{
19 /* add the usermode to the available slot */
20 user_modes['h'] = find_umode_slot();
21 construct_umodebuf();
22
23 return 0;
24}
25
26static void
27_moddeinit(void)
28{
29 /* disable the umode and remove it from the available list */
30 user_modes['h'] = 0;
31 construct_umodebuf();
32}
33
34static void check_umode_change(void *data);
35static void check_new_user(void *data);
36mapi_hfn_list_av1 ip_cloaking_hfnlist[] = {
37 { "umode_changed", (hookfn) check_umode_change },
38 { "new_local_user", (hookfn) check_new_user },
39 { NULL, NULL }
40};
41
42DECLARE_MODULE_AV1(ip_cloaking, _modinit, _moddeinit, NULL, NULL,
04f832b7 43 ip_cloaking_hfnlist, NULL);
0469849f
AC
44
45static void
29d224a1 46distribute_hostchange(struct Client *client_p, char *newhost)
0469849f 47{
29d224a1
KB
48 if (newhost != client_p->orighost)
49 sendto_one_numeric(client_p, RPL_HOSTHIDDEN, "%s :is now your hidden host",
9f409b63 50 newhost);
0469849f 51 else
29d224a1 52 sendto_one_numeric(client_p, RPL_HOSTHIDDEN, "%s :hostname reset",
9f409b63 53 newhost);
0469849f
AC
54
55 sendto_server(NULL, NULL,
56 CAP_EUID | CAP_TS6, NOCAPS, ":%s CHGHOST %s :%s",
9f409b63 57 use_id(&me), use_id(client_p), newhost);
0469849f
AC
58 sendto_server(NULL, NULL,
59 CAP_TS6, CAP_EUID, ":%s ENCAP * CHGHOST %s :%s",
9f409b63 60 use_id(&me), use_id(client_p), newhost);
29d224a1
KB
61
62 change_nick_user_host(client_p, client_p->name, client_p->username, newhost, 0, "Changing host");
63
64 if (newhost != client_p->orighost)
65 SetDynSpoof(client_p);
0469849f 66 else
29d224a1 67 ClearDynSpoof(client_p);
0469849f
AC
68}
69
70#define Nval 0x8c3a48ac
71#define HOSTLEN 63
72#define INITDATA "98fwqefnoiqefv03f423t34gbv3vb89tg432t3b8" /* change this */
73
74static inline unsigned int
75get_string_entropy(const char *inbuf)
76{
77 unsigned int accum = 1;
78
79 while(*inbuf != '\0')
80 accum += *inbuf++;
81
82 return accum;
83}
84
85/* calls get_string_entropy() and toasts it against INITDATA */
86static inline unsigned int
87get_string_weighted_entropy(const char *inbuf)
88{
89 static int base_entropy = 0;
90 unsigned int accum = get_string_entropy(inbuf);
91
92 /* initialize the algorithm if it is not yet ready */
93 if (base_entropy == 0)
94 base_entropy = get_string_entropy(INITDATA);
95
96 return (Nval * accum) ^ base_entropy;
97}
98
99static void
100do_host_cloak_ip(const char *inbuf, char *outbuf)
101{
102 char *tptr;
103 unsigned int accum = get_string_weighted_entropy(inbuf);
104 char buf[HOSTLEN];
105 int ipv6 = 0;
106
107 strncpy(buf, inbuf, HOSTLEN);
108 tptr = strrchr(buf, '.');
109
110 if (tptr == NULL)
111 {
112 tptr = strrchr(buf, ':');
113 ipv6 = 1;
114 }
115
116 if (tptr == NULL)
117 {
118 strncpy(outbuf, inbuf, HOSTLEN);
119 return;
120 }
121
122 *tptr++ = '\0';
123
124 if(ipv6)
125 {
5203cba5 126 snprintf(outbuf, HOSTLEN, "%s:%x", buf, accum);
0469849f
AC
127 }
128 else
129 {
5203cba5 130 snprintf(outbuf, HOSTLEN, "%s.%x", buf, accum);
0469849f
AC
131 }
132}
133
134static void
135do_host_cloak_host(const char *inbuf, char *outbuf)
136{
137 char b26_alphabet[] = "abcdefghijklmnopqrstuvwxyz";
138 char *tptr;
139 unsigned int accum = get_string_weighted_entropy(inbuf);
140
141 strncpy(outbuf, inbuf, HOSTLEN);
142
55abcbb2 143 /* pass 1: scramble first section of hostname using base26
0469849f
AC
144 * alphabet toasted against the weighted entropy of the string.
145 *
146 * numbers are not changed at this time, only letters.
147 */
148 for (tptr = outbuf; *tptr != '\0'; tptr++)
149 {
150 if (*tptr == '.')
151 break;
152
29c92cf9 153 if (isdigit((unsigned char)*tptr) || *tptr == '-')
0469849f
AC
154 continue;
155
156 *tptr = b26_alphabet[(*tptr * accum) % 26];
157 }
158
159 /* pass 2: scramble each number in the address */
160 for (tptr = outbuf; *tptr != '\0'; tptr++)
161 {
29c92cf9 162 if (isdigit((unsigned char)*tptr))
0469849f
AC
163 {
164 *tptr = 48 + ((*tptr * accum) % 10);
165 }
55abcbb2 166 }
0469849f
AC
167}
168
169static void
170check_umode_change(void *vdata)
171{
172 hook_data_umode_changed *data = (hook_data_umode_changed *)vdata;
173 struct Client *source_p = data->client;
174
175 if (!MyClient(source_p))
176 return;
177
178 /* didn't change +h umode, we don't need to do anything */
179 if (!((data->oldumodes ^ source_p->umodes) & user_modes['h']))
180 return;
181
182 if (source_p->umodes & user_modes['h'])
183 {
184 if (IsIPSpoof(source_p) || source_p->localClient->mangledhost == NULL || (IsDynSpoof(source_p) && strcmp(source_p->host, source_p->localClient->mangledhost)))
185 {
186 source_p->umodes &= ~user_modes['h'];
187 return;
188 }
189 if (strcmp(source_p->host, source_p->localClient->mangledhost))
190 {
29d224a1 191 distribute_hostchange(source_p, source_p->localClient->mangledhost);
0469849f
AC
192 }
193 else /* not really nice, but we need to send this numeric here */
194 sendto_one_numeric(source_p, RPL_HOSTHIDDEN, "%s :is now your hidden host",
195 source_p->host);
196 }
197 else if (!(source_p->umodes & user_modes['h']))
198 {
199 if (source_p->localClient->mangledhost != NULL &&
200 !strcmp(source_p->host, source_p->localClient->mangledhost))
201 {
29d224a1 202 distribute_hostchange(source_p, source_p->orighost);
0469849f
AC
203 }
204 }
205}
206
207static void
208check_new_user(void *vdata)
209{
210 struct Client *source_p = (void *)vdata;
211
212 if (IsIPSpoof(source_p))
213 {
214 source_p->umodes &= ~user_modes['h'];
215 return;
216 }
217 source_p->localClient->mangledhost = rb_malloc(HOSTLEN);
218 if (!irccmp(source_p->orighost, source_p->sockhost))
219 do_host_cloak_ip(source_p->orighost, source_p->localClient->mangledhost);
220 else
221 do_host_cloak_host(source_p->orighost, source_p->localClient->mangledhost);
222 if (IsDynSpoof(source_p))
223 source_p->umodes &= ~user_modes['h'];
224 if (source_p->umodes & user_modes['h'])
225 {
226 rb_strlcpy(source_p->host, source_p->localClient->mangledhost, sizeof(source_p->host));
227 if (irccmp(source_p->host, source_p->orighost))
228 SetDynSpoof(source_p);
229 }
230}