]>
Commit | Line | Data |
---|---|---|
762cc38c 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" | |
762cc38c AC |
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, "$Revision: 3522 $"); | |
44 | ||
45 | static void | |
29d224a1 | 46 | distribute_hostchange(struct Client *client_p, char *newhost) |
762cc38c | 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); |
762cc38c | 51 | else |
29d224a1 | 52 | sendto_one_numeric(client_p, RPL_HOSTHIDDEN, "%s :hostname reset", |
9f409b63 | 53 | newhost); |
762cc38c 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); |
762cc38c 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); | |
762cc38c | 66 | else |
29d224a1 | 67 | ClearDynSpoof(client_p); |
762cc38c AC |
68 | } |
69 | ||
70 | static void | |
71 | do_host_cloak(const char *inbuf, char *outbuf, int ipmask) | |
72 | { | |
29c92cf9 | 73 | unsigned int cyc; |
762cc38c | 74 | unsigned int hosthash = 1, hosthash2 = 1; |
55abcbb2 | 75 | unsigned int maxcycle = strlen(inbuf); |
762cc38c AC |
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 | { | |
5203cba5 | 95 | snprintf(outbuf, HOSTLEN, "%s-%X%X", |
762cc38c AC |
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; | |
1f9de103 | 104 | rb_strlcat(outbuf, rest, HOSTLEN); |
762cc38c AC |
105 | } |
106 | else | |
5203cba5 | 107 | snprintf(outbuf, HOSTLEN, "%X%X.%s", |
762cc38c AC |
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 | { | |
29d224a1 | 133 | distribute_hostchange(source_p, source_p->localClient->mangledhost); |
762cc38c AC |
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 | { | |
29d224a1 | 144 | distribute_hostchange(source_p, source_p->orighost); |
762cc38c AC |
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 | } | |
954012d3 | 159 | source_p->localClient->mangledhost = rb_malloc(HOSTLEN); |
762cc38c AC |
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 | { | |
f427c8b0 | 168 | rb_strlcpy(source_p->host, source_p->localClient->mangledhost, sizeof(source_p->host)); |
762cc38c AC |
169 | if (irccmp(source_p->host, source_p->orighost)) |
170 | SetDynSpoof(source_p); | |
171 | } | |
172 | } |