]> jfr.im git - solanum.git/blob - extensions/ip_cloaking_old.c
modules: Add AV2 descriptions to all m_u* modules
[solanum.git] / extensions / ip_cloaking_old.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 "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
16 static 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
26 static 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
34 static void check_umode_change(void *data);
35 static void check_new_user(void *data);
36 mapi_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
42 DECLARE_MODULE_AV1(ip_cloaking, _modinit, _moddeinit, NULL, NULL,
43 ip_cloaking_hfnlist, NULL);
44
45 static void
46 distribute_hostchange(struct Client *client_p, char *newhost)
47 {
48 if (newhost != client_p->orighost)
49 sendto_one_numeric(client_p, RPL_HOSTHIDDEN, "%s :is now your hidden host",
50 newhost);
51 else
52 sendto_one_numeric(client_p, RPL_HOSTHIDDEN, "%s :hostname reset",
53 newhost);
54
55 sendto_server(NULL, NULL,
56 CAP_EUID | CAP_TS6, NOCAPS, ":%s CHGHOST %s :%s",
57 use_id(&me), use_id(client_p), newhost);
58 sendto_server(NULL, NULL,
59 CAP_TS6, CAP_EUID, ":%s ENCAP * CHGHOST %s :%s",
60 use_id(&me), use_id(client_p), newhost);
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);
66 else
67 ClearDynSpoof(client_p);
68 }
69
70 static void
71 do_host_cloak(const char *inbuf, char *outbuf, int ipmask)
72 {
73 unsigned int cyc;
74 unsigned int hosthash = 1, hosthash2 = 1;
75 unsigned int maxcycle = strlen(inbuf);
76 int len1;
77 const char *rest, *next;
78
79 for (cyc = 0; cyc < maxcycle - 2; cyc += 2)
80 hosthash *= (unsigned int) inbuf[cyc];
81
82 /* safety: decrement ourselves two steps back */
83 for (cyc = maxcycle - 1; cyc >= 1; cyc -= 2)
84 hosthash2 *= (unsigned int) inbuf[cyc];
85
86 /* lets do some bitshifting -- this pretty much destroys the IP
87 * sequence, while still providing a checksum. exactly what
88 * we're shooting for. --nenolod
89 */
90 hosthash += (hosthash2 / KEY);
91 hosthash2 += (hosthash / KEY);
92
93 if (ipmask == 0)
94 {
95 snprintf(outbuf, HOSTLEN, "%s-%X%X",
96 ServerInfo.network_name, hosthash2, hosthash);
97 len1 = strlen(outbuf);
98 rest = strchr(inbuf, '.');
99 if (rest == NULL)
100 rest = ".";
101 /* try to avoid truncation -- jilles */
102 while (len1 + strlen(rest) >= HOSTLEN && (next = strchr(rest + 1, '.')) != NULL)
103 rest = next;
104 rb_strlcat(outbuf, rest, HOSTLEN);
105 }
106 else
107 snprintf(outbuf, HOSTLEN, "%X%X.%s",
108 hosthash2, hosthash, ServerInfo.network_name);
109 }
110
111 static void
112 check_umode_change(void *vdata)
113 {
114 hook_data_umode_changed *data = (hook_data_umode_changed *)vdata;
115 struct Client *source_p = data->client;
116
117 if (!MyClient(source_p))
118 return;
119
120 /* didn't change +h umode, we don't need to do anything */
121 if (!((data->oldumodes ^ source_p->umodes) & user_modes['h']))
122 return;
123
124 if (source_p->umodes & user_modes['h'])
125 {
126 if (IsIPSpoof(source_p) || source_p->localClient->mangledhost == NULL || (IsDynSpoof(source_p) && strcmp(source_p->host, source_p->localClient->mangledhost)))
127 {
128 source_p->umodes &= ~user_modes['h'];
129 return;
130 }
131 if (strcmp(source_p->host, source_p->localClient->mangledhost))
132 {
133 distribute_hostchange(source_p, source_p->localClient->mangledhost);
134 }
135 else /* not really nice, but we need to send this numeric here */
136 sendto_one_numeric(source_p, RPL_HOSTHIDDEN, "%s :is now your hidden host",
137 source_p->host);
138 }
139 else if (!(source_p->umodes & user_modes['h']))
140 {
141 if (source_p->localClient->mangledhost != NULL &&
142 !strcmp(source_p->host, source_p->localClient->mangledhost))
143 {
144 distribute_hostchange(source_p, source_p->orighost);
145 }
146 }
147 }
148
149 static void
150 check_new_user(void *vdata)
151 {
152 struct Client *source_p = (void *)vdata;
153
154 if (IsIPSpoof(source_p))
155 {
156 source_p->umodes &= ~user_modes['h'];
157 return;
158 }
159 source_p->localClient->mangledhost = rb_malloc(HOSTLEN);
160 if (!irccmp(source_p->orighost, source_p->sockhost))
161 do_host_cloak(source_p->orighost, source_p->localClient->mangledhost, 1);
162 else
163 do_host_cloak(source_p->orighost, source_p->localClient->mangledhost, 0);
164 if (IsDynSpoof(source_p))
165 source_p->umodes &= ~user_modes['h'];
166 if (source_p->umodes & user_modes['h'])
167 {
168 rb_strlcpy(source_p->host, source_p->localClient->mangledhost, sizeof(source_p->host));
169 if (irccmp(source_p->host, source_p->orighost))
170 SetDynSpoof(source_p);
171 }
172 }