]> jfr.im git - irc/rqf/shadowircd.git/blame - extensions/ip_cloaking_old.c
strlcpy -> rb_strlcpy
[irc/rqf/shadowircd.git] / extensions / ip_cloaking_old.c
CommitLineData
762cc38c 1/* $Id: ip_cloaking_old.c 3522 2007-07-06 07:48:28Z 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 "s_conf.h"
10#include "s_user.h"
11#include "s_serv.h"
762cc38c 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,
44 ip_cloaking_hfnlist, "$Revision: 3522 $");
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);
62 sendto_server(NULL, NULL,
63 NOCAPS, CAP_TS6, ":%s ENCAP * CHGHOST %s :%s",
64 me.name, client->name, client->host);
65 if (irccmp(client->host, client->orighost))
66 SetDynSpoof(client);
67 else
68 ClearDynSpoof(client);
69}
70
71static void
72do_host_cloak(const char *inbuf, char *outbuf, int ipmask)
73{
74 int cyc;
75 unsigned int hosthash = 1, hosthash2 = 1;
76 unsigned int maxcycle = strlen(inbuf);
77 int len1;
78 const char *rest, *next;
79
80 for (cyc = 0; cyc < maxcycle - 2; cyc += 2)
81 hosthash *= (unsigned int) inbuf[cyc];
82
83 /* safety: decrement ourselves two steps back */
84 for (cyc = maxcycle - 1; cyc >= 1; cyc -= 2)
85 hosthash2 *= (unsigned int) inbuf[cyc];
86
87 /* lets do some bitshifting -- this pretty much destroys the IP
88 * sequence, while still providing a checksum. exactly what
89 * we're shooting for. --nenolod
90 */
91 hosthash += (hosthash2 / KEY);
92 hosthash2 += (hosthash / KEY);
93
94 if (ipmask == 0)
95 {
71f6ebfa 96 rb_snprintf(outbuf, HOSTLEN, "%s-%X%X",
762cc38c 97 ServerInfo.network_name, hosthash2, hosthash);
98 len1 = strlen(outbuf);
99 rest = strchr(inbuf, '.');
100 if (rest == NULL)
101 rest = ".";
102 /* try to avoid truncation -- jilles */
103 while (len1 + strlen(rest) >= HOSTLEN && (next = strchr(rest + 1, '.')) != NULL)
104 rest = next;
105 strlcat(outbuf, rest, HOSTLEN);
106 }
107 else
71f6ebfa 108 rb_snprintf(outbuf, HOSTLEN, "%X%X.%s",
762cc38c 109 hosthash2, hosthash, ServerInfo.network_name);
110}
111
112static void
113check_umode_change(void *vdata)
114{
115 hook_data_umode_changed *data = (hook_data_umode_changed *)vdata;
116 struct Client *source_p = data->client;
117
118 if (!MyClient(source_p))
119 return;
120
121 /* didn't change +h umode, we don't need to do anything */
122 if (!((data->oldumodes ^ source_p->umodes) & user_modes['h']))
123 return;
124
125 if (source_p->umodes & user_modes['h'])
126 {
127 if (IsIPSpoof(source_p) || source_p->localClient->mangledhost == NULL || (IsDynSpoof(source_p) && strcmp(source_p->host, source_p->localClient->mangledhost)))
128 {
129 source_p->umodes &= ~user_modes['h'];
130 return;
131 }
132 if (strcmp(source_p->host, source_p->localClient->mangledhost))
133 {
907468c4 134 rb_strlcpy(source_p->host, source_p->localClient->mangledhost, HOSTLEN);
762cc38c 135 distribute_hostchange(source_p);
136 }
137 else /* not really nice, but we need to send this numeric here */
138 sendto_one_numeric(source_p, RPL_HOSTHIDDEN, "%s :is now your hidden host",
139 source_p->host);
140 }
141 else if (!(source_p->umodes & user_modes['h']))
142 {
143 if (source_p->localClient->mangledhost != NULL &&
144 !strcmp(source_p->host, source_p->localClient->mangledhost))
145 {
907468c4 146 rb_strlcpy(source_p->host, source_p->orighost, HOSTLEN);
762cc38c 147 distribute_hostchange(source_p);
148 }
149 }
150}
151
152static void
153check_new_user(void *vdata)
154{
155 struct Client *source_p = (void *)vdata;
156
157 if (IsIPSpoof(source_p))
158 {
159 source_p->umodes &= ~user_modes['h'];
160 return;
161 }
c51d32ba 162 source_p->localClient->mangledhost = rb_malloc(HOSTLEN);
762cc38c 163 if (!irccmp(source_p->orighost, source_p->sockhost))
164 do_host_cloak(source_p->orighost, source_p->localClient->mangledhost, 1);
165 else
166 do_host_cloak(source_p->orighost, source_p->localClient->mangledhost, 0);
167 if (IsDynSpoof(source_p))
168 source_p->umodes &= ~user_modes['h'];
169 if (source_p->umodes & user_modes['h'])
170 {
907468c4 171 rb_strlcpy(source_p->host, source_p->localClient->mangledhost, sizeof(source_p->host));
762cc38c 172 if (irccmp(source_p->host, source_p->orighost))
173 SetDynSpoof(source_p);
174 }
175}